Docker Swarm 是 Docker 的集群管理工具。它将Docker 主机池转变为单个虚拟 Docker 主机。swarm 集群由管理节点(manager)和工作节点(work node)构成。
swarm mananger:负责整个集群的管理工作包括集群配置、服务管理等所有跟集群有关的工作。
work node:即图中的 available node,主要负责运行相应的服务来执行任务(task)。
开启Docker API
修改配置文件
vim /lib/systemd/system/docker.service
将
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
修改为
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
重启服务
[root@master ~]# systemctl daemon-reload
[root@master ~]# systemctl restart docker
swarm集群创建
主节点开启
[root@master ~]# docker swarm init --advertise-addr 192.168.1.10
#创建好主节点时会提示—token配置参数,在配置子节点时需要使用该参数(如图)
子节点开启
使用命令docker swarm join --token
[root@node ~]# docker swarm join --token SWMTKN-1-55cvdjet5qrnkayuaa70aozysu3ztjh1ic21y0x8gw9sb5fgcb-b2om9dp4scs452l4gkqmd0814 192.168.1.10:2377
This node joined a swarm as a worker.
可使用命令查询join使用的token值
docker swarm join-token worker
使用docker node ls查询节点状态
[root@master ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
y8xp0iwjwkmfbxq4rc6qfekpc * master Ready Active Leader 18.09.6
cscxignpj1qb60c4y8dsgf7s7 node Ready Active 18.09.6
swarm集群使用
部署服务
部署一个httpd的service,创建名称为web-server
[root@master ~]# docker service create --name web-server httpd
iwjwdip5t893m58tetpegh6wu
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
查看服务状态
查看当前集群中的service
[root@master ~]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
iwjwdip5t893 web-server replicated 1/1 httpd:latest
查看部署的web-server状态
[root@master ~]# docker service ps web-server
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
ggrt8ldrjts3 web-server.1 httpd:latest node Running Running 45 seconds ago
#此时web-server的NODE为node节点,状态为running
服务伸缩/拓展副本
将服务数增加副本,同时运行多个实例
service scale可调整副本数,增加或减少
[root@master ~]# docker service scale web-server=5
web-server 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
#此时查看服务状态就有5个web-server部署在node节点或master节点
[root@master ~]# docker service ps web-server
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE
ggrt8ldrjts3 web-server.1 httpd:latest node Running Running 6 minutes ago
ue2rls32fdyi web-server.2 httpd:latest master Running Running 41 seconds ago
n5e626vifmv8 web-server.3 httpd:latest node Running Running about a minute ago
iq7y8ru24oy5 web-server.4 httpd:latest master Running Running 41 seconds ago
tp56mrrszkel web-server.5 httpd:latest node Running Running about a minute ago
服务访问
将web-server的80端口映射到外部
[root@master ~]# docker service update --publish-add 80:80 web-server
web-server
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
测试
Portainer安装
Portainer 是 Docker 的图形化管理工具,提供状态显示面板、应用模板快速部署、容器镜像网络数据卷的基本操作(包括上传和下载镜像、创建容器等操作)、事件日志显示、容器控制台操作、Swarm 集群和服务等集中管理和操作、登录用户管理和控制等功能。功能 十分全面,基本能满足中小型企业对容器管理的全部需求。
Master 节点,安装 Portainer。
[root@master ~]# docker service create --name portainer --publish 9000:9000 --replicas=1 --constraint 'node.role == manager' --mount type=bind,src=//var/run/docker.sock,dst=/var/run/docker.sock --mount type=volume,src=portainer_data,dst=/data portainer/portainer -H unix:///var/run/docker.sock
wbpvqhg13fzrqrwl7epcbhqgo
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
portainer以一个容器服务开启,可在容器进程中查询
[root@master ~]# docker ps
网页访问
http://ip:9000首次登陆设置用户密码
登陆后控制台首页