learning docker steps(3) ----- docker services 初次体验

参考:https://docs.docker.com/get-started/part3/#docker-composeyml

docker 的 service样例, 我们可以理解成是一个本地负载均衡的样例,一次性创建5个容器,处理请求http请求,并返回处理请求的主机。

 

1. docker swarm 服务初始化: 进入集群模式
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker swarm init
Swarm initialized: current node (oyunvuhucng5600g5xve3tiad) is now a manager.

To add a worker to this swarm, run the following command:

docker swarm join –token SWMTKN-1-2j08q2027t3vkazrqg8btessvub0rdjh7nwswqeysmljt1st3n-drqgpun6zvla7ok7d7jhk8see 192.168.0.119:2377

To add a manager to this swarm, run ‘docker swarm join-token manager’ and follow the instructions.

2. docker stack deploy 集群启动,通过-c指定文件,getstartedlab为名称
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker stack deploy -c docker-compose.yml getstartedlab
Creating network getstartedlab_webnet
Creating service getstartedlab_web
3. 查看当前的服务
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
snqp0guylvnq getstartedlab_web replicated 5/5 pan19881018/get-start:part2 *:4000->80/tcp

4. 查看服务内的容器
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker service ps getstartedlab_web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
3h3qhz5zdl4l getstartedlab_web.1 pan19881018/get-start:part2 vmuser-virtual-machine Running Running about a minute ago
xym5mer3ymx3 getstartedlab_web.2 pan19881018/get-start:part2 vmuser-virtual-machine Running Running about a minute ago
t3i3ntoy5mke getstartedlab_web.3 pan19881018/get-start:part2 vmuser-virtual-machine Running Running about a minute ago
7wvii3mug6gm getstartedlab_web.4 pan19881018/get-start:part2 vmuser-virtual-machine Running Running about a minute ago
yq7die0874vz getstartedlab_web.5 pan19881018/get-start:part2 vmuser-virtual-machine Running Running about a minute ago

5 查看当前的容器
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker container ls -q
a7e47e7cfaa0
85ed544aa172
af09c78bc902
9002394e8581
6fbc188a3e5a

6. 测试负载均衡,可以发现我们每次的请求,后台返回的主机名称是不一样的,从而实现负载均衡。
vmuser@vmuser-virtual-machine:~$ curl http://localhost:4888
<h3>Hello World!</h3><b>Hostname:</b> 4464de9fd6ee<br/><b>Visits:</b> <i>cannot connect to Redis, counter disablvmuser@vmuser-virtual-machine:~$ curl 4 http://localhost:4000
curl: (7) Couldn’t connect to server
<h3>Hello World!</h3><b>Hostname:</b> af09c78bc902<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>vmuser@vmuser-virtual-machine:~$ curl 4 http://localhost:4000
curl: (7) Couldn’t connect to server
<h3>Hello World!</h3><b>Hostname:</b> 9002394e8581<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>vmuser@vmuser-virtual-machine:~$ curl 4 http://localhost:4000
curl: (7) Couldn’t connect to server
<h3>Hello World!</h3><b>Hostname:</b> a7e47e7cfaa0<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>vmuser@vmuser-virtual-machine:~$ curl 4 http://localhost:4000
curl: (7) Couldn’t connect to server
<h3>Hello World!</h3><b>Hostname:</b> 6fbc188a3e5a<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>vmuser@vmuser-virtual-machine:~$ curl 4 http://localhost:4000
curl: (7) Couldn’t connect to server
<h3>Hello World!</h3><b>Hostname:</b> 85ed544aa172<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>vmuser@vmuser-virtual-machine:~$ curl 4 http://localhost:4000
curl: (7) Couldn’t connect to server
<h3>Hello World!</h3><b>Hostname:</b> af09c78bc902<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>vmuser@vmuser-virtual-machine:~$ curl 4 http://localhost:4000
curl: (7) Couldn’t connect to server
<h3>Hello World!</h3><b>Hostname:</b> 9002394e8581<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>vmuser@vmuser-virtual-machine:~$ curl 4 http://localhost:4000
curl: (7) Couldn’t connect to server
<h3>Hello World!</h3><b>Hostname:</b> a7e47e7cfaa0<br/><b>Visits:</b> <i>cannot connect to Redis, counter disablvmuser@vmuser-virtual-machine:~$

root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker container ls -q
a7e47e7cfaa0
85ed544aa172
af09c78bc902
9002394e8581
6fbc188a3e5a

7. 删除服务
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker stack rm getstartedlab
Removing service getstartedlab_web
Removing network getstartedlab_webnet

8. 强制离开服务,退出集群模式
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker swarm leave –force
Node left the swarm.
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker container ls -q
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker service ls
Error response from daemon: This node is not a swarm manager. Use “docker swarm init” or “docker swarm join” to connect this node to swarm and try again.

posted @ 2018-12-25 15:46  嵌入式实操  阅读(187)  评论(0编辑  收藏  举报