准备三台Docker主机
这里是基于Ubuntu18.04
Server A:192.168.3.20
Server B:192.168.3.252
Server C:192.168.3.246
sudo apt install docker.io
vim /etc/apt/sources.list
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
vim /etc/docker/daemon.json
{
"registry-mirrors" : [
"https://mirror.ccs.tencentyun.com",
"http://registry.docker-cn.com",
"http://docker.mirrors.ustc.edu.cn",
"http://hub-mirror.c.163.com"
],
"insecure-registries" : [
"registry.docker-cn.com",
"docker.mirrors.ustc.edu.cn"
],
"log-driver":"json-file",
"log-opts": {"max-size":"500m", "max-file":"3"}
}
在Server A上
baloneo@baloneo:~$ sudo docker swarm init
Swarm initialized: current node (7kk0huvknn6891vfc231lls97) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-243ondqi7ujhu8jes5ygasds2ej5rdv767o4p2diexe64tbhzl-1sj0qydzanf2vvytlcf9p3gnq 192.168.3.20:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
在Server B上:
sudo docker swarm join --token SWMTKN-1-243ondqi7ujhu8jes5ygasds2ej5rdv767o4p2diexe64tbhzl-1sj0qydzanf2vvytlcf9p3gnq 192.168.3.20:2377
在Server C上
➜ docker swarm join --token SWMTKN-1-243ondqi7ujhu8jes5ygasds2ej5rdv767o4p2diexe64tbhzl-1sj0qydzanf2vvytlcf9p3gnq 192.168.3.20:2377
>>>
This node joined a swarm as a worker.
Server C、Server B作为工作节点。Server A作为管理节点。
docker-swarm.png在管理节点上运行sudo docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
5t5680v5lu86zx8y3np4lz33x baloneo Ready Active 19.03.6
7kk0huvknn6891vfc231lls97 * baloneo Ready Active Leader 19.03.6
jcdlrk389dvdl7otsjv4t7ixw baloneo-pc Ready Active 19.03.6
在Server A(管理节点):
sudo docker pull nginx
sudo docker service create --replicas 3 -p 80:80 --name nginx nginx
开启三个nginx负载,当我们开启之后,就已经实现了负载均衡了,访问任意一个节点的ip:80都会进行负载均衡
➜ feem http 192.168.3.246
HTTP/1.1 200 OK
Accept-Ranges: bytes
Connection: keep-alive
Content-Length: 18
Content-Type: text/html
Date: Wed, 24 Jun 2020 06:19:48 GMT
ETag: "5ef2b7c2-12"
Last-Modified: Wed, 24 Jun 2020 02:17:38 GMT
Server: nginx/1.19.0
<h1>Server C</h1>
➜ feem http 192.168.3.246
HTTP/1.1 200 OK
Accept-Ranges: bytes
Connection: keep-alive
Content-Length: 18
Content-Type: text/html
Date: Wed, 24 Jun 2020 06:19:50 GMT
ETag: "5ef2f07f-12"
Last-Modified: Wed, 24 Jun 2020 06:19:43 GMT
Server: nginx/1.19.0
<h1>Server B</h1>
➜ feem
网友评论