环境准备:
manager:192.168.1.200
node1:192.168.1.211
node2:192.168.1.212
1.加入管理节点并初始化swarm(manager上面运行)
docker swarm init --advertise-addr 192.168.1.200
执行后manager会自动加入到swarm集群,查看命令docker info
image.png2.将node节点加入到swarm机器
a、首先在manager上面执行命令:
docker swarm join-token worker
得到如下脚本:
image.pngdocker swarm join \
--token SWMTKN-1-2wauqqvmxrv8lri2092xdxf0imhd340038eg8i58hq21kvy0p9-9ojr35v4bnunlcndkegeb72l4 \
192.168.1.200:2377
注意:脚本命令以实际在manager执行docker swarm join-token worker为准
b、在node端执行以上脚本命令即可加入到集群
c、在manager机器上面执行命令查看集群node等信息
image.png3.基本命令
示例:在swarm部署一个service集群
3.1 创建
[skc@dev-11 ubt14-hadoop-hbase]$ docker service create --replicas 1 --name helloworld alpine ping [docker.com](http://docker.com/)
u0p8d9j0p8x2bbv9e8w43ztfp
其中, --name 指定了service的名称; --replicas 指定了需要几个task同时运行;alpine ping docker.com 是使用alpine容器执行ping docker.com 的命令。
3.2 查看信息
使用docker service inspect --pretty helloworld 来查看service的信息。
使用docker service ps helloworld 来查看helloworld的服务在哪个节点上运行。
[skc@dev-11 ubt14-hadoop-hbase]$ docker service ps helloworld
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
hx9lqu9ksq1v helloworld.1 alpine:latest dev-12 Running Running about a minute ago
mejq6j42ozv0 \_ helloworld.1 alpine:latest dev-11 Shutdown Failed about a minute ago "task: non-zero exit (1)"
上图中出现两个task,原因是之前在dev-11上执行命令使task挂掉了,swarm自动在dev-12上新启动了一个task。此时在 dev-12上执行 docker ps,可以看到有容器正在运行。
[skc@dev-12 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a075e16a7dd alpine@sha256:dfbd4a3a8ebca874ebd2474f044a0b33600d4523d03b0df76e5c5986cb02d7e8 "ping [docker.com](http://docker.com/)" About a minute ago Up About a minute helloworld.1.hx9lqu9ksq1vp9dfx2hs0dlk8
3.3 修改service的task个数
$ docker service scale <SERVICE-ID>=<NUMBER-OF-TASKS>
[skc@dev-11 ubt14-hadoop-hbase]$ docker service scale helloworld=2
helloworld scaled to 2
[skc@dev-11 ubt14-hadoop-hbase]$ docker service ps helloworld
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
hx9lqu9ksq1v helloworld.1 alpine:latest dev-12 Running Running 10 minutes ago
mejq6j42ozv0 \_ helloworld.1 alpine:latest dev-11 Shutdown Failed 10 minutes ago "task: non-zero exit (1)"
0kxu1cqetga0 helloworld.2 alpine:latest dev-11 Running Running 8 seconds ago
3.4 删除service
docker service rm helloworld
3.5 更改节点的availablity状态
swarm集群中node的availability状态可以为 active或者drain。其中active状态下,node可以接受来自manager节点的任务分派;而drain状态下,node节点会结束task,且不再接受来自manager节点的任务分派。
$ docker node update --availability drain worker1
$ docker node update --availability active worker1
创建xxbmm-shop商城swarm集群
docker service create \
--name shop-service \
--replicas 3 \
-p 9091:8081 \
--mount type=bind,source=/home/xxbmm/src/webapps,destination=/opt/local/tomcat8/webapps \
--mount type=bind,source=/root/logs,destination=/opt/local/tomcat8/logs \
xxbmm.shop_07271053/centos7:07271053
![image.png](https://img.haomeiwen.com/i15631858/5078d425aa30f4e8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image.png](https://img.haomeiwen.com/i15631858/9661173b7d011dab.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image.png](https://img.haomeiwen.com/i15631858/268bc6a71b2a0698.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
观察到有启动失败的,3个service只启动了1个,原因是另外两台node机器内存不足无法启动service
查看服务详细信息
详细信息外部可通过[http://192.168.1.200:9091/)访问
端口9091
网友评论