Detailed explanation of the command of docker swarm service arrangement

Time:2022-1-15

1、 Introduction

Docker has an orchestration tool docker compose, which can orchestrate and manage multiple docker containers that should form a certain container at the same time. Similarly, in swarm cluster, docker stack can be used to organize and manage a group of associated services.

Docker stack is also a yaml file and a docker compose YML files are similar, and the instructions are basically the same. However, compared with compose, it does not support build, links and network_ mode。 Docker stack has a new instruction deploy.

Note: stack does not support instructions

  

2、 Deploy

Deploy is used to specify the relevant configurations for the deployment and runtime of swarm services, and it will only take effect when deploying a swarm cluster using docker stack deploy. This option is ignored if docker compose up or docker compose run is used. To use the deploy option, the version in the compose file should 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

   Specifies the mode of swarm service discovery

  • endpoint_ Mode: VIP – docker assigns a virtual IP (VIP) to the swarm cluster service as the “front end” for the client to reach the cluster service. Docker routes service requests between clients and available work nodes. The client does not need to know how many nodes participate in the service or the IP / port of these nodes. (this is the default mode)
  • endpoint_mode: dnsrr –

The DNS polling (dnsrr) service found that a single virtual IP was not used. Docker sets the DNS entry for the service, so that the DNS query of the service name returns an IP address list, and the client directly connects to one of them. DNS polling is useful if you want to use your own load balancer or mix 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 tags are set only on the service, not on any container of the service


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

To set the label on the container instead, use the label 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 replica (specify the number of containers). The default value is copied.


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

  (4)PLACEMENT

Specify 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 replica mode (the default mode), you can specify the number of containers that the service runs.


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

  (6)RESOURCES

Resource restriction 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 limits the use of no more than 50m of memory and 0.50 (50%) of available processing time (CPU), and has 20m of memory and 0.25 CPU time (always available).

  (7)RESTART_POLICY

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

  • Condition: none, on failure, and any (the default is any)
  • Delay: how long to wait between restart attempts (default 0)
  • max_ Attempts: the number of attempts to restart (the 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 upgraded at the same time
  • Delay: container upgrade interval
  • failure_ Action: the action after the upgrade fails (continue, rollback and pause. Default pause).
  • Monitor: the time when the update is really successful (ns|us|ms|s|m|h).
  • (default 0s) max_ failure_ Ratio: allowable failure rate during update o
  • Order: operation sequence during update. Stop priority (the old task stops before starting the new task) or start first (start the new task first, and the running tasks overlap briefly) (default stop priority) Note: only V3.0 is supported 4 and later.

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 server. It 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 arrays or dictionaries. Any Boolean value; True / false, yes / no, need to be enclosed in quotation marks to ensure that 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

Open the container’s ports instead of exposing them on the host. They can only be obtained by the associated service. Only internal ports can be specified. 


expose:
 - "3000"
 - "8000"

The above is the details of the command of docker swarm service orchestration. For more information about docker swarm service orchestration, please pay attention to other related articles of developeppaer!