A docker service is a group of containers running the same image, which can be scaled over multiple nodes in a swarm, updated with newer versions etc, thus easily distributed and managed.
We will assume that we have setup a docker swarm with three nodes (single manager node and two worker nodes).
docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION ccn5ut84cgfrylf5wds7q6dcb node2 Ready Active 19.03.2 c19rzvk5c0l8vw5tlqj3vn2sr node3 Ready Active 19.03.2 lqljj1kqrc847x5j1h2jk49kl * node1 Ready Active Leader 19.03.2
Creating a service
We will create a service that runs nginx version 1.16.1 and have two replicas of the same for load balancing purposes. We will name our service ‘webservice’. We will map the container port 80 to docker host’s port 1213.
Run the following on manager node to create a service named ‘webservice’.
docker service create --replicas 2 -p 1213:80 --name webservice nginx:1.16.1
overall progress: 2 out of 2 tasks 1/2: running [==================================================>] 2/2: running [==================================================>] verify: Service converged
List and inspect services
Run the following on Manager node to list services.
docker service ls
ID NAME MODE REPLICAS IMAGE PORTS qsjwqiuiqsnd webservice replicated 2/2 nginx:1.16.1 *:1213->80/tcp
docker service ps webservice
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS eb2x695knsl7 webservice.1 nginx:1.16.1 node1 Running Running 21 minutes ago tz17eogzgciq webservice.2 nginx:1.16.1 node1 Running Running 21 minutes ago
The above output shows that two replicas of nginx webservice that was created, is running on ‘node1’
To get all the details about the service, run the following command on Manager node.
docker service inspect webservice --pretty
ID: qsjwqiuiqsndu88ltx0utzmbd Name: webservice Service Mode: Replicated Replicas: 2 Placement: UpdateConfig: Parallelism: 1 On failure: pause Monitoring Period: 5s Max failure ratio: 0 Update order: stop-first RollbackConfig: Parallelism: 1 On failure: pause Monitoring Period: 5s Max failure ratio: 0 Rollback order: stop-first ContainerSpec: Image: nginx:1.16.1@sha256:0d0af9bc6ca2db780b532a522a885bef7fcaddd52d11817fc4cb6a3ead3eacc0 Init: false Resources: Endpoint Mode: vip Ports: PublishedPort = 1213 Protocol = tcp TargetPort = 80 PublishMode = ingress
Scaling services
We will scale up the services to 5 replicas from 2 replicas.
Run the following command on manager node.
docker service scale web=5
webservice scaled to 5 overall progress: 5 out of 5 tasks 1/5: running [==================================================>] 2/5: running [==================================================>] 3/5: running [==================================================>] 4/5: running [==================================================>] 5/5: running [==================================================>] verify: Service converged
Run the following on Manager node to check the status of service.
docker service ps webservice
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS eb2x695knsl7 webservice.1 nginx:1.16.1 node1 Running Running 2 hours ago tz17eogzgciq webservice.2 nginx:1.16.1 node1 Running Running 2 hours ago vcbs2t5kmjsv webservice.3 nginx:1.16.1 node1 Running Running 7 minutes ago vj0bkro1we0q \_ webservice.3 nginx:1.16.1 node2 Shutdown Shutdown 2 minutes ago dp7wj7r17wa5 webservice.4 nginx:1.16.1 node1 Running Running 7 minutes ago 5eypzevneakz \_ webservice.4 nginx:1.16.1 node2 Shutdown Shutdown 2 minutes ago 23vdife1bvbo \_ webservice.4 nginx:1.16.1 node3 Shutdown Starting 9 minutes ago ja7bf8dumtc8 webservice.5 nginx:1.16.1 node1 Running Running 7 minutes ago saxhoh6muh5e \_ webservice.5 nginx:1.16.1 node2 Shutdown Shutdown 2 minutes ago yxylpxzncfg6 \_ webservice.5 nginx:1.16.1 node3 Shutdown Starting 9 minutes ago
Note that the service shows up with running status with a count of 5 across the nodes.
Running updates on images deployed in swarm
Suppose we want to update the nginx version to 1.17.4, run the following command on Manager node.
docker service update --image nginx:1.17.4 webservice
overall progress: 5 out of 5 tasks 1/5: running [==================================================>] 2/5: running [==================================================>] 3/5: running [==================================================>] 4/5: running [==================================================>] 5/5: running [==================================================>] verify: Service converged
Docker updates the versions one replica at a time and waits for a pre configured interval before updating the next replica. Upon failure, it pauses the update altogether.
Run the following on Manager node, to check the status of updates on all replicas.
docker service ps webservice
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS l2fakwiqc6vd webservice.1 nginx:1.17.4 node2 Running Running 4 minutes ago eb2x695knsl7 \_ webservice.1 nginx:1.16.1 node1 Shutdown Shutdown 4 minutes ago t2yuti85kwnl webservice.2 nginx:1.17.4 node1 Running Running 3 minutes ago tz17eogzgciq \_ webservice.2 nginx:1.16.1 node1 Shutdown Shutdown 3 minutes ago ku2dkabpioxv webservice.3 nginx:1.17.4 node1 Running Running 3 minutes ago vcbs2t5kmjsv \_ webservice.3 nginx:1.16.1 node1 Shutdown Shutdown 3 minutes ago vj0bkro1we0q \_ webservice.3 nginx:1.16.1 node3 Shutdown Shutdown 33 minutes ago 0mstkiea7aac webservice.4 nginx:1.17.4 node3 Running Running 4 minutes ago dp7wj7r17wa5 \_ webservice.4 nginx:1.16.1 node1 Shutdown Shutdown 4 minutes ago 5eypzevneakz \_ webservice.4 nginx:1.16.1 node3 Shutdown Shutdown 33 minutes ago 23vdife1bvbo \_ webservice.4 nginx:1.16.1 node2 Shutdown Shutdown 28 minutes ago iz7nfjgj7jnz webservice.5 nginx:1.17.4 node2 Running Running 4 minutes ago ja7bf8dumtc8 \_ webservice.5 nginx:1.16.1 node1 Shutdown Shutdown 4 minutes ago saxhoh6muh5e \_ webservice.5 nginx:1.16.1 node3 Shutdown Shutdown 33 minutes ago yxylpxzncfg6 \_ webservice.5 nginx:1.16.1 node2 Shutdown Shutdown 28 minutes ago
Deleting a service
Run the following on Manager node to delete a service.
docker service rm webservice
webservice