root@moreality:~# docker swarm join --token SWMTKN-1-3lbmvgaz07xylkgofhukkhmxvescqa8l5b3lasw94ocor5hrp7-b0rtmn323qazz2eidfund4orn 32.121.74.215:2377 This node joined a swarm as a worker.
进入worker的机器, 运行:
1
root@asdf:~# docker node ls
即可查看目前的manager和worker节点
1 2 3 4
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION 7we8v0yso3js5u56ypk60ffbb * iZ0jlfl8zktqzybrmxk2s9Z Ready Active Leader 20.10.17 mka9mbeqhivcaxdwqdb29gio3 moreality Ready Active 18.09.7
其中VERSION表示对应机器上docker的版本
创建和加入服务
按照官网输入如下创建服务的命令
1
docker service create --replicas 1 --name helloworld alpine ping docker.com
docker service create: 创建服务。
--name: 命名服务helloworld。
--replicas: 指定一个副本运行
alpine ping docker.com: 将服务定义为执行命令的 Alpine Linux 容器并运行ping docker.com。
然后运行docker service ls就可以查看刚才建立的服务了:
1 2 3
root@asdf:~# docker service ls ID NAME MODE REPLICAS IMAGE PORTS s119e5qgow4i helloworld replicated 1/1 alpine:latest
查看和监视服务的命令
docker service inspect --pretty helloworld: 查看该服务的配置详细信息, 加pretty表示以易读的方式打印, 可以选择不加展示原始的json信息
docker service ps <SERVICE-ID>: 查看正在运行该服务的节点
1 2 3 4 5 6 7 8 9
root@asdf:~# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION 7we8v0yso3js5u56ypk60ffbb * asdf Ready Active Leader 20.10.17 mka9mbeqhivcaxdwqdb29gio3 moreality Ready Active 18.09.7
root@asdf:~# docker service ps helloworld ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS mwl78f2p88zi helloworld.1 alpine:latest asdf Running Running 4 minutes ago
则表示该服务证运行在worker节点上(对比NODE和HOSTNAME)
在服务运行的节点上使用docker ps可以查看详细信息
服务扩展 (Scale)
将服务部署后, 可以很轻松的扩展服务的任务数量
在服务(service)中运行的容器(container)称之为任务(task)
运行:
1
docker service scale <SERVICE-ID>=<NUMBER-OF-TASKS>
如对于上面的hello world
运行:
1
docker service scale helloworld=5
表示将任务数增加到5个
1 2 3 4 5 6 7 8 9
root@asdf:~# docker service scale helloworld=5 helloworld scaled to 5 overall progress: 5 out of 5 tasks 1/5: running 2/5: running 3/5: running 4/5: running 5/5: running verify: Service converged
这时我们再在worker上用docker service ps helloworld可以发现:
1 2 3 4 5 6 7
root@asdf:~# docker service ps helloworld; ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS mwl78f2p88zi helloworld.1 alpine:latest asdf Running Running 15 minutes ago 58nn77cngpkk helloworld.2 alpine:latest moreality Running Running 4 minutes ago ibgir80e5vfc helloworld.3 alpine:latest moreality Running Running 4 minutes ago vo1de2mmagle helloworld.4 alpine:latest moreality Running Running 4 minutes ago tg6evmi7uk8u helloworld.5 alpine:latest asdf Running Running 4 minutes ago
任务被均匀地分配到了不同的主机上
服务删除
1
docker service rm helloworld
就把helloworld服务删了
运行
1 2 3
root@asdf:~# docker service inspect helloworld [] Status: Error: no such service: helloworld, Code: 1
可以使用 docker service inspect --pretty redis查看更新是否失败和错误信息
如果更新失败, 可以直接使用
1
docker service update redis
重新执行更新
可以使用docker service ps redis查看整个滚动更新:
1 2 3 4 5 6 7 8 9
root@asdf:~# docker service ps redis ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS nwub9yrzm9q5 redis.1 redis:3.0.7 moreality Running Running about a minute ago ptauimv31hx8 \_ redis.1 redis:3.0.6 moreality Shutdown Shutdown about a minute ago ahzh1kalw2yo redis.2 redis:3.0.7 asdf Running Running about a minute ago hokzeh28ueaq \_ redis.2 redis:3.0.6 asdf Shutdown Shutdown about a minute ago 8ypuvaxvzorj redis.3 redis:3.0.7 asdf Running Running about a minute ago g0ddca51aqwy \_ redis.3 redis:3.0.6 moreality Shutdown Shutdown 2 minutes ago
root@asdf:~# docker service ps -f "desired-state=running" redis ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS nwub9yrzm9q5 redis.1 redis:3.0.7 moreality Running Running 9 minutes ago
tc04mbhfpbgt redis.2 redis:3.0.7 moreality Running Running 3 minutes ago
qjfrb4g4l7jl redis.3 redis:3.0.7 moreality Running Running 3 minutes ago
root@asdf:~# docker service ps -f "desired-state=shutdown" redis ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS ptauimv31hx8 redis.1 redis:3.0.6 moreality Shutdown Shutdown 10 minutes ago
ahzh1kalw2yo redis.2 redis:3.0.7 asdf Shutdown Shutdown 4 minutes ago
hokzeh28ueaq \_ redis.2 redis:3.0.6 asdf Shutdown Shutdown 10 minutes ago
8ypuvaxvzorj redis.3 redis:3.0.7 asdf Shutdown Shutdown 4 minutes ago
g0ddca51aqwy \_ redis.3 redis:3.0.6 moreality Shutdown Shutdown 11 minutes ago
可以采用
1
docker node update --availability active <NODE-ID>