1. 程式人生 > >docker version 1.12+ swarm 集群

docker version 1.12+ swarm 集群

docker swarm

初次接觸docker 是在2016年初,當時是在一家初創公司


Docker從v1.12.0後開始集成swarm mode(swarmkit),關於swarm的所有操作可以直接使用docker swarm 命令來完成。Docker Swarm是docker原生的用於管理docker集群的工具,內置kv存儲功能,不再需要外置的etcd,目前的功能主要有:

a. Docker節點集群的管理,包括集群的創建,master,worker節點的加入刪除。

b. 服務的管理,包括service的創建刪除,update,scale等。

c. 跨主機的網絡管理(overlay)

d. 服務發現(內建DNS服務,服務可以通過dns發現)

e. 服務負載均衡LB(端口暴露routing meshing, VIP, DNS-round-robin)

Docker Swarm Mode和Swarm是有區別的,雖然都是集群管理服務編排工具。


本文將使用兩個管理節點,一個工作節點

要求:

系統 system: linux (此列使用centos 7)

內核 kernel: 3.10+

docker version: 1.12+

準備

主機host: 三臺,linux系統

192.168.56.101 (manager1)

192.168.56.102 (manager2)

192.168.56.103 (worker1)


修改主機名為: manager1 manager2 worker1

1. 編輯hostname 文件修改

#vim /etc/hostname


2. hostnamectl 命令設置

這裏要修改的是靜態名

#hostnamectl set-hostname "host1" --static

重啟systemd-hostname 服務生效

#systemctl restart systemd-hostname


實施:

docker 安裝不在此做說明,請參考 http://morrowind.blog.51cto.com/1181631/1873723

初始化swarm

#192.168.56.102

#docker swarm init --advertise-addr 192.168.56.102
Swarm initialized: current node (dcpp5zxdh35g2du00q1pvhtc2) is now a manager.
To add a worker to this swarm, run the following command:
    docker swarm join     --token SWMTKN-1-4sqil7es18zvthb0s7cjdtpwe1q1fkw0m6jeikh30e70sal0sb     -dj34fdb8w9v0k0u5cege14ty0     192.168.56.102:2377
To add a manager to this swarm, run ‘docker swarm join-token manager‘ and follow the instructions.

初始化後默認輸出的信息為worker 工作節點加入方式,在工作節點執行,就可以將節點加入到集群


如下命令可以查看manager 管理節點加入方式,要在管理節點執行才有效

#docker swarm join-token manager
To add a manager to this swarm, run the following command:
    docker swarm join     --token SWMTKN-1-4sqil7es18zvthb0s7cjdtpwe1q1fkw0m6jeikh30e70sal0sb     -4gqw9f7fdylqcxwfz07jq8bfs     192.168.56.102:2377



#192.168.56.103

將103 做為管理節點加入集群

#docker swarm join --token SWMTKN-1-4sqil7es18zvthb0s7cjdtpwe1q1fkw0m6jeikh30e70sal0sb -dj34fdb8w9v0k0u5cege14ty0 192.168.56.102:2377
This node joined a swarm as a worker.


#192.168.56.104

將104 做為管理節點加入集群

#docker swarm join     --token SWMTKN-1-4sqil7es18zvthb0s7cjdtpwe1q1fkw0m6jeikh30e70sal0sb     -dj34fdb8w9v0k0u5cege14ty0     192.168.56.102:2377
This node joined a swarm as a worker.


在管理節點執行節點查看命令,可看到個節點的狀態

#docker node ls
ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
dcpp5zxdh35g2du00q1pvhtc2    manager1  Ready   Active        Leader
sxkcz3xhjyuks0gwtb22h6q0t    worker1   Ready   Active        
w8d2dbp7l3j6df6r0v3feuscq *  mangger2  Ready   Active        Reachable


三臺主機的集群,到這就算創建完了,後續是網絡和管理節點發出指令


網絡:

swarm 已經內置網絡解決方案,可實現不同主機間的容器互相通信。Docker Swarm 模式提供一套默認覆蓋網絡,其負責配合 libnetwork 與 libkv 實現一套基於 VxLAN 的解決方案。當然,大家也可以選擇 Flannel、Calico 或者 Weave 等其它覆蓋網絡驅動方案


查看下目前都有哪些網絡驅動

#docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
3bea9b80b33e        bridge              bridge              local
00c0561f8ad2        docker_gwbridge     bridge              local
efac93cdb8c2        host                host                local
ye6qsmmt2spd        ingress             overlay             swarm
f77b4c6271fc        none                null                local


bridge:

容器使用獨立網絡Namespace,並連接到docker0虛擬網卡(默認模式)

當 Docker server 啟動時,會在主機上創建一個名為 docker0 的虛擬網橋,此主機上啟動的 Docker 容器會連接到這個虛擬網橋上


host:

容器與主機共享網絡Namespace,擁有與主機相同的網絡設備


overlay:

Overlay網絡是指在不改變現有網絡基礎設施的前提下,通過某種約定通信協議,把二層報文封裝在IP報文之上的新的數據格式。這樣不但能夠充分利用成熟的IP路由協議進程數據分發,而且在Overlay技術中采用擴展的隔離標識位數,能夠突破VLAN的4000數量限制,支持高達16M的用戶,並在必要時可將廣播流量轉化為組播流量,避免廣播數據泛濫。因此,Overlay網絡實際上是目前最主流的容器跨節點數據傳輸和路由方案。


說到Overlay網絡,許多人的第一反應便是:低效,這種認識其實是帶有偏見的。Overlay網絡的實現方式可以有許多種,其中IETF(國際互聯網工程任務組)制定了三種Overlay的實現標準,分別是:虛擬可擴展LAN(VXLAN)、采用通用路由封裝的網絡虛擬化(NVGRE)和無狀態傳輸協議(SST),其中以VXLAN的支持廠商最為雄厚,可以說是Overlay網絡的事實標準。

而在這三種標準以外還有許多不成標準的Overlay通信協議,例如Weave、Flannel、Calico等工具都包含了一套自定義的Overlay網絡協議(Flannel也支持VXLAN模式),這些自定義的網絡協議的通信效率遠遠低於IETF的標準協議[5],但由於他們使用起來十分方便,一直被廣泛的采用而造成了大家普遍認為Overlay網絡效率低下的印象。然而,根據網上的一些測試數據來看,采用VXLAN的網絡的傳輸速率與二層VLAN網絡是基本相當的。


null:

此模式默認容器只有回環地址,容器沒有任何網卡,適合不需要與外部通過網絡通信的容器。


創建好swarm 集群後,overlay 驅動網絡已經默認創建,但這個默認的overlay 網絡是用於swarm 管理節點做為負載均衡路由用的,容器並不能使用此默認網絡。

因此需要創建一個容器使用的overlay 網絡

#docker network create -drvice=overlay over0


部署服務:

#docker service create --name nginx --network over0 --replicas 1 --mount type=bind,source=/tmp/html/,destination=/usr/share/nginx/html --mount type=bind,source=/tmp/test/,destination=/usr/share/nginx/html/test --publish 80:80 nginx:1.10-alpine

參數:

--name 容器名稱

--network 使用的網絡名稱

--replicas 部署容器個數

--mount 掛載


OptionRequiredDescription
types

The type of mount, can be either volume, bind, or tmpfs. Defaults to volume if no type is specified.

  • volume: mounts a [managed volume](volume_create.md) into the container.

  • bind: bind-mounts a directory or file from the host into the container.

  • tmpfs: mount a tmpfs in the container

src or sourcefor type=bind only>
  • type=volume: src is an optional way to specify the name of the volume (for example, src=my-volume). If the named volume does not exist, it is automatically created. If no src is specified, the volume is assigned a random name which is guaranteed to be unique on the host, but may not be unique cluster-wide. A randomly-named volume has the same lifecycle as its container and is destroyed when the container is destroyed (which is upon service update, or when scaling or re-balancing the service)

  • type=bind: src is required, and specifies an absolute path to the file or directory to bind-mount (for example, src=/path/on/host/). An error is produced if the file or directory does not exist.

  • type=tmpfs: src is not supported.

dst or destination or target

yes

Mount path inside the container, for example /some/path/in/container/. If the path does not exist in the container‘s filesystem, the Engine creates a directory at the specified location before mounting the volume or bind-mount.

readonly or ro


The Engine mounts binds and volumes read-write unless readonly option is given when mounting the bind or volume.

  • true or 1 or no value: Mounts the bind or volume read-only.

  • false or 0: Mounts the bind or volume read-write.


--publish 端口發布,發布端口由管理節點路由到可用容器,任意主機ip:port都可以訪問,而不用管容器具體在哪臺機器上

縮放集群中服務

通過docker service scale 命令來擴展

#docker service scale nginx=3
nginx scaled to 3


#docker service ls
ID            NAME    MODE        REPLICAS  IMAGE
hyaxe83wj1s7  nginx   replicated  3/3       nginx:1.10-alpine


刪除服務:

#docker service rm nginx
nginx


滾動更新

更新容器鏡像

docker service update --image nginx:1.11-alpine nginx

查看容器服務

docker service inspect --pretty nginx
ID:        hyaxe83wj1s7qh3b4ywnw0pfi
Name:        nginx
Service Mode:    Replicated
 Replicas:    3
UpdateStatus:
 State:        updating
 Started:    15 seconds
 Message:    update in progress
Placement:
UpdateConfig:
 Parallelism:    1
 On failure:    pause
 Max failure ratio: 0
ContainerSpec:
 Image:        nginx:[email protected]:5aadb68304a38a8e2719605e4e180413f390cd6647602bee9bdedd59753c3590
Mounts:
  Target = /usr/share/nginx/html
   Source = /tmp/html/
   ReadOnly = false
   Type = bind
Resources:
Networks: over0 
Endpoint Mode:    vip
Ports:
 PublishedPort 80
  Protocol = tcp
  TargetPort = 80


state狀態為updating


更新延遲,可在更新時指定時間,亦可在啟動容器時加入延遲參數--update-delay

啟動時指定的時間可被更新時指定的時間覆蓋

docker service update --update-delay 10s --image nginx:1.11-alpine nginx
docker service create --replicas 3 --name nginx --update-delay 10s nginx:1.10-alpine



swarm 常用命令

docker swarm init

docker node ls

docker node rm

docker swarm leave

docker swarm join


docker service create

docker serivce ls

docker service ps

docker service update

docker service rm

docker service inspect

docker service scale


docker network ls

docker network create

docker network rm

docker ps

docker images

docker rmi

docker rm



參考

mount詳情參考官方資料

https://docs.docker.com/engine/reference/commandline/service_create/#add-bind-mounts-or-volumes


遇到的問題

部署時查看部署狀態有error

No such image: [email protected]:0…

其中有兩臺機器是我有意將外網網絡關閉了,因為在多節點部署的時候,我發現部署時間有點長,各節點已經先通過docker load < nginx.tar 導入到了主機,本不應該部署時間長的,於是想到是不是又從網絡去下載鏡像了,而沒有使用我導入的鏡像,為了驗證此猜想,隧關閉了幾個節點的網絡訪問,然後就出現了此類報錯。

通過查資料發現,導入的image缺少sha信息,需要重新將sha信息pull下來

docker images  --digests
REPOSITORY   TAG          DIGEST                         IMAGE ID       
nginx        1.10-alpine  sha256:4aacdcf186934dcb02f642579314075910f1
                          855590fd3039d8fa4c9f96e48315   f94d6dd9b576     
CREATED             SIZE
3 months ago        54 MB

因此,只要在各個節點pull一遍image就可以解決了


初始化報錯

Error response from daemon: --live-restore daemon configuration is incompatible with swarm mode


修改/etc/docker/daemon.json文件,true改為false

{
    "live-restore": false
}




本文出自 “morrowind” 博客,請務必保留此出處http://morrowind.blog.51cto.com/1181631/1948405

docker version 1.12+ swarm 集群