当前位置:   article > 正文

docker-swarm集群搭建

docker-swarm集群搭建

目录

 一、docker swarm介绍

二、部署docker

三、搭建集群

3.1  工作模式

 3.2  将当前主机作为leader

3.3  将第二个节点slave1加入到worker

3.4  将第三个节点slave2也加入到worker

3.5  将第四个节点(slave3)加入到manager

四、总结


 一、docker swarm介绍

Docker Swarm是一个用于构建和管理容器集群的工具。它允许你将多个Docker主机组成一个虚拟的单一主机,以便更好地扩展应用程序,并提供高可用性和负载均衡。Swarm使用了一种称为"swarm mode"的特殊模式来管理集群。

在Swarm中,你可以将多个Docker主机连接到一个Swarm集群,并使用docker service命令来创建和管理服务。服务是由一组任务(或容器)组成的,并且可以指定副本数、网络配置、容器启动参数等。Swarm会自动将任务分配到可用的主机上,以实现负载均衡和高可用性

Docker Swarm还提供了内置的负载均衡和服务发现机制,它能够自动将流量路由到运行任务的节点上,并保证服务的可用性。此外,Swarm还具备故障恢复的能力,当某个节点出现故障时,Swarm会自动重新调度任务到其他可用的节点上。

保证四个机子都安装成功docker


二、搭建集群


2.1  工作模式

为了便于查看,更改主机名

节点的主机名分别为:master   slave1   slave2   slave3

查看当前网络

  1. [root@master ~]# docker network ls
  2. NETWORK ID NAME DRIVER SCOPE
  3. b786a56417fd bridge bridge local
  4. 7bcb407e6553 host host local
  5. 47f66a3a9185 none null local

查看帮助

  1. [root@master ~]# docker swarm --help
  2. Usage: docker swarm COMMAND
  3. Manage Swarm
  4. Commands:
  5. init Initialize a swarm
  6. join Join a swarm as a node and/or manager
  7. Run 'docker swarm COMMAND --help' for more information on a command.
  8. [root@master ~]# docker swarm init --help
  9. Usage: docker swarm init [OPTIONS]
  10. Initialize a swarm
  11. Options:
  12. --advertise-addr string Advertised address (format: "<ip|interface>[:port]")
  13. --autolock Enable manager autolocking (requiring an unlock key to start a
  14. stopped manager)
  15. --availability string Availability of the node ("active", "pause", "drain") (default
  16. "active")
  17. --cert-expiry duration Validity period for node certificates (ns|us|ms|s|m|h)
  18. (default 2160h0m0s)
  19. --data-path-addr string Address or interface to use for data path traffic (format:
  20. "<ip|interface>")
  21. --data-path-port uint32 Port number to use for data path traffic (1024 - 49151). If no
  22. value is set or is set to 0, the default port (4789) is used.
  23. --default-addr-pool ipNetSlice default address pool in CIDR format (default [])
  24. --default-addr-pool-mask-length uint32 default address pool subnet mask length (default 24)
  25. --dispatcher-heartbeat duration Dispatcher heartbeat period (ns|us|ms|s|m|h) (default 5s)
  26. --external-ca external-ca Specifications of one or more certificate signing endpoints
  27. --force-new-cluster Force create a new cluster from current state
  28. --listen-addr node-addr Listen address (format: "<ip|interface>[:port]") (default
  29. 0.0.0.0:2377)
  30. --max-snapshots uint Number of additional Raft snapshots to retain
  31. --snapshot-interval uint Number of log entries between Raft snapshots (default 10000)
  32. --task-history-limit int Task history retention limit (default 5)

 2.2  将当前主机作为leader

  1. [root@master ~]# ip a
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  3. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4. inet 127.0.0.1/8 scope host lo
  5. valid_lft forever preferred_lft forever
  6. inet6 ::1/128 scope host
  7. valid_lft forever preferred_lft forever
  8. 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  9. link/ether 00:0c:29:d9:41:26 brd ff:ff:ff:ff:ff:ff
  10. inet 192.168.200.10/24 brd 192.168.200.255 scope global noprefixroute ens33
  11. valid_lft forever preferred_lft forever
  12. inet6 fe80::20c:29ff:fed9:4126/64 scope link
  13. valid_lft forever preferred_lft forever
  14. 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
  15. link/ether 02:42:2f:5c:d0:b8 brd ff:ff:ff:ff:ff:ff
  16. inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
  17. valid_lft forever preferred_lft forever
  18. [root@master ~]# docker swarm init --advertise-addr 192.168.200.10
  19. Swarm initialized: current node (0r9pr636ujer02raru7kcpgw9) is now a manager.
  20. To add a worker to this swarm, run the following command:
  21. docker swarm join --token SWMTKN-1-2pwtfth6qpuypkez62l411vlcsng0zw48q6immmg3aeoydf5fj-4m1wj6met556fugcqbl2dk2uz 192.168.200.10:2377
  22. To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

 节点1(master)为leader

初始化节点 docker swarm init

docker swarm join 加入一个节点!

# 获取令牌

docker swarm join-token manager

docker swarm join-token worker

2.3  将第二个节点slave1加入到worker

  1. [root@master ~]# docker swarm join --token SWMTKN-1-2pwtfth6qpuypkez62l411vlcsng0zw48q6immmg3aeoydf5fj-4m1wj6met556fugcqbl2dk2uz 192.168.200.10:2377
  2. This node joined a swarm as a worker.

第一个节点master查看node

  1. [root@master ~]# docker node ls
  2. ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
  3. 0r9pr636ujer02raru7kcpgw9 * master Ready Active Leader 25.0.4
  4. o622mci81bykwzj5ujczut3jy slave1 Ready Active 25.0.4

创建worker密钥

  1. [root@master ~]# docker swarm join-token worker
  2. To add a worker to this swarm, run the following command:
  3. docker swarm join --token SWMTKN-1-2pwtfth6qpuypkez62l411vlcsng0zw48q6immmg3aeoydf5fj-4m1wj6met556fugcqbl2dk2uz 192.168.200.10:2377

2.4  将第三个节点slave2也加入到worker

  1. [root@slave2 ~]# docker swarm join --token SWMTKN-1-2pwtfth6qpuypkez62l411vlcsng0zw48q6immmg3aeoydf5fj-4m1wj6met556fugcqbl2dk2uz 192.168.200.10:2377
  2. This node joined a swarm as a worker.

master节点再次查看node

  1. [root@master ~]# docker node ls
  2. ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
  3. 0r9pr636ujer02raru7kcpgw9 * master Ready Active Leader 25.0.4
  4. o622mci81bykwzj5ujczut3jy slave1 Ready Active 25.0.4
  5. ykwdxdwl589cxox8ffevap4hb slave2 Ready Active 25.0.4

2.5  将第四个节点(slave3)加入到manager

创建manager密钥

  1. [root@master ~]# docker swarm join-token manager
  2. To add a manager to this swarm, run the following command:
  3. docker swarm join --token SWMTKN-1-2pwtfth6qpuypkez62l411vlcsng0zw48q6immmg3aeoydf5fj-5ntj8tg2i2vahtljs614qjydj 192.168.200.10:2377

在slave3节点上使用

  1. [root@slave3 ~]# docker swarm join --token SWMTKN-1-2pwtfth6qpuypkez62l411vlcsng0zw48q6immmg3aeoydf5fj-5ntj8tg2i2vahtljs614qjydj 192.168.200.10:2377
  2. This node joined a swarm as a manager.

master节点再次查看

  1. [root@master ~]# docker node ls
  2. ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
  3. 0r9pr636ujer02raru7kcpgw9 * master Ready Active Leader 25.0.4
  4. o622mci81bykwzj5ujczut3jy slave1 Ready Active 25.0.4
  5. ykwdxdwl589cxox8ffevap4hb slave2 Ready Active 25.0.4
  6. 73lftyll7or01s0s0ld4urips slave3 Ready Active Reachable 25.0.4

搭建完成

两个管理节点,两个工作节点

两主两从


三、总结

1、生成主节点 init

2、加入(管理者、worker)

Docker Swarm适合构建和管理容器集群,以实现高可用性和负载均衡

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/330426
推荐阅读
相关标签
  

闽ICP备14008679号