service
创建 swarm
$ docker swarm init --advertise-addr 192.168.56.205
Swarm initialized: current node (9fu8b0ot055ayazhcxu61jv33) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-1onpqfhmh9krhfkdw5bucfvr4vriulm0ber2f1gmbrqp7eh0m0-ewm3hnddibbq45492igzx2rxr 192.168.56.205:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
发布 stack
$ docker stack deploy -c docker-compose.yml getstartedlab
Creating network getstartedlab_webnet
Creating service getstartedlab_web
Let’s inspect one task and limit the ouput to container ID:
通过 docker service
查询 service
信息
Get the service ID for the one service in our application:
$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
jhkiqvpxov65 getstartedlab_web replicated 3/3 uyinn28/friendlyhello:latest *:80->80/tcp
Docker swarms run tasks that spawn containers. Tasks have state and their own IDs:
$ docker service ps getstartedlab_web
# 结果中的 ID 就是 task ID
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
igyp06sdql0s getstartedlab_web.1 uyinn28/friendlyhello:latest S005_Ubuntu1604S Running Running 6 minutes ago
wuc02ndv48f7 \_ getstartedlab_web.1 uyinn28/friendlyhello:latest S005_Ubuntu1604S Shutdown Shutdown 6 minutes ago
qsssyx6rwz07 getstartedlab_web.6 uyinn28/friendlyhello:latest S005_Ubuntu1604S Running Running 6 minutes ago
to0xg7tp59bk getstartedlab_web.10 uyinn28/friendlyhello:latest S005_Ubuntu1604S Running Running 6 minutes ago
通过 docker container
查询 container
的信息
$ docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ea8167447298 uyinn28/friendlyhello:latest "python app.py" 10 minutes ago Up 9 minutes 80/tcp getstartedlab_web.6.qsssyx6rwz07ppch07jud2rnk
100bcb7eb051 uyinn28/friendlyhello:latest "python app.py" 10 minutes ago Up 9 minutes 80/tcp getstartedlab_web.10.to0xg7tp59bkshrrvgf4yhutp
e12bb83371fd uyinn28/friendlyhello:latest "python app.py" 10 minutes ago Up 9 minutes 80/tcp getstartedlab_web.1.igyp06sdql0s8eo0eyg6dz9lq
354f388506c7 uyinn28/friendlyhello:latest "python app.py" 36 minutes ago Exited (137) 9 minutes ago getstartedlab_web.1.wuc02ndv48f7jp71sm0s0jya6
b712972c07d5 registry:2 "/entrypoint.sh /e..." 24 hours ago Up 24 hours 0.0.0.0:5000->5000/tcp registry
# 注意,<task> 是 NAMES 对应值中,以 `.` 分割的最后一位。
task_id 和 container_id 互查
# 通过 task 值查询 container id
$ docker inspect --format='{{.Status.ContainerStatus.ContainerID}}' <task>
$ docker inspect --format='{{.Status.ContainerStatus.ContainerID}}' igyp06sdql0s
e12bb83371fd9a661e34d16e69dc7c56bb0a832d92ecd620042823da6a807725
Vice versa, inspect the container ID, and extract the task ID:
# 通过 container id 查询 task id
$ docker inspect --format="{{index .Config.Labels \"com.docker.swarm.task.id\"}}" <container>
$ docker inspect --format="{{index .Config.Labels \"com.docker.swarm.task.id\"}}" 100bcb7eb051
to0xg7tp59bkshrrvgf4yhutp
Scale the app
You can scale the app by changing the replicas
value in docker-compose.yml, saving the change, and re-running the docker stack deploy command:
$ docker stack deploy -c docker-compose.yml getstartedlab
Updating service getstartedlab_web (id: oauk2ifq9mfkg1k2kdr8cqgur)
Docker will do an in-place update
, no need to tear the stack down
first or kill any containers.
$ docker container ls -q
ea8167447298
100bcb7eb051
e12bb83371fd
b712972c07d5
Take down the app and the swarm
Take the app down with docker stack rm:
$ docker stack rm getstartedlab
Removing service getstartedlab_web
Removing network getstartedlab_webnet
This removes the app, but our one-node swarm is still up and running (as shown by docker node ls
). Take down the swarm with docker swarm leave --force
.
退出 swarm
- 节点退出
docker swarm leave
- swarm manager 必须使用
--force
强制退出
# 查看节点
$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
9fu8b0ot055ayazhcxu61jv33 * S005_Ubuntu1604S Ready Active Leader
$ docker swarm leave
# swarm manager 必须使用 `--force` 强制退出
Error response from daemon: You are attempting to leave the swarm on a node that is participating as a manager. Removing the last manager erases all current state of the swarm. Use `--force` to ignore this message.
# 退出
$ docker swarm leave --force
Node left the swarm.
$ docker node ls
Error response from daemon: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.
本节命令
docker stack ls # List stacks or apps
docker stack deploy -c <composefile> <appname> # Run the specified Compose file
docker service ls # List running services associated with an app
docker service ps <service> # List tasks associated with an app
docker inspect <task or container> # Inspect task or container
docker container ls -q # List container IDs
docker stack rm <appname> # Tear down an application