Docker Machine is a tool that offers solutions by deploying a cluster of Docker running on a local machine or a cloud platform. You can use Docker Swarm to enhance high performance by allocating it to different Docker hosts in a cluster.
Cluster feature is an essential feature for high availability. It allows the user to manage clusters in what is known as a swarm cluster manager. This further allows the user to increase the number of container instance for a single application.
In this case, we will use Docker 1.12 which doesn’t require an external discovery service as it comes with an inbuilt memory key-value store for this purpose. We will try to setup Docker Swarm cluster on Ubuntu version 16.04.
To get started, you will need:
- Knowledge of Docker and Ubuntu version 16.04
- At least two nodes with Docker installed.
- An IP address that is configured on Worker Node and Manager Node.
Let’s take a look at the nodes
Docker Clusters comprises of two major parts:
Manager Nodes: They deal with management of clusters including maintaining the state of clusters, preparation of services, and servicing swarm mode endpoints (HTTP API). A key feature in the manager quorum that store critical data about the Swarm cluster.
Worker Nodes: These execute containers. They are not involved in scheduling decisions. A Worker node must have no less than one Manager Node. It’s possible to upgrade a worker node to a Manager node when a latter is under maintenance.
Before you start, make sure your repository system is up-to-date. Use the command below to update it:
sudo apt-get update -y && sudo apt-get upgrade -y
Once your system is updated with the current repository system, restart your machine to apply these updates.
Step 1: Installing Docker
Install Docker machine on each of the nodes. Since Docker Swarm is doesn’t exist in the default mode of Ubuntu version 16.04 default, you need to run it first.
Run the command below to install the necessary packages:
sudo apt-get install apt-transport-https software-properties-common ca-certificates -y
Don’t forget to include the GPG key for your Docker’s application:
wget https://download.docker.com/linux/ubuntu/gpg && sudo apt-key add gpg
Also, include the Docker Swarm repository then update the system:
sudo echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable" >> /etc/apt/sources.list sudo apt-get update -y
Lastly, use command below to install the Docker engine:
sudo apt-get install docker-ce -y
Once the installation of Docker engine is done, run the Docker and let it run within the boot time:
sudo systemctl start docker && sudo systemctl enable docker
In its default mode, Docker daemon can only run as the system’s root user, so other users cannot access it unless they use sudo. To run your Docker without sudo, you need to create a Uniform group known as docker then add the number of users you want. Run the command below to make the process successful:
sudo groupadd docker && sudo usermod -aG docker dockeruser
Log out the system then log in to updates your membership. Run the command on individually for each node.
Step 2: Configuring firewall
A specific cluster should have at least one node that acts as a Manager nodes, but for a smooth process, three managers should be used. For this tutorial, let’s takes the first node as the Swarm manager while the remaining two nodes will represent the worker nodes.
Next, you need to open network ports on these nodes to make part of a cluster that will make the application function properly. So, using the UFW firewall allow access to the following ports 80, 2377, 2376, 2376, 4789, and 7946. Then run the command below:
sudo ufw allow 2376/tcp && sudo ufw allow 7946/udp && sudo ufw allow 7946/tcp && sudo ufw allow 80/tcp && sudo ufw allow 2377/tcp && sudo ufw allow 4789/udp
Now, reload the firewall and set it up to start once it boots:
sudo ufw reload && sudo ufw enable
Restart your Docker system to apply the new rules:
sudo systemctl restart docker
Step 3: Creating the Docker Cluster
In this case, we have decided that our cluster manager will be node-1, so log in to your node as follows:
docker-machine ssh node-1
After running this command prompt, the system will change to show that you’re logged into that node. To identify the node as the Swarm manager, run the command below:
root@node-1:˜# docker swarm init --advertise-addr node_ip_address
In this case, nope_ip_address indicates the node’s IP address.
The output should look like this:
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-5p5f6p6tv1cmjzq9ntx3zmck9kpgt355qq0uaqoj2ple629dl4-5880qso8jio78djpx5mzbqcfu 192.168.0.103:2377
To add a manager to this swarm, run ‘docker swarm join-token manager’ and follow the instructions.
This token will be useful in adding nodes to the swarm cluster in the next process. The Docker machine will join the swarm cluster based on the token provided to the command.
Now, go ahead and check the Manager Node status by typing the command below:
Check to see if the output look something like this:
Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 17.09.0-ce Storage Driver: overlay2 Backing Filesystem: extfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog Swarm: active NodeID: iwjtf6u951g7rpx6ugkty3ksa Is Manager: true ClusterID: fo24c1dvp7ent771rhrjhplnu Managers: 1 Nodes: 1 Orchestration: Task History Retention Limit: 5 Raft: Snapshot Interval: 10000 Number of Old Snapshots to Retain: 0 Heartbeat Tick: 1 Election Tick: 3 Dispatcher: Heartbeat Period: 5 seconds CA Configuration: Expiry Duration: 3 months Force Rotate: 0 Autolock Managers: false Root Rotation In Progress: false Node Address: 192.168.0.103 Manager Addresses: 192.168.0.103:2377 Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 06b9cb35161009dcb7123345749fef02f7cea8e0 runc version: 3f2f8b84a77f73d38244dd690525642a72156c64 init version: 949e6fa Security Options: apparmor seccomp Profile: default Kernel Version: 4.4.0-45-generic Operating System: Ubuntu 16.04.1 LTS OSType: linux Architecture: x86_64 CPUs: 1 Total Memory: 992.5MiB Name: Manager-Node ID: R5H4:JL3F:OXVI:NLNY:76MV:5FJU:XMVM:SCJG:VIL5:ISG4:YSDZ:KUV4 Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false
Step 4: Adding Worker Nodes to the Swarm Cluster
Now that the Worker Node is configured, add it to the Cluster.
Start by connecting to node-2 from your local machine:
$ docker-machine ssh node-2
Then complete the command with the text your_swarm_token being the token you received when the cluster was created, while manager_node_ip_address represents the IP address of the Swarm manager:
Once the command is successfully executed, you will see the output below:
Log out of the node-2, and log into the Manager Node and then run the command below to list:
docker node ls
The Worker Node should look like this:
Step 5: Launch Services in the Docker Swarm
Now that you have installed Docker Swarm successfully, it’s time to test container and see how the Swarm Manager handles it.
So, on your Manager Node, set up a web service with the following command:
docker service create --name webserver -p 80:80 httpd
In this command, we intend to map port 80 in the container to port 80 on the cluster to gain full access to the default mode on Apache server.
Check the service is running by typing this command:
docker service ls
You should see an output like this:
ID NAME MODE REPLICAS IMAGE PORTS
nnt7i1lipo0h webserver replicated 0/1 apache:latest *:80->80/tcp
Step 6: Testing Docker Swarm
Now the Apache server should be running smoothly on Manager Node. The web server can be accessed by directing your browser to http://node-1_ip-address.
You should see the following output:
Now Apache server is available on each node.
Docker Service can scale a service. To test for the server’s high availability, stop running Docker on the Worker Node:
sudo systemctl stop docker
Use the following command to determine the Apache Server status:
docker service ps webserver
A fresh container on the Manager Node should be launched with the following output:
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS ia2qc8a5f5n4 webserver.1 httpd:latest Manager-Node Ready Ready 1 second ago 7roily9zpjvq \_ webserver.1 httpd:latest Worker-Node Shutdown Running 15 seconds ago r7nzo325cu73 webserver.2 httpd:latest Manager-Node Running Running 23 minutes ago
Congratulations! It’s as simple as that. Now you have a complete set up of Docker Swarm on Ubuntu version 16.04. Also, you now know how to do several management tasks on Swarm Cluster. With this basic understanding, you still need to configure your Docker cluster with several Manager Node depending on your administration’s requirements.
Check out the top 3 VPS services:
- Check the recommendations for the best VPS and get a suitable one.