搭建Docker Swarm集群实战(三)(swarm mode)

前言

docker在1.12版本之后,把swarm的功能内置到docker engine本身,因此之前使用consul等第三方服务发现和键值服务的方法就可以作古了。

这篇就是用docker自带的swarm mode来进行swarm的集群搭建。

这篇实操的主要参考是docker官网的教程。

个人建立的Docker爱好者交流QQ群:472149402,欢迎大家来此交流经验和问题,一起成长。

架构

总共三台虚拟机,每一台虚拟机上都有docker daemon。
* 1号机manager1,IP=192.168.99.105
* 2号机worker1, IP=192.168.99.106
* 3号机worker1, IP=192.168.99.107

开始

制作1号机

$ docker-machine create -d=virtualbox manager1
Running pre-create checks...
Creating machine...
(manager1) Copying /Users/jiandaojiao/.docker/machine/cache/boot2docker.iso to /Users/jiandaojiao/.docker/machine/machines/manager1/boot2docker.iso...
(manager1) Creating VirtualBox VM...
(manager1) Creating SSH key...
(manager1) Starting the VM...
(manager1) Check network to re-create if needed...
(manager1) 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 manager1

$ docker-machine ssh manager1

$ docker swarm init --advertise-addr 192.168.99.105
Swarm initialized: current node (yyqeur66jm8vdjem5701wjqc7) is now a manager.

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

    docker swarm join --token SWMTKN-1-4buvs0ux86szv2e7gc2x5i0viccmrhspfcaa40byu1xvsrjchu-6208r1mz51g3mp3nb39wgjrhp 192.168.99.105:2377

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

$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
yyqeur66jm8vdjem5701wjqc7 *   manager1            Ready               Active              Leader

制作2号机

$ docker-machine create -d=virtualbox worker1
Running pre-create checks...
Creating machine...
(worker1) Copying /Users/jiandaojiao/.docker/machine/cache/boot2docker.iso to /Users/jiandaojiao/.docker/machine/machines/worker1/boot2docker.iso...
(worker1) Creating VirtualBox VM...
(worker1) Creating SSH key...
(worker1) Starting the VM...
(worker1) Check network to re-create if needed...
(worker1) 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 worker1
$ docker swarm join --token SWMTKN-1-4buvs0ux86szv2e7gc2x5i0viccmrhspfcaa40byu1xvsrjchu-6208r1mz51g3mp3nb39wgjrhp 192.168.99.105:2377
This node joined a swarm as a worker.

下面在manager1上执行docker node ls

$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
yyqeur66jm8vdjem5701wjqc7 *   manager1            Ready               Active              Leader
tlslvt7foadfrhzqsii8huzx6     worker1             Ready               Active  

制作3号机

3号机的方法和2号机一模一样,只要把worker1全部换成worker2。

尝试使用swarm

发布一个容器到集群

$ docker service create --replicas 1 --name helloworld alpine ping docker.com
r937aud3gi8wj8ozuxmewm48n
Since --detach=false was not specified, tasks will be created in the background.
In a future release, --detach=false will become the default.

alpine的一个实例,一直去ping docker.com,命名为helloworld

如果目标机器上没有alpine的image的话,可能service会失败并自动去下载,等下载完,service会在此尝试启动。

查看所有的service

docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
r937aud3gi8w        helloworld          replicated          1/1                 alpine:latest       

查看名字为helloworld的service

$ docker service ps helloworld
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR                              PORTS
y5xdhd678k0f        helloworld.1        alpine:latest       manager1            Running             Running 6 minutes ago                                       
mu92gmxr9l9w         \_ helloworld.1    alpine:latest       manager1            Shutdown            Rejected 8 minutes ago   "No such image: alpine:latest@…"   

这里可以看到有两个helloworld,一个是running的,一个是shutdown的,都在manager1节点。其实shutdown的就是一开始manager1这个节点没有找到alpine的image,所以失败了。等它自动pull完alpine后,在此启动就成功了。

inspect helloworld

$ docker service inspect --pretty helloworld

ID:     r937aud3gi8wj8ozuxmewm48n
Name:       helloworld
Service Mode:   Replicated
 Replicas:  1
Placement:
UpdateConfig:
 Parallelism:   1
 On failure:    pause
 Monitoring Period: 5s
 Max failure ratio: 0
 Update order:      stop-first
RollbackConfig:
 Parallelism:   1
 On failure:    pause
 Monitoring Period: 5s
 Max failure ratio: 0
 Rollback order:    stop-first
ContainerSpec:
 Image:     alpine:latest@sha256:f006ecbb824d87947d0b51ab8488634bf69fe4094959d935c0c103f4820a417d
 Args:      ping docker.com 
Resources:
Endpoint Mode:  vip

提升helloworld服务的个数

$ docker service scale helloworld=2
helloworld scaled to 2
Since --detach=false was not specified, tasks will be scaled in the background.
In a future release, --detach=false will become the default.
$ docker service ps helloworld
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE                  ERROR                              PORTS
y5xdhd678k0f        helloworld.1        alpine:latest       manager1            Running             Running 15 minutes ago                                            
mu92gmxr9l9w         \_ helloworld.1    alpine:latest       manager1            Shutdown            Rejected 17 minutes ago        "No such image: alpine:latest@…"   
x9mimsah73qk        helloworld.2        alpine:latest       worker1             Running             Preparing about a minute ago   

删除helloworld服务

一直ping docker.com会压垮docker公司的吧,那么我们把helloworld服务删除了吧

$ docker service rm helloworld
helloworld
$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS

drain一个node

把worker1从active的状态撤下来。这里假设manager1和worker1和worker2都运行着helloworld的容器。

$ docker node update --availability drain worker1
worker1
$ docker service ps helloworld
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
u28niq3x2az8        helloworld.1        alpine:latest       manager1            Running             Running 5 seconds ago                        
sokgxqe620st         \_ helloworld.1    alpine:latest       worker1             Shutdown            Shutdown 5 seconds ago                       
59ps3v4t58i5        helloworld.2        alpine:latest       worker2             Running             Running 7 minutes ago                        
y2ll90r60def        helloworld.3        alpine:latest       manager1            Running             Running 9 minutes ago                        
$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
yyqeur66jm8vdjem5701wjqc7 *   manager1            Ready               Active              Leader
tlslvt7foadfrhzqsii8huzx6     worker1             Ready               Drain               
lnlcz1ec90vwzxw0p9e2ojtp4     worker2             Ready               Active  

可以发现几件事情:
* worker1的helloworld的容器停止,并且manager1又新启动了一个helloworld容器
* worker1的node节点状态变成drain,从此不再接受容器任务
* 运行docker node update –availability active worker1可以使它重新回到active状态

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值