Docker Swarm-Service

 

How services works : 

To deploy an application image when Docker Engine is in swarm mode, you create a service. Frequently a service is the image for a microservice within the context of some larger application. Examples of services might include an HTTP server, a database, or any other type of executable program that you wish to run in a distributed environment.

When you create a service, you specify which container image to use and which commands to execute inside running containers. You also define options for the service including:

-- the port where the swarm makes the service available outside the swarm

-- an overlay network for the service to connect to other services in the swarm

-- CPU and memory limits and reservations

-- a rolling update policy

-- the number of replicas of the image to run in the swarm


Services, tasks, and containers

When you deploy the service to the swarm, the swarm manager accepts your service definition as the desired state for the service. Then it schedules the service on nodes in the swarm as one or more replica tasks. The tasks run independently of each other on nodes in the swarm.

For example, imagine you want to load balance between three instances of an HTTP listener. The diagram below shows an HTTP listener service with three replicas. Each of the three instances of the listener is a task in the swarm.


A container is an isolated process. In the swarm mode model, each task invokes exactly one container. A task is analogous to a “slot” where the scheduler places a container. Once the container is live, the scheduler recognizes that the task is in a running state. If the container fails health checks or terminates, the task terminates.

Tasks and scheduling

A task is the atomic unit of scheduling within a swarm. When you declare a desired service state by creating or updating a service, the orchestrator realizes the desired state by scheduling tasks. For instance, you define a service that instructs the orchestrator to keep three instances of an HTTP listener running at all times. The orchestrator responds by creating three tasks. Each task is a slot that the scheduler fills by spawning a container. The container is the instantiation of the task. If an HTTP listener task subsequently fails its health check or crashes, the orchestrator creates a new replica task that spawns a new container.

A task is a one-directional mechanism. It progresses monotonically through a series of states: assigned, prepared, running, etc. If the task fails the orchestrator removes the task and its container and then creates a new task to replace it according to the desired state specified by the service.

Docker Swarm-Service Commands

docker run -it alpine ping 172.31.15.233
docker service --help
docker service create --name myservice -d  alpine ping 172.31.15.233
docker service inspect <<service name>> | less
docker service logs <<service name>>

Docker service ps command
----------------------------
docker service create --name myservice -d --replicas 4 alpine ping <<host IP>>
docker service ps myservice
remove containers on one of the worker node and find the status of the service

Docker service scale command
-------------------------------
docker service scale myservice=2
docker service scale myservice=5
docker service rm myservice

Docker service port mapping
---------------------------------
docker service --name webservice create -d -p 80:80 nginx

Docker service to run it on each server
-----------------------------------------
docker service create --name webservice -d --mode=global --publish=80:80 nginx

Docker Constraint
-----------------------------------
docker service create --name webservice -d --constraint="node.role==manager" --publish=80:80 nginx
docker service create --name webservice -d  --constraint="node.role==worker"  --publish 80:80 nginx

Docker node labels
----------------------------------
docker node update --label-add="webserver=true" worker01
docker service create --name webservice -d --constraint="node.labels.webserver==true" --publish 80:80 nginx

Docker service update and Rollback
----------------------------------
docker service create --name redis --replicas 5 --update-delay 10s redis:3.0.6
docker service ls
docker service ps redis
docker service update redis --image redis:3.0.7
docker service update redis --image redis:21
docker service ls
docker service rollback redis

Comments

Popular posts from this blog

Terraform

Scrum Master Interview help - Bootcamp

Kubernetes