Would you like to build your own personal cloud using native Docker tools? Here is how you can do that.

Cluster Configuration

We will start with three nodes. One of these will run Etcd and Swarm Manager. Other two will be Swarm nodes.

  • 10.140.0.2 (Etcd + Swarm Manager)
  • 10.140.0.3 (Swarm Node)
  • 10.140.0.4 (Swarm Node)

Setting up Etcd

We need a key value store which we will use for discovering various components like network and co-ordinate between Docker Swarm components. Etcd fits nicely in this role.

$ sudo yum install etcd

Configure Etcd to bind and advertise the IP of the machine by editing /etc/etcd/etcd.conf. Find out the lines below and edit them.

ETCD_LISTEN_CLIENT_URLS="http://10.140.0.2:2379"
ETCD_ADVERTISE_CLIENT_URLS="http://10.140.0.2:2379"

After this, we just need to restart the Etcd service.

$ sudo systemctl restart etcd

Start Docker with Cluster Options

Assuming that the Docker is already installed, we need to start it with cluster options like below on all the machines:

sudo docker daemon -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --cluster-store=etcd://10.140.0.2:2379 --cluster-advertise=eth0:2376

Create Overlay Network

Overlay Network spread across different machines and enables network communication between different hosts.

$ sudo docker network create --driver overlay overlay-net1

Start Swarm Manager

Swarm Manager will manager the Swarm Nodes. We will run the containers on the Swarm Manager and they will be run on one of the Swarm Nodes.

$ sudo docker run -d -p 4000:4000 swarm manage -H :4000 --replication --advertise 10.140.0.2:4000  etcd://10.140.0.2:2379

Start Swarm Nodes

Swarm Nodes do the actual heavy lifting of running the container. We will start the Swarm Nodes while pointing them to the Etcd endpoint. On 10.140.0.3, run the following

$ sudo docker run -d swarm join --advertise=10.140.0.3:2375 etcd://10.140.0.2:2379

On 10.140.0.4, run the following

$ sudo docker run -d swarm join --advertise=10.140.0.4:2375 etcd://10.140.0.2:2379

Our tiny cloud is up and running.

Running Container on the Cloud

Now we just fire the regular Docker commands against the Swarm manager port.

sudo docker -H :4000 ps

Whenever we need to run a container which needs to talk to other containers, we put it in the overlay network.

sudo docker -H :4000 run -d --net=overlay-net1 -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql

There we have our multi-node cloud with Docker ready to go!

Next Post Previous Post