参考: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.

learning docker steps(3) ----- docker services 初次体验的更多相关文章

  1. learning docker steps(8) ----- docker network 初次体验

    参考: docker network 主要是介绍了docker 容器之间的组网模式, 一般来说实像组网主要是依赖于bridge iptalbes vlan来实现,但是附带的如端口转发会降低效率. 新型 ...

  2. learning docker steps(2) ----- docker contailner 初次体验

    参考:https://docs.docker-cn.com/get-started/part2/ Dockerfile的内容如下所示: # 将官方 Python 运行时用作父镜像 FROM pytho ...

  3. learning docker steps(1) ----- docker 安装

    docker 安装 参考:https://docs.docker.com/install/linux/docker-ce/ubuntu/ 按如下指令可安装: $ sudo apt-get instal ...

  4. learning docker steps(4) ----- docker swarm 初次体验

    参考:https://docs.docker.com/get-started/part4/ 了解 swarm 集群 swarm 是一组运行 Docker 并且已加入集群中的机器.执行此操作后,您可以继 ...

  5. learning docker steps(5) ----- docker stack 初次体验

    参考:https://docs.docker.com/get-started/part5/ stack 技术栈.技术栈是一组相关的服务,它们共享依赖项并且可以一起进行编排和扩展.单个技术栈能够定义和协 ...

  6. learning docker steps(7) ----- docker registry 搭建

    参考: https://docs.docker.com/engine/reference/builder/ https://hub.docker.com/_/registry/ https://www ...

  7. learning docker steps(6) ----- docker 镜像加速

    http://www.docker-cn.com/registry-mirror 通过 Docker 官方镜像加速,中国区用户能够快速访问最流行的 Docker 镜像.该镜像托管于中国大陆,本地用户现 ...

  8. 在docker中初次体验.net core 2.0

    .net core的跨平台有了Linux,不能没有docker……网上的系列文章一大推,特别是docker还有了中文官网:https://www.docker-cn.com/ .上面说的很清楚了,这里 ...

  9. docker初次体验-管理MySQL+tomcat镜像

    引言 平时经常用linux,我没少吃苦后悔linux没好好研究研究.装一些软件配一些环境时很是害怕,多亏有了docker.docker是一个应用容器引擎,可以管理很多的软件镜像,这些镜像被官方放在了d ...

随机推荐

  1. vs2012旗舰版 有效注册密钥

    Microsoft Visual Studio Ultimate 2012 旗舰版 有效注册密钥: YKCW6-BPFPF-BT8C9-7DCTH-QXGWC

  2. openwrt如何单独编译uboot

    答:make package/boot/uboot-<chip series>/compile

  3. ubuntu查询某个库的相关情况

    环境:Ubuntu 14.04 64bit 1.如:查询libjpeg库的位置 ldconfig -p |grep libjpeg 2.如:查询libjpeg库的相关名称 dpkg -l '*jpeg ...

  4. JS事件监听器

    JS事件监听器 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Javasc ...

  5. MIMO雷达比幅单脉冲测角精度分析(系统工程与电子技术)

    MIMO雷达比幅单脉冲测角精度分析(系统工程与电子技术)

  6. 03_Flume多节点Failover实践

    1.实践场景 模拟上游Flume Agent在发送event时的故障切换 (failover) 1)初始:上游Agent向active的下游节点Collector1传递event 2)Collecto ...

  7. HDU 5889 Barricade(最短路+最小割)

    http://acm.hdu.edu.cn/showproblem.php?pid=5889 题意: 给出一个图,帝国将军位于1处,敌军位于n处,敌军会选择最短路到达1点.现在帝国将军要在路径上放置障 ...

  8. jquery和bootstrap获取checkbox选中的多行数据

    在项目中,经常遇到,于是整理 引用bootstrap的js和css 代码解释: $("#dgFlowList").find(":checkbox:checked" ...

  9. hdu 5651 xiaoxin juju needs help 逆元 两种求解方式

    xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  10. shell 字符串提取数字

    echo "2014年7月21日" | tr -cd "[0-9]" 这样就可以提取出2014721