安装并启动
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce docker-ce-cli containerd.io
systemctl start docker
初始化集群
在任意一个节点上执行初始化命令:
docker swarm init
我们可以得到work节点加入的命令:
docker swarm join --token **** ****:2377
再剩下的所有机器中执行上面得到的命令,所有机器将加入到同一个集群,此时在第一个节点中执行如下命令可以查看所有机器的在线状况:
docker node ls
至此,环境搭建完毕!就是这么简单。
启动服务
这里以ngnix为例,我总共有四个节点,所以副本数为4
docker service create --name perfect-server --replicas 4 -p 8088:80 nginx:latest
执行完上述命令后,管理节点将会把4个ngnix任务分配到四个不同节点,我们可以通过如下命令查看:
[root@VM-32-17-centos ~]# docker service ps perfect-server
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
mwpvghhvx9bo perfect-server.1 nginx:latest VM-32-17-centos Running Running 13 minutes ago
y9zc3p627sqk perfect-server.2 nginx:latest VM-32-6-centos Running Running 8 minutes ago
ycxfgf4v0opb perfect-server.3 nginx:latest VM-32-8-centos Running Running 8 minutes ago
kvpi5z9qorcn perfect-server.4 nginx:latest VM-32-11-centos Running Running 8 minutes ago
测试负载均衡
为了方便测试,我们手动修改每台nginx的index.html,在每台机器上执行分别如下脚本(这里默认你只有一个docker任务):
echo node1 > index.html
docker cp index.html $(docker ps -q):/usr/share/nginx/html
通过curl测试,我们得到如下效果:
[root@VM-32-17-centos ~]# curl node1.cocofhu.com:8088
node3
[root@VM-32-17-centos ~]# curl node1.cocofhu.com:8088
node4
[root@VM-32-17-centos ~]# curl node1.cocofhu.com:8088
node2
[root@VM-32-17-centos ~]# curl node1.cocofhu.com:8088
node1
[root@VM-32-17-centos ~]# curl node1.cocofhu.com:8088
node3
[root@VM-32-17-centos ~]# curl node1.cocofhu.com:8088
node4
[root@VM-32-17-centos ~]# curl node1.cocofhu.com:8088
node2
[root@VM-32-17-centos ~]# curl node1.cocofhu.com:8088
node1
1 条评论
נערות ליווי במרכז · 2022年4月20日 下午8:58
Next time I read a blog, Hopefully it does not disappoint me as much as this one. After all, Yes, it was my choice to read through, nonetheless I truly thought youd have something helpful to talk about. All I hear is a bunch of crying about something that you can fix if you were not too busy seeking attention.