Taxonomy Icon

Blockchain

Ethereum is a open source blockchain platform that lets anyone build decentralized applications that run on blockchain. Ethereum provides a decentralized Turing-complete virtual machine called Ethereum Virtual Machine (EVM), which can execute scripts using an international network of public nodes. Ethereuem is adaptable and flexible to use, and it also has the feature of deploying smart contracts on the blockchain using the Solidity programming language.

Like Docker, IBM Cloud Kubernetes Service is an open source development tool that enables you to package your application and run it anywhere with a barebone Linux machine and all required dependencies to run your application. Containerization means that your application runs in an isolated container, an explicitly defined, reproducible, and portable environment. The analogy is taken from freight transport where you ship your goods in containers. A container of an app is the app’s operating environment in our computing scenario. With Docker, you ship the operating environment along with your application. With IBM Cloud Kubernetes Service, you can leverage Docker containers to containerize your application and run it on IBM Cloud.

Learning objectives

Learn how to easily deploy a private Ethereum blockchain on IBM Cloud.

Prerequisites

Estimated time

Roughly one hour deploy the blockchain network to the IBM Cloud.

Steps

Build the Docker image

  1. Create a new directory and navigate to it.

     mkdir blockchain
     cd blockchain
    
  2. Create an empty Docker file using touch Dockerfile, and paste the following in the Dockerfile:

     FROM ubuntu:latest
     # Get the Ethereum Stuffs
     RUN apt-get update
     RUN apt-get install -y software-properties-common
     RUN apt-get install -y build-essential git
     RUN add-apt-repository ppa:longsleep/golang-backports
     RUN apt-get update
     RUN apt-get install -y golang-go
     RUN git clone https://github.com/ethereum/go-ethereum
     WORKDIR /go-ethereum
     RUN make geth
     WORKDIR /
     # House the data here
     RUN mkdir /block-data
     # Pass in the genesis block.
     COPY GenesisBlock.json GenesisBlock.json
     RUN ln -sf /go-ethereum/build/bin/geth /bin/geth
     EXPOSE 22 8088 50070 8545
     ENTRYPOINT geth --datadir /block-data init /GenesisBlock.json; geth --port 3000 --networkid 58342 --nodiscover --datadir=./block-data --maxpeers=0  --rpc  --rpcaddr 0.0.0.0 --rpcport 8545 --rpccorsdomain "*" --rpcapi "eth,net,web3,personal,miner"
    
  3. Create an empty file that helps to configure your blockchain network using touch GenesisBlock.json, and paste the following into your new configuration file:

     {
         "config": {
           "chainId": 456719,
           "homesteadBlock": 0,
           "eip155Block": 0,
           "eip158Block": 0
         },
         "nonce": "0x0000000000000042",
         "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
         "difficulty": "0x000",
         "coinbase": "0x3333333333333333333333333333333333333333",
         "timestamp": "0x0",
         "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
         "extraData": "0x",
         "gasLimit": "0x8888000",
         "alloc": {
         }
       }
    
  4. Build your Docker image:

     docker build -t myimage .
    

  5. Ensure the image is built by running the docker images command:

Deploy the Docker image to IBM Cloud Kubernetes service

Create a container service on IBM Cloud

  1. Go to IBM Cloud’s Kubernetes-based container service and create a new cluster. Provide your cluster with a unique name; let’s call it ether for now.

    1

  2. Wait until the service is fully deployed before using it.

    1

Set up your Container

  1. Install the IBM Cloud CLI plugin for the container service by running this command:

     bx plugin install container-service -r Bluemix
    
  2. Log in to your IBM Cloud account with the IBM Cloud CLI:

     bx login -a https://api.ng.bluemix.net
    
  3. Set your terminal context to your cluster:

     bx cs cluster-config <cluster_name_or_id>
    

    1

  4. After downloading the configuration files, a command is displayed that you can use to set the path to the local Kubernetes configuration file as an environment variable. For example:

     export KUBECONFIG=/Users/saifrehman/.bluemix/plugins/container-service/clusters/ether/kube-config-dal10-ether.yml
    

Push the image from your machine to IBM Cloud Private registery

Now push your image to the Container Registry on IBM Cloud. To do this, you will tag an image to point to this registry, choose a local image, target a namespace, and target a repository.

  1. Choose a name for your first namespace, and create that namespace. For example:

     bx cr namespace-add etherprivate
    
  2. Log your local Docker daemon into the IBM Cloud Container Registry:

     bx cr login
    
  3. Tag your Docker image. For example:

     docker tag myimage registry.ng.bluemix.net/etherprivate/myimage
    
  4. Push the image to IBM private registry. For example:

     docker push registry.ng.bluemix.net/etherprivate/myimage
    

Deploy your image to the Kubernetes cluster on IBM Cloud

  1. Create an empty file that will configure your Kubernetes deployment by using touch service.deployment.yml, and paste the following into your new configuration file:

     apiVersion: extensions/v1beta1
     kind: Deployment
     metadata:
      name: blockchain-server-test # give any name
     spec:
      replicas: 3
      template:
        metadata:
          name: blockchain-server-test
          labels:
            run: etherprivate
        spec:
          containers:
            - name: ether
              image: "registry.ng.bluemix.net/etherprivate/myimage" # your registery name
              imagePullPolicy: Always
     --
     apiVersion: v1
     kind: Service
     metadata:
      name: my-service-test-11 # give a service name
      labels:
        run: etherprivate
     spec:
      type: NodePort
      selector:
        run: etherprivate
      ports:
       - protocol: TCP
         port: 8545 # it shall be 8545 and no other port !!!!!
         nodePort: 30090 # Give a port to access the application publically
    

A few things to consider changing:

  • Give a metadata name to your application
  • The name of your conntainer you created
  • The name of the image you created in the IBM private registry
  • The port (in this case, we are using port 8545)
  • The node port (it can be any port from 30000 to 60000)

  • Save the Kubernetes configuration of your cluster and run the following:

     kubectl create -f service-deployment.yml
    
  • Once it is deployed, you can access the application with the public IP of a worker node:

Test your Private Ethereum Blockchain

  1. To ensure your application is running, issue a curl command. (Don’t worry if you don’t see any output; it will not return you anything just yet.)

     curl http://169.47.252.115:30090
    
  2. Access your private Ethereum blockchain running on IBM Cloud Kubernetes Service by using geth:

     geth attach http://169.47.252.115:30090
    

    Add Model

You now have an Ethereum blockchain running on IBM Cloud!

Common commands to start mining

  • Create new account: personal.newAccount('password')
  • Unlock account: personal.unlockAccount(web3.eth.coinbase, "password", 15000)
  • Start mining: miner.start()
  • Stop mining: miner.stop()

Summary

Congratulations! You have successfully set up your private Ethereum blockchain on IBM Cloud! You can now access the blockchain and deploy Solidity smart contracts to create your distributed applications on IBM Cloud.