Detailed explanation of Docker Swarm service orchestration commands

Detailed explanation of Docker Swarm service orchestration commands

1. Introduction

Docker has an orchestration tool called docker-compose, which can orchestrate and manage multiple docker containers that make up a task. Similarly, in a Swarm cluster, you can use docker stack to orchestrate and manage a group of related services.

Docker stack is also a yaml file, similar to a docker-compose.yml file, and the instructions are basically the same. But compared to compose, it does not support build, links and network_mode. Docker stack has a new command deploy.

Note: stack does not support instructions

2. Deploy

Deploy is used to specify the configuration related to the deployment and runtime of swarm services, and it will only take effect when the swarm cluster is deployed using docker stack deploy. This option is ignored when using docker-compose up or docker-compose run. To use the deploy option, the version in the compose-file must be 3 or 3+.

version: '3'
services:
  redis:
    image: redis:alpine
    deploy:
      replicas: 6
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure

(1) ENDPOINT_MODE

Specify the swarm service discovery mode

  • endpoint_mode: vip - Docker assigns a virtual IP (VIP) to the swarm cluster service as the "front end" for clients to reach the cluster service. Docker routes requests for services between clients and available worker nodes. The client does not need to know how many nodes are participating in the service or the IP/port of these nodes. (This is the default mode)
  • endpoint_mode: dnsrr -

DNS round-robin (DNSRR) service discovery does not use a single virtual IP. Docker sets up DNS entries for services so that a DNS query for the service name returns a list of IP addresses, and clients connect directly to one of them. DNS round-robin is useful if you want to use your own load balancer, or if you have a mix of Windows and Linux applications.

Note: version 3.3+

version: "3.3"
 
services:
  wordpress:
    image: wordpress
    ports:
      -8080:80
    networks:
      - overlay
    deploy:
      mode: replicated
      replicas: 2
      endpoint_mode: vip
 
  mysql:
    image: mysql
    volumes:
       -db-data:/var/lib/mysql/data
    networks:
       - overlay
    deploy:
      mode: replicated
      replicas: 2
      endpoint_mode: dnsrr
 
volumes:
  db-data:
 
networks:
  overlay:

(2) LABELS

Specifies the label of the service. These labels are set only on the service, not on any of the service's containers.

version: "3"
services:
  web:
    image: web
    deploy:
      labels:
        com.example.description: "This label will appear on the web service"

To set labels on the container instead, use the labels key outside of deploy.

version: "3"
services:
  web:
    image: web
    labels:
      com.example.description: "This label will appear on all containers for the web service"

(3) MODE

Global (only one container per cluster node) or replicas (specify the number of containers). The default value is copied.

version: '3'
services:
  worker:
    image: dockersamples/examplevotingapp_worker
    deploy:
      mode: global

(4) Placement

Specifying constraints and preferences

version: '3'
services:
  db:
    image: postgres
    deploy:
      placement:
        constraints:
          - node.role == manager
          -engine.labels.operatingsystem==ubuntu 14.04
        preferences:
          - spread: node.labels.zone

(5) REPLICAS

If the service is in replicated mode (the default), you can specify the number of containers that should run for the service.

version: '3'
services:
  worker:
    image: dockersamples/examplevotingapp_worker
    networks:
      -frontend
      - backend
    deploy:
      mode: replicated
      replicas: 6

(6) RESOURCES

Resource Limit Configuration

version: '3'
services:
  redis:
    image: redis:alpine
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 50M
        reservations:
          cpus: '0.25'
          memory: 20M

In the following example, the redis service is limited to use no more than 50MB of memory and 0.50 (50%) of the available processing time (CPU), and has 20MB of memory and 0.25 of the CPU time (always available).

(7) RESTART_POLICY

Configure whether and how to restart the container when it exits. Replaces the restart command.

  • condition: none, on-failure, and any (default is any)
  • delay : How long to wait between restart attempts (default 0)
  • max_attempts : The number of restart attempts (default is to restart until successful)
  • window: The window time to wait before confirming whether a restart is successful
version: "3"
services:
  redis:
    image: redis:alpine
    deploy:
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s

(8) UPDATE_CONFIG

How to upgrade the configuration service

  • parallelism: The number of containers that are upgraded at the same time
  • delay: container upgrade interval
  • failure_action: The action after the upgrade fails (continue, rollback, and pause. The default is pause).
  • monitor: The time after which the update is completed successfully (ns|us|ms|s|m|h).
  • (Default 0s) max_failure_ratio: The allowed failure rate during the update.
  • rder: The order of operations during update. Stop first (old tasks are stopped before starting new ones) or Start first (new tasks are started first and running tasks are briefly overlapped) (default stop first) Note: Only supported in v3.4 and above.
version: '3.4'
services:
  vote:
    image: dockersamples/examplevotingapp_vote:before
    depends_on:
      - redis
    deploy:
      replicas: 2
      update_config:
        parallelism: 2
        delay: 10s
        order: stop-first

(9) depends_on

Represents dependencies between services

version: '3'
services:
  web:
    build: .
    depends_on:
      -db
      - redis
  redis:
    image: redis
  db:
    image: postgres

(10) DNS

Custom DNS servers. Can be a single value or a list.

dns: 8.8.8.8
dns:
  - 8.8.8.8
  - 9.9.9.9

(11) dns_search

dns_search: example.com
dns_search:
  - dc1.example.com
  - dc2.example.com

(12) Environment

Add environment variables. You can use an array or a dictionary. Any Boolean values; true/false, yes/no, need to be enclosed in quotes to ensure they are not converted to True or False by the YML parser.

environment:
  RACK_ENV: development
  SHOW: 'true'
  SESSION_SECRET:
 
environment:
  - RACK_ENV=development
  - SHOW=true
  -SESSION_SECRET

(13) expose

Opening ports in containers does not expose ports on the host; they are accessible only to the associated services. Only internal ports can be specified. 

expose:
 - "3000"
 - "8000"

The above is the detailed content of the Docker Swarm service orchestration command. For more information about Docker Swarm service orchestration, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of Docker Swarm service discovery and load balancing principles
  • Detailed explanation of docker swarm cluster failures and exceptions
  • Detailed explanation of using Docker 1.12 to build a multi-host Docker swarm cluster
  • How to install Docker and use it in Docker Swarm mode
  • Detailed explanation of Docker Compose service orchestration

<<:  Summary of MySQL slow log related knowledge

>>:  Experience of redesigning the homepage of TOM.COM

Recommend

Five things a good user experience designer should do well (picture and text)

This article is translated from the blog Usability...

Specific use of Linux gcc command

01. Command Overview The gcc command uses the C/C...

How to build ssh service based on golang image in docker

The following is the code for building an ssh ser...

Some lesser-known sorting methods in MySQL

Preface ORDER BY 字段名升序/降序, I believe that everyon...

What are the benefits of semantic HTML structure?

one: 1. Semantic tags are just HTML, there is no ...

Two ways to prohibit clearing the input text input cache in html

Most browsers will cache input values ​​by defaul...

How to install MySQL and Redis in Docker

This article is based on the CentOS 7.3 system en...

Q&A: Differences between XML and HTML

Q: I don’t know what is the difference between xml...

Implementing password box verification information based on JavaScript

This article example shares the specific code of ...

How to deal with too many Docker logs causing the disk to fill up

I have a server with multiple docker containers d...

Docker+selenium method to realize automatic health reporting

This article takes the health reporting system of...

Methods and steps for Etcd distributed deployment based on Docker

1. Environmental Preparation 1.1 Basic Environmen...

Some common advanced SQL statements in MySQL

MySQL Advanced SQL Statements use kgc; create tab...