目录
[一、docker swarm介绍](#一、docker swarm介绍)
[3.1 工作模式](#3.1 工作模式)
[3.2 将当前主机作为leader](#3.2 将当前主机作为leader)
[3.3 将第二个节点slave1加入到worker](#3.3 将第二个节点slave1加入到worker)
[3.4 将第三个节点slave2也加入到worker](#3.4 将第三个节点slave2也加入到worker)
[3.5 将第四个节点(slave3)加入到manager](#3.5 将第四个节点(slave3)加入到manager)
一、docker swarm介绍
Docker Swarm是一个用于构建和管理容器集群的工具。它允许你将多个Docker主机组成一个虚拟的单一主机,以便更好地扩展应用程序,并提供高可用性和负载均衡。Swarm使用了一种称为"swarm mode"的特殊模式来管理集群。
在Swarm中,你可以将多个Docker主机连接到一个Swarm集群,并使用docker service命令来创建和管理服务。服务是由一组任务(或容器)组成的,并且可以指定副本数、网络配置、容器启动参数等。Swarm会自动将任务分配到可用的主机上,以实现负载均衡和高可用性。
Docker Swarm还提供了内置的负载均衡和服务发现机制,它能够自动将流量路由到运行任务的节点上,并保证服务的可用性。此外,Swarm还具备故障恢复的能力,当某个节点出现故障时,Swarm会自动重新调度任务到其他可用的节点上。
二、部署docker
创建四台虚拟机或者四台服务器
部署过程如下
安装部署docker全部过程以及基础使用命令入门必看!-CSDN博客
保证四个都安装成功
三、搭建集群
3.1 工作模式
为了便于查看,更改主机名
节点的主机名分别为:master slave1 slave2 slave3
查看当前网络
bash
[root@master ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
b786a56417fd bridge bridge local
7bcb407e6553 host host local
47f66a3a9185 none null local
查看帮助
bash
[root@master ~]# docker swarm --help
Usage: docker swarm COMMAND
Manage Swarm
Commands:
init Initialize a swarm
join Join a swarm as a node and/or manager
Run 'docker swarm COMMAND --help' for more information on a command.
[root@master ~]# docker swarm init --help
Usage: docker swarm init [OPTIONS]
Initialize a swarm
Options:
--advertise-addr string Advertised address (format: "<ip|interface>[:port]")
--autolock Enable manager autolocking (requiring an unlock key to start a
stopped manager)
--availability string Availability of the node ("active", "pause", "drain") (default
"active")
--cert-expiry duration Validity period for node certificates (ns|us|ms|s|m|h)
(default 2160h0m0s)
--data-path-addr string Address or interface to use for data path traffic (format:
"<ip|interface>")
--data-path-port uint32 Port number to use for data path traffic (1024 - 49151). If no
value is set or is set to 0, the default port (4789) is used.
--default-addr-pool ipNetSlice default address pool in CIDR format (default [])
--default-addr-pool-mask-length uint32 default address pool subnet mask length (default 24)
--dispatcher-heartbeat duration Dispatcher heartbeat period (ns|us|ms|s|m|h) (default 5s)
--external-ca external-ca Specifications of one or more certificate signing endpoints
--force-new-cluster Force create a new cluster from current state
--listen-addr node-addr Listen address (format: "<ip|interface>[:port]") (default
0.0.0.0:2377)
--max-snapshots uint Number of additional Raft snapshots to retain
--snapshot-interval uint Number of log entries between Raft snapshots (default 10000)
--task-history-limit int Task history retention limit (default 5)
3.2 将当前主机作为leader
bash
[root@master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:d9:41:26 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.10/24 brd 192.168.200.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fed9:4126/64 scope link
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:2f:5c:d0:b8 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
[root@master ~]# docker swarm init --advertise-addr 192.168.200.10
Swarm initialized: current node (0r9pr636ujer02raru7kcpgw9) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-2pwtfth6qpuypkez62l411vlcsng0zw48q6immmg3aeoydf5fj-4m1wj6met556fugcqbl2dk2uz 192.168.200.10:2377
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
3.3 将第二个节点slave1加入到worker
bash
[root@master ~]# docker swarm join --token SWMTKN-1-2pwtfth6qpuypkez62l411vlcsng0zw48q6immmg3aeoydf5fj-4m1wj6met556fugcqbl2dk2uz 192.168.200.10:2377
This node joined a swarm as a worker.
第一个节点master查看node
bash
[root@master ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
0r9pr636ujer02raru7kcpgw9 * master Ready Active Leader 25.0.4
o622mci81bykwzj5ujczut3jy slave1 Ready Active 25.0.4
创建worker密钥
bash
[root@master ~]# docker swarm join-token worker
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-2pwtfth6qpuypkez62l411vlcsng0zw48q6immmg3aeoydf5fj-4m1wj6met556fugcqbl2dk2uz 192.168.200.10:2377
3.4 将第三个节点slave2也加入到worker
bash
[root@slave2 ~]# docker swarm join --token SWMTKN-1-2pwtfth6qpuypkez62l411vlcsng0zw48q6immmg3aeoydf5fj-4m1wj6met556fugcqbl2dk2uz 192.168.200.10:2377
This node joined a swarm as a worker.
master节点再次查看node
bash
[root@master ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
0r9pr636ujer02raru7kcpgw9 * master Ready Active Leader 25.0.4
o622mci81bykwzj5ujczut3jy slave1 Ready Active 25.0.4
ykwdxdwl589cxox8ffevap4hb slave2 Ready Active 25.0.4
3.5 将第四个节点(slave3)加入到manager
创建manager密钥
bash
[root@master ~]# docker swarm join-token manager
To add a manager to this swarm, run the following command:
docker swarm join --token SWMTKN-1-2pwtfth6qpuypkez62l411vlcsng0zw48q6immmg3aeoydf5fj-5ntj8tg2i2vahtljs614qjydj 192.168.200.10:2377
在slave3节点上使用
bash
[root@slave3 ~]# docker swarm join --token SWMTKN-1-2pwtfth6qpuypkez62l411vlcsng0zw48q6immmg3aeoydf5fj-5ntj8tg2i2vahtljs614qjydj 192.168.200.10:2377
This node joined a swarm as a manager.
master节点再次查看
bash
[root@master ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
0r9pr636ujer02raru7kcpgw9 * master Ready Active Leader 25.0.4
o622mci81bykwzj5ujczut3jy slave1 Ready Active 25.0.4
ykwdxdwl589cxox8ffevap4hb slave2 Ready Active 25.0.4
73lftyll7or01s0s0ld4urips slave3 Ready Active Reachable 25.0.4
搭建完成
两个管理节点,两个工作节点
两主两从
四、总结
1、生成主节点 init
2、加入(管理者、worker)
Docker Swarm适合构建和管理容器集群,以实现高可用性和负载均衡