Docker swarm 使用筆記
阿新 • • 發佈:2018-07-26
刪除 ise 3.5 yuv 維護 man desire running pub ###########docker swarm ###############配置
https://docs.docker.com/engine/swarm/manage-nodes/
https://docs.docker.com/engine/swarm/manage-nodes/#promote-or-demote-a-node
####################################
1,docker 1.12 版本以上
manager-192.168.120.131
work-192.168.120.132
1.管理端執行;
[root@docker-manager ~]# docker swarm init --advertise-addr 192.168.120.131
或者 docker swarm init --listen-addr 192.168.18.131:2377
Swarm initialized: current node (0qo8kjtlwpaymmlor6zl8wc9n) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-0wsk4hmv66dje8f5h7fv4bzs99i8lw999nu3i29cy5nlzoczjb-bb35ylrnwfmk2vl6f8wtxs8ur 192.168.120.131:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
Swarm集群端口, Docker Swarm集群開放了三個端口:
2377端口, 用於集群管理通信
7946端口, 用於集群節點之間的通信
4789端口, 用於overlay網絡流量
[root@docker-manager ~]# firewall-cmd --zone=public --add-port=7946/tcp --permanent
[root@docker-manager ~]# firewall-cmd --zone=public --add-port=4789/tcp --permanent
[root@docker-manager ~]# firewall-cmd --zone=public --add-port=2377/tcp --permanent
firewall-cmd --zone=public --add-rich-rule 'rule family="ipv4" source address=10.10.92.0/24 accept'
查看集群節點信息;
[root@docker-manager ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
329rf0jsxjezl4k3qnr11xtmp * docker-manager Ready Active Leader
7dcx2xlrc3zgo6x8d9u17tjy1 docker-work Ready Active
db6rjsszjn5z5cxwowwxisf6n docker-work Down Active
[root@docker-manager ~]#
在Docker Swarm集群中,在刪除Docker集群節點後,集群中該節點仍然存在,但是狀態顯示為Down, 需要在Swarm的Manager節點執行以下命令, 刪除已經移除的Worker節點:
[root@centos7-Master ~]# docker node rm --force 4ukr7ghj4iuvb89gu0g5ok1d
4ukr7ghj4iuvb89gu0g5ok1d
2.3.2 更新Swarm集群節點
如果需要對Docker Swarm節點進行更新,需要在manager節點上執行命令:
[root@centos7-Master ~]# docker swarm update
Swarm updated.
3.3.3 獲取 work 節點token
[root@docker-manager ~]# docker swarm join-token worker
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-607nbmlndpzmch1hdqtnpzhqrx4eufyt3cwfrr6ne3ge9iexdt-5xmz1ecalg9bhp3hiioy1n9ge 192.168.120.131:2377
3.3.4 獲取manage 節點信息;
[root@docker-manager ~]# docker swarm join-token manager
To add a manager to this swarm, run the following command:
docker swarm join --token SWMTKN-1-607nbmlndpzmch1hdqtnpzhqrx4eufyt3cwfrr6ne3ge9iexdt-5z4j7n2sn97zelbgvtm3uck3b 192.168.120.131:2377
3.3.4 停機維護;
[root@docker-manager ~]# docker node update --availability drain docker-manager ########主機名稱########
[root@docker-manager ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
329rf0jsxjezl4k3qnr11xtmp * docker-manager Ready Drain Leader
7dcx2xlrc3zgo6x8d9u17tjy1 docker-work Ready Active
3.3.5 開啟狀態;
[root@docker-manager ~]# docker node update --availability active docker-manager #######主機名稱########
docker-manager
[root@docker-manager ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
329rf0jsxjezl4k3qnr11xtmp * docker-manager Ready Active Leader
7dcx2xlrc3zgo6x8d9u17tjy1 docker-work Ready Active
3.3.6 升級角色;(manger )
[root@docker-manager ~]# docker node promote docker-work
Node docker-work promoted to a manager in the swarm.
[root@docker-manager ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
329rf0jsxjezl4k3qnr11xtmp * docker-manager Ready Active Leader
7dcx2xlrc3zgo6x8d9u17tjy1 docker-work Ready Active Reachable
3.3.7 降級角色;
[root@docker-manager ~]# docker node demote docker-work
Manager docker-work demoted in the swarm.
[root@docker-manager ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
329rf0jsxjezl4k3qnr11xtmp * docker-manager Ready Active Leader
7dcx2xlrc3zgo6x8d9u17tjy1 docker-work Ready Active
3.3.8 清除work 記錄:
docker node rm 主機名
3.3.9 創建service;
[root@docker-manager ~]# docker service create --name my_web nginx
ezqmbbrsn4ecz975u2baajnoc
3.3.10 查看service
[root@docker-manager ~]# docker service ls
ID NAME REPLICAS IMAGE COMMAND
ezqmbbrsn4ec my_web 0/1 nginx
[root@docker-manager ~]# docker service ps my_web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR
1a1diqyh70ot9wquf0yuvt0q3 my_web.1 nginx docker-manager Running Preparing about a minute ago
3.3.11 刪除service
[root@docker-manager ~]# docker service rm helloworld
[root@docker-manager ~]# docker service rm my_web
3.3.12 創建副本;
docker service create --replicas 3 --name web-nginx2 ping g.cn
3.3.13 擴容;scale
[root@docker-manager ~]# docker service scale helloworld=5
helloworld scaled to 5
[root@docker-manager ~]# docker service ls
ID NAME REPLICAS IMAGE COMMAND
8d6yob2chojp helloworld 1/5 alpine ping docker.com
3.3.14 減少:
[root@docker-manager ~]# docker service scale helloworld=2
helloworld scaled to 5
[root@docker-manager ~]# docker service ls
8d6yob2chojp helloworld 0/1 alpine ping docker.com
3.3.15 滾動更新服務;
[root@docker-manager ~]# docker service update --image helloworld.1.2 helloworld
helloworld
.3.16 更新節點狀態;(節點自動遷移)
[root@docker-manager ~]# docker node update --availability drain docker-work
docker-work
3.3.17
######################(節點恢復)############
[root@docker-manager ~]# docker node update --availability active docker-work
[root@docker-manager ~]#docker service scale hellowrold=0
[root@docker-manager ~]#docker service scale hellowrold=4
3.18 Docker swarm mode overlay network security model
[root@docker-manager ~]# docker network ls
[root@docker-manager ~]# docker network create --driver overlay --subnet 10.10.10.0/24 --opt encrypted my-network
8mrnvm0rv7gdca48lazvyd8k8
創建服務 加入網絡
[root@docker-manager ~]# docker service create --name my-web --replicas 3 --network my-network bazhuazhu/nginx
d660tgracne49w5q1mk36y0lf
服務端口暴露; 本機端口:容器端口
[root@docker-manager ~]# docker service update --publish-add 8080:80 my-web 服務名稱
3.3.16 更新節點狀態;
2.work 客戶端執行;
(1).添加到集群;
[root@docker-work ~]# docker swarm join --token SWMTKN-1-607nbmlndpzmch1hdqtnpzhqrx4eufyt3cwfrr6ne3ge9iexdt-5xmz1ecalg9bhp3hiioy1n9ge 192.168.120.131:2377
This node joined a swarm as a worker.
(2).查看work 信息;
[root@docker-manager ~]# docker node inspect 7dcx2xlrc3zgo6x8d9u17tjy1 --pretty
(3).刪除Swarm集群節點
[root@centos7-WorkerA ~]# docker swarm leave
Node left the swarm.
(4).如果想在Docker Swarm集群中刪除Docker的節點, 需要在須刪除的Docker節點下執行命令:
3.3.19 更新節點狀態;
2.work 客戶端執行;
(1).添加到集群;
[root@docker-work ~]# docker swarm join --token SWMTKN-1-607nbmlndpzmch1hdqtnpzhqrx4eufyt3cwfrr6ne3ge9iexdt-5xmz1ecalg9bhp3hiioy1n9ge 192.168.120.131:2377
This node joined a swarm as a worker.
(2).查看work 信息;
[root@docker-manager ~]# docker node inspect 7dcx2xlrc3zgo6x8d9u17tjy1 --pretty
(3).刪除Swarm集群節點
[root@centos7-WorkerA ~]# docker swarm leave
Node left the swarm.
(4).如果想在Docker Swarm集群中刪除Docker的節點, 需要在須刪除的Docker節點下執行命令
Docker swarm 使用筆記