create docker swarm

1. Configure Cluster Hosts

For whichever number of servers deployed, one will be the manager node and the rest worker nodes. Manager node handles all cluster management tasks while the worker nodes run the containers. In this article, we will use two nodes, for example:

192.0.2.11 manager
192.0.2.12 worker-1
  1. SSH to all Ubuntu server as a non-root user with sudo access.

  2. Edit host file /etc/hosts in all the nodes.

    $ sudo nano /etc/hosts
  3. Add the following code in the file.

    192.0.2.11 manager
    192.0.2.12 worker-1
  4. Ping all the nodes using their hostnames.

    $ ping -c 4 manager
    $ ping -c 4 worker-1

2. Install Docker CE

Install Docker CE on all the nodes. Perform all stages in this step on all nodes.

  1. Update the system packages.

    $ sudo apt update
  2. Install all required packages.

    $ sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
  3. Install Docker repository signing keys.

    $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  4. Add the Docker repository.

    $ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
  5. Update the system packages.

    $ sudo apt update
  6. Install Docker CE.

    $ sudo apt install docker-ce -y
  7. Confirm the status of Docker.

    $ sudo systemctl status docker
  8. Enable the Docker service to automatically run at system boot.

    $ sudo systemctl enable docker
  9. Add your current user to docker group.

    $ sudo usermod -aG docker ${USER}

3. Create Docker Swarm Cluster

To create a Docker Swarm cluster, you will first need to initialize the swarm mode on the manager node. Then, join the worker nodes to the cluster. Strictly use the nodes IP address.

  1. Initialize the swarm mode.

    $ sudo docker swarm init --advertise-addr 192.0.2.11
  2. Go to the worker-1 node and add it to the cluster. Modify the --token value with your own.

    docker swarm join --token SWMTKN-1-0oo2f0x6fm98likcbnna283lgwd1iao3jm2dj454dddbfk1pvv-bheoj1jjfss7tg1b1c00bk9vm 10.110.111.103:2377
  3. Go to the manager node and verify if all the worker nodes successfully joined the cluster.

    $ sudo docker node ls

4. Deploy Application to the Cluster

  1. Go to the manager node and create a 'Docker getting started web-page' service named docker-tutorial that will run on default http port 80 and expose it to port 80 on the host server.

    $ sudo docker service create --name docker-tutorial --publish 80:80 docker/getting-started
  2. Verify the status of the created service.

    $ sudo docker service ls

5. Create Replicas of the Service

With two nodes in our cluster, we will make two replicas of the service. This will enable access to the service form both the manager and worker nodes.

  1. Create replicas.

    $ sudo docker service scale docker-tutorial=2
  2. Verify the status of the service replicas.

    $ sudo docker service ls
  3. Go to your browser and access the service from all your nodes. For example:

    manager node.

    http://192.0.2.11

    worker-1 node.

    http://192.0.2.12

Additional commands

# run the service, & set it to active
docker service create --name battlebot --hostname battlebot -p 22:22 -p 25:25 -p 53:53 -p 80:80 -p 443:443 -p 587:587 -p 3001:3001 -p 3002:3002 -p 3003:3003 -p 8080:8080 --replicas-max-per-node=1 --with-registry-auth -d -t server-nodemon:latest bash    

################################################# export a container
docker export --output ubuntu-v2.tar ac34f6f48058

# import a container with all changes
docker import ubuntu-v2.tar ubuntu-v2

# attach the same process to the container and start the container
docker run -t -i -d -p 22:22 -p 443:443 -p 2121:21 -p 53:53  imagename:latest
docker exec -it containerid bash

# now attach your own shell to the newly made container
docker attach 26e4b93807fe

# in order to restart your container
docker start 26e4b93807fe

# and re-attach if needed
docker attach 26e4b93807fe

References

Last updated