在Docker1.12版本中,一个大的功能点是swarm集群(基于swarmkit项目),通过docker命令可以直接实现docker-engine相互发现,并组建成为一个容器集群。有关集群的docker命令如下:

  • (1)docker swarm:集群管理,子命令有init, join, leave, update
  • (2)docker service:服务创建,子命令有create, inspect, update, remove, tasks
  • (3)docker node:节点管理,子命令有accept, promote, demote, inspect, update, tasks, ls, rm
  • (4)docker stack/deploy:试验特性,用于多应用部署 下面在一个三节点环境中,针对各个命令进行试用,详细展开说明

1、swarm

swarm命令用于集群管理,子命令如下:

[root@host1 ~]# docker swarm --help
 
Usage:  dockerswarmCOMMAND
 
ManageDockerSwarm
 
Options:
      --help  Printusage
 
Commands:
  init        Initialize a Swarm.
  join        Join a Swarmas a nodeand/or manager.
  update      updatetheSwarm.
  leave      Leave a Swarm.
  inspect    InspecttheSwarm
 
Run 'docker swarm COMMAND --help' for moreinformationon a command.
 

可以看到,提供了init、join、update、leave、inspect命令

(1)swarm init命令用于初始化一个集群
[root@host1 ~]# docker swarm init --help
 
Usage:  dockerswarminit [OPTIONS]
 
Initialize a Swarm.
 
Options:
      --auto-acceptvalue  Autoacceptancepolicy (worker, manager, or none)
      --force-new-cluster  Forcecreate a new clusterfromcurrentstate.
      --help                Printusage
      --listen-addrvalue  Listenaddress (default 0.0.0.0:2377)
      --secretstring      Setsecretvalueneededto acceptnodesintocluster
 

在第一个节点上运行init命令初始化一个集群,初始化完成后会监听2377端口,接受其他节点的加入请求

[root@host1 ~]# docker swarm init
Swarminitialized: currentnode (6mjmescd8473lh6jpvmx2khkq) is now a manager.
 
(2)swarm join命令用于加入一个现有集群
[root@host1 ~]# docker swarm join --help
 
Usage:  dockerswarmjoin [OPTIONS] HOST:PORT
 
Join a Swarmas a nodeand/or manager.
 
Options:
      --ca-hashstring      HashoftheRootCertificateAuthoritycertificateusedfor trustedjoin
      --help                Printusage
      --listen-addrvalue  Listenaddress (default 0.0.0.0:2377)
      --manager            Try joiningas a manager.
      --secretstring      Secretfor nodeacceptance
 

在第二个、第三个节点上通过join命令加入集群

[root@host2 ~]# docker swarm join 186.100.40.90:2377
This nodejoined a Swarmas a worker.
[root@host3 ~]# docker swarm join 186.100.40.90:2377
This nodejoined a Swarmas a worker.
 

在第一个节点上,通过docker node ls查看节点信息

[root@host1 ~]# docker node ls
ID                          NAME  MEMBERSHIP  STATUS  AVAILABILITY  MANAGERSTATUS  LEADER
3xto6bh4htx7thj7b222datfd    host2  Accepted    Ready  Active                        
6mjmescd8473lh6jpvmx2khkq *  host1  Accepted    Ready  Active        Reachable      Yes
bwkk0n044fuod36dvghi9jbkv    host3  Accepted    Ready  Active
 

可以看到当前集群中有3个节点,Manager节点为host1,由于当前只有一个manager,所以这一节点也为是leader(swarmkit采用raft协议构建集群)

(3)swarm leave命令由于离开集群
(4)swarm inspect命令用于查看集群详细信息
[root@host1 ~]# docker swarm inspect
[
    {
        "ID": "90wkw2fmy0x5fjlh0mtuhtkrd",
        "Version": {
            "Index": 11
        },
        "CreatedAt": "2016-06-19T12:47:40.260925387Z",
        "UpdatedAt": "2016-06-19T12:47:40.665401298Z",
        "Spec": {
            "Name": "default",
            "AcceptancePolicy": {
                "Policies": [
                    {
                        "Role": "worker",
                        "Autoaccept": true
                    },
                    {
                        "Role": "manager",
                        "Autoaccept": false
                    }
                ]
            },
            "Orchestration": {
                "TaskHistoryRetentionLimit": 10
            },
            "Raft": {
                "SnapshotInterval": 10000,
                "LogEntriesForSlowFollowers": 500,
                "HeartbeatTick": 1,
                "ElectionTick": 3
            },
            "Dispatcher": {
                "HeartbeatPeriod": 5000000000
            },
            "CAConfig": {
                "NodeCertExpiry": 7776000000000000
            }
        }
    }
]
 

2、service

通过service命令可以管理一个服务,子命令如下:

[root@host1 ~]# docker service --help
 
Usage:  dockerserviceCOMMAND
 
ManageDockerservices
 
Options:
      --help  Printusage
 
Commands:
  create      Create a new service
  inspect    Inspect a service
  tasks      Listthetasksof a service
  ls          Listservices
  rm          Remove a service
  scale      Scaleoneor multipleservices
  update      Update a service
 
Run 'docker service COMMAND --help' for moreinformationon a command.
 

(1)service create命令用于创建一个服务[root@host1 ~]# docker service create –help

Usage:  dockerservicecreate [OPTIONS] IMAGE [COMMAND] [ARG...]
 
Create a new service
 
Options:
      --constraintvalue            Placementconstraints (default [])
      --endpoint-modestring        Endpointmode(Validvalues: VIP, DNSRR)
  -e, --envvalue                    Setenvironmentvariables (default [])
      --help                        Printusage
  -l, --labelvalue                  Servicelabels (default [])
      --limit-cpuvalue              LimitCPUs
      --limit-memoryvalue          LimitMemory
      --modestring                  Servicemode (replicatedor global) (default "replicated")
  -m, --mountvalue                  Attach a mountto theservice
      --namestring                  Servicename
      --networkvalue                Networkattachments (default [])
  -p, --publishvalue                Publish a portas a nodeport (default [])
      --replicasvalue              Numberoftasks (default none)
      --reserve-cpuvalue            ReserveCPUs
      --reserve-memoryvalue        ReserveMemory
      --restart-conditionstring    Restartwhenconditionis met (none, on_failure, or any)
      --restart-delayvalue          Delaybetweenrestartattempts (default none)
      --restart-max-attemptsvalue  Maximumnumberofrestartsbeforegivingup (default none)
      --restart-windowvalue        Windowusedto evalulatetherestartpolicy (default none)
      --stop-grace-periodvalue      Timeto waitbeforeforcekilling a container (default none)
      --update-delayduration        Delaybetweenupdates
      --update-parallelismuint      Maximumnumberoftasksupdatedsimultaneously (default 1)
  -u, --userstring                  Usernameor UID
  -w, --workdirstring              Workingdirectoryinsidethecontainer
 

在参数中有两个比较重要的参数: * replicas用于描述服务对应的实例数 * mode用来描述服务的类型,replicated多实例类型,global全局服务类型(在每一个节点上都会创建)

在第一个节点上,通过create命令创建服务:

[root@host1 ~]# docker service create --name test_service --replicas 2 busybox ping 186.100.40.1
et1i181ni4dj2ocm1l4frbqzm
 
(2)service ls命令用户查看service列表
[root@host1 ~]# docker service ls
ID            NAME          SCALE  IMAGE    COMMAND
et1i181ni4dj  test_service  2      busybox  ping 186.100.40.1
 
(3)service tasks可以查看service对应的任务
[root@host1 ~]# docker service tasks test_service
ID                        NAME            SERVICE      IMAGE    LASTSTATE              DESIREDSTATE  NODE
1yewid0xbviyggyvjv10adjrs  test_service.1  test_service  busybox  RunningAbout a minute  Running        host3
9l4z9aqlg9iwums14yxu0lym7  test_service.2  test_service  busybox  RunningAbout a minute  Running        host1
 

可以看到,刚才创建的service对应的2个实例分别在host3和host1上

(4)service update可以对服务的参数进行更新

对刚才创建的service实力数目进行更新

[root@host1 ~]# docker service update --replicas 3 test_service
test_service
[root@host1 ~]# docker service tasks test_service
ID                        NAME            SERVICE      IMAGE    LASTSTATE          DESIREDSTATE  NODE
1yewid0xbviyggyvjv10adjrs  test_service.1  test_service  busybox  Running 5 minutes  Running        host3
9l4z9aqlg9iwums14yxu0lym7  test_service.2  test_service  busybox  Running 5 minutes  Running        host1
emnuxg8sb37hbxbpdt63mpy1a  test_service.3  test_service  busybox  Running 18 seconds  Running        host2
 
(5)service inspect命令用户查看service详细信息
[root@host1 ~]# docker service inspect test_service
[
    {
        "ID": "et1i181ni4dj2ocm1l4frbqzm",
        "Version": {
            "Index": 33
        },
        "CreatedAt": "2016-06-19T13:07:29.549242777Z",
        "UpdatedAt": "2016-06-19T13:12:23.210552545Z",
        "Spec": {
            "Name": "test_service",
            "TaskTemplate": {
                "ContainerSpec": {
                    "Image": "busybox",
                    "Args": [
                        "ping",
                        "186.100.40.1"
                    ]
                },
                "Resources": {
                    "Limits": {},
                    "Reservations": {}
                },
                "RestartPolicy": {
                    "Condition": "any",
                    "MaxAttempts": 0
                },
                "Placement": {}
            },
            "Mode": {
                "Replicated": {
                    "Replicas": 3
                }
            },
            "UpdateConfig": {
                "Parallelism": 1
            },
            "EndpointSpec": {
                "Mode": "vip"
            }
        },
        "Endpoint": {
            "Spec": {}
        }
    }
]
 

3、node

node命令用户节点管理,比如将一个节点变为manager节点、同意节点加入请求等。

[root@host1 ~]# docker node --help
 
Usage:  dockernodeCOMMAND
 
ManageDockerSwarmnodes
 
Options:
      --help  Printusage
 
Commands:
  accept      Accept a nodein theswarm
  demote      Demote a nodefrommanagerin theswarm
  inspect    Inspect a nodein theswarm
  ls          Listnodesin theswarm
  promote    Promote a nodeto a managerin theswarm
  rm          Remove a nodefromtheswarm
  tasks      Listtasksrunningon a node
  update      Update a node
 
Run 'docker node COMMAND --help' for moreinformationon a command.
 
(1)node accept

accpet命令用户同意节点加入请求,在初始化swarm集群时候通过–auto-accept可以指定节点默认加入的较色,如果为none的话,需要通过accpet命令同意加入节点,成为worker节点 重新初始化三节点,集群初始化时,采用–auto-accept=none,最终可以看到新加入的两个节点MEMBERSHIP为Pending

[root@host1 ~]# docker node ls
ID                          NAME  MEMBERSHIP  STATUS  AVAILABILITY  MANAGERSTATUS  LEADER
15wyq8vc1vxe7r4n3yyl0p93j *  host1  Accepted    Ready    Active        Reachable      Yes
2fh91rbwtj2xosqixzlc7s3mg          Pending    Unknown  Active                        
5okhpif49b4d6byohpdr4vdmw          Pending    Unknown  Active 
 

通过accept命令同意节点加入:

[root@host1 ~]# docker node accept 2fh91rbwtj2xosqixzlc7s3mg
2fh91rbwtj2xosqixzlc7s3mg
[root@host1 ~]# docker node ls
ID                          NAME  MEMBERSHIP  STATUS  AVAILABILITY  MANAGERSTATUS  LEADER
15wyq8vc1vxe7r4n3yyl0p93j *  host1  Accepted    Ready    Active        Reachable      Yes
2fh91rbwtj2xosqixzlc7s3mg    host3  Accepted    Ready    Active                        
5okhpif49b4d6byohpdr4vdmw          Pending    Unknown  Active  
[root@host1 ~]# docker node accept 5okhpif49b4d6byohpdr4vdmw
5okhpif49b4d6byohpdr4vdmw
5okhpif49b4d6byohpdr4vdmw attemptingto accept a nodein theswarm.
[root@host1 ~]# docker node ls
ID                          NAME  MEMBERSHIP  STATUS  AVAILABILITY  MANAGERSTATUS  LEADER
15wyq8vc1vxe7r4n3yyl0p93j *  host1  Accepted    Ready  Active        Reachable      Yes
2fh91rbwtj2xosqixzlc7s3mg    host3  Accepted    Ready  Active                        
5okhpif49b4d6byohpdr4vdmw    host2  Accepted    Ready  Active  
 
(2)node promote命令用于将worker节点提升为manager节点,用户manager的HA
[root@host1 ~]# docker node promote 2fh91rbwtj2xosqixzlc7s3mg
2fh91rbwtj2xosqixzlc7s3mg
2fh91rbwtj2xosqixzlc7s3mg attemptingto promote a nodeto a managerin theswarm.
[root@host1 ~]# docker node ls
ID                          NAME  MEMBERSHIP  STATUS  AVAILABILITY  MANAGERSTATUS  LEADER
15wyq8vc1vxe7r4n3yyl0p93j *  host1  Accepted    Ready  Active        Reachable      Yes
2fh91rbwtj2xosqixzlc7s3mg    host3  Accepted    Ready  Active        Reachable      
5okhpif49b4d6byohpdr4vdmw    host2  Accepted    Ready  Active  
[root@host1 ~]# docker node promote 5okhpif49b4d6byohpdr4vdmw
5okhpif49b4d6byohpdr4vdmw
5okhpif49b4d6byohpdr4vdmw attemptingto promote a nodeto a managerin theswarm.
[root@host1 ~]# docker node ls
ID                          NAME  MEMBERSHIP  STATUS  AVAILABILITY  MANAGERSTATUS  LEADER
15wyq8vc1vxe7r4n3yyl0p93j *  host1  Accepted    Ready  Active        Reachable      Yes
2fh91rbwtj2xosqixzlc7s3mg    host3  Accepted    Ready  Active        Reachable      
5okhpif49b4d6byohpdr4vdmw    host2  Accepted    Ready  Active        Reachable 
 
(3)node demote命令用户将manger节点变为worker节点
[root@host1 ~]# docker node demote 15wyq8vc1vxe7r4n3yyl0p93j
15wyq8vc1vxe7r4n3yyl0p93j
15wyq8vc1vxe7r4n3yyl0p93j attemptingto demote a managerin theswarm.
[root@host1 ~]# docker node ls
Errorresponsefromdaemon: rpcerror: code = 4 desc = contextdeadlineexceeded
 

可以看到变为worker节点以后,就无法再此节点上对集群进行管理

[root@host2 ~]# docker node ls
ID                          NAME  MEMBERSHIP  STATUS  AVAILABILITY  MANAGERSTATUS  LEADER
15wyq8vc1vxe7r4n3yyl0p93j    host1  Accepted    Ready  Active                        
2fh91rbwtj2xosqixzlc7s3mg    host3  Accepted    Ready  Active        Reachable      Yes
5okhpif49b4d6byohpdr4vdmw *  host2  Accepted    Ready  Active        Reachable 
 

可以看到此时,host2已经变为了leader

(4)node inspect查看节点详细信息
(5)node ls查看节点列表
(6)node rm删除一个节点
(7)node tasks可以查看节点上运行的任务task
(8)node update可以更新一个节点
[root@host2 ~]# docker node update --help
 
Usage:  dockernodeupdate [OPTIONS] NODE
 
Update a node
 
Options:
      --availabilitystring  Availabilityofthenode (active/pause/drain)
      --help                  Printusage
      --membershipstring    Membershipofthenode (accepted/rejected)
      --rolestring          Roleofthenode (worker/manager)
 

4、stack/deploy

这两个命令都是experiment阶段,用来部署一个STACK,一个STACK可以描述多个service。 在创建Stack前,需要通过compose.yml文件描述一个组应用,然后再用compose bundle产生一个dsb文件,用户stack的创建。

[root@host2 ~]# docker stack --help
 
Usage:  dockerstackCOMMAND
 
ManageDockerstacks
 
Options:
      --help  Printusage
 
Commands:
  config      Printthestackconfiguration
  deploy      Createand update a stack
  rm          Removethestack
  tasks      Listthetasksin thestack
 
Run 'docker stack COMMAND --help' for moreinformationon a command.
 

5、总结

Docker1.12通过swarm实现了容器集群,在集群之上可以service命令创建一个服务,实现服务的多实例。在服务之上,通过stack可以描述一组服务,最终实现一个大系统的部署。可见,Docker在公司在下一盘很大的棋,从镜像生态到容器引擎,从容器引擎到容器集群,Anything is possible in Docker。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐