docker swarm 使用示例
节点 1:192.168.57.120
节点 2:192.168.57.127
**********************
集群操作
创建集群:docker swarm init
[root@centos ~]# docker swarm init
Swarm initialized: current node (sv2g7ljrt9leiw0caet9d9wj5) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-2k0pyekm18le5xn3s4nm8wee115r89eibropgdj55idb5vtjx7-6s2bwk6xgcoguravw3efdxwv4 192.168.57.127:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
添加worker node,在对应节点执行命令 :docker swarm join --token ...
添加manager node,当前节点执行命令:docker swarm join-token manager,再执行后续操作
******************
添加worker node
192.168.57.120执行如下命令
[root@centos ~]# docker swarm join --token SWMTKN-1-2k0pyekm18le5xn3s4nm8wee115r89eibropgdj55idb5vtjx7-6s2bwk6xgcoguravw3efdxwv4 192.168.57.127:2377
This node joined a swarm as a worker.
节点作为worker node加入集群
查看节点:docker node ls
[root@centos ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
51kgawqwx5hp3ytl55m9g8mcf centos Ready Active 19.03.6-rc2
sv2g7ljrt9leiw0caet9d9wj5 * centos Ready Active Leader 19.03.13-beta2
[
说明:查看节点需要在manager node上执行
manager node查看集群信息:docker info
[root@centos ~]# docker info
Client:
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 40
Server Version: 19.03.13-beta2
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: active
NodeID: sv2g7ljrt9leiw0caet9d9wj5
Is Manager: true
ClusterID: 4ehmddg5fn49wjz9zi88fi3pe
Managers: 1
Nodes: 2
Default Address Pool: 10.0.0.0/8
SubnetSize: 24
Data Path Port: 4789
Orchestration:
Task History Retention Limit: 5
Raft:
Snapshot Interval: 10000
Number of Old Snapshots to Retain: 0
Heartbeat Tick: 1
Election Tick: 10
Dispatcher:
Heartbeat Period: 5 seconds
CA Configuration:
Expiry Duration: 3 months
Force Rotate: 0
Autolock Managers: false
Root Rotation In Progress: false
Node Address: 192.168.57.127
Manager Addresses:
192.168.57.127:2377
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-1127.18.2.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.777GiB
Name: centos
ID: OW47:PHLP:5MHC:TKKD:REMM:YJJL:5OWX:GJI4:HQPJ:QEY4:MK6Z:IBZD
Docker Root Dir: /var/lib/docker
Debug Mode: false
Username: lihu12344
Registry: https://index.docker.io/v1/
Labels:
provider=generic
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://rbp1yphj.mirror.aliyuncs.com/
Live Restore Enabled: false
managers:1、nodes:2 ==> 集群中1个manager node,共2个节点
node address:192.168.57.127 ==> 当前节点ip地址
manager addresses:192.168.57.120:2377 ==> manager 节点 ip地址
worker node 查看集群信息:docker info
[root@centos ~]# docker info
Client:
Debug Mode: false
Server:
Containers: 4
Running: 0
Paused: 0
Stopped: 4
Images: 39
Server Version: 19.03.6-rc2
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: active
NodeID: 51kgawqwx5hp3ytl55m9g8mcf
Is Manager: false
Node Address: 192.168.57.120
Manager Addresses:
192.168.57.127:2377
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339
runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-957.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.777GiB
Name: centos
ID: GJSG:3CFX:XLTR:YOJU:ZOOL:QBES:YWSR:JRFH:TWXR:W5CM:Y2Y5:H2B5
Docker Root Dir: /var/lib/docker
Debug Mode: false
Username: lihu12344
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://rbp1yphj.mirror.aliyuncs.com/
Live Restore Enabled: false
is manager:false ==> 当前节点不为manager
node address: 192.168.57.120 ==> 当前节点ip
manager addresses: 192.168.57.127:2377 ==> manager 节点 ip地址
******************
添加 manager node
192.168.57.120 退出集群:docker swarm leave -f
manager node 192.168.57.127 执行命令:docker swarm join-token manager
[root@centos ~]# docker swarm join-token manager
To add a manager to this swarm, run the following command:
docker swarm join --token SWMTKN-1-2k0pyekm18le5xn3s4nm8wee115r89eibropgdj55idb5vtjx7-0hfio4cb9wqk95nipipq5g7ii 192.168.57.127:2377
192.168.57.120 作为manager node加入
[root@centos ~]# docker swarm join --token SWMTKN-1-2k0pyekm18le5xn3s4nm8wee115r89eibropgdj55idb5vtjx7-0hfio4cb9wqk95nipipq5g7ii 192.168.57.127:2377
This node joined a swarm as a manager.
查看节点:docker node ls
[root@centos ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
51kgawqwx5hp3ytl55m9g8mcf centos Down Active 19.03.6-rc2
ewa94hkxarjjkuydx2jq2y3bm * centos Ready Active Reachable 19.03.6-rc2
sv2g7ljrt9leiw0caet9d9wj5 centos Ready Active Leader 19.03.13-beta2
说明:status 为down的节点为192.168.57.120退出集群导致
查看集群信息:docker info
[root@centos ~]# docker info
Client:
Debug Mode: false
Server:
Containers: 4
Running: 0
Paused: 0
Stopped: 4
Images: 39
Server Version: 19.03.6-rc2
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: active
NodeID: ewa94hkxarjjkuydx2jq2y3bm
Is Manager: true
ClusterID: 4ehmddg5fn49wjz9zi88fi3pe
Managers: 2
Nodes: 3
Default Address Pool: 10.0.0.0/8
SubnetSize: 24
Data Path Port: 4789
Orchestration:
Task History Retention Limit: 5
Raft:
Snapshot Interval: 10000
Number of Old Snapshots to Retain: 0
Heartbeat Tick: 1
Election Tick: 10
Dispatcher:
Heartbeat Period: 5 seconds
CA Configuration:
Expiry Duration: 3 months
Force Rotate: 0
Autolock Managers: false
Root Rotation In Progress: false
Node Address: 192.168.57.120
Manager Addresses:
192.168.57.120:2377
192.168.57.127:2377
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339
runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-957.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.777GiB
Name: centos
ID: GJSG:3CFX:XLTR:YOJU:ZOOL:QBES:YWSR:JRFH:TWXR:W5CM:Y2Y5:H2B5
Docker Root Dir: /var/lib/docker
Debug Mode: false
Username: lihu12344
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://rbp1yphj.mirror.aliyuncs.com/
Live Restore Enabled: false
WARNING: Running Swarm in a two-manager configuration. This configuration provides
no fault tolerance, and poses a high risk to lose control over the cluster.
Refer to https://docs.docker.com/engine/swarm/admin_guide/ to configure the
Swarm for fault-tolerance.
managers:2、nodes:3 ==> 管理节点有2个,共有3个节点
is manager:true ==> 当前节点为manager 节点
node address: 192.168.57.120 ==> 当前节点的ip地址为 192.168.57.120
manager addresses:192.168.57.120:2377、192.168.57.127:2377 ==> 管理节点的ip地址
**********************
服务操作
创建服务
[root@centos ~]# docker service create -p 6379:6379 --replicas 1 --name redis redis
rvodfdouqjqrl9dnzcgpeiw4v
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
说明:创建服务操作需要在manager node上执行
查看服务列表:docker service ls
[root@centos ~]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
rvodfdouqjqr redis replicated 1/1 redis:latest *:6379->6379/tcp
查看服务运行信息:docker service ps redis
[root@centos ~]# docker service ps redis
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
ma4ifebdonse redis.1 redis:latest centos Running Running 3 minutes ago
查看服务日志:docker service logs redis
[root@centos ~]# docker service logs redis
redis.1.ma4ifebdonse@centos | 1:C 22 Aug 2020 02:41:19.210 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis.1.ma4ifebdonse@centos | 1:C 22 Aug 2020 02:41:19.210 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=1, just started
redis.1.ma4ifebdonse@centos | 1:C 22 Aug 2020 02:41:19.210 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis.1.ma4ifebdonse@centos | 1:M 22 Aug 2020 02:41:19.214 * Running mode=standalone, port=6379.
redis.1.ma4ifebdonse@centos | 1:M 22 Aug 2020 02:41:19.215 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis.1.ma4ifebdonse@centos | 1:M 22 Aug 2020 02:41:19.215 # Server initialized
redis.1.ma4ifebdonse@centos | 1:M 22 Aug 2020 02:41:19.215 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis.1.ma4ifebdonse@centos | 1:M 22 Aug 2020 02:41:19.215 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis.1.ma4ifebdonse@centos | 1:M 22 Aug 2020 02:41:19.216 * Ready to accept connections
更新服务,副本数变为2:docker service update --replicas 2 redis
[root@centos ~]# docker service update --replicas 2 redis
redis
overall progress: 2 out of 2 tasks
1/2: running [==================================================>]
2/2: running [==================================================>]
verify: Service converged
查看服务:docker service ps redis
[root@centos ~]# docker service ps redis
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
ma4ifebdonse redis.1 redis:latest centos Running Running 11 minutes ago
d7nkflpyw8ds redis.2 redis:latest centos Running Running 23 seconds ago
副本数变为2
查看服务详细信息:docker service inspect redis
[root@centos ~]# docker service inspect redis
[
{
"ID": "rvodfdouqjqrl9dnzcgpeiw4v",
"Version": {
"Index": 24
},
"CreatedAt": "2020-08-22T02:41:13.095574991Z",
"UpdatedAt": "2020-08-22T02:52:06.00798984Z",
"Spec": {
"Name": "redis",
"Labels": {},
"TaskTemplate": {
"ContainerSpec": {
"Image": "redis:latest@sha256:09c33840ec47815dc0351f1eca3befe741d7105b3e95bc8fdb9a7e4985b9e1e5",
"Init": false,
"StopGracePeriod": 10000000000,
"DNSConfig": {},
"Isolation": "default"
},
"Resources": {
"Limits": {},
"Reservations": {}
},
"RestartPolicy": {
"Condition": "any",
"Delay": 5000000000,
"MaxAttempts": 0
},
"Placement": {
"Platforms": [
{
"Architecture": "amd64",
"OS": "linux"
},
{
"OS": "linux"
},
{
"OS": "linux"
},
{
"Architecture": "arm64",
"OS": "linux"
},
{
"Architecture": "386",
"OS": "linux"
},
{
"Architecture": "mips64le",
"OS": "linux"
},
{
"Architecture": "ppc64le",
"OS": "linux"
},
{
"Architecture": "s390x",
"OS": "linux"
}
]
},
"ForceUpdate": 0,
"Runtime": "container"
},
"Mode": {
"Replicated": {
"Replicas": 2
}
},
"UpdateConfig": {
"Parallelism": 1,
"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": 6379,
"PublishedPort": 6379,
"PublishMode": "ingress"
}
]
}
},
"PreviousSpec": {
"Name": "redis",
"Labels": {},
"TaskTemplate": {
"ContainerSpec": {
"Image": "redis:latest@sha256:09c33840ec47815dc0351f1eca3befe741d7105b3e95bc8fdb9a7e4985b9e1e5",
"Init": false,
"DNSConfig": {},
"Isolation": "default"
},
"Resources": {
"Limits": {},
"Reservations": {}
},
"Placement": {
"Platforms": [
{
"Architecture": "amd64",
"OS": "linux"
},
{
"OS": "linux"
},
{
"OS": "linux"
},
{
"Architecture": "arm64",
"OS": "linux"
},
{
"Architecture": "386",
"OS": "linux"
},
{
"Architecture": "mips64le",
"OS": "linux"
},
{
"Architecture": "ppc64le",
"OS": "linux"
},
{
"Architecture": "s390x",
"OS": "linux"
}
]
},
"ForceUpdate": 0,
"Runtime": "container"
},
"Mode": {
"Replicated": {
"Replicas": 1
}
},
"EndpointSpec": {
"Mode": "vip",
"Ports": [
{
"Protocol": "tcp",
"TargetPort": 6379,
"PublishedPort": 6379,
"PublishMode": "ingress"
}
]
}
},
"Endpoint": {
"Spec": {
"Mode": "vip",
"Ports": [
{
"Protocol": "tcp",
"TargetPort": 6379,
"PublishedPort": 6379,
"PublishMode": "ingress"
}
]
},
"Ports": [
{
"Protocol": "tcp",
"TargetPort": 6379,
"PublishedPort": 6379,
"PublishMode": "ingress"
}
],
"VirtualIPs": [
{
"NetworkID": "0yjl71w64xlkfw5ititsuao1z",
"Addr": "10.0.0.4/24"
}
]
}
}
]
服务回滚:docker service rollback redis
[root@centos ~]# docker service rollback redis
redis
service rolled back: rollback completed
查看服务信息:docker service ps redis
[root@centos ~]# docker service ps redis
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
ma4ifebdonse redis.1 redis:latest centos Running Running 16 minutes ago
副本数回滚为1