参考:https://docs.docker.com/get-started/part4/

了解 swarm 集群

swarm 是一组运行 Docker 并且已加入集群中的机器。执行此操作后,您可以继续运行已使用的 Docker 命令,但现在它们在集群上由 swarm 管理节点执行。 swarm 中的机器可以为物理或虚拟机。加入 swarm 后,可以将它们称为节点。

swarm 管理节点可以使用多项策略来运行容器,例如“最空的节点”– 这将使用容器填充使用最少的机器。或“全局”,这将确保每台机器恰好获得指定容器的一个实例。您可以指示 swarm 管理节点使用 Compose 文件中的这些策略,就像您已使用的策略一样。

swarm 管理节点是 swarm 中可以执行命令或授权其他机器加入 swarm 作为工作节点的唯一机器。工作节点仅用于提供功能,并且无权告知任何其他机器它可以做什么和不能做什么。

在前面我们学习了docker services 在本地机器创建5个副本,这次我们可以docker swarm来体验一下在两台机器内,创建5个副本,这5个副本不在同一台机器上,实现真正的集群并且有负载均衡的功能。

首先我们得先来安培装一个工具docker-machine,

参考 :https://docs.docker.com/machine/install-machine/

$ base=https://github.com/docker/machine/releases/download/v0.14.0 &&   \

curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine &&  \

sudo install /tmp/docker-machine /usr/local/bin/docker-machine

先创建两台机器:

root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine create –driver virtualbox myvm1
Running pre-create checks…
(myvm1) No default Boot2Docker ISO found locally, downloading the latest release…
(myvm1) Latest release for github.com/boot2docker/boot2docker is v18.06.1-ce
(myvm1) Downloading /root/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v18.06.1-ce/boot2docker.iso…
(myvm1) 0%….10%….20%….30%….40%….50%….60%….70%….80%….90%….100%
Creating machine…
(myvm1) Copying /root/.docker/machine/cache/boot2docker.iso to /root/.docker/machine/machines/myvm1/boot2docker.iso…
(myvm1) Creating VirtualBox VM…
(myvm1) Creating SSH key…
(myvm1) Starting the VM…
(myvm1) Check network to re-create if needed…
(myvm1) Found a new host-only adapter: “vboxnet0”
(myvm1) Waiting for an IP…
Waiting for machine to be running, this may take a few minutes…
Detecting operating system of created instance…
Waiting for SSH to be available…
Detecting the provisioner…
Provisioning with boot2docker…
Copying certs to the local machine directory…
Copying certs to the remote machine…
Setting Docker configuration on the remote daemon…
Checking connection to Docker…
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env myvm1
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine create –driver virtualbox myvm2
Running pre-create checks…
Creating machine…
(myvm2) Copying /root/.docker/machine/cache/boot2docker.iso to /root/.docker/machine/machines/myvm2/boot2docker.iso…
(myvm2) Creating VirtualBox VM…
(myvm2) Creating SSH key…
(myvm2) Starting the VM…
(myvm2) Check network to re-create if needed…
(myvm2) Waiting for an IP…
Waiting for machine to be running, this may take a few minutes…
Detecting operating system of created instance…
Waiting for SSH to be available…
Detecting the provisioner…
Provisioning with boot2docker…
Copying certs to the local machine directory…
Copying certs to the remote machine…
Setting Docker configuration on the remote daemon…
Checking connection to Docker…
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env myvm2

查看创建的docker-machine:

root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
myvm1 – virtualbox Running tcp://192.168.99.100:2376 v18.06.1-ce
myvm2 – virtualbox Running tcp://192.168.99.101:2376 v18.06.1-ce

myvm1集群初始化:
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine ssh myvm1 “docker swarm init –advertise-addr 192.168.99.100:2376”
Swarm initialized: current node (mwy9dpd6ea1bqzvhqpi4j2ov3) is now a manager.

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

docker swarm join –token SWMTKN-1-1n6a2nkkkutdrij7hdg71hih8x5c2l7oanxa08dmvm9u20kli4-3okzvyjr4hpzu8j4q7ldhfchb 192.168.99.100:2377

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

myvm2加入集群当中:

root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine ssh myvm2 “docker swarm join –token SWMTKN-1-1n6a2nkkkutdrij7hdg71hih8x5c2l7oanxa08dmvm9u20kli4-3okzvyjr4hpzu8j4q7ldhfchb 192.168.99.100:2377”
This node joined a swarm as a worker.
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine ssh myvm1 “docker node ls”
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
mwy9dpd6ea1bqzvhqpi4j2ov3 * myvm1 Ready Active Leader 18.06.1-ce
ecvpnan32s3oug4h28bf66rxl myvm2 Ready Active 18.06.1-ce

root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine env myvm1
export DOCKER_TLS_VERIFY=”1″
export DOCKER_HOST=”tcp://192.168.99.100:2376″
export DOCKER_CERT_PATH=”/root/.docker/machine/machines/myvm1″
export DOCKER_MACHINE_NAME=”myvm1″
# Run this command to configure your shell:
# eval $(docker-machine env myvm1)

eval指令是为了登录myvm1做次备:

root@vmuser-virtual-machine:/home/vmuser/workdir/services# eval $(docker-machine env myvm1)
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
myvm1 * virtualbox Running tcp://192.168.99.100:2376 v18.06.1-ce
myvm2 – virtualbox Running tcp://192.168.99.101:2376 v18.06.1-ce

root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine ssh myvm1
## .
## ## ## ==
## ## ## ## ## ===
/”””””””””””””””””\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
_ _ ____ _ _
| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __
| ‘_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ ‘__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 18.06.1-ce, build HEAD : c7e5c3e – Wed Aug 22 16:27:42 UTC 2018
Docker version 18.06.1-ce, build e68fc7a
docker@myvm1:~$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
mwy9dpd6ea1bqzvhqpi4j2ov3 * myvm1 Ready Active Leader 18.06.1-ce
ecvpnan32s3oug4h28bf66rxl myvm2 Ready Active 18.06.1-ce
docker@myvm1:~$ exit

将docker-compose.yml传到myvm1当中:
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine scp docker-compose.yml myvm1:~
docker-compose.yml 100% 371 72.8KB/s 00:00

在myvm1上面创建服务:
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine ssh myvm1 “docker stack deploy -c docker-compose.yml getstartedlab”
Creating network getstartedlab_webnet
Creating service getstartedlab_web

通过如下指令我们可以看到5个副本当中2个副本在myvm1当中,3个副本在myvm2当中,从而所现本地化集群:
root@vmuser-virtual-machine:/home/vmuser/workdir/services# docker-machine ssh myvm1 “docker stack ps getstartedlab”
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
y597za9yvdcb getstartedlab_web.1 pan19881018/get-start:part2 myvm2 Running Preparing 20 seconds ago
0x8gp8t2r8v2 getstartedlab_web.2 pan19881018/get-start:part2 myvm1 Running Preparing 20 seconds ago
qwkkru77pyib getstartedlab_web.3 pan19881018/get-start:part2 myvm2 Running Preparing 20 seconds ago
v7jry6xie03j getstartedlab_web.4 pan19881018/get-start:part2 myvm2 Running Preparing 20 seconds ago
cucyq1fdgzbh getstartedlab_web.5 pan19881018/get-start:part2 myvm1 Running Preparing 20 seconds ago

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

  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(5) ----- docker stack 初次体验

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

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

    参考:https://docs.docker.com/get-started/part3/#docker-composeyml docker 的 service样例, 我们可以理解成是一个本地负载均衡 ...

  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 swarm - 使用体验 1+2

    背景 凭借敏捷开发部署理念的推行,相信对于很多人来说docker这项容器技术已经并不陌生,Docker 1.12引擎发布了快两个月,新引擎中包含了许多特性.诸如: Swarm模式,容器集群的健康检查, ...

随机推荐

  1. USB/232/485/TTL/CMOS(串口通信)⭐⭐⭐

    1.USB:电脑的USB口信号时USB信号,为差分信号,电压范围:+400mV~-400mV间变化:直流电压5V 驱动电流500MA 2.232电平: 逻辑1(MARK)=-3V--15V 逻辑0(S ...

  2. Python3基础 file for+文件指针 读取txt文本并 一行一行的输出(高效率)

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

  3. 51nod 1102 面积最大的矩形(单调栈)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1102 题意: 思路: 做法就是求出每个长方形向左向右所能延伸的最大距离. ...

  4. HDU 6071 Lazy Running(很牛逼的最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=6071 题意: 1.2.3.4四个点依次形成一个环,现在有个人从2结点出发,每次可以往它相邻的两个结点跑,求最后回 ...

  5. Java 文件夹递归遍历

    import java.io.File; public class Demo1 { public static void main(String[] args) { File dir=new File ...

  6. Spring中bean作用域属性scope

    关键字: spring中属性scope的prototype是什么意思   默认情况下,从bean工厂所取得的实例为Singleton(bean的singleton属性) Singleton: Spri ...

  7. Ubuntu禁用触摸板

    参考链接: http://www.linuxidc.com/Linux/2012-08/68831.htm http://blog.sina.com.cn/s/blog_a3052b4a0100z4u ...

  8. python获取文件扩展名的方法

    主要介绍了python获取文件扩展名的方法,涉及Python针对文件路径的相关操作技巧 import os.path def file_extension(path): ] print file_ex ...

  9. ubuntu14.04 设置开机自启动脚本

    方法1.将脚本放入/etc/rc.local文件中. 方法2.添加一个ubuntu的开机启动服务 分为以下几个步骤: 1)新建一个脚本文件 new_services.sh #!/bin/bash # ...

  10. grep -v grep

    ps -ef|grep /usr/local/tomcat_coachqa/ |grep -v grep |awk '{print $2}'|xargs kill -9 grep -v grep gr ...