How Setup and Configure Docker Swarm Cluster on Ubuntu

Docker Swarm is a tool that allows you to deploy a cluster of Docker Hosts. It’s a native clustering tool provided by Docker which provides high-availability and high-performance for your application by distributing it to all nodes inside the swarm cluster.

In this tutorial, we will show you step-by-step how to create a Swarm Cluster using Ubuntu 16.04. We will create a swarm cluster using 2 ubuntu server machines, 1 server node as a manager and 1 another as a worker. And then we will try to deploy the simple Nginx service to the swarm cluster.

Prerequisites

  • 2 or more – Ubuntu 16.04 Server
    • manager     132.92.41.4
    • worker01   132.92.41.5
  • Root privileges

What we will do?

  1. Configure Hosts
  2. Install Docker-ce
  3. Docker Swarm Initialization
  4. Deploying First Service to the Cluster

Step 1 – Configure Hosts

Before installing any packages for the swarm cluster, we will configure the hosts file on both servers.

Run commands below on all servers, ‘manager’ and ‘worker01’.

Edit the ‘/etc/hosts’ file using vim editor.

vim /etc/hosts

Add the following configuration to the end of the line.

132.92.41.4    manager
132.92.41.5    worker01

Save and exit.

Now ping all the nodes using ‘hostname’ instead using IP address.

ping -c 3 manager
ping -c 3 worker01

And make sure it’s working at all hosts.

Configure hosts

Step 2 – Install Docker-ce

To create the swarm cluster, we need to install docker on all server nodes. In this step, we will install Docker-ce Community Edition on both servers manager and worker01.

Install Docker-ce dependencies using the apt command below.

sudo apt install apt-transport-https software-properties-common ca-certificates -y

Now add the Docker key and the Docker-ce repository to our servers.

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add –
sudo echo “deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable” > /etc/apt/sources.list.d/docker-ce.list

Update the repository and install Docker-ce packages using apt install command below.

sudo apt update
sudo apt install docker-ce -y

After the installation is complete, start the docker service and enable it to launch every time at system boot.

systemctl start docker
systemctl enable docker

Docker-ce is now installed on our server nodes.

Next, we will configure docker to run as a normal user or non-root user.

Create a new user named ‘mohammad’ and add it to the ‘docker’ group.

useradd -m -s /bin/bash mohammad
sudo usermod -aG docker mohammad

Now login to the ‘mohammad’ user and run the docker hello-world command as below.

su – mohammad
docker run hello-world

And you will get the hello world from docker as shown below.

Install Docker-ce

Step 3 – Create the Swarm Cluster

In this step, we will create the Swarm Cluster of our nodes. And in order to create the swarm cluster nodes, we need to initialize the swarm mode on the ‘manager’ node and then join the ‘worker01’ node to the cluster.

Initialize the Docker Swarm mode by running the docker command below on the ‘manager’ node.

docker swarm init –advertise-addr 132.92.41.4

And you will get the result as shown below.

Create the Swarm Cluster

You will see ‘join-token’ has been generated by the ‘manager’ node.

Next, we need to add the ‘worker01’ node to the cluster ‘manager’. And to do that, we need a ‘join-token’ from the cluster ‘manager’ node, so make sure to write it on your note.

Run the docker swarm join command on the ‘worker01’ node.

docker swarm join –token SWMTKN-1-5p5ujrr67rl2rlmyvrj56fksblbcrtaeirf7fj5r4snid2vn6y-918gbqr02m64xct43i2ssi4qs 132.92.41.4:2377

Now you will get the result as shown below.

docker swarm join

The ‘worker01’ node has been joined to the cluster.

Check it by running the following command on the ‘manager’ node.

docker node ls

Now you see the ‘worker01’ node has been joined to the swarm cluster.

node has been joined to the swarm cluster

The Swarm Cluster has been created.

Step 4 – Deploying First Service to the Cluster

In this step, we will create and deploy our first service to the swarm cluster. We want to create new service Nginx web server that will run on default http port 80, and then expose it to the port 8080 on the host server, and then try to replicate the nginx service inside the swarm cluster.

Create Service

Create new Nginx service named ‘my-web’ and expose the HTTP port of the container to the port 8080 on the host.

docker service create –name my-web –publish 8080:80 nginx:1.13-alpine

And when it’s created, check using docker service command below.

docker service ls

And you will get the result as shown below.

Create a service

The Nginx service has been created and deployed to the swarm cluster as a service named ‘my-web’, it’s based on the Nginx Alpine Linux, expose the HTTP port of the container service to the port ‘8080’ on the host, and it has only 1 replicas.

Replicas and Scale the Service

Now we will make replicas for the ‘my-web’ service. We will make 2 replicas of the ‘my-web’ service, so the service is accessible on the ‘manager’ and ‘worker01’ nodes.

To replicate the ‘my-web’ service, run the following command.

docker service scale my-web=2

And after it’s complete, check again using docker service command.

docker service ls

And now the server has 2 replicates.

Replicas and Scale the Service

Open your web browser and type the manager node IP address with port 8080.

http://manager:8080/

And you will get the Nginx default page.

Management node

Below is the result from the ‘worker01’ node.

http://worker01:8080/

Worker node

The Swarm Cluster has been created, and the Nginx service has been completed deployed to our Swarm Cluster.

Reference

Share this page:

how setup and configure docker swarm cluster on ubuntu 9
how setup and configure docker swarm cluster on ubuntu 10
how setup and configure docker swarm cluster on ubuntu 11
how setup and configure docker swarm cluster on ubuntu 12

نوشته های مشابه