How services work (2024)

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

When you create a service, you specify which container image to use and whichcommands to execute inside running containers. You also define options for theservice 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

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

For example, imagine you want to load balance between three instances of an HTTPlistener. 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.

How services work (1)

A container is an isolated process. In the Swarm mode model, each task invokesexactly one container. A task is analogous to a “slot” where the schedulerplaces a container. Once the container is live, the scheduler recognizes thatthe task is in a running state. If the container fails health checks orterminates, the task terminates.

Tasks and scheduling

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

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

The underlying logic of Docker's Swarm mode is a general purpose scheduler andorchestrator. The service and task abstractions themselves are unaware of thecontainers they implement. Hypothetically, you could implement other types oftasks such as virtual machine tasks or non-containerized process tasks. Thescheduler and orchestrator are agnostic about the type of the task. However, thecurrent version of Docker only supports container tasks.

The diagram below shows how Swarm mode accepts service create requests andschedules tasks to worker nodes.

How services work (3)

Pending services

A service may be configured in such a way that no node currently in theswarm can run its tasks. In this case, the service remains in state pending.Here are a few examples of when a service might remain in state pending.

TipIf your only intention is to prevent a service frombeing deployed, scale the service to 0 instead of trying to configure it insuch a way that it remains in pending.

  • If all nodes are paused or drained, and you create a service, it ispending until a node becomes available. In reality, the first node to becomeavailable gets all of the tasks, so this is not a good thing to do in aproduction environment.

  • You can reserve a specific amount of memory for a service. If no node in theswarm has the required amount of memory, the service remains in a pendingstate until a node is available which can run its tasks. If you specify a verylarge value, such as 500 GB, the task stays pending forever, unless youreally have a node which can satisfy it.

  • You can impose placement constraints on the service, and the constraints maynot be able to be honored at a given time.

This behavior illustrates that the requirements and configuration of your tasksare not tightly tied to the current state of the swarm. As the administrator ofa swarm, you declare the desired state of your swarm, and the manager works withthe nodes in the swarm to create that state. You do not need to micro-manage thetasks on the swarm.

There are two types of service deployments, replicated and global.

For a replicated service, you specify the number of identical tasks you want torun. For example, you decide to deploy an HTTP service with three replicas, eachserving the same content.

A global service is a service that runs one task on every node. There is nopre-specified number of tasks. Each time you add a node to the swarm, theorchestrator creates a task and the scheduler assigns the task to the new node.Good candidates for global services are monitoring agents, anti-virus scannersor other types of containers that you want to run on every node in the swarm.

The diagram below shows a three-service replica in gray and a global servicein black.

How services work (5)

Learn more

  • Read about how Swarm modenodes work.
  • Learn howPKI works in Swarm mode.
How services work (2024)
Top Articles
Latest Posts
Article information

Author: Fredrick Kertzmann

Last Updated:

Views: 6142

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Fredrick Kertzmann

Birthday: 2000-04-29

Address: Apt. 203 613 Huels Gateway, Ralphtown, LA 40204

Phone: +2135150832870

Job: Regional Design Producer

Hobby: Nordic skating, Lacemaking, Mountain biking, Rowing, Gardening, Water sports, role-playing games

Introduction: My name is Fredrick Kertzmann, I am a gleaming, encouraging, inexpensive, thankful, tender, quaint, precious person who loves writing and wants to share my knowledge and understanding with you.