Getting Started with Docker Swarm: Deploying and Updating Services

Getting Started with Docker Swarm: Deploying and Updating Services

Docker Swarm is a container orchestration tool that allows you to manage a cluster of Docker hosts. It provides features such as service discovery, load balancing, and rolling updates, making it easier to deploy and manage your applications.

To get started with Docker Swarm, you'll need a few things:

  1. Docker is installed on your machine

  2. A few Docker hosts (virtual or physical machines) to form the cluster

  3. A way to communicate between the hosts (e.g. Same/shared network)

Once you have these setup, you can follow these steps to create a Docker Swarm cluster:

  1. Initialize the Swarm on one of the hosts:

    $ docker swarm init

    This command creates a new Swarm and makes the current host the manager node.

  2. Join the Swarm on the other hosts:

    $ docker swarm join --token [token] [IP address of manager]

    This command joins the other hosts to the Swarm as worker nodes.

  3. Deploy service to the Swarm [On Master Node]:

    $ docker service create --replicas 4 --name [my-service] [my-image]

    This command creates a new service with 4 replicas and assigns it the name "my-app" using the Docker image "chaitannyaa/calculator".

    Note: 4 replicas are distributed amongts Swarm on master and worker nodes

  4. Check the status of the service:

    $ docker service ls

    This command lists all the services in the Swarm.

  5. Scale the service up or down [on master node]:

    $ docker service scale [my-service]=6

    This command scales the "myapp" service up to 6 replicas.

  6. Access your service from both nodes/host IPs:

  7. Update the service[On master node]:

    $ docker service update --image [my-new-image] [my-service]

    This command updates the "myapp" service to use the Docker image "chaitannyaa/webpage".

That's it! You now have a Docker Swarm cluster up and running, and you can deploy and manage your services with ease. There are many more features and options available with Docker Swarm, so be sure to check out the official documentation for more information.

Summary:

In this blog, We started by setting up a Swarm cluster with two nodes and deploying a simple service using the Docker CLI. We explored the Swarm commands to create a swarm cluster, add worker nodes to the swarm, view the status of the nodes and services, create services, scale services and update services in the cluster. With Swarm's powerful features and easy-to-use CLI, you can quickly and easily deploy and manage containerized applications on a cluster of nodes.

I hope you learned something today with me!

Stay tuned for my next blog on "CICD Pipeline: Jenkins". I will keep sharing my learnings and knowledge here with you.

Let's learn together! I appreciate any comments or suggestions you may have to improve my blog content.

Thank you,

Chaitannyaa Gaikwad

Did you find this article valuable?

Support Chaitannyaa Gaikwad by becoming a sponsor. Any amount is appreciated!