Docker swarm叢集實現負載均衡
阿新 • • 發佈:2018-12-28
實驗環境:
server1:172.25.40.11 swarm
server2:172.25.40.12 node
server3:172.25.40.13 node
搭建叢集
【server1】
[[email protected] ~]# ls
docker-engine-17.03.1.ce-1.el7.centos.x86_64.rpm
docker-engine-selinux-17.03.1.ce-1.el7.centos.noarch.rpm
[[email protected] ~]# yum install -y docker-engine-*
[[email protected] ~]# scp * server2:
[[email protected] ~]# scp * server3:
[[email protected] ~]# systemctl start docker
[[email protected] ~]# docker swarm init #初始化
在當前主機上啟動swarm模式,要儲存初始化後token,因為在節點加入時要使用token作為通訊的金鑰
【server2】
[root@server2 ~]# yum install * -y
[root@server2 ~]# systemctl start docker
[root@server2 ~]# docker swarm join \
> --token SWMTKN-1-52ppekqqbl06qdk23onaqa7txzku0q4mxazyi325nz4w4slkeg-0lybzr54rqypko80a0qsyylcs \
> 172.25.40.11:2377 # 加入到swarm叢集中
【server3】
[root@server3 ~]# systemctl start docker
[root@server3 ~]# docker swarm join \
> --token SWMTKN-1-52ppekqqbl06qdk23onaqa7txzku0q4mxazyi325nz4w4slkeg-0lybzr54rqypko80a0qsyylcs \
> 172.25.40.11:2377
在【server1】檢視節點
部署服務
[root@foundation40 ~]# docker ps
[root@foundation40 ~]# 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
[root@foundation40 docker]# docker ps
[root@foundation40 docker]# docker push westos.org/nginx
將物理主機的證書服務傳到三臺服務機上:(證書生成看上篇部落格)
[root@foundation40 ~]# cd /etc/docker/
[root@foundation40 docker]# ls
certs.d daemon.json key.json
[root@foundation40 docker]# scp -r certs.d/ [email protected]:/etc/docker
[root@foundation40 docker]# scp -r certs.d/ [email protected]:/etc/docker
[root@foundation40 docker]# scp -r certs.d/ [email protected]:/etc/docker
在【server1】【server2】【server3】上新增解析
[root@server1 ~]# vim /etc/hosts
172.25.40.250 westos.org
【server1】
在manager節點部署nginx服務,服務數量為3個,公開指定埠是8080對映容器80,使用nginx映象
[root@server1 ~]# docker service create --name nginx --publish 80:80 --replicas 3 westos.org/nginx #建立nginx服務
[root@server1 ~]# docker service ls #開啟過程慢
[root@server1 ~]# docker service ls
[root@server1 ~]# docker service ps nginx
[root@server1 ~]# docker ps
[root@server1 ~]# docker service scale nginx=6
[root@server1 ~]# docker service ps nginx
[root@server1 ~]# docker ps
在網頁訪問三個主機的IP都可以看到nginx網頁
負載均衡
在物理機上:
需要一個visualizer.tar
[root@foundation40 ~]# docker load -i visualizer.tar
[root@foundation40 ~]# docker images
dockersamples/visualizer latest 17e55a9b2354 11 months ago 148 MB
[root@foundation40 ~]# docker tag dockersamples/visualizer westos.org/visualizer #更換名字
[root@foundation40 ~]# docker push westos.org/visualizer
【server1】
[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
[root@server1 ~]# docker service ls
[root@server1 ~]# docker service scale nginx=3
[root@server1 ~]# docker ps
[root@server1 ~]# echo server1 > index.html
[root@server1 ~]# docker container cp index.html nginx.3.8e8h01k29kfxvy1g0lh3mjcl9:/usr/share/nginx/html
【server2】
[root@server2 ~]# docker ps
[root@server2 ~]# echo server2 > index.html
[root@server2 ~]# docker container cp index.html nginx.4.yab0b56egf6d1qf5xdzvvb3lo:/usr/share/nginx/html
【server3】
[root@server3 ~]# docker ps
[root@server3 ~]# echo server3 > index.html
[root@server3 ~]# docker container cp index.html nginx.2.w5ibu3y3jl6sctmiglkxz13r1:/usr/share/nginx/html
在主機上檢視是否有負載均衡:
[[email protected] ~]# for i in {1..10}; do curl 172.25.40.11;done
網頁訪問http://172.25.40.11:8080/檢視三臺主機的nginx負載均衡
如果任意關閉一臺主機的docker,此時的負載均衡就在兩臺主機上實現
滾動更新
[root@foundation40 ~]# docker images
[root@foundation40 ~]# docker tag rhel7:v1 westos.org/rhel7:v1
[root@foundation40 ~]# docker push westos.org/rhel7:v1
[root@server1 ~]# docker service scale nginx=30
[root@server1 ~]# docker service update --image westos.org/rhel7:v1 --update-parallelism 3 --update-delay 10s nginx