Docker的命令之集群服务管理 Service

应该说docker service是swarm(docker集群)最重要的管理指令,可以实现部署运行服务、服务扩容缩容、删除服务、滚动更新等功能,学习本章节的内容尤其重要,本文将详细展开docker service 中的每一个选项的用法,不同于介绍其他的docker指令。

在Swarm集群上部署服务,必须在Manager Node上进行操作。先说明一下Service、Task、Container(容器)这个三个概念的关系,如下图(出自Docker官网)非常清晰地描述了这个三个概念的含义:

20170315210902

Usage:    docker service COMMAND

Manage services

Options:
      --help   Print usage

Commands:
  create      Create a new service
  inspect     Display detailed information on one or more services
  logs        Fetch the logs of a service or task
  ls          List services
  ps          List the tasks of one or more services
  rm          Remove one or more services
  scale       Scale one or multiple replicated services
  update      Update a service

Run 'docker service COMMAND --help' for more information on a command.
# 可以使用 docker service COMMAND --help ,继续查看使用说明。

 一、详细介绍

1、创建服务 docker service create

格式: docker service create [OPTIONS] IMAGE [COMMAND] [ARG...]
–config Specify configurations to expose to the service
–constraint list Placement constraints
定位容器,可以理解为通过标签查找容器。
constraint的使用方法参考:Label的使用
–container-label list Container labels
定义容器的标签.
例如:#docker service create -td –container-label=serviceName=redis0101 –name redis redis
查看容器信息:docker inspect 6d59d5f082c0
“Labels”: {
“com.docker.swarm.node.id”: “wgoesjv447qeqvh5s7fqnv68h”,
“com.docker.swarm.service.id”: “7ijdu38kc3ggylx64i8ml5p17″,
“com.docker.swarm.service.name”: “redis”,
“com.docker.swarm.task”: “”,
“com.docker.swarm.task.id”: “y3lmjs4py93ras5d21pkuj5h5″,
“com.docker.swarm.task.name”: “redis.1.y3lmjs4py93ras5d21pkuj5h5″,
“serviceName”: “redis0101″
}
–credential-spec credential-spec Credential spec for managed service account (Windows only)
仅对windows系统有效
-d, –detach Exit immediately instead of waiting for the service to converge (default true)
后端创建
例如:docker service create -d –name=linux centos:latest
–dns list Set custom DNS servers
设定容器的DNS地址
参考:Docker的启动参数
–dns-option list Set DNS options
容器 /etc/resolv.conf 文件,其他设置
参考:Docker的启动参数
–dns-search list search 设定容器的搜索域
参考:Docker的启动参数
–endpoint-mode string Endpoint mode (vip or dnsrr) (default “vip”)
–entrypoint command Overwrite the default ENTRYPOINT of the image
-e, –env list Set environment variables
设置容器实例的环境变量
例如:docker service create –name=redis –replicas=5 –env=GOROOT=/usr/local –env=GOPATH=/data/go  redis
–env-file list Read in a file of environment variables
读入配置文件
例如:docker service create –name=redis –replicas=5 –env-file=/root/file.env  redis
cat /root/file.env
GOPATH=/usr/local/go
PHPROOT=/data/www/web
NGINX_CONF=/usr/local/nginx/conf/nginx.conf
–group list Set one or more supplementary user groups for the container
–health-cmd string Command to run to check health
–health-interval duration Time between running the check (ns|us|ms|s|m|h)
–health-retries int Consecutive failures needed to report unhealthy
–health-start-period duration Start period for the container to initialize before counting retries towards unstable(ns|us|ms|s|m|h)
–health-timeout duration Maximum time to allow one check to run (ns|us|ms|s|m|h)
–help Print usage
帮忙文档
–host list Set one or more custom host-to-IP mappings (host:ip)
–hostname string Container hostname
容器的hostName,在容器中使用 cat /etc/hostname 查看
也可以使用模板变量来设置,参考:模板变量
-l, –label list Service labels
设置服务标签,使用可参考:Label的使用
通过 docker service inspect 可以查看。
–limit-cpu decimal Limit CPUs
–limit-memory bytes Limit Memory
–log-driver string Logging driver for service
–log-opt list Logging driver options
–mode string Service mode (replicated or global) (default “replicated”)
服务类型,提供了两种模式:一种是replicated,我们可以指定服务Task的个数(也就是需要创建几个冗余副本),这也是Swarm默认使用的服务类型;另一种是global,这样会在Swarm集群的每个Node上都创建一个服务。
replicated模式,根据指定的数量运行任务。
global模式,任务运行在集群中所有活跃的节点上。
–mount mount Attach a filesystem mount to the service
–name string Service name
服务的名称
例如:docker service create -d –name=linux centos:latest
–network list Network attachments
节点的网络,此处必需是scope为swarm的网络
docker service create –network=over10 –name=ins01 –replicas=5 centos
–no-healthcheck Disable any container-specified HEALTHCHECK
–placement-pref pref Add a placement preference
-p, –publish port Publish a port as a node port
-q, –quiet Suppress progress output
–read-only Mount the container’s root filesystem as read only
–replicas uint Number of tasks
服务副本数,也就是启动N个Docker容器来运行该服务.
例如 :docker service create -d –name=linux –replicas=2 centos:latest
–reserve-cpu decimal Reserve CPUs
–reserve-memory bytes Reserve Memory
–restart-condition string Restart when condition is met (“none”|”on-failure”|”any”) (default “any”)
–restart-delay duration Delay between restart attempts (ns|us|ms|s|m|h) (default 5s)
–restart-max-attempts uint Maximum number of restarts before giving up
–restart-window duration Window used to evaluate the restart policy (ns|us|ms|s|m|h)
–rollback-delay duration Delay between task rollbacks (ns|us|ms|s|m|h) (default 0s)
–rollback-failure-action string Action on rollback failure (“pause”|”continue”) (default “pause”)
–rollback-max-failure-ratio float Failure rate to tolerate during a rollback (default 0)
–rollback-monitor duration Duration after each task rollback to monitor for failure (ns|us|ms|s|m|h) (default 5s)
–rollback-order string Rollback order (“start-first”|”stop-first”) (default “stop-first”)
–rollback-parallelism uint Maximum number of tasks rolled back simultaneously (0 to roll back all at once) (default 1)
–secret secret Specify secrets to expose to the service
–stop-grace-period duration Time to wait before force killing a container (ns|us|ms|s|m|h) (default 10s)
–stop-signal string Signal to stop the container
-t, –tty Allocate a pseudo-TTY
–update-delay duration Delay between updates (ns|us|ms|s|m|h) (default 0s)
–update-failure-action string Action on update failure (“pause”|”continue”|”rollback”) (default “pause”)
–update-max-failure-ratio float Failure rate to tolerate during an update (default 0)
–update-monitor duration Duration after each task update to monitor for failure (ns|us|ms|s|m|h) (default 5s)
–update-order string Update order (“start-first”|”stop-first”) (default “stop-first”)
–update-parallelism uint Maximum number of tasks updated simultaneously (0 to update all at once) (default 1)
-u, –user string Username or UID (format: <name|uid>[:<group|gid>])
–with-registry-auth Send registry authentication details to swarm agents
-w, –workdir string Working directory inside the container

2、列出正在运行的服务 docker service ls

3、删除服务 docker service rm

4、查看服务的详细信息 docker service inspect

docker service inspect redis

5、删除服务 docker service rm

 

Tips 01:模板变量

  • 模板变量使用GO “text/template” 的语法
  • 模板变量目前仅使用 --hostname --mount --env 3个标签。
Placeholder Description
.Service.ID Service ID,例如:xpeo80ow3bx6tejgvej288inl
.Service.Name Service name,例如:redis
.Service.Labels Service labels,例如:map[]
.Node.ID Node ID,例如:oompcumjq1by4qicrhaa362nl
.Task.ID Task ID,例如:tcv622s57f3sr23mtadg63img
.Task.Name Task name,例如:redis.4.tcv622s57f3sr23mtadg63img
.Task.Slot Task slot,例如:4
 例如:docker service create --name=redis --hostname="{{.Service.ID}}-{{.Service.Name}}" \
 --env PARAMS="{{.Node.ID}}-{{.Task.ID}}-{{.Task.Name}}-{{.Task.Slot}}" \ 
--replicas=5 redis:latest

Tips 02:Label的使用

Lable的使用主要用户定位、查找节点。假设有个docker集群,3台manager主机 ,2台worker主机,在创建docker服务时,仅想在指定的一台或多台的主机上创建服务容器,即可使用label的方法。

  • 有哪些label(标签)可以使用?
# docker node inspect wgoesjv447qeqvh5s7fqnv68h

"ID": "wgoesjv447qeqvh5s7fqnv68h", //节点id,加入swarm后自动创建
"Spec": {
    "Labels": {
        "nodeName": "host110" //参考:Docker的命令之集群节点管理 Swarm node,中的update 
                               --label-add和--label-rm 设置
    },
    "Role": "worker", //参考:Docker的命令之集群节点管理 Swarm node,中的 update --role来设置
 },
.......
"Description": {
    "Hostname": "110.100.com", //从主机的cat /etc/hostname 
    "Engine": {
        "Labels": { //参考:Docker的启动参数中,--lable或daemon.json中的 labels来设置
            "nodeName": "node-110",
             "nodeRole": "worker"
         },

上述的5个标签分别是:

node属性 举例
node.id –constraint=”node.id==wgoesjv447qeqvh5s7fqnv68h”
node.hostname –constraint=”node.hostname==122.100.com”
node.role –constraint=”node.role!=manager” –constraint=”node.hostname==122.100.com”
node.labels –constraint=”node.labels.nodeName==host110″
engine.labels –constraint=”engine.labels.nodeName==node-110″

 

推荐相关文档:

Docker的命令之集群服务管理 Service》上有6条评论

  1. Pingback引用通告: Docker的命令之集群节点管理 Swarm node | 精彩每一天

  2. Pingback引用通告: Docker的启动参数 | 精彩每一天

  3. Pingback引用通告: Docker的命令之镜像管理 Image | 精彩每一天

  4. Pingback引用通告: Docker中的组件介绍 | 精彩每一天

  5. Pingback引用通告: Docker的命令之 Docker stacks | 精彩每一天

  6. Pingback引用通告: Docker create / Docker run 的选项详解 | 精彩每一天

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>