一:安装Docker
二:基础环境配置
1:设置主机名
(1)manager主机
[root@localhost ~]# hostnamectl set-hostname manager
[root@localhost ~]# bash
(2)worker01主机
[root@localhost ~]# hostnamectl set-hostname worker01
[root@localhost ~]# bash
(3)worker02主机
[root@localhost ~]# hostnamectl set-hostname worker02
[root@localhost ~]# bash
2:修改所有主机的hosts文件
[root@manager ~]# vim /etc/hosts
192.168.10.101 manager
192.168.10.102 worker01
192.168.10.103 worker02
3:设置所有主机的firewalld防火墙(如果防火墙关闭,可不用设置)
[root@manager ~]# firewall-cmd --zone=public --add-port=2377/tcp --permanent
[root@manager ~]# firewall-cmd --zone=public --add-port=7946/tcp --permanent
[root@manager ~]# firewall-cmd --zone=public --add-port=7946/udp --permanent
[root@manager ~]# firewall-cmd --zone=public --add-port=4789/tcp --permanent
[root@manager ~]# firewall-cmd --zone=public --add-port=4789/udp --permanent
[root@manager ~]# firewall-cmd --reload
firewall-cmd --zone=public --add-port=2377/tcp --permanent
firewall-cmd --zone=public --add-port=7946/tcp --permanent
firewall-cmd --zone=public --add-port=7946/udp --permanent
firewall-cmd --zone=public --add-port=4789/tcp --permanent
firewall-cmd --zone=public --add-port=4789/udp --permanent
firewall-cmd --reload
三:部署 Docker Swarm 集群
1:在manager主机上创建 Docker Swarm 集群
[root@manager ~]# docker swarm init --advertise-addr 192.168.10.101
Swarm initialized: current node (x3v12rmfw9c0isq1nppaaoogh) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-6d54xvraob3ea6peasvp5pgrtqp2f83vlfm7ag9q8ra6x1jqz3-1r93r88x483n7ofx96qj0cvjk 192.168.10.201:2377
2:在所有worker节点加入集群
[root@worker01 ~]# docker swarm join --token SWMTKN-1-6d54xvraob3ea6peasvp5pgrtqp2f83vlfm7ag9q8ra6x1jqz3-1r93r88x483n7ofx96qj0cvjk 192.168.10.201:2377
[root@worker02 ~]# docker swarm join --token SWMTKN-1-6d54xvraob3ea6peasvp5pgrtqp2f83vlfm7ag9q8ra6x1jqz3-1r93r88x483n7ofx96qj0cvjk 192.168.10.201:2377
3:查看集群信息
[root@manager ~]# docker info
........
Swarm: active
NodeID: x3v12rmfw9c0isq1nppaaoogh
Is Manager: true
ClusterID: 0mps950k70s2oqphe75jzykqq
Managers: 1
Nodes: 3
........
4:查看 Swarm 集群中 Node 节点的详细状态信息
[root@manager ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
x3v12rmfw9c0isq1nppaaoogh * manager Ready Active Leader 20.10.1
mripmqlv2rw84xzywaclvqolj worker01 Ready Active 20.10.1
gxxa4apflvapwcygvdgy0mdvh worker02 Ready Active 20.10.1
查看 manager 节点详细信息:docker node inspect manager
查看 worker01 节点详细信息:docker node inspect worker01
查看 worker02 节点详细信息:docker node inspect worker02
四:Docker Swarm 节点管理
1:将管理节点的 AVAILABILITY 值修改为 Drain 状态,使其只具备管理功能
[root@manager ~]# docker node update --availability drain manager
manager
[root@manager ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
rerfkmyfkbc74a04x22tpfo5j * manager Ready Drain Leader 19.03.9
irwxgrg6o8l69b6i024rkbury worker01 Ready Active 19.03.9
j7yit1q52486uhonee8jmhwgo worker02 Ready Active 19.03.9
注意:
节点的 AVAILABILITY 有三种状态:Active、 Pause、Drain。
Active:集群中该节点可以被指派任务。
Pause:集群中该节点不可以被指派新的任务,但是其他已经存在的任务保持运行。
Drain:集群中该节点不可以被指派新的任务,Swarm Scheduler 停掉已经存在的任务,并将它们调度到可用的节点上
2:添加标签元数据
- 支持给每个节点添加标签元数据
- 根据标签可选择性地调度某个服务部署到期望的一组节点上
[root@manager ~]# docker node update --label-add group=g2 worker01
[root@manager ~]# docker node inspect worker01
[root@manager ~]# docker service create --name mynginx --constraint 'node.labels.group==g2' nginx
备注:
constraint 约束
查看mynginx容器所在的主机
[root@manager ~]# docker service ps mynginx
3:节点提权/降权
将 worker01 和 worker02 都升级为管理节点
[root@manager ~]# docker node promote worker01 worker02
[root@manager ~]# docker node ls
对上面已提权的 worker01 和 worker02 执行降权操作
[root@manager ~]# docker node demote worker01 worker02
4:退出 Swarm 集群
[root@manager ~]# docker swarm leave
Error response from daemon: You are attempting to leave the swarm on a node that is participating as a manager. Removing the last manager erases all current state of the swarm. Use `--force` to ignore this message.
注意:
如果集群中还存在其它的工作节点,同时希望管理节点退出集群,则需要加上一个强制
选项
[root@manager ~]# docker swarm leave --force
Node left the swarm.
在work节点上退出swarm
[root@worker01 ~]# docker swarm leave
注意:
工作节点退出集群后通过 docker swarm join 命令并指定对应的 token 值重新加入集群。
如果管理节点也推出了群,需要在管理节点上使用docker swarm init --advertise-addr 192.168.10.201命令再生成一次群,其他节点再加入一次群即可,相当于重新创建一次群集
主要:
集群解散后,swarm创建的service会被删除掉
五:Docker Swarm 服务管理
创建好swarm群集
1:创建服务
(1)使用 docker service create 命令创建 Docker 服务,从 Docker 镜像 nginx 创建一个名称为 web 的服务,指定服务副本数为 2。
[root@manager ~]# docker service create -p 80:80 --replicas 2 --name web nginx
[root@manager ~]# docker service logs -f web
注意:
看到提示 Configuration complete; ready for start up后,可以Ctrl+C
(2)查看当前已经部署启动的全部应用服务
[root@manager ~]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
zgxqdeu3dph2 web replicated 2/2 nginx:latest
(3)可以查询指定服务的详细信息。
[root@manager ~]# docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
nc0lvzyt6fv8 web.1 nginx:latest worker01 Running Running 4 minutes ago
rknekib3x3eg web.2 nginx:latest manager Running Running 4 minutes ago
[root@manager ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
305d6fe1e1e4 nginx:latest "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 80/tcp web.2.rknekib3x3egb9atn4qxub8nj
2:显示服务详细信息
(1)以 JSON 格式显示方法 Web 服务的详细信息
[root@manager ~]# docker service inspect web
(2)以易于阅读方式显示 Web 服务的详细信息
[root@manager ~]# docker service inspect --pretty web
3:服务的扩容缩容
(1)将前面已经部署的 2 个副本的 Web 服务,扩容到 3 个副本
[root@manager ~]# docker service scale web=3
(2)通过 docker service ps web 命令查看服务扩容结果
[root@manager ~]# docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
nc0lvzyt6fv8 web.1 nginx:latest worker01 Running Running 10 minutes ago
rknekib3x3eg web.2 nginx:latest manager Running Running 10 minutes ago
oxrqtrsuswcr web.3 nginx:latest worker02 Running Running 13 seconds ago
(3)缩容
进行服务缩容操作时只需要设置副本数小于当前应用服务拥有的副本数即可,大于指定缩容副本数的副本会被删除
[root@manager ~]# docker service scale web=1
web scaled to 1
overall progress: 1 out of 1 tasks
1/1: running
verify: Service converged
[root@manager ~]# docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
nc0lvzyt6fv8 web.1 nginx:latest worker01 Running Running 11 minutes ago
4:删除服务
[root@manager ~]# docker service rm web
5:滚动更新
[root@manager ~]# docker service create --replicas 3 --name redis --update-delay 10s redis:3.0.6
[root@manager ~]# docker service ps redis
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
9dtcqqyptmbn redis.1 redis:3.0.6 manager Running Running 3 minutes ago
p4agvfmprmgm redis.2 redis:3.0.6 worker01 Running Running 3 minutes ago
vqo6493g9cws redis.3 redis:3.0.6 worker02 Running Running 3 minutes ago
更新已经部署服务所在容器中使用的镜像版本
[root@manager ~]# docker service update --image redis:3.0.7 redis
6:添加自定义 Overlay 网络
在创建服务时,通过--network选项指定使用的网络为已存在的 Overlay 网络
[root@manager ~]# docker network create --driver overlay my-network
[root@manager ~]# docker service create --replicas 3 --network my-network --name myweb nginx
备注:
如果 Swarm 集群中其他节点上的 Docker 容器也使用 my-network 网络,那么处于该
Overlay 网络中的所有容器之间都可以进行通信
7:数据卷创建与应用
(1)创建数据卷
[root@manager ~]# docker volume create product-kgc
(2)将nginx容器连接到数据卷
[root@manager ~]# docker service create --mount type=volume,src=product-kgc,dst=/usr/share/nginx/html --replicas 1 --name kgc-web-01 nginx
[root@manager ~]# docker service ps kgc-web-01
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
58g31r3ppwid kgc-web-01.1 nginx:latest worker01 Running Running 24 seconds ago
注意:
要确定好容器是在哪个节点运行的,可以看出,此案例中实在work01中运行的
(3)查看数据卷
[root@manager ~]# docker volume inspect product-kgc
[
{
"CreatedAt": "2021-01-24T09:19:33+08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/product-kgc/_data",
"Name": "product-kgc",
"Options": {},
"Scope": "local"
}
]
(4)到work01中查看数据是否进行同步
[root@worker01 ~]# cd /var/lib/docker/volumes/product-kgc/_data/
[root@worker01 _data]# mkdir test01 test02
[root@worker01 _data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8875962fb67b nginx:latest "/docker-entrypoint.…" 16 minutes ago Up 16 minutes 80/tcp kgc-web-01.1.58g31r3ppwidwi7x4m8vdy14y
41fdf50c16c2 nginx:latest "/docker-entrypoint.…" 17 minutes ago Up 17 minutes 80/tcp myweb.1.m89ipfy43w38n5f0qtckq55gb
4aa3abe80967 redis:3.0.7 "docker-entrypoint.s…" 18 minutes ago Up 18 minutes 6379/tcp redis.2.mczchtpy4uy7i52tyycwjd9uk
[root@worker01 _data]# docker exec -it 8875962fb67b bash
root@8875962fb67b:/# ls /usr/share/nginx/html/
50x.html index.html test01 test02
8:使用 bind 类型挂载数据卷
(1)创建测试目录
[root@manager ~]# mkdir -p /var/vhost/www/aa
[root@worker01 ~]# mkdir -p /var/vhost/www/aa
[root@worker02 ~]# mkdir -p /var/vhost/www/aa
在manager节点安装nfs-utils
[root@manager ~]# yum -y install nfs-utils
设置nfs共享
[root@manager ~]# vim /etc/exports
/var/vhost/www/aa *(rw,sync,no_root_squash)
启动nfs服务
[root@manager ~]# systemctl start nfs
[root@manager ~]# systemctl start rpcbind
在其他work节点挂载
yum -y install nfs-utils
mount 192.168.10.101:/var/vhost/www/aa /var/vhost/www/aa/
(2)创建 2 个 kgc-web-02 服务
[root@manager ~]# docker service create --replicas 2 --mount type=bind,src=/var/vhost/www/aa,dst=/usr/share/nginx/html/ --name kgc-web-02 nginx
(3)验证数据是否同步
[root@worker01 _data]# touch /var/vhost/www/aa/mytest
[root@worker01 _data]# docker service ps kgc-web-02
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
toejwbvg8nau kgc-web-02.1 nginx:latest manager Running Running 2 minutes ago
kv04aj4vt84g kgc-web-02.2 nginx:latest worker02 Running Running 2 minutes ago
[root@worker01 _data]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d292452b1f39 nginx:latest "/docker-entrypoint.…" 57 seconds ago Up 56 seconds 80/tcp kgc-web-02.1.toejwbvg8nau452rvexc0i1c9
6360a54d2bd1 nginx:latest "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 80/tcp myweb.3.51xngm6q6zj9jo4vllohbptqn
930b56c8671c redis:3.0.7 "docker-entrypoint.s…" 23 minutes ago Up 23 minutes 6379/tcp redis.1.o2regcre7k626suqytpmqpidi
ada092f9959f redis:3.0.6 "/entrypoint.sh redi…" 27 minutes ago Exited (0) 23 minutes ago redis.1.9dtcqqyptmbnybz1ub3wfzuzj
[root@worker01 _data]# docker exec -it d292452b1f39 bash
root@d292452b1f39:/# ls /usr/share/nginx/html/
mytest