原文地址:https://docs.docker.com/swarm/scheduler/filter

Swarm的过滤条件用于调度过程中选择满足指定条件的主机节点,常用于在swarm集群中用Docker创建镜像或运行容器。

使用affinity过滤的示例

 # 选择存在redis镜像的主机运行redis容器(manager_ip:port是swarm-manager的主机及IP>)
 docker tcp:<manager_ip:port> run -d -e affinity:image==redis redis

过滤器分类

Filters分两类:节点过滤器容器过滤器

节点过滤器:以Docker Deamon的配置或Docker主机的特性为条件进行过滤的,它包括:
- constraint
- health

容器过滤器:以Docker容器的配置作为条件过滤,它包括:
- affinity
- dependency
- port

使用容器过滤器时,过滤条件将应用于所有容器,包括stopped状态的容器。
容器过滤器中dependency和port两种,会利用docker运行容器时的相关参数自动选择符合条件的节点,不需额外指定参数。

过滤器全局可用性设置

在使用swarm manage命令启动Swarm管理端时,默认所有的过滤器可用。
如果要指定只允许部分过滤器可用,可使用多个--filter参数指定可用的过滤器。

 swarm manage --filter=health --filter=dependency

使用constraint过滤

constraint使用Docker主机的的默认标记(tags)自定义标签(labels)作为条件。

默认标记来源于docker info信息,包括:
- 节点id或节点名称
- storagedriver
- executiondriver
- kernelversion
- operatingsystem

自定义标签是在docker deamon启动时用一个或多个--label指定的参数。

# 启动docker deamon守护进程时指定label
docker deamon --label storage="ssd"
# 将当前Docker deamon主机加入集群
swarm join --advertise=192.168.0.42:2375 token://XXXXXXXXXXXXXXXXXX

# 使用上述指定了label的主机运行容器
docker tcp://<manager_ip:manager_port>  run -d -P -e constraint:storage==ssd --name db mysql
# 指定构建镜像时选中集群中节点的条件(constraint:storage==disk)
docker build --build-arg=constraint:storage==disk -t ouruser/sinatra:v2 .

使用health过滤

当节点down机了,或无法与Cluster通信了,表示节点处于unhealth状态。
health过滤条件在集群启动时指定通过--filter=health指定,它强制集群在后续操作选择节点时使用health状态的节点。

使用affinity过滤

affinity过滤器,用于选择满足以下条件(具有相关性)的节点:
- container(容器name或id)
- images(镜像名称)
- label(容器上的自定义label)

示例1

docker tcp://<manager_ip:manager_port> run -d --name logger -e affinity:container==87c4376856a8

示例2

# 在三个主机(node-1、node-2、node-3)上下载不同的镜像
docker -H node-1:2375 pull redis
docker -H node-2:2375 pull mysql

# swarm选取存在redis镜像的节点作为容器运行的主机
docker tcp://<manager_ip:manager_port> run -d --name redis1 -e affinity:image==redis redis

使用dependency过滤

在运行容器时,可以指定以下三种依赖:
- –volumns-from=dependency
- –link=dependency:alias
- –net=container:dependency

使用swarm创建容器时,会自动根据满足这三种依赖的主机环境作为容器运行的节点。

使用端口过滤

在运行容器时,通过-p指定主机与容器的端口映射,在swarm环境中,会自动选择指定端口可用(未被占用)的主机,避免端口冲突。

Filters表达式书写规则

根据上述介绍的五种过滤器,只有constraints和affinity两种需要通过-e参数额外指定Filter表达式。

<filter-type>:<key><operator><value>

    filter-type: constraints和affinity两种
    key: container、node、节点默认tag、节点或容器自定义label
    operator:包括`==`、`!=`两种,可在后面加'~'表示软匹配,在条件无匹配时,自动忽略过滤条件
    value:字符串表达式(字符、数据、点号、下划线等组成),可以是
        全局匹配 —— abc*
        正则匹配 —— /node\d/

示例如下

  • constraint:node==node1 matches node node1.
  • constraint:node!=node1 matches all nodes, except node1.
  • constraint:region!=us* matches all nodes outside with a region tag prefixed with us.
  • constraint:node==/node[12]/ matches nodes node1 and node2.
  • constraint:node==/node\d/ matches all nodes with node + 1 digit.
  • constraint:node!=/node-[01]/ matches all nodes, except node-0 and node-1.
  • constraint:node!=/foo[bar]/ matches all nodes, except foo[bar]. You can see the use of escape characters here.
  • constraint:node==/(?i)node1/ matches node node1 case-insensitive. So NoDe1 or NODE1 also match.
  • affinity:image==~redis tries to match for nodes running container with a redis image
  • constraint:region==~us* searches for nodes in the cluster belonging to the us region
  • affinity:container!=~redis* schedule a new redis5 container to a node without a container that satisfies redis*
Logo

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

更多推荐