Bowen outline:
a, Docker swarm Introduction
II environment to prepare
three initialization Swarm cluster
IV configuration node02, node03 join, leave swarm cluster
V. deploy graphical UI
six, docker swarm cluster service service configuration
Seven, implementation of service expansion, shrink
eight, docker Swarm cluster commonly used commands
nine, docker swarm summary
A, Docker swarm Profile
Docker swarm with docker-compose , are docker scheduling tool official launch of the docker container. But the difference is, Docker Compose is created on a single server or host tools more containers, while Docker Swarm you can create a container cluster services on multiple servers or hosts, for the deployment of micro-services, will be more obviously Docker Swarm Suitable for.
Docker swarm: Its role is to host a number of Docker abstract as a whole, and unified management of these resources on a variety of Docker Docker host through a portal. Swarm and Kubernetes relatively similar, but much lighter, has fewer functions than kubernetes number.
Starting Docker 1.12.0 version, Docker Swarm already included in Docker engine (docker swarm), and has built a service discovery tool, we do not need as before, and then configure Etcd or Consul to configure the service discovery.
Docker swarm has three roles:
- Manager node: responsible for the implementation and management of scheduling cluster container to keep and maintain the desired state of the swarm in, swarm manager node can have multiple, they will automatically negotiate to elect a leader to perform choreography task; but instead, can not do without manager node ;
- Worker node: accepted and executed by the manager node to distribute tasks, and the default manager node is a work node, but you can set it to manager-only node, it is responsible for scheduling and management;
- Service: used to define the command to be executed on the worker;
Note: In a Docker Swarm cluster, the role of each server can docker is manager, but not all worker, that is to say, not without a leader, and participating in all cluster host name, do not conflict .
Second, prepare the environment
Precautions:
- Ensure time synchronization;
- Turn off the firewall and SElinux (experimental environment);
- Change the host name;
- Write host file, i can guarantee the domain name resolution;
Third, the cluster initialization Swarm
[root@node01 ~]# tail -3 /etc/hosts
192.168.1.1 node01
192.168.1.2 node02
192.168.1.3 node03
//三台主机都需配置hosts文件实现域名解析的效果
[root@node01 ~]# docker swarm init --advertise-addr 192.168.1.1
//--advertise-addr:指定与其他node通信的地址
Command returns information, as shown:
Figure ① command: worker status to join swarm cluster;
② command: to join manager identity swarm cluster;
appear on the map represents a successful initialization! Note: - token representation for a period of 24 hours;
Fourth, the configuration node02, node03 join, leave the swarm cluster
###################node02的操作如下###################
[root@node02 ~]# docker swarm join --token SWMTKN-1-4pc1gjwjrp9h4dny52j58m0lclq88ngovis0w3rinjd05lklu5-ay18vjhwu7w8gsqvct84fv8ic 192.168.1.1:2377
###################node03的操作如下###################
[root@node03 ~]# docker swarm join --token SWMTKN-1-4pc1gjwjrp9h4dny52j58m0lclq88ngovis0w3rinjd05lklu5-ay18vjhwu7w8gsqvct84fv8ic 192.168.1.1:2377
//node02、node03默认是以worker身份加入的
###################node01的操作如下###################
[root@node01 ~]# docker node ls
//查看节点详细信息(只能是manager身份才可查看)
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
mc3xn4az2r6set3al79nqss7x * node01 Ready Active Leader 18.09.0
olxd9qi9vs5dzes9iicl170ob node02 Ready Active 18.09.0
i1uee68sxt2puzd5dx3qnm9ck node03 Ready Active 18.09.0
//可以看出node01、node02、node03的状态为Active
###################node02的操作如下###################
[root@node02 ~]# docker swarm leave
###################node03的操作如下###################
[root@node03 ~]# docker swarm leave
//node02、node03申请离开群集
###################node01的操作如下###################
[root@node01 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
mc3xn4az2r6set3al79nqss7x * node01 Ready Active Leader 18.09.0
olxd9qi9vs5dzes9iicl170ob node02 Down Active 18.09.0
i1uee68sxt2puzd5dx3qnm9ck node03 Down Active 18.09.0
///可以看到node02、node03的状态为Down
[root@node01 ~]# docker node rm node02
[root@node01 ~]# docker node rm node03
//node01将node02、node03移除群集
The above command can be added to one node, remove the cluster, but the addition is the use of worker identity, if you want your identity to join the cluster node is a manager, you need to use the following command:
[root@node01 ~]# docker swarm join-token manager
//查询以manager身份加入群集的命令
[root@node01 ~]# docker swarm join-token worker
//查询以worker身份加入群集的命令
Figure:
###################node02的操作如下###################
[root@node02 ~]# docker swarm join --token SWMTKN-1-2c0gcpxihwklx466296l5jp6od31pshm04q990n3ssncby3h0c-78rnxee2e990axj0q7td74zod 192.168.1.1:2377
###################node03的操作如下###################
[root@node03 ~]# docker swarm join --token SWMTKN-1-2c0gcpxihwklx466296l5jp6od31pshm04q990n3ssncby3h0c-78rnxee2e990axj0q7td74zod 192.168.1.1:2377
//node02、node03加入群集是以manager身份加入
###################node01的操作如下###################
[root@node01 ~]# docker node ls //查看节点的详细信息
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
exr8uoww0eih43iujqz5cbv6q * node01 Ready Active Leader 18.09.0
r35f48huyw5hvnkuzatrftj1r node02 Ready Active Reachable 18.09.0
gsg1irl1bywgdsmfawi9rna7p node03 Ready Active Reachable 18.09.0
//从MANAGER STATUS这列就可看出
Although the time to join the cluster, you can specify manager, worker status, but can also be carried out by the following command to downgrade, upgrade, operation is as follows:
[root@node01 ~]# docker node demote node02
[root@node01 ~]# docker node demote node03
//将node02、node03降级为worker
[root@node01 ~]# docker node promote node02
[root@node01 ~]# docker node promote node03
//将node02、node03升级为manager
//自行验证
Fifth, the deployment of graphical UI
Deploy graphical UI interface is done by node01!
[root@node01 ~]# docker run -d -p 8080:8080 -e HOST=192.168.1.1 -e PORT=8080 -v /var/run/docker.sock:/var/run/docker.sock --name visualizer dockersamples/visualizer
Use a browser to access:
the browser can access, then the graphical UI interface deployment is complete!
Six, docker swarm cluster service Service Configuration
node01发布一个任务,(必须在manager角色的主机上)运行六个容器,命令如下:
[root@node01 ~]# docker service create --replicas 6 --name web -p 80:80 nginx
// --replicas:副本数量;大概可以理解为一个副本就是一个容器
容器运行完成后,可以登录web页面进行查看,如图:
注意:如果另外两台节点服务器上没有相应的惊醒,则默认会从docker Hub上自动进行下载!
[root@node01 ~]# docker service ls //查看创建的service
ID NAME MODE REPLICAS IMAGE PORTS
nbfzxltrcbsk web replicated 6/6 nginx:latest *:80->80/tcp
[root@node01 ~]# docker service ps web //查看创建的service都运行在那些容器上
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
v7pmu1waa2ua web.1 nginx:latest node01 Running Running 6 minutes ago
l112ggmp7lxn web.2 nginx:latest node02 Running Running 5 minutes ago
prw6hyizltmx web.3 nginx:latest node03 Running Running 5 minutes ago
vg38mso99cm1 web.4 nginx:latest node01 Running Running 6 minutes ago
v1mb0mvtz55m web.5 nginx:latest node02 Running Running 5 minutes ago
80zq8f8252bj web.6 nginx:latest node03 Running Running 5 minutes ago
如果现在node02、node03发生宕机时,service并不会因为节点的当即而死掉,而是自动跑到正常的节点上。
模拟node02宕机,web页面如下:
恢复node02,web页面如下:
即使node02恢复正常,也不会将service分配给node02。
因此,可以得出一个结论:如果node发生故障,service会自动跑到可用的节点上;反之,如果node没有发生故障,默认情况下,service不会轻易更改节点!
七、实现service的扩容、收缩
扩容:就i是添加几台service;
收缩:就是减少几台service;
针对上述环境实现收缩、扩容;
(1)service扩容
[root@node01 ~]# docker service scale web=8
//原本有6个service,现在增加到8台
web页面如下:
关于service分配到那台node节点上,是根据docker swarm自身的算法进行分配的。
(2)service收缩
[root@node01 ~]# docker service scale web=4
//原本有8个service,现在减少到4台
web页面如下:
(3)设置某个node不运行service
上述环境中,三台都是manager,即使是一台manager、两台worker默认情况manager也是进行工作的,将node02、node03降级为worker,执行如下命令:
[root@node01 ~]# docker node demote node02
[root@node01 ~]# docker node demote node03
如图:
可以通过设置,使某台节点不运行service,如下:
[root@node01 ~]# docker node update --availability drain node01
//设置noder01以后不运行容器,但已经运行的容器并不会停止
// --availability:选项后面共有三个选项可配置,如下:
active:工作;pause:暂时不工作;drain:永久性的不工作
web页面如下:
[root@node01 ~]# docker node update --availability drain node02
//node02也不参加工作,但已经运行的容器并不会停止
如图:
由此可以得出:不是只有manager才有不工作的权力!
八、docker Swarm群集常用命令
[root@node02 ~]# docker swarm leave
//那个节点想推出swarm群集,就在那台节点上执行这条命令
//节点自动退出swarm群集(相当于辞职)
[root@node01 ~]# docker node rm 节点名称
//由manager主动删除节点(相当于开除)
[root@node01 ~]# docker node promote 节点名称
//将节点升级
[root@node01 ~]# docker node demote 节点名称
//将节点降级
[root@node01 ~]# docker node ls
//查看swarm群集的信息(只可以在manager角色的主机上查看)
[root@node01 ~]# docker node update --availability drain 节点名称
//调整节点不参加工作
[root@node01 ~]# docker swarm join-token worker
//查看加入swarm群集的令牌(可以是worker也可以是manager)
[root@node01 ~]# docker service scale web=4
//扩容、收缩swarn群集servie的数量(取决与群集原本的数量)
//比原本群集数量多,就是扩容,反之、收缩
[root@node01 ~]# docker service ls
//查看创建的service
[root@node01 ~]# docker service ps service的名称
//查看创建的service运行在那些容器上
[root@node01 ~]# docker service create --replicas 6 --name web -p 80:80 nginx
//指定运行的service副本数量
九、docker swarm总结
- 参与群集的主机名一定不能冲突,并且可以互相解析对方的主机名;
- 集群内的所有节点可以都是manager角色,但是不可以都是worker角色;
当指定运行的镜像时,如果群集中的节点本地没有该镜像,那么它将会自动下载对应的镜像;- When the cluster to function correctly, if a running container docker server goes down, then it runs all containers, will be transferred to other nodes on the normal operation, and, even if the server downtime occurs to resume normal operation , it will not take over the running of the vessel before;
------------ end of this article, thanks for reading ----------