Docker35:Docker Swarm集群搭建

Docker35:Docker Swarm集群搭建

Created
Nov 19, 2021 02:22 AM
Last Edited
Last updated April 18, 2022
Tags

1. Docker Swarm官方文档

notion image
 

2. 工作模式

  • 10机器 Swarm
  • 更多 K8S(使用较多)
notion image
工作节点(work)工作
管理节点(manager)操作

3. Swarm使用

3.1 查看机器ip地质

ip addr
notion image

3.2 绑定ip

docker01机器
docker01中初始化节点
docker swarm init --advertise-addr 192.168.92.138
notion image
# 获取令牌 docker swarm join-token manager docker swarm join-token worker
docker02机器
docker02以worker方式加入docker01中
root@docker02:/home# docker swarm join --token SWMTKN-1-2c5y4hmr1aozzxsixj3w2byokwojjt7a1mudzacshsjjt2js3n-4le7c6t6aur8cq757kcm6rtlo 192.168.92.138:2377
notion image
docker01机器
  • 查看信息
root@docker01:/home# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION 2zs5wbbsn12omyp7btmbdnas9 * docker01 Ready Active Leader 20.10.11 khzujzz2jhp8hq6arqerhwhui docker02 Ready Active 20.10.11 root@docker01:/home#
docker01机器
  • 生成worker命令
root@docker01:/home# docker swarm join-token worker To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-2c5y4hmr1aozzxsixj3w2byokwojjt7a1mudzacshsjjt2js3n-4le7c6t6aur8cq757kcm6rtlo 192.168.92.138:2377 root@docker01:/home#
notion image
docker03机器
root@docker03:/home# docker swarm join --token SWMTKN-1-2c5y4hmr1aozzxsixj3w2byokwojjt7a1mudzacshsjjt2js3n-4le7c6t6aur8cq757kcm6rtlo 192.168.92.138:2377 This node joined a swarm as a worker. root@docker03:/home#
docker01机器
查看信息
notion image
docker01机器
  • 生成manager命令
root@docker01:/home# docker swarm join-token manager To add a manager to this swarm, run the following command: docker swarm join --token SWMTKN-1-2c5y4hmr1aozzxsixj3w2byokwojjt7a1mudzacshsjjt2js3n-bn1rz2sp80s6zy9ujxgil89uk 192.168.92.138:2377 root@docker01:/home#
docker04机器
  • 以主机点加入
root@docker04:/home# docker swarm join --token SWMTKN-1-2c5y4hmr1aozzxsixj3w2byokwojjt7a1mudzacshsjjt2js3n-bn1rz2sp80s6zy9ujxgil89uk 192.168.92.138:2377 This node joined a swarm as a manager. root@docker04:/home#
docker01机器
查看信息
notion image
 
 

4. 步骤总结

  1. 生成节点init
  1. 加入(manager、worker两种方式)

5. Raft协议

集群至少大于3台
  • 至少有3个主节点
  • Raft:至少保证大多数节点存活,才可以使用,高可用
  • 弹性、扩缩容

6. Raft协议测试

  • 当docker01机器停止,宕机!当前是两主两从。
 

6.1 模拟宕机

停止docker01
root@docker01:/home# sudo systemctl stop docker
docker04成为leader
notion image

6.2 docker03节点离开

docker03
root@docker03:/home# docker swarm leave Node left the swarm. root@docker03:/home#
docker01、docker04任意一个主节点都可以查看,docker03 状态为down
root@docker04:/home# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION 2zs5wbbsn12omyp7btmbdnas9 docker01 Ready Active Reachable 20.10.11 khzujzz2jhp8hq6arqerhwhui docker02 Ready Active 20.10.11 4ehul5i4jfmbza857ejyeabww docker03 Down Active 20.10.11 i7nuattd1reioyo4n7gx2bzc2 * docker04 Ready Active Leader 20.10.11 root@docker04:/home#
 

6.3 将docker03成为主节点

docker01、docker04任意一个主节点都可生成manager令牌
root@docker01:/home# docker swarm join-token manager To add a manager to this swarm, run the following command: docker swarm join --token SWMTKN-1-2c5y4hmr1aozzxsixj3w2byokwojjt7a1mudzacshsjjt2js3n-bn1rz2sp80s6zy9ujxgil89uk 192.168.92.138:2377 root@docker01:/home#
docker03
root@docker03:/home# docker swarm join --token SWMTKN-1-2c5y4hmr1aozzxsixj3w2byokwojjt7a1mudzacshsjjt2js3n-bn1rz2sp80s6zy9ujxgil89uk 192.168.92.138:2377 This node joined a swarm as a manager. root@docker03:/home#
notion image