Thursday, December 6, 2018

How to Setup Two Nodes Docker Swarm Cluster - Ubuntu 16.04






Docker Swarm - Docker Swarm is clustering scheduling tool for Docker Container, Using Docker Swarm IT administrator and developers can build a Cluster using Single or Multiple nodes and run container to provide high tolerance application environment.

Using Docker Swarm, we can easily upscale and downscale environment easily. 

Features of Docker Swarm:

  • Cluster Managment integration with Docker Engine
  • Scaling
  •  Desired state of reconciliation
  • Multi-host networking
  • Load Balancing 
  • Rolling update


Prerequisites-
  • Two Linux box installed Docker
  • A user with sudo access on both the system
  • Docker hub repository account
  • Node 1 as Master - 192.168.102.10
  • Node 2 as a worker - 192.168.102.11
Step 1- Install Docker
Please follow my previous blog - How to install Docker in Ubuntu 16.04 LTS

Step 2- Initialize Docker Swarm Cluster: 
Go to Node1 (192.168.102.10) and run the following command to initialise the Docker Swarm Cluster.

node-1$ docker swarm init --advertise-addr 192.168.102.10









Your Docker Swarm cluster has initialized successfully.
Let's check your node status

node-1$ sudo docker node ls

ID                                                     HOSTNAME             STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
xmuozdml3m8hvw6r6l903u6nm *             Server1             Ready               Active                  Leader                         18.09.0

Our Leader(master) node is ready.
Step 3- Join Node2 to Swarm Cluster
To Join the Swarm Cluster run following command from Node 2(192.168.102.11)

node-2$ sudo docker swarm join --token SWMTKN-1-2zqpknpvf91vbbtl0ppknt6sptwkck01jyevoftb76gfc4ren0-bdwaea2vhiy34zqajp5iha6ao 192.168.102.10:2377
This node joined a swarm as a worker.
Our Node2 has successfully joined the Swarm cluster. Let's verify running the command below on Leader node.

node-1$ sudo docker node ls


ID                                                   HOSTNAME              STATUS               AVILABILITY        MANAGER STATUS      ENGINE VERSION
xmuozdml3m8hvw6r6l903u6nm *         Server1             Ready                           Active                     Leader             18.09.0
b0qqkihr9txh9ou7rzd2wvvyf                 ubuntu-18          Ready                           Active                                             18.09.0

Both the node are available on Swarm manager.
Step 4- Setup docker hub login on Leader node"
We need to setup docker hub login on the leader node to pull our image, that we will use in docker-compose.yml file. let's run following command to setup docker hub login.
We will use apache docker image from my docker hub account.


node-1$ sudo docker login

Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: amarsingh3d
Password:   *******
WARNING! Your password will be stored unencrypted in /home/amar/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

Docker hub login has been set up successfully.

Step 5- Create docker-compose.yml file to deploy your stack.
To create docker-compose.yml file go to Node1(192.168.102.10) and run following command.
node-1$ sudo vi docker-compose.yml
Append following lines into the file and save it.

version: "3"
services:
  web:
    # replace username/repo:tag with your name and image details
    image: amarsingh3d/apache2.4 
    deploy:
      replicas: 2
      resources:
        limits:
          cpus: "0.1"
          memory: 1024M
      restart_policy:
        condition: on-failure
    ports:
      - "8181:80"
    networks:
      - webnet
networks:
  webnet:
Please update your docker hub repository username and your image name in the above file (amarsingh3d/apache2.4) , Save and exit from the file.

Step 6- Create the first Stack.
To create your first web stack run the following command on Leader node (192.168.102.10), it will create our first stack and service as well. service that allows our container's access from outside it acts as a load balancer.

node-1$ sudo docker stack deploy -c docker-compose.yml webstack

Creating network webstack_webnet
Creating service webstack_web
Let's check Docker stack and Docker Service.

node-1$ sudo docker stack ls

NAME                SERVICES            ORCHESTRATOR
webstack            1                   Swarm
Check Docker service

node-1$ sudo docker service ls 

ID                  NAME                     MODE            REPLICAS            IMAGE                                        PORTS
djdfv8vaonii   webstack_web        replicated          2/2                 amarsingh3d/apache2.4:latest      *:8181->80/tcp
Check service details including how many replicas are running.

node-1$  sudo docker service ps webstack_web
ID                       NAME                                IMAGE                          NODE                DESIRED STATE       CURRENT STATE            
ifepcsexec8p        webstack_web.1      amarsingh3d/apache2.4:latest   Server1             Running             Running 12 seconds ago
yuxwcemuxlps        webstack_web.2      amarsingh3d/apache2.4:latest   ubuntu-18           Running             Running 14 seconds ago
In the above service detail, you can see both containers are running on different Node.

Our stack has been deployed successfully.
Step 7- Test our container service.
To test our container's service, open browser and access Leader node IP address with port 8181- https://192.168.102.10:8181




We have successfully deployed our Stack on Docker Swarm cluster.


Step 8-  Scale up and Scale down our service. 
We can scale up and scale down our service to add and remove any numbers of the container to server better performance for your application. 

Let's use the following command:
scaleup

node-1$  sudo docker service scale webstack_web=4

webstack_web scaled to 4
overall progress: 4 out of 4 tasks
1/4: running   [==================================================>]
2/4: running   [==================================================>]
3/4: running   [==================================================>]
4/4: running   [==================================================>]
verify: Service converged
Verify running Containers- Now our service webstack_web have 4 running containers 2 on each host:
node-1$ sudo docker service ps webstack_web
ID                         NAME                               IMAGE                           NODE                DESIRED STATE        CURRENT STATE           
opfjx8e5en2i        webstack_web.1       amarsingh3d/apache2.4:latest   Server1             Running              Running 9 minutes ago                            
xi2980nmaaci       webstack_web.2       amarsingh3d/apache2.4:latest   ubuntu-18          Running             Running 9 minutes ago                                                        
95c4jr5ew7m3      webstack_web.3       amarsingh3d/apache2.4:latest   Server1             Running             Running 2 minutes ago                            
t3radpwktovb       webstack_web.4       amarsingh3d/apache2.4:latest   ubuntu-18          Running             Running 2 minutes ago  
scaledown our service:

node-1$  sudo docker service scale webstack_web=2

webstack_web scaled to 2
overall progress: 2 out of 2 tasks
1/2: running   [==================================================>]
2/2: running   [==================================================>]
verify: Service converged

Verify current running container:

node-1$ sudo docker service ps webstack_web
ID                         NAME                               IMAGE                           NODE                DESIRED STATE        CURRENT STATE           
opfjx8e5en2i        webstack_web.1       amarsingh3d/apache2.4:latest   Server1             Running              Running 9 minutes ago                            
xi2980nmaaci       webstack_web.2       amarsingh3d/apache2.4:latest   ubuntu-18          Running             Running 9 minutes ago                     
We are successfully able to scaleup and scaledown our service.

No comments:

Post a Comment