Docker swarm mode and dockerswarm Mode
Docker 1.12 and later versions integrate swarmkit and other orchestration services. The current docker version is 1.12-rc2. This article will try to use this mode to create a native docker Cluster
This article includes the following points:
Initialize a docker engine cluster in swarm Mode
Add node to swarm
Publish an application service to swarm
Perform swarm management after running
Set
The following requirements are required to try this example:
Three interconnected hosts:
Install docker 1.12 or later
IP address of the Management Node
Enable ports between hosts
TCP port 2377 Cluster Management Port
Communication Port Between TCP and UDP port 7946 nodes
TCP and UDP port 4789 overlay Network Communication Port
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
Create a swarm Cluster
Create a management Node
Docker swarm init -- listen-addr: 1
As follows:
[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/data
WARNING: Usage of loopback devices is stronugly 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/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: ipv6ba735e22975ecba482e122e881fc1cac28ed43443e0bf1a4127f354f2cb3
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
Add a node to a cluster
Run the following command on the node Machine
Docker swarm join: 1
For example:
Docker swarm join 172.18.18.201: 2377
This node joined a Swarm as a worker.
View nodes on the manager node
[Vagrant @ centos01 ~] $ Docker node ls
ID NAME MEMBERSHIP STATUS AVAILABILITY MANAGER STATUS LEADER
8i07v3mx5ogdz1z68iide3yrn centos03 Accepted Ready Active
Cioe124z1315ucs5rwd8n2dv * centos01 Accepted Ready Active Reachable Yes
Dfsp35af3tvt1ztf6ffcehtko centos02 Accepted Ready Active
Publish Service
Run the following command on the manager:
[Vagrant @ centos01 ~] $ Docker service create -- replicas 1 -- name helloworld alpine ping docker.com
6gxhvlwx8uqbio7fn2yxnw1yg
Docker service create command to create a service.
-- Name: the service is named helloworld.
-- Replicas label to declare one running entity in detail.
The parameter alpine ping docker.com defines the execution of pingg docker.com as the service of the alpine container.
Use docker service ls to view services
[Vagrant @ centos01 ~] $ Docker service ls
ID NAME SCALE IMAGE COMMAND
6gxhvlwx8uqb helloworld 1 alpine ping docker.com
Use docker service inspect Review service
[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
Use docker service tasks to view the node on which the service runs
[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
For example:
[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
20172ini5ovypy24umr85grcm6g 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
Exe4vkbsboy2l2lwylbi9g8o helloworld.5 helloworld alpine Running 19 seconds Running centos01
Delete service
[Vagrant @ centos01 ~] $ Docker service rm helloworld
Helloworld
[Vagrant @ centos01 ~] $ Docker service inspect helloworld
[]
Error: no such service: helloworld
Rolling upgrade
$ Docker service create -- replicas 3 -- name redis -- update-delay 10 s -- 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: 3.0.6 Running 16 minutes Running centos03
7xnp1n5ii9667xbvr1eaefyqu redis.2 redis: 3.0.6 Running 16 minutes Running centos02
8w3pnr57j0sb8jpieu9wkuudf redis.3 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: 3.0.6 Running 17 minutes Running centos03
Bkq2qi6j5uq6tl3jn37nurq0z redis.2 redis: 3.0.7 Preparing 6 seconds Running centos02
8w3pnr57j0sb8jpieu9wkuudf redis.3 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: 10 s
ContainerSpec:
Image: redis: 3.0.7
-- Update-parallelism label to configure the number of synchronization upgrade tasks in the service.
-- Update-delay label: configure the delay for upgrading a service task or a series of tasks.
Node offline
In some cases, you need to maintain a node. In this case, the node may be disconnected from the network or shut down, making the service available on the node. Use docker node update -- availability drain to deprecate the node. swarm closes the containers on the current node and starts the containers on other nodes. After the maintenance is complete, you need to go online and change the node status to active. The command is as follows: docker node update -- availability active