频道栏目
首页 > 系统 > 其他 > 正文

正式使用docker的swarm模式

2018-01-10 10:04:46      个评论    来源:李天泉  
收藏   我要投稿

docker 1.12以及之后的版本集成了swarmkit等编排服务,现在docker的版本为1.12-rc2版本。本文来尝试试用此种模式建立原生的docker集群

本文将包含如下几点:

在swarm模式下初始化一个docker引擎的集群

添加节点至swarm中

发布一个应用服务到swarm中

当运行起来之后进行swarm管理

设置

尝试本例子需要如下几点要求:

三台网络互通的宿主机:

安装1.12或更高版本的docker

管理节点的IP地址

开启宿主机之间的端口

TCP端口2377集群管理端口

TCP与UDP端口7946节点之间通讯端口

TCP与UDP端口4789 overlay网络通讯端口

iptables -A INPUT -p tcp --dport 2377 -j ACCEPT

iptables -A INPUT -p tcp --dport 7946 -j ACCEPT

iptables -A INPUT -p udp --dport 7946 -j ACCEPT

iptables -A INPUT -p tcp --dport 4789 -j ACCEPT

iptables -A INPUT -p udp --dport 4789 -j ACCEPT

创建swarm集群

创建管理节点

docker swarm init --listen-addr :1

如下所示:

[root@centos01 container]# docker swarm init --listen-addr 172.18.18.201:2377

Swarm initialized: current node (4am2qb52uw8r2ubxlkq3bxzyl) is now a manager.

[root@centos01 container]# docker info

Containers: 11

Running: 7

Paused: 0

Stopped: 4

Images: 5

Server Version: 1.12.0-rc1

Storage Driver: devicemapper

Pool Name: docker-253:0-2098542-pool

Pool Blocksize: 65.54 kB

Base Device Size: 10.74 GB

Backing Filesystem: xfs

Data file: /dev/loop0

Metadata file: /dev/loop1

Data Space Used: 1.251 GB

Data Space Total: 107.4 GB

Data Space Available: 36.45 GB

Metadata Space Used: 2.642 MB

Metadata Space Total: 2.147 GB

Metadata Space Available: 2.145 GB

Thin Pool Minimum Free Space: 10.74 GB

Udev Sync Supported: true

Deferred Removal Enabled: false

Deferred Deletion Enabled: false

Deferred Deleted Device Count: 0

Data loop file: /var/lib/docker/devicemapper/devicemapper/data

WARNING: Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.thinpooldev` or use `--storage-opt dm.no_warn_on_loop_devices=true` to suppress this warning.

Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata

Library Version: 1.02.107-RHEL7 (2015-12-01)

Logging Driver: json-file

Cgroup Driver: cgroupfs

Plugins:

Volume: local

Network: null bridge host overlay

Swarm: active

NodeID: 4am2qb52uw8r2ubxlkq3bxzyl

IsManager: Yes

Managers: 1

Nodes: 1

CACertHash: sha256:4306ba735e22975ecba482e122e881fc1cac28ed43443e0bf1a4397f354f2cb3

Runtimes: default

Default Runtime: default

Kernel Version: 4.6.2-1.el7.elrepo.x86_64

Operating System: CentOS Linux 7 (Core)

OSType: linux

Architecture: x86_64

CPUs: 2

Total Memory: 992 MiB

Name: centos01

ID: 5OPS:GL5A:IKVO:74U5:3T3T:EBSW:5REH:USD3:AM3S:DRU4:ZLOW:EEKK

Docker Root Dir: /var/lib/docker

Debug Mode (client): false

Debug Mode (server): false

Registry: https://index.docker.io/v1/

WARNING: bridge-nf-call-ip6tables is disabled

Insecure Registries:

127.0.0.0/8

[root@centos01 container]# docker node ls

ID NAME MEMBERSHIP STATUS AVAILABILITY MANAGER STATUS LEADER

4am2qb52uw8r2ubxlkq3bxzyl * centos01 Accepted Ready Active Reachable Yes

添加节点到集群

在节点机器上运行如下命令

docker swarm join :1

如:

docker swarm join 172.18.18.201:2377

This node joined a Swarm as a worker.

在manager节点上查看节点

[vagrant@centos01 ~]$ docker node ls

ID NAME MEMBERSHIP STATUS AVAILABILITY MANAGER STATUS LEADER

8i07v3mx5ogdz1z68iide3yrn centos03 Accepted Ready Active

cioe124z1315ucs5rwd8n2mdv * centos01 Accepted Ready Active Reachable Yes

dfsp35af3tvt1ztf6ffcehtko centos02 Accepted Ready Active

发布服务

在manager上执行如下命令:

[vagrant@centos01 ~]$ docker service create --replicas 1 --name helloworld alpine ping docker.com

6gxhvlwx8uqbio7fn2yxnw1yg

docker service create命令创建一个 service.

--name标签命名service为helloworld.

--replicas标签来详细声明1个运行实体.

参数alpine ping docker.com定义执行pingg docker.com作为alpine容器的服务.

使用docker service ls查看服务

[vagrant@centos01 ~]$ docker service ls

ID NAME SCALE IMAGE COMMAND

6gxhvlwx8uqb helloworld 1 alpine ping docker.com

使用docker service inspect审查服务

[vagrant@centos01 ~]$ docker service inspect --pretty helloworld

ID: 6gxhvlwx8uqbio7fn2yxnw1yg

Name: helloworld

Mode: REPLICATED

Replicas: 1

Placement:

Strategy: SPREAD

UpateConfig:

Parallelism: 1

ContainerSpec:

Image: alpine

Args: ping docker.com

使用docker service tasks 查看服务运行在哪个节点上

[vagrant@centos01 ~]$ docker service tasks helloworld

ID NAME SERVICE IMAGE LAST STATE DESIRED STATE NODE

30nkhczhr8g1ot6jh9couixed helloworld.1 helloworld alpine Running 10 minutes Running centos01

[vagrant@centos01 ~]$ docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

8b1e88baadfd alpine:latest "ping docker.com" 10 minutes ago Up 10 minutes helloworld.1.30nkhczhr8g1ot6jh9couixed

Scale service

docker service scale =1

如:

[vagrant@centos01 ~]$ docker service scale helloworld=5

helloworld scaled to 5

[vagrant@centos01 ~]$ docker service tasks helloworld

ID NAME SERVICE IMAGE LAST STATE DESIRED STATE NODE

30nkhczhr8g1ot6jh9couixed helloworld.1 helloworld alpine Running 13 minutes Running centos01

3g2ini5ovypy24umr85grcm6g helloworld.2 helloworld alpine Preparing 19 seconds Running centos03

4trr53ebdpzxo7wnjfet7gpl6 helloworld.3 helloworld alpine Preparing 19 seconds Running centos02

38t0tzje48uk63ros32olrfou helloworld.4 helloworld alpine Preparing 19 seconds Running centos02

exe4vkbsbaoy2l2lwylbi9g8o helloworld.5 helloworld alpine Running 19 seconds Running centos01

删除service

[vagrant@centos01 ~]$ docker service rm helloworld

helloworld

[vagrant@centos01 ~]$ docker service inspect helloworld

[]

Error: no such service: helloworld

滚动升级

$ docker service create --replicas 3 --name redis --update-delay 10s --update-parallelism 1 redis:3.0.6

0u6a4s31ybk7yw2wyvtikmu50

[root@centos01 docker]# docker service ls

ID NAME SCALE IMAGE COMMAND

3h9kbnrw5lgg redis 3 redis:3.0.6

[root@centos01 docker]# docker service tasks redis

ID NAME SERVICE IMAGE LAST STATE DESIRED STATE NODE

bstvlwynaw6shfv9rgiinyuvp redis.1 redis redis:3.0.6 Running 16 minutes Running centos03

7xnp1n5ii9667xbvr1eaefyqu redis.2 redis redis:3.0.6 Running 16 minutes Running centos02

8w3pnr57j0sb8jpieu9wkuudf redis.3 redis redis:3.0.6 Running 16 minutes Running centos01

[vagrant@centos01 ~]$ docker service update --image redis:3.0.7 redis

redis

[root@centos01 docker]# docker service ls

ID NAME SCALE IMAGE COMMAND

3h9kbnrw5lgg redis 3 redis:3.0.7

[root@centos01 docker]# docker service tasks redis

ID NAME SERVICE IMAGE LAST STATE DESIRED STATE NODE

bstvlwynaw6shfv9rgiinyuvp redis.1 redis redis:3.0.6 Running 17 minutes Running centos03

bkq2qi6j5uq6tl3jn37nurq0z redis.2 redis redis:3.0.7 Preparing 6 seconds Running centos02

8w3pnr57j0sb8jpieu9wkuudf redis.3 redis redis:3.0.6 Running 17 minutes Running centos01

[vagrant@centos01 ~]$ docker service inspect --pretty redis

ID: 3h9kbnrw5lggasae7zl8umw7m

Name: redis

Mode: REPLICATED

Replicas: 3

Placement:

Strategy: SPREAD

UpateConfig:

Parallelism: 1

Delay: 10s

ContainerSpec:

Image: redis:3.0.7

--update-parallelism标签配置服务中同步升级的任务数量.

--update-delay标签配置一个服务任务或一系列任务升级的时延.

node下线

有些时候需要维护一个节点,此时此节点可能会网络断开或者需要关机,造成节点上服务可用。使用docker node update --availability drain 将节点下线,swarm会将当前节点上的容器关闭并在其他节点上启动。当维护完成,需要上线是,将节点状态修改为active状态即可,命令如下:docker node update --availability active

上一篇:mac系统如何利用parallels desktop安装centos 7
下一篇:Docker Swarm 入门学习
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站