Deploy services to a swarm
In this blog, we will walk you through the process of deploying services to your Docker Swarm cluster, covering the essential steps and best practices. By the end of this tutorial, you will be able to confidently deploy and manage services on your Swarm cluster
If this is your first time watching this blog, please visit the below link. In this, we will mention how to create the Docker Swarm cluster.
Create a Docker service:
Use the docker service create command to create a service. Specify the desired options, such as the service name, number of replicas, exposed ports, and the Docker image to use.
docker service create --name myapp --replicas 3 -p 8080:80 nginx
This command creates a service named “myapp” with three replicas, exposes port 8080 on the host, and uses the “nginx” Docker image.
Verify service deployment:
Run the following command to check the status of the deployed services
docker service ls
- This command will list all the services running in the Swarm along with their status, replicas, and other details.
- Ensure that the desired number of replicas for your service are running and that there are no issues reported.
Scale the service:
If you want to scale the service to more or fewer replicas, you can use the docker service scale command.
If you want to reduce the service, you can use the same command.
docker service scale myapp=5
Update the service:
If you need to update the service configuration, such as changing the Docker image, adding environment variables, or modifying other options, use the docker service update command.
docker service update --image newimage:tag myapp
To deploy a service in global mode:
In Docker Swarm, the “global” mode is a deployment mode that allows you to run a service with a single replica on each node in the Swarm cluster. This ensures that every node in the cluster runs an instance of the service.
The global mode is particularly useful for deploying services that need to run on every node in the Swarm cluster, such as monitoring agents or logging collectors. It ensures consistent distribution and availability across the entire cluster.
docker service create --name my-app --mode global my-image
Verify the service deployment:
docker service ls
Scale the service
docker service scale my-app=3
For publishing the service
docker service update --publish-add 8080:80 my-app
If you need to update the published port for an existing service
First remove the service
docker service update --publish-rm 8080:80 myapp
Add the service:
docker service update --publish-add 8082:80 myapp
Demo
https://www.youtube.com/watch?v=jMoPDPxZVi8
Thanks!