docker stack
单机模式下,我们可以使用 Docker Compose 来编排多个服务。
Docker Swarm 通过 Docker Stack,只需对已有的 docker-compose.yml 配置文件稍加改造就可以完成 Docker 集群环境下的多服务编排。
stack->task->service
Docker compose示例见: Docker常用操作
-
myservice-stack.yml
version: "3.4" services: alpine: image: alpine # 镜像名称 command: # 命令 - "ping" - "www.baidu.com" networks: - "my-overlay" # 指定网络 deploy: replicas: 1 # 负载/实例数量 restart_policy: condition: on-failure resources: limits: cpus: "0.1" memory: 50M #depends_on: # 会阻塞nginx服务启动? # - nginx nginx: image: nginx networks: - "my-overlay" ports: - "8081:80" # 端口映射 deploy: replicas: 1 # 负载/实例数量 restart_policy: condition: on-failure resources: limits: cpus: "0.1" memory: 50M networks: my-overlay: # 网络需先创建好 external: true
docker stack deploy [OPTIONS] STACK Deploy a new stack or update an existing stack -c, --compose-file 指定compose文件路径,"-"从stdin读取 --prune 修剪不再被引用的服务 --resolve-image string 查询resistry服务解析image,always|changed|never(default "always") --with-registry-auth 登录registry参数
docker network create -d overlay my-overlay docker stack deploy -c myservice-stack.yml myservice
docker stack deploy -c myservice-stack.yml myservice Creating service myservice_nginx Creating service myservice_alpine docker stack ls NAME SERVICES myservice 2 docker stack ps myservice ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS qig31fucyoj4 myservice_alpine.1 alpine:latest swarm-manager Running Running about a minute ago pwj4wikgw70i myservice_nginx.1 nginx:latest swarm-node2 Running Running 59 seconds ago docker stack services myservice ID NAME MODE REPLICAS IMAGE PORTS w55p2eetzq7d myservice_alpine replicated 1/1 alpine:latest k7g89zs8k68a myservice_nginx replicated 1/1 nginx:latest *:8081->80/tcp