管理Docker Swarm 集群
1 .节点状态变更管理
[[email protected] ~]# docker node update --availability drain manager
Manager
[[email protected] ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
3ugwexq8el6e9ouj1iyguxtnq * manager Ready Drain Reachable 19.03.1
6p4qed1dyurk81nuxs2b3s72p worker01 Ready Active Leader 19.03.1
zegtd8sspiw0lsctzc98cc0g4 worker02 Ready Active Reachable 19.03.1
此时manager 只能时管理节点,不能指派任务
2 . 添加标签元数据
[[email protected] ~]# docker node update --label-add GM-IDC-01 worker01
[[email protected] ~]# docker node inspect worker01
"CreatedAt": "2019-08-22T07:09:10.403549405Z",
"UpdatedAt": "2019-08-22T08:16:14.952425008Z",
"Spec": {
"Labels": {
"GM-IDC-01": ""
…………………..省略了部分
3 . 节点的提权/降权
例如:将worker01,worker02都升级为管理节点
[[email protected] ~]# docker node promote worker01 worker02
Node 6p4qed1dyurk81nuxs2b3s72p is already a manager.
Node zegtd8sspiw0lsctzc98cc0g4 is already a manager.
例如:将worker01,worker02都降权为普通节点
[[email protected] ~]# docker node demote worker01 worker02
Manager worker01 demoted in the swarm.
Manager worker02 demoted in the swarm.
4 .退出集群
管理节点退出
[[email protected] ~]# docker swarm leave –force
普通节点退出
[[email protected] ~]# docker swarm leave
Docker Swarm 服务管理
示例:从Docker镜像httpd 创建一个名称为web 的服务,指定副本为2.
[[email protected] ~]# docker pull httpd ##下载镜像
注意:httpd 服务要注释掉 ‘ServerName‘
[[email protected] ~]# docker service create --replicas 2 --name web httpd
overall progress: 2 out of 2 tasks
1/2: running
2/2: running
verify: Service converged
[[email protected] ~]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
edfomyazxto9 web replicated 2/2 httpd:latest
显示服务的详细信息 (省略了部分)
[[email protected] ~]# docker service inspect web
[
{
"ID": "edfomyazxto9f7t7kbau2h353",
"Version": {
"Index": 1524
},
"CreatedAt": "2019-08-22T09:38:04.259651137Z",
"UpdatedAt": "2019-08-22T09:38:04.259651137Z",
"Spec": {
"Name": "web",
"Labels": {},
"TaskTemplate": {
"ContainerSpec": {
"Image": "httpd:[email protected]:98caed3e3a90ed9db8d25dcbb98eebe0ce56358a9dbbc940d7eb66a8e2b88252",
……………………………………………
删除服务
[[email protected] ~]# docker service rm web
2.重新加入群集,部署nginx 的web
特别注意::每一台节点上都要有nginx 的镜像,才能部署。
9 docker load --input mynginx ##载入镜像,每一台上都做
10 docker images
11 docker tag 848ea241199d nginx
12 docker images
13 clear
14 docker service create --replicas 2 --name web nginx ##指定副本为2
15 docker service logs -f web
16 docker service ls
17 docker service ps web
[[email protected] ~]# docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
pb3hw77bm022 web.1 nginx:latest manager Running Running 12 minutes ago
9f6u648z18gk web.2 nginx:latest worker01 Running Running 12 minutes ago
18 docker service inspect web
19 docker service inspect --pretty web ##查看信息
……………………………….
ContainerSpec:
Image: nginx:latest
Init: false
Resources:
Endpoint Mode: vip
服务的扩容/缩容
20 docker service scale web=3 ##服务扩容
21 docker service ps web
[[email protected] ~]# docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
pb3hw77bm022 web.1 nginx:latest manager Running Running 15 minutes ago
9f6u648z18gk web.2 nginx:latest worker01 Running Running 15 minutes ago
an6nc2lyhet5 web.3 nginx:latest worker02 Running Running 10 minutes ago
服务的缩容
[[email protected] ~]# docker service scale web=1
[[email protected] ~]# docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
pb3hw77bm022 web.1 nginx:latest manager Running Running 17 minutes ago
添加Overlay网络
在Swarm 集群中。添加Overlay网络时,可以连接一个或多个服务。那么处于该网络中的所有容器之间都可以进行通信!!
1.管理节点创建
[[email protected] ~]# docker network create --driver overlay my-network
0c5k9xirc0pp1a9qqgoksvyf5
2.创建服务,使用my-network 网络
[[email protected] ~]# docker service create --replicas 3 --network my-network --name myweb nginx
mm3gtfocd8dm63a2d3b4uivjq
overall progress: 3 out of 3 tasks
1/3: running
2/3: running
3/3: running
verify: Service converged
数据卷的创建与应用
创建数据卷
[[email protected] _data]# docker volume create product-kgc
product-kgc
查看创建的数据卷
[[email protected] _data]# docker volume ls
DRIVER VOLUME NAME
local product-kgc
创建本机上的目录
[[email protected] _data]# mkdir -p /usr/share/nginx/html
创建容器
[[email protected] _data]# docker service create --mount type=volume,src=product-kgc,dst=/usr/share/nginx/html --replicas 1 --name kgc-web-01 nginx ##镜像的存在
查看创建的容器
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
bbqd215gwlog kgc-web-01.1 nginx:latest manager Running Running 11 minutes ago
查看数据卷的详细信息
[[email protected] ~]# docker volume inspect product-kgc
[
{
"CreatedAt": "2019-08-25T09:25:30+08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/product-kgc/_data",
"Name": "product-kgc",
"Options": {},
"Scope": "local"
}
]
查看数据是否同步
[[email protected] ~]# cd /var/lib/docker/volumes/product-kgc/_data/
[[email protected] ~]# mkdir test01 test02
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
32f426b2a047 nginx:latest "/run.sh" 16 minutes ago Up 16 minutes 80/tcp, 443/tcp kgc-web-01.1.bbqd215gwlog1v4a5j2ck2nvq
进入容器,查看 ##在本地目录下创建的目录,容器中也存在
[[email protected] _data]# docker exec -it 32f426b2a047 bash
[[email protected] nginx]# ls /usr/share/nginx/html/
test01 test02
附加::第二种方式创建数据卷,可以采用 bind 类型
[[email protected] ~]# docker service create --replicas 3 --mount type=bind,src=/var/vhost/www/aa,dst=/usr/share/nginx/html --name kgc-web-02 nginx
d6c5iw9gxo980embkb7aas1ky
overall progress: 3 out of 3 tasks
1/3: running
2/3: running
3/3: running
verify: Service converged
[[email protected] ~]# touch /var/vhost/www/aa/abcd
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
807ff87f1b2b nginx:latest "/run.sh" 40 minutes ago Up 40 minutes 80/tcp, 443/tcp kgc-web-02.2.pkwe5ql9oaf7994iv5qt44cke
32f426b2a047 nginx:latest "/run.sh" About an hour ago Up About an hour 80/tcp, 443/tcp kgc-web-01.1.bbqd215gwlog1v4a5j2ck2nvq
4f71487e7989 nginx:latest "/run.sh" About an hour ago Created myweb.2.j771vgcjzw0t3cne3fauz3ydf
d4c2cf0b0953 nginx:latest "/run.sh" About an hour ago Created myweb.1.hayst7oh4ck442xl9g51e54xl
[[email protected] ~]# docker exec -it 807ff87f1b2b bash
[[email protected] nginx]# ls /usr/share/nginx/html/
abcd
验证成功!
原文地址:https://blog.51cto.com/14355475/2447337