参考: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. intent bundle的使用

    1.什么是bundle Bundle主要用于传递数据:它保存的数据,是以key-value(键值对)的形式存在的.我们经常使用Bundle在Activity之间传递数据,传递的数据可以是boolean ...

  2. BZOJ 3529 数表(莫比乌斯+树状数组)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3529 题意:有一张n×m的数表,其第i行第j列的数值为能同时整除i和j的所有自然数 ...

  3. 权限管理,pymysql模块

    权限管理 权限管理重点 MySQL 默认有个root用户,但是这个用户权限太大,一般只在管理数据库时候才用.如果在项目中要连接 MySQL 数据库,则建议新建一个权限较小的用户来连接. 在 MySQL ...

  4. Eclipse中离线安装ADT插件详细教程

    在搭建Android开发环境的时候,我们需要为Eclipse安装ADT(Android Development Tools)插件,这个插件可以为用户提供一个强大的Android集成开发环境.通过给Ec ...

  5. ubuntu下交叉编译ffmpeg

    环境:ubuntu16.04 交叉编译器版本:4.8.3 依赖x264,lame x264: 1.wget ftp://ftp.videolan.org/pub/x264/snapshots/last ...

  6. Python 开发环境搭建

    Python分别有两个大的版本,分别是2和3 下载地址:Python-3.6.2   Python-2.7.13 现在安装路径:D:\Program Files\Python 安装完成以后要安装 pi ...

  7. POJ 2752 Seek the Name, Seek the Fame(KMP中next的理解)题解

    题意: 要求你给出每个前后缀相同的串的长度,比如: "alala"的前缀分别为{"a", "al", "ala", &q ...

  8. 解决Eclipse 项目报错:Unbound classpath container: ‘JRE System Library [JavaSE-1.7]

    MyEclipse出现下面两条报错: The project cannot be built until build path errors are resolved HelloWord Unknow ...

  9. HashMap和LinkedHashMap的比较使用

      由于现在项目中用到了LinkedHashMap,并不是太熟悉就到网上搜了一下. import java.util.HashMap; import java.util.Iterator; impor ...

  10. pandas 设置单元格的值

    import pandas as pd import pickle import numpy as np dates = pd.date_range() df = pd.DataFrame(np.ar ...