Docker Swarm基本操作(1.12) 2016-08-13 17:20

说明

Docker Swarn已经内置在Docker Engine1中,无需额外安装。

使用

一个主节点:docker1

两个从节点:docker2、docker3

先在主节点上创建一个Swarm:

# 192.168.1.21是宿主机的对外IP
[root@docker1 ~]# docker swarm init --advertise-addr 192.168.1.21
Swarm initialized: current node (6jz7e32c78fbf8cql0pukpswc) is now a manager.

To add a worker to this swarm, run the following command:
    docker swarm join \
    --token SWMTKN-1-54wbmgd9v244p3iklxjddbp47hrsnuxydpdevufiovek2m3g93-3jw0x6mq7e3s2mp6tgrqrsa6c \
    192.168.1.21:2377

To add a manager to this swarm, run the following command:
    docker swarm join \
    --token SWMTKN-1-54wbmgd9v244p3iklxjddbp47hrsnuxydpdevufiovek2m3g93-2hfbzqo9o27oe88onji718hti \
    192.168.1.21:2377
[root@docker1 ~]#

在两台从节点上执行如下命令加入此Swarm(该命令从主节点执行docker swarm init返回的报文中拷贝):

docker swarm join \
    --token SWMTKN-1-54wbmgd9v244p3iklxjddbp47hrsnuxydpdevufiovek2m3g93-3jw0x6mq7e3s2mp6tgrqrsa6c \
    192.168.1.21:2377

在主节点上执行docker info可以看到已经开启Swarn,并且有三个节点:

[root@docker1 ~]# docker info
……
Swarm: active
 NodeID: 6jz7e32c78fbf8cql0pukpswc
 Is Manager: true
 ClusterID: 15hbjjtg9prewpeor1x98tjrj
 Managers: 1
 Nodes: 3
 Orchestration:
  Task History Retention Limit: 5
 Raft:
  Snapshot interval: 10000
  Heartbeat tick: 1
  Election tick: 3
 Dispatcher:
  Heartbeat period: 5 seconds
 CA configuration:
  Expiry duration: 3 months
 Node Address: 192.168.1.21
……
[root@docker1 ~]#

在从节点上执行docker info可以看到已经加入Swarn中,并且不是manager:

[root@docker2 ~]# docker info
Swarm: active
 NodeID: 6gm55n95ue4y2hvy7l32sxrdi
 Is Manager: false
 Node Address: 192.168.1.22
[root@docker2 ~]#

在主节点,可以查看当前有哪几个节点:

[root@docker1 ~]# docker node ls
ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
6gm55n95ue4y2hvy7l32sxrdi    docker2   Ready   Active
6jz7e32c78fbf8cql0pukpswc *  docker1   Ready   Active        Leader
aimafbe40cp216ulafcwgy92g    docker3   Ready   Active
[root@docker1 ~]#

创建一个Service,两副本,执行alpine镜像,运行镜像中的ping docker.com命令。该命令会在三个节点中总共创建两个相同的容器。

docker service create --replicas 1 --name helloworld alpine ping docker.com

查看Service列表:

[root@docker1 ~]# docker service ls
ID            NAME         REPLICAS  IMAGE   COMMAND
a1091plwigqf  helloworld   2/2       alpine  ping docker.com
[root@docker1 ~]#

查看Service详情

命令:

docker service inspect [--pretty] SERVICE_NAME/SERVICE_ID

例如:

[root@docker1 ~]# docker service inspect helloworld
[
    {
        "ID": "a1091plwigqf0efd5i969dwgk",
        "Version": {
            "Index": 42
        },
        "CreatedAt": "2016-08-13T09:17:35.614352395Z",
        "UpdatedAt": "2016-08-13T09:17:35.614352395Z",
        "Spec": {
            "Name": "helloworld3",
            "TaskTemplate": {
                "ContainerSpec": {
                    "Image": "alpine",
                    "Args": [
                        "ping",
                        "docker.com"
                    ]
                },
                "Resources": {
                    "Limits": {},
                    "Reservations": {}
                },
                "RestartPolicy": {
                    "Condition": "any",
                    "MaxAttempts": 0
                },
                "Placement": {}
            },
            "Mode": {
                "Replicated": {
                    "Replicas": 2
                }
            },
            "UpdateConfig": {
                "Parallelism": 1,
                "FailureAction": "pause"
            },
            "EndpointSpec": {
                "Mode": "vip"
            }
        },
        "Endpoint": {
            "Spec": {}
        },
        "UpdateStatus": {
            "StartedAt": "0001-01-01T00:00:00Z",
            "CompletedAt": "0001-01-01T00:00:00Z"
        }
    }
]
[root@docker1 ~]# docker service inspect --pretty a1091plwigqf
ID:        a1091plwigqf0efd5i969dwgk
Name:        helloworld3
Mode:        Replicated
 Replicas:    2
Placement:
UpdateConfig:
 Parallelism:    1
 On failure:    pause
ContainerSpec:
 Image:        alpine
 Args:        ping docker.com
Resources:
[root@docker1 ~]#

可查看到已经自动创建了相应的容器:

[root@docker1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
7248c5ade490        alpine:latest       "ping docker.com"   26 minutes ago      Up 26 minutes                           helloworld3.1.6cgsfnngr9godq1x10alkzf33
add4ebbf4484        alpine:latest       "ping docker.com"   28 minutes ago      Up 28 minutes                           helloworld.1.5dk9p21aym938h7a9kkzgv319
[root@docker1 ~]#

查看此服务在哪几个节点上运行:

[root@docker1 ~]# docker service ps helloworld
ID                         NAME           IMAGE   NODE     DESIRED STATE  CURRENT STATE           ERROR
6cgsfnngr9godq1x10alkzf33  helloworld.1   alpine  docker1  Running        Running 27 minutes ago
5n81ljndiisiuqu6ri16wfu5w  helloworld.2   alpine  docker3  Running        Running 27 minutes ago
[root@docker1 ~]#

调整Service中的Replicas数目:

[root@docker1 ~]# docker service scale helloworld=4
helloworld scaled to 4
[root@docker1 ~]# docker service ps helloworld
ID                         NAME          IMAGE   NODE     DESIRED STATE  CURRENT STATE           ERROR
5dk9p21aym938h7a9kkzgv319  helloworld.1  alpine  docker1  Running        Running 31 minutes ago
9bmd7cu586lp23vt7clslrb11  helloworld.2  alpine  docker3  Running        Running 15 seconds ago
67bvrtdt327m7tazxh1hmwiqb  helloworld.3  alpine  docker2  Running        Running 14 seconds ago
f2wuggyvexz3en423mkqf4db9  helloworld.4  alpine  docker1  Running        Running 15 seconds ago
[root@docker1 ~]#
  • 删除Service
docker service rm helloworld
Tags: #Docker    Post on Docker