一. 架構
Dokcer swarm主要有兩部分組成:一個奇數的manger 節點 的a raft consensus group,和一組工做結點,工做結點之間使用的是流言網絡協議 ,也叫作控制層面。下圖對架構的闡述:node
每一個管理節點在它本地的raft存儲都有swarm狀態的全部信息,它們的通訊方式使用的是同步的一種方式。linux
工做節點之間因爲擴容的緣由,使用是異步通訊方式web
二. 建立 Docker Swarm
1. swarm的初始化
xiodi@c720131:~$ sudo docker swarm init
Swarm initialized: current node (6h6a4hf307jil12pa891siaib) is now a manager.sqlTo add a worker to this swarm, run the following command:docker
docker swarm join --token SWMTKN-1-08odkkfd4mui46wvxlehv0y5klis7z30q28mrp7pnw89y4a11y-0rvkmr0tikgulqjgl8h2g6xtw 192.168.20.131:2377json
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.ubuntu
2. 列出swarm的節點
xiodi@c720131:~$ sudo docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
6h6a4hf307jil12pa891siaib * c720131.xiodi.cn Ready Active Leader 18.05.0-ce網絡
3. 檢查節點的詳細信息
xiodi@c720131:~$ sudo docker node inspect 6h6a4hf307jil12pa891siaib
[
{
"ID": "6h6a4hf307jil12pa891siaib",
"Version": {
"Index": 9
},
"CreatedAt": "2018-06-06T07:09:41.581326206Z",
"UpdatedAt": "2018-06-06T07:09:42.09767829Z",
"Spec": {
"Labels": {},
"Role": "manager",
"Availability": "active"
},
"Description": {
"Hostname": "c720131.xiodi.cn",
"Platform": {
"Architecture": "x86_64",
"OS": "linux"
},
"Resources": {
"NanoCPUs": 2000000000,
"MemoryBytes": 4143411200
},
"Engine": {
"EngineVersion": "18.05.0-ce",
"Plugins": [
{
"Type": "Log",
"Name": "awslogs"
},
{
"Type": "Log",
"Name": "fluentd"
},
{
"Type": "Log",
"Name": "gcplogs"
},
{
"Type": "Log",
"Name": "gelf"
},
{
"Type": "Log",
"Name": "journald"
},
{
"Type": "Log",
"Name": "json-file"
},
{
"Type": "Log",
"Name": "logentries"
},
{
"Type": "Log",
"Name": "splunk"
},
{
"Type": "Log",
"Name": "syslog"
},
{
"Type": "Network",
"Name": "bridge"
},
{
"Type": "Network",
"Name": "host"
},
{
"Type": "Network",
"Name": "macvlan"
},
{
"Type": "Network",
"Name": "null"
},
{
"Type": "Network",
"Name": "overlay"
},
{
"Type": "Volume",
"Name": "local"
}
]
},
"TLSInfo": {
"TrustRoot": "-----BEGIN CERTIFICATE-----\nMIIBajCCARCgAwIBAgIUWWC4eyklvnLS5Jg/voHIkWorxCIwCgYIKoZIzj0EAwIw\nEzERMA8GA1UEAxMIc3dhcm0tY2EwHhcNMTgwNjA2MDcwNTAwWhcNMzgwNjAxMDcw\nNTAwWjATMREwDwYDVQQDEwhzd2FybS1jYTBZMBMGByqGSM49AgEGCCqGSM49AwEH\nA0IABO8UDtnIWogbHOVgdV2Gq7dqlrmGxiS9t2S53bciCJEcSZEARRdq2qEfGI3N\nDFksU70gOhMPTzYwyyqWUGlnZzCjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMB\nAf8EBTADAQH/MB0GA1UdDgQWBBQqIjWQ8OWpVx6ZKXrEyaV4OuaoQTAKBggqhkjO\nPQQDAgNIADBFAiA2OiJMd2OwuXFH4uQixic7TvCLRaemuLnkR4MXKdhJKAIhAP+Y\nf6FL0wZw/NVDRw3Z6lNDl9Wayv5lHwC6V36C9DpP\n-----END CERTIFICATE-----\n",
"CertIssuerSubject": "MBMxETAPBgNVBAMTCHN3YXJtLWNh",
"CertIssuerPublicKey": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE7xQO2chaiBsc5WB1XYart2qWuYbGJL23ZLndtyIIkRxJkQBFF2raoR8Yjc0MWSxTvSA6Ew9PNjDLKpZQaWdnMA=="
}
},
"Status": {
"State": "ready",
"Addr": "192.168.20.131"
},
"ManagerStatus": {
"Leader": true,
"Reachability": "reachable",
"Addr": "192.168.20.131:2377"
}
}
]
架構
三. 使用swarm建立單個服務
(1)建立一個stack文件
version: "3.5"
services:
whoami:
image: training/whoami:latest
networks:
- test-net
ports:
- 81:8000
deploy:
replicas: 6
update_config:
parallelism: 2
delay: 10s
labels:
app: sample-app
environment: prod-southappnetworks:
test-net:
driver: overlay
2. 進行部署stack
xiodi@c720131:~/docker$ sudo docker stack deploy -c stack.yaml sample-stack
[sudo] password for xiodi:
Creating network sample-stack_test-net
Creating service sample-stack_whoami
3. 查看部署的stack
xiodi@c720131:~/docker$ sudo docker stack ls
NAME SERVICES
sample-stack 1
4. 查看部署的服務
xiodi@c720131:~/docker$ sudo docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
t98tjpn7xpby sample-stack_whoami replicated 6/6 training/whoami:latest *:81->8000/tcp
5. 檢查服務和它的任務
xiodi@c720131:~/docker$ sudo docker service ps sample-stack_whoami
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
ib67sdpbu7xc sample-stack_whoami.1 training/whoami:latest ubuntu Running Running 3 minutes ago
3qhfu2lj4qis sample-stack_whoami.2 training/whoami:latest c720131.xiodi.cn Running Running 3 minutes ago
xdrxww29h2wi sample-stack_whoami.3 training/whoami:latest c720132.xiodi.cn Running Running 3 minutes ago
mtyc6cw65qu0 sample-stack_whoami.4 training/whoami:latest ubuntu Running Running 3 minutes ago
x8gtlzrqwm3j sample-stack_whoami.5 training/whoami:latest c720131.xiodi.cn Running Running 3 minutes ago
vfq7gksylhom sample-stack_whoami.6 training/whoami:latest c720132.xiodi.cn Running Running 3 minutes ago
6. 列出在該節點運行的容器
xiodi@c720131:~/docker$ sudo docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0cafe15b204c training/whoami:latest "/app/http" 5 minutes ago Up 5 minutes 8000/tcp sample-stack_whoami.2.3qhfu2lj4qisgk5akkskzqnop
42b4d5626c58 training/whoami:latest "/app/http" 5 minutes ago Up 5 minutes 8000/tcp sample-stack_whoami.5.x8gtlzrqwm3juduzonczhima1
stack---service---任務--和容器對應關係以下圖:
7. 獲取服務的詳細信息
xiodi@c720131:~/docker$ sudo docker service inspect sample-stack_whoami
[
{
"ID": "t98tjpn7xpbyryk7ikbtucwut",
"Version": {
"Index": 25
},
"CreatedAt": "2018-06-06T07:29:03.797319458Z",
"UpdatedAt": "2018-06-06T07:29:03.802084194Z",
"Spec": {
"Name": "sample-stack_whoami",
"Labels": {
"app": "sample-app",
"com.docker.stack.image": "training/whoami:latest",
"com.docker.stack.namespace": "sample-stack",
"environment": "prod-south"
},
"TaskTemplate": {
"ContainerSpec": {
"Image": "training/whoami:latest@sha256:4d54f5ea6d2c562ae96add2792e8d71a56b7613cbc4e7b8e20a4e4d3085bbaff",
"Labels": {
"com.docker.stack.namespace": "sample-stack"
},
"Privileges": {
"CredentialSpec": null,
"SELinuxContext": null
},
"StopGracePeriod": 10000000000,
"DNSConfig": {},
"Isolation": "default"
},
"Resources": {},
"RestartPolicy": {
"Condition": "any",
"Delay": 5000000000,
"MaxAttempts": 0
},
"Placement": {
"Platforms": [
{
"Architecture": "amd64",
"OS": "linux"
}
]
},
"Networks": [
{
"Target": "ojgp9mdhz2xaorsxca0g64zv2",
"Aliases": [
"whoami"
]
}
],
"ForceUpdate": 0,
"Runtime": "container"
},
"Mode": {
"Replicated": {
"Replicas": 6
}
},
"UpdateConfig": {
"Parallelism": 2,
"Delay": 10000000000,
"FailureAction": "pause",
"Monitor": 5000000000,
"MaxFailureRatio": 0,
"Order": "stop-first"
},
"RollbackConfig": {
"Parallelism": 1,
"FailureAction": "pause",
"Monitor": 5000000000,
"MaxFailureRatio": 0,
"Order": "stop-first"
},
"EndpointSpec": {
"Mode": "vip",
"Ports": [
{
"Protocol": "tcp",
"TargetPort": 8000,
"PublishedPort": 81,
"PublishMode": "ingress"
}
]
}
},
"Endpoint": {
"Spec": {
"Mode": "vip",
"Ports": [
{
"Protocol": "tcp",
"TargetPort": 8000,
"PublishedPort": 81,
"PublishMode": "ingress"
}
]
},
"Ports": [
{
"Protocol": "tcp",
"TargetPort": 8000,
"PublishedPort": 81,
"PublishMode": "ingress"
}
],
"VirtualIPs": [
{
"NetworkID": "1ov2xqk3ws0017wq9x1eo9sv3",
"Addr": "10.255.0.5/16"
},
{
"NetworkID": "ojgp9mdhz2xaorsxca0g64zv2",
"Addr": "10.0.0.5/24"
}
]
}
}
]
8. 獲取服務的日誌信息
xiodi@c720131:~/docker$ sudo docker service logs sample-stack_whoami
sample-stack_whoami.2.3qhfu2lj4qis@c720131.xiodi.cn | Listening on :8000
sample-stack_whoami.5.x8gtlzrqwm3j@c720131.xiodi.cn | Listening on :8000
sample-stack_whoami.3.xdrxww29h2wi@c720132.xiodi.cn | Listening on :8000
sample-stack_whoami.6.vfq7gksylhom@c720132.xiodi.cn | Listening on :8000
sample-stack_whoami.4.mtyc6cw65qu0@ubuntu | Listening on :8000
sample-stack_whoami.1.ib67sdpbu7xc@ubuntu | Listening on :8000
9. 協調至滿意狀態(在某個容器因爲某緣由不能提供服務時,swarm會自動修復這種狀態)
(1)在此刪除其中一個實列。
xiodi@c720131:~$ sudo docker container rm -f sample-stack_whoami.2.3qhfu2lj4qisgk5akkskzqnop
sample-stack_whoami.2.3qhfu2lj4qisgk5akkskzqnop
10. 從第9步能夠看到,docker-swarm是能夠自動恢復的。
11. 接下來演示一下中止掉一個worker節點,看會怎麼樣。
從上圖能夠看到,咱們中止一個worker節點後,仍然可以自動恢復。
12. 刪除服務或者stack.
xiodi@c720131:~$ sudo docker stack rm sample-stack
Removing service sample-stack_whoami
Removing network sample-stack_test-net
四. 使用Docker swarm部署多個服務stack.
1. stack文件內容以下
xiodi@c720131:~/docker$ cat pet-stack.yaml
version: "3.5"
services:
web:
image: fundamentalsofdocker/ch08-web:1.0
networks:
- pets-net
ports:
- 3000:3000
deploy:
replicas: 3
db:
image: fundamentalsofdocker/ch08-db:1.0
networks:
- pets-net
volumes:
- pets-data:/var/lib/postgresql/datavolumes:
pets-data:networks:
pets-net:
driver: overlay
2. 部署服務應用
xiodi@c720131:~/docker$ sudo docker stack deploy -c pet-stack.yaml pets
Creating network pets_pets-net
Creating service pets_db
Creating service pets_web
3. 在pets stack列出全部的任務
xiodi@c720131:~/docker$ sudo docker stack ps pets
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
99uuyw2m5fbc pets_web.1 fundamentalsofdocker/ch08-web:1.0 c720132.xiodi.cn Running Running about a minute ago
8key67a3aaai pets_db.1 fundamentalsofdocker/ch08-db:1.0 c720131.xiodi.cn Running Running about a minute ago
nkogzxahoxhq pets_web.2 fundamentalsofdocker/ch08-web:1.0 ubuntu Running Running about a minute ago
6230n44cvt04 pets_web.3 fundamentalsofdocker/ch08-web:1.0 c720131.xiodi.cn Running Running about a minute ago