Create Docker Swarm on Alpine Linux

In this post, I am going to give you a tutorial to setup your own Docker Swarm for small businesses.

A Docker swarm is a cluster of Docker hosts, with one being a master and the others being workings. You can have more than one master, but this is not what we are going to be focusing on today.

Master Node

First thing we need to do is setup the master node. This will be the controller of your Docker Swarm. Workers will not have the priveleges of creating services and stacks, only the Master(s) can do this.

We are also going to use the master as a central point to store persistant files, if your services require it)

Step 1 – Install Alpine Linux

First thing we need to do is setup a new Alpine Linux virtiual machine.
For more information on how to do this, please see my tutorial on Getting Started with Alpine Linux.

Step 2 – Enable the Community Repository Mirror

First of we need to open up /etc/apk/repositories in our editor of choice. I am going to use vim. Once open we need to comment in the line that has the version and community in it.





Final part of this step is to run an apk update to pull down the community repository configuration.

Step 3 – Install, Enable and Start Docker

Now we are going to install Docker. This is a simple apk command.

docker-master:~# apk add docker

Next, we need to tell Alpine to automatically start Docker on boot.

docker-master:~# rc-update add docker
 * service docker added to runlevel default

Finally, lets get Docker running.

docker-master:~# /etc/init.d/docker start
 * Caching service dependencies ...                                 [ ok ]
 * Mounting cgroup filesystem ...                                   [ ok ]
 * /var/log/docker.log: creating file
 * /var/log/docker.log: correcting owner
 * Starting docker ...                                              [ ok ]

Step 4 – Initialise Swarm

Now we need to initilise the Docker Swarm. This enables us to create stacks and services as well as add worker nodes later on.

Again, like most things, it’s one command

docker-master:~# docker swarm init
Swarm initialized: current node (tufha0oua5dvhjcx9cjev3z6t) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-420lg3j258pt24jd5gpl9pox9odhirk3omc91lxksvdnwuakgj-1dvx70ao059lkfse089hikepc

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

Lets keep a note of the docker swarm join command that comes back from your init. We will need this later on.

Step 5 – Install an NFS Server

Now we have Docker ready to go, we need to create somewhere for the persistant stroage to live, if it is required by one or more of your services.

First thing we will do is create a /data directory.

mkdir /data

Next, we are going to install the NFS server.

apk add nfs-utils

Again, like Docker, we need to enable and start the NFS Server.

docker-master:~# rc-update add nfs
 * service nfs added to runlevel default
docker-master:~# /etc/init.d/nfs start
 * Caching service dependencies ...                                 [ ok ]
 * Starting rpcbind ...                                             [ ok ]
 * Starting NFS statd ...                                           [ ok ]
 * Mounting nfsd filesystem in /proc ...                            [ ok ]
 * Starting NFS mountd ...                                          [ ok ]
 * Starting NFS daemon ...                                          [ ok ]
 * Starting NFS smnotify ...                                        [ ok ]

Now we need to tell NFS to expose the /data directory. Lets open up /etc/exports in our preferred editor and add the following line.

/data			*(rw,insecure,no_root_squash,no_subtree_check)

To apply the new export, we will need to reload the NFS server.

docker-master:~# /etc/init.d/nfs reload
 * Reloading /etc/exports ...                                       [ ok ]

We’re Done

The master is now ready to create services on. Keep reading if you keen on setting up a worker or 2.

Worker Node

Step 1 – Copy and Paste

Follow Steps 1 – 3 of the Master Node Tutoral.

Step 2 – Install, Enable and Start NFS Client

First this we need to do is create a /data directory.

mkdir /data

We also need to install NFS utils like we did on the manager node.

apk add nfs-utils

Now we are going to open up /etc/fstab in our desired editor and add an entry.	/data	nfs	defaults,_netdev 0 0

Now we need to enable the nfsmount service to make sure the NFS share is mounted on boot.

docker-worker:~# rc-update add nfsmount
 * service nfsmount added to runlevel default

Next, we are going to start the service.

docker-worker:~# /etc/init.d/nfsmount start
 * Caching service dependencies ...                                 [ ok ]
 * Starting rpcbind ...                                             [ ok ]
 * Starting NFS statd ...                                           [ ok ]
 * Starting NFS sm-notify ...                                       [ ok ]
 * Mounting NFS filesystems ...                                     [ ok ]

Step 3 – Join Worker to the Swarm

Now for the last bit. We are going to add the worker we’ve just created to the swarm. Remember the command we were given when we initialised the swarm on the master? Now we are going to use it.

docker-worker:~# docker swarm join --token SWMTKN-1-420lg3j258pt24jd5gpl9pox9odhirk3omc91lxksvdnwuakgj-1dvx70ao059lkfse089hikepc
This node joined a swarm as a worker.

That’s it. You have created a Docker Swarm.

Leave a Reply

Your email address will not be published. Required fields are marked *