開始試用docker的swarm模式

開始試用docker的swarm模式

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 <MANAGER-IP>:<PORT>

如下所示:

[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 <MANAGER-IP>:<PORT>

如:

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 <SERVICE-ID>查看服務運行在哪個節點上

[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 <SERVICE-ID>=<NUMBER-OF-TASKS>

如:

[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 <NODE-ID>將節點下線,swarm會將當前節點上的容器關閉並在其他節點上啓動。當維護完成,需要上線是,將節點狀態修改爲active狀態即可,命令如下:docker node update --availability active <NODE-ID>

發佈了112 篇原創文章 · 獲贊 11 · 訪問量 45萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章