Docker Swarm从部署到基本操作( 二 )

[root@m1 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
0isfyre69mdu1hm11esf1q3dk m2 Ready Active Reachable
7585zt09o2sat82maef0ocf42 m0 Ready Active Leader
slc0hjbs7jh2hdi8ai3wohy23 * m1 Ready Active Reachable
加入工作节点w0、w1

  1. 在w0上执行前面取得的工作节点的join token:
[root@w0 ~]# docker swarm join
> --token SWMTKN-1-5huefb5501cv7p8i2op1am2oevasoqu4te8vpvapndkudvszb4-e8l6755jstd7urpdo5smyi8fv
> 192.168.121.142:2377
This node joined a swarm as a worker.
  1. 在w1上做同样的操作;
  2. 在m0、m1、m2其中的任意一台上执行命令查看Swarm集群的现状 , 可见工作节点已经全部就绪:
[root@m0 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
0isfyre69mdu1hm11esf1q3dk m2 Ready Active Reachable
7585zt09o2sat82maef0ocf42 * m0 Ready Active Leader
i71bcxt1auc804syybroajtan w1 Ready Active
slc0hjbs7jh2hdi8ai3wohy23 m1 Ready Active Reachable
wqcwcccva3d3mxgi5p423d4fv w0 Ready Active
至此 , Swarm集群环境搭建完毕 , 接下来可以验证了 。
验证Swarm集群环境
  1. 创建名为Tomcat-net的覆盖网络(Overlay Netowork) , 这是个二层网络 , 处于该网络下的docker容器 , 即使宿主机不一样 , 也能相互访问:
docker network create -d overlay tomcat-net
  1. 创建名为tomcat的服务 , 使用了刚才创建的覆盖网络:
docker service create --name tomcat
--network tomcat-net
-p 8080:8080
--replicas 3
tomcat:7.0.96-jdk8-openjdk
  1. 执行命令查看当前所有服务:
[root@m0 ~]# docker service ls
ID NAME MODE REPLICAS IMAGE
kguawc4b5th4 tomcat replicated 3/3 tomcat:7.0.96-jdk8-openjdk
  1. 执行命令查看名为tomcat的服务 , 可见三个容器分别部署在m0、m2、w1机器上:
[root@m0 ~]# docker service ps tomcat
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
n1gs9f1plce2 tomcat.1 tomcat:7.0.96-jdk8-openjdk w1 Running Running 19 minutes ago
q8jyg088ci21 tomcat.2 tomcat:7.0.96-jdk8-openjdk m2 Running Running 19 minutes ago
h9ww33dpw56m tomcat.3 tomcat:7.0.96-jdk8-openjdk m0 Running Running 19 minutes ago
  1. 执行命令查看名为tomcat的服务的详细信息(去掉--pretty可以看到更完整的):
[root@m0 ~]# docker service inspect --pretty tomcat
ID: kguawc4b5th4qlwlsv183qtai
Name: tomcat
Service Mode: Replicated
Replicas: 3
Placement:
UpdateConfig:
Parallelism: 1
On failure: pause
Max failure ratio: 0
ContainerSpec:
Image: tomcat:7.0.96-jdk8-openjdk@sha256:91eadffb59d9a35ada2d39fcd616a749ac580aa5e834499b7128f27be2e46623
Resources:
Networks: tomcat-net
Endpoint Mode: vip
Ports:
PublishedPort 8080
Protocol = tcp
TargetPort = 8080
  1. 打开浏览器 , 尝试访问m0、m1、m2、w0、w1这个五个机器的8080端口 , 都可以成功访问tomcat首页:

Docker Swarm从部署到基本操作

文章插图
  1.  
服务模式
  1. 服务模式一共有两种:Ingress和Host , 如果不指定 , 则默认的是Ingress;
  2. Ingress模式下 , 到达Swarm任何节点的8080端口的流量 , 都会映射到任何服务副本的内部80端口 , 就算该节点上没有tomcat服务副本也会映射;
  3. Host模式下 , 仅在运行有容器副本的机器上开放端口 , 使用Host模式的命令如下:
docker service create --name tomcat
--network tomcat-net
--publish published=8080,target=8080,mode=host
--replicas 3
tomcat:7.0.96-jdk8-openjdk
服务扩缩容
  1. 执行命令将副本数从3调整为5:
[root@m0 ~]# docker service scale tomcat=5
tomcat scaled to 5
  1. 执行命令查看名为tomcat的服务 , 可见每台机器上都分布了一个容器:
[root@m0 ~]# docker service ps tomcat
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
w32tjahze2fk tomcat.1 tomcat:7.0.96-jdk8-openjdk m2 Running Running 42 minutes ago
yj5czwwhrrsh tomcat.2 tomcat:7.0.96-jdk8-openjdk m0 Running Running 42 minutes ago
pq40995nbd0k tomcat.3 tomcat:7.0.96-jdk8-openjdk w1 Running Running 42 minutes ago
y1y6z1jczel1 tomcat.4 tomcat:7.0.96-jdk8-openjdk m1 Running Running about a minute ago
w0dcii8f79os tomcat.5 tomcat:7.0.96-jdk8-openjdk w0 Running Running about a minute ago
滚动升级
  1. 当前tomcat服务中 , tomcat镜像的tag是 , 我们来尝试升级到 , 执行以下命令:


    推荐阅读