参考: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. Python3基础 str translate 将指定字符转换成另一种特定字符

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. 【旧版本】Ubuntu 14.04 下 P416编译器 p4c的安装

    注:此为2017年5月份的安装方法,最新的p4c安装方法见: Ubuntu14.04下 安装p4c 参考: p4c README Ubuntu 14.04 下 P4v16编译器 p4c的安装 系统要求 ...

  3. HDU 1043 Eight(双向BFS+康托展开)

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...

  4. 使用rz,sz需要安装lrzsz

    ... tar zxvf lrzsz-1.12.20.tar.gz 4.进入目录 cd lrzsz-1.12.20 5../configure --prefix=/usr/local/lrzsz 6. ...

  5. 发现 一个 http 压测库

    代码库:https://github.com/wg/wrk 安装 https://github.com/wg/wrk

  6. samtools一些文档

    https://github.com/samtools/hts-specs

  7. sublime+LatexTools引用参考文献

        在用sublime+LatexTools一段时间之后,发现用它来写Latex真的是非常方便,配置好TexLive之后直接CTRL+B就可以直接编译运行了,so cool!但是最近写课程论文的时 ...

  8. hdu 5524 Subtrees dfs

    Subtrees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Probl ...

  9. c 宏的定义

    #include <stdio.h> #include <conio.h> #define VAL 40 #ifdef VAL #undef VAL #endif #defin ...

  10. python 本地化 local

    locale 模块提供了 C 本地化( localization )函数的接口, 如 Example 8-1 所示. 同时提供相关函数, 实现基于当前 locale 设置的数字, 字符串转换. (而 ...