构建集群
实验环境:rhel7.3
test1: 172.25.52.11 主节点
test2: 172.25.52.22 辅节点
test3: 172.25.52.33 辅节点
实验:
(1).搭建实验环境
做此实验的前提是真机已经完成了证书的制作
[root@test1 ~]# ls
anaconda-ks.cfg
docker-engine-17.03.1.ce-1.el7.centos.x86_64.rpm
docker-engine-selinux-17.03.1.ce-1.el7.centos.noarch.rpm
[root@test1 ~]# yum install *
[root@test1 ~]# systemctl start docker
[root@test1 ~]# yum install -y bash-*
[root@test1 ~]# docker swarm init
Swarm initialized: current node (534lg9252rj3w8s88o0lb8kkz) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-4331clm1g9imylu1ymt8lgg04ou64wxrnjmnacc9tu3cu96340-294r7jtbu5rz4xpocyxhb89ib \
172.25.52.11:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
[root@test2 ~]# ls
anaconda-ks.cfg
docker-engine-17.03.1.ce-1.el7.centos.x86_64.rpm
docker-engine-selinux-17.03.1.ce-1.el7.centos.noarch.rpm
[root@test2 ~]# yum install -y *
[root@test2 ~]# systemctl start docker
[root@test2 ~]# docker swarm join \
> --token SWMTKN-1-4331clm1g9imylu1ymt8lgg04ou64wxrnjmnacc9tu3cu96340-294r7jtbu5rz4xpocyxhb89ib \
> 172.25.52.11:2377
This node joined a swarm as a worker.
[root@test3 ~]# ls
anaconda-ks.cfg
docker-engine-17.03.1.ce-1.el7.centos.x86_64.rpm
docker-engine-selinux-17.03.1.ce-1.el7.centos.noarch.rpm
[root@test3 ~]# yum install * -y
[root@test3 ~]# systemctl start docker
[root@test3 ~]# docker swarm join \
> --token SWMTKN-1-4331clm1g9imylu1ymt8lgg04ou64wxrnjmnacc9tu3cu96340-294r7jtbu5rz4xpocyxhb89ib \
> 172.25.52.11:2377
This node joined a swarm as a worker.
测试:
[root@test1 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
534lg9252rj3w8s88o0lb8kkz * test1 Ready Active Leader
89sgbz9lg0yk7mjnuyqt2k1hr test3 Ready Active
l0hypr052p9hlz4lch5djm7bu test2 Ready Active
(2).传输证书
[root@foundation52 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d5e1dec99b8d registry:2 "/entrypoint.sh /e..." 42 minutes ago Up 42 minutes 0.0.0.0:443->443/tcp, 5000/tcp registry
##将制作好的证书发送给3个节点
[root@foundation52 ~]# cd /etc/docker/
[root@foundation52 docker]# ls
certs.d daemon.json key.json
[root@foundation52 docker]# scp -r certs.d/ [email protected]:/etc/docker
[root@foundation52 docker]# scp -r certs.d/ [email protected]:/etc/docker
[root@foundation52 docker]# scp -r certs.d/ [email protected]:/etc/docker
(3).本地解析
[root@test1 ~]# vim /etc/hosts
############### 172.25.52.250 westos.org [root@test2 ~]# vim /etc/hosts ###############
172.25.52.250 westos.org
[root@test3 ~]# vim /etc/hosts
############### 172.25.52.250 westos.org
(4).上传nginx镜像
[root@foundation52 ~]# cd /etc/docker
[root@foundation52 docker]# docker tag nginx westos.org/nginx
[root@foundation52 docker]# docker push westos.org/nginx
The push refers to a repository [westos.org/nginx]
08d25fa0442e: Pushed
a8c4aeeaa045: Pushed
cdb3f9544e4c: Pushed
latest: digest: sha256:2de9d5fc6585b3f330ff5f2c323d2a4006a49a476729bbc0910b695771526e3f size: 948
(5).创建一个指定name、port、replicas的服务
[root@test1 ~]# docker service create --name nginx --publish 80:80 --replicas 3 westos.org/nginx
[root@test1 ~]# docker service ls
ID NAME MODE REPLICAS IMAGE
r6qmvdilhaas nginx replicated 3/3 westos.org/nginx:latest
[root@test1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fc022d9ec4dd westos.org/nginx@sha256:2de9d5fc6585b3f330ff5f2c323d2a4006a49a476729bbc0910b695771526e3f "nginx -g 'daemon ..." 12 seconds ago Up 10 seconds 80/tcp nginx.1.ylvbx0spf1l4htc6fiq5nmmi8
[root@test1 ~]# docker service ps nginx
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
ylvbx0spf1l4 nginx.1 westos.org/nginx:latest test1 Running Running 2 minutes ago
si9ogd5al8wo nginx.2 westos.org/nginx:latest test2 Running Running 2 minutes ago
7724391c17s6 nginx.3 westos.org/nginx:latest test3 Running Running 2 minutes ago
##将nginx服务缩放为10个任务
[root@test1 ~]# docker service scale nginx=3
nginx scaled to 3
[root@test1 ~]# docker service ps nginx
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
ylvbx0spf1l4 nginx.1 westos.org/nginx:latest test1 Running Running 3 minutes ago
si9ogd5al8wo nginx.2 westos.org/nginx:latest test2 Running Running 3 minutes ago
7724391c17s6 nginx.3 westos.org/nginx:latest test3 Running Running 3 minutes ago
##每台主机均分一个nginx进程
[root@test1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fc022d9ec4dd westos.org/nginx@sha256:2de9d5fc6585b3f330ff5f2c323d2a4006a49a476729bbc0910b695771526e3f "nginx -g 'daemon ..." 6 minutes ago Up 6 minutes 80/tcp nginx.1.ylvbx0spf1l4htc6fiq5nmmi8
[root@test2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
105d9b5e314c westos.org/nginx@sha256:2de9d5fc6585b3f330ff5f2c323d2a4006a49a476729bbc0910b695771526e3f "nginx -g 'daemon ..." 13 minutes ago Up 13 minutes 80/tcp nginx.2.si9ogd5al8wod3xdr1khaxb7a
[root@test3 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3b775e72b42 westos.org/nginx@sha256:2de9d5fc6585b3f330ff5f2c323d2a4006a49a476729bbc0910b695771526e3f "nginx -g 'daemon ..." 14 minutes ago Up 13 minutes 80/tcp nginx.3.7724391c17s6mr5kfi71uzfuz
实验负载均衡
先下载visualizer.tar镜像
##导入镜像
[root@foundation52 Desktop]# docker load -i visualizer.tar
[root@foundation52 Desktop]# docker images
dockersamples/visualizer latest 17e55a9b2354 11 months ago 148 MB
[root@foundation52 Desktop]# docker tag dockersamples/visualizer westos.org/visualizer
##上传镜像
[root@foundation52 Desktop]# docker push westos.org/visualizer
[root@test1 ~]# 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
jcuxca7ihoa99oji1bfqmz5ta
[root@test1 ~]# docker service ls
ID NAME MODE REPLICAS IMAGE
jcuxca7ihoa9 viz replicated 0/1 westos.org/visualizer:latest
y1hli5s1d4zi nginx replicated 3/3 westos.org/nginx:latest
##耐心等待 直到查看到REPLICAS变为1/1
[root@test1 ~]# docker service ls
ID NAME MODE REPLICAS IMAGE
jcuxca7ihoa9 viz replicated 1/1 westos.org/visualizer:latest
y1hli5s1d4zi nginx replicated 3/3 westos.org/nginx:latest
[root@test1 ~]# echo test1 > index.html
[root@test1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cdfa83d2fa4d westos.org/visualizer@sha256:ea603c00f25b18c9a3aa49703c47bd5ca6c105637fe6f4887b606043c66ab528 "npm start" 4 minutes ago Up 4 minutes (healthy) 8080/tcp viz.1.mqpbtsmeq5bqpq0o4itrrforp
fc022d9ec4dd westos.org/nginx@sha256:2de9d5fc6585b3f330ff5f2c323d2a4006a49a476729bbc0910b695771526e3f "nginx -g 'daemon ..." 25 minutes ago Up 25 minutes 80/tcp nginx.1.ylvbx0spf1l4htc6fiq5nmmi8
[root@test1 ~]# docker container cp index.html nginx.1.ylvbx0spf1l4htc6fiq5nmmi8:/usr/share/nginx/html
[root@test2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
105d9b5e314c westos.org/nginx@sha256:2de9d5fc6585b3f330ff5f2c323d2a4006a49a476729bbc0910b695771526e3f "nginx -g 'daemon ..." 28 minutes ago Up 28 minutes 80/tcp nginx.2.si9ogd5al8wod3xdr1khaxb7a
[root@test2 ~]# echo test2 > index.html
[root@test2 ~]# docker container cp index.html nginx.2.si9ogd5al8wod3xdr1khaxb7a:/usr/share/nginx/html
[root@test3 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3b775e72b42 westos.org/nginx@sha256:2de9d5fc6585b3f330ff5f2c323d2a4006a49a476729bbc0910b695771526e3f "nginx -g 'daemon ..." 34 minutes ago Up 34 minutes 80/tcp nginx.3.7724391c17s6mr5kfi71uzfuz
[root@test3 ~]# echo test3 > index.html
[root@test3 ~]# docker container cp index.html nginx.3.7724391c17s6mr5kfi71uzfuz:/usr/share/nginx/html
测试:
[root@foundation52 Desktop]# for i in {1..10}; do curl 172.25.52.11;done
test2
test1
test3
test2
test1
test3
test2
test1
test3
test2
在网页上输入172.25.52.11:8080 (主节点的ip)
监控负载均衡
##查看到仓库里有game2048镜像
[root@foundation52 Desktop]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
game2048 latest 19299002fdbe 20 months ago
[root@foundation52 Desktop]# docker tag game2048 westos.org/game2048
##上传镜像
[root@foundation52 Desktop]# docker push westos.org/game2048
The push refers to a repository [westos.org/game2048]
88fca8ae768a: Pushed
6d7504772167: Pushed
192e9fad2abc: Pushed
36e9226e74f8: Pushed
011b303988d2: Pushed
latest: digest: sha256:31a46998f55ed03da6d62197f3a91b0a100c0abdd7380d88142ea44bce325001 size: 1364
[root@test1 ~]# docker service update --image westos.org/game2048 --update-parallelism 3 --update-delay 10s nginx
nginx