搭建docker swarm集群实现负载均衡
作者:互联网
Swarm简介:
Swarm是Docker官方提供的一款集群管理工具,其主要作用是把若干台Docker主机抽象为一个整体,并且通过一个入口统一管理这些Docker主机上的各种Docker资源。
Swarm和Kubernetes比较类似,但是更加轻,具有的功能也较kubernetes更少一些
实验环境:
server1:172.25.66.1 manager
server2:172.25.66.2 node1
server3:172.25.66.3 node2
1.部署docker swarm集群
1.在物理机上制作证书
[root@foundation66 ~]# vim /etc/hosts
[root@foundation66 ~]# cd /tmp/docker/
[root@foundation66 docker]# mkdir certs
[root@foundation66 docker]# openssl req -newkey rsa:4096 -nodes -sha256 -keyout certs/domain.key -x509 -days 365 -out certs/domain.crt
[root@foundation66 docker]# cd certs/
[root@foundation66 certs]# ll
total 8
-rw-r--r--. 1 root root 2098 Mar 14 18:44 domain.crt
-rw-r--r--. 1 root root 3272 Mar 14 18:44 domain.key
[root@foundation66 certs]# cd ..
[root@foundation66 docker]# docker run -d \
> --restart=always \
> --name registry \
> -v `pwd`/certs:/certs \
> -e REGISTRY_HTTP_ADDR=0.0.0.0:443 \
> -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt \
> -e REGISTRY_HTTP_TLS_KEY=/certs/domain.key \
> -p 443:443 \
> registry:2
Unable to find image 'registry:2' locally
2: Pulling from library/registry
Digest: sha256:3b00e5438ebd8835bcfa7bf5246445a6b57b9a50473e89c02ecc8e575be3ebb5
Status: Downloaded newer image for registry:2
21de9d97895639d9a371b4dde56ae121c36716947985589e3a9b4546d6fb5735
[root@foundation66 docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21de9d978956 registry:2 "/entrypoint.sh /etc…" 20 seconds ago Up 18 seconds 0.0.0.0:443->443/tcp, 5000/tcp registry
[root@foundation66 docker]# netstat -antlp |grep :443
tcp 0 0 192.168.43.230:44402 111.7.188.1:443 ESTABLISHED 3264/firefox
tcp6 0 0 :::443 :::* LISTEN 8963/docker-proxy
[root@foundation66 docker]# cd /etc/docker
[root@foundation66 docker]# ls
certs.d daemon.json key.json
2.配置主结点
(1).下载并安装docker-engine
安装包:
docker-engine-17.05.0.ce-1.el7.centos.x86_64.rpm
docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch.rpm
[root@server1 ~]# ls
docker-engine-17.05.0.ce-1.el7.centos.x86_64.rpm
docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch.rpm
[root@server1 ~]# yum install -y *
[root@sever1 ~]# yum install -y bash-*
(2).启动docker
[root@server1 ~]# systemctl start docker
[root@sever1 ~]# netstat -antlp
(3).初始化swarm集群
[root@server1 ~]# docker swarm init
3.配置从结点
[root@sever1 ~]# scp * root@172.25.66.2:
[root@sever1 ~]# scp * root@172.25.66.3:
在server2上:
(1).安装docker-engine
[root@server2 ~]# ls
docker-engine-17.05.0.ce-1.el7.centos.x86_64.rpm
docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch.rpm
[root@server2 ~]# yum install -y *
(2)启动docker
[root@server2 ~]# systemctl start docker
(3).加入swarm集群
[root@server2 ~]# docker swarm join \
> --token SWMTKN-1-442o8zq8ph6h83sne03le48n2kslswc5yxrfd7tqef4vvr6dmw-d80ugn7jvlrs6b336r0zkptpe \
> 172.25.66.1:2377
This node joined a swarm as a worker.
在server3上:(操作同server2)
(1).安装docker-engine
[root@server3 ~]# ls
docker-engine-17.05.0.ce-1.el7.centos.x86_64.rpm
docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch.rpm
[root@server3 ~]# yum install -y *
(2)启动docker
[root@server3 ~]# systemctl start docker
(3).加入warm集群
[root@server3 ~]# docker swarm join \
> --token SWMTKN-1-442o8zq8ph6h83sne03le48n2kslswc5yxrfd7tqef4vvr6dmw-d80ugn7jvlrs6b336r0zkptpe \
> 172.25.66.1:2377
This node joined a swarm as a worker.
测试:
[root@server1 ~]# docker node ls
4.传输证书
[root@foundation66 ~]# cd /etc/docker/
[root@foundation66 docker]# ls
certs.d daemon.json key.json
[root@foundation66 docker]# scp -r certs.d/ root@172.25.66.1:/etc/docker
[root@foundation66 docker]# scp -r certs.d/ root@172.25.66.2:/etc/docker
[root@foundation66 docker]# scp -r certs.d/ root@172.25.66.3:/etc/docker
5.添加解析
[root@server1 ~]# vim /etc/hosts
[root@server2 ~]# vim /etc/hosts
[root@server3 ~]# vim /etc/hosts
6.上传镜像
[root@foundation66 ~]# cd /etc/docker/
[root@foundation66 docker]# docker tag nginx westos.org/nginx
[root@foundation66 docker]# docker push westos.org/nginx
7.创建nginx服务
#服务数量为3,公开指定端口是8080映射容器80,使用nginx镜像
[root@server1 ~]# docker service create --name nginx --publish 80:80 --replicas 3 westos.org/nginx
#多执行几次,看到3/3就表示节点部署成功
[root@server1 ~]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
aaw4wl9q7bvd nginx replicated 3/3 westos.org/nginx:latest *:80->80/tcp
[root@server1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ceace64fe731 westos.org/nginx:latest "nginx -g 'daemon ..." 9 minutes ago Up 8 minutes 80/tcp nginx.1.ijwxybakh8iuzz5hu6hf2m0ma
[root@server1 ~]# docker service ps nginx
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
ijwxybakh8iu nginx.1 westos.org/nginx:latest server1 Running Running 9 minutes ago
rol3e3dh55eh nginx.2 westos.org/nginx:latest server2 Running Running 9 minutes ago
xghzsv46okz1 nginx.3 westos.org/nginx:latest server3 Running Running 9 minutes ago
#添加3个nginx服务
[root@server1 ~]# docker service scale nginx=3
nginx scaled to 3
[root@server1 ~]# docker service ps nginx
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
ijwxybakh8iu nginx.1 westos.org/nginx:latest server1 Running Running 12 minutes ago
rol3e3dh55eh nginx.2 westos.org/nginx:latest server2 Running Running 12 minutes ago
xghzsv46okz1 nginx.3 westos.org/nginx:latest server3 Running Running 12 minutes ago
#发现每个结点均分一个nginx服务
[root@server1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ceace64fe731 westos.org/nginx:latest "nginx -g 'daemon ..." 12 minutes ago Up 12 minutes 80/tcp nginx.1.ijwxybakh8iuzz5hu6hf2m0ma
[root@server2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
48385ae882d3 westos.org/nginx:latest "nginx -g 'daemon ..." 12 minutes ago Up 12 minutes 80/tcp nginx.2.rol3e3dh55eh1ox9wmv430ydy
[root@server3 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
42ca62659a89 westos.org/nginx:latest "nginx -g 'daemon ..." 12 minutes ago Up 12 minutes 80/tcp nginx.3.xghzsv46okz1l2b1yvullo90w
注意:如果nginx服务创建错误,执行以下命令即可删除,再重新创建即可
[root@sever1 ~]# docker service rm nginx
nginx
测试:
无论输入3个节点的ip均可以看到nginx默认发布首页
2.集群负载均衡
1.拉取镜像
[root@foundation66 Desktop]# docker pull docker.io/dockersamples/visualizer
2.上传镜像
#更改名称
[root@foundation66 Desktop]# docker tag dockersamples/visualizer westos.org/visualizer
#上传镜像
[root@foundation66 Desktop]# docker push westos.org/visualizer
3.创建visualizer服务
[root@server1 ~]# docker service create \
> --name=viz \
> --publish=8080:8080/tcp \
> --constraint=node.role==manager \
> --mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
> westos.org/visualizer
8lxnuxxt8ayuaifk04209nd7m
Since --detach=false was not specified, tasks will be created in the background.
In a future release, --detach=false will become the default.
#多执行几次,直到查看到REPLICAS变为1/1
[root@server1 ~]# docker service ls
4.编写发布页面
在server1上:
#1.编写文件
[root@server1 docker]# echo server1 > index.html
[root@server1 ~]# cat index.html
server1
#2.拷贝文件
[root@server1 ~]# docker ps
[root@server1 ~]# docker container cp index.html nginx.2.ykxkni9lxcy306jtyyr089e9u:/usr/share/nginx/html
在server2上:
[root@server2 ~]# echo server2 > index.html
[root@sever2 ~]# cat index.html
server2
[root@server2 ~]# docker ps
[root@server2 ~]# docker container cp index.html nginx.3.tcitff6ljd980kzua8os540lq:/usr/share/nginx/html
在server3上:
[root@server3 ~]# echo server3 > index.html
[root@server3 ~]# cat index.html
server3
[root@server3 ~]# docker ps
[root@server3 ~]# docker container cp index.html nginx.1.vc2goxg1rjxchn2mbyfvntqqw:/usr/share/nginx/html
测试:
#利用for循环,访问10次。发现server1和server2和server3交替,说明负载均衡搭建成功
[root@foundation66 ~]# for i in {1..10}; do curl 172.25.66.1;done
在网页上输入:172.25.66.1:8080
模拟server3结点宕机
[root@server3 ~]# systemctl stop docker
刷新网页:
标签:负载,nginx,server1,server3,swarm,docker,root,foundation66 来源: https://blog.csdn.net/lilygg/article/details/88559028