Docker Swarm
服务部署条件约束可以匹配 node
标签和 engine
标签, engine.labels
适用于 Docker Engine
标签,如操作系统,驱动程序等, node.labels
适用于人为添加到节点的标签,详情参考笔记:Docker Swarm 节点标签。
示例:
Service
方式
$ docker service create \ --name nginx_2 \ --constraint 'node.labels.role == gateway' \ nginx点击复制复制失败已复制
Stack
方式
version: '3.6' services: mycat: image: nginx ports: - target: 8080 published: 8080 protocol: tcp mode: ingress deploy: mode: global placement: constraints: # 添加条件约束 - node.labels.role==gateway restart_policy: condition: on-failure max_attempts: 3点击复制复制失败已复制
条件约束汇总如下:
类别 | 属性 | 示例 |
node.id | ID | node.id==sxkwoaik9bbj2oihxqrxxjxtc |
node.hostname | Description. Hostname | node.hostname!=node1 |
node.role | Spec. Role | node.role==manager |
node.labels | 自定义标签,Spec. Lables中键值对 | node.labels.role==gateway |
engine.labels | Docker 引擎自带标签 | engine.labels.operatingsystem==ubuntu 20.04 |
附:节点信息示例
{ "ID": "sxkwoaik9bbj2oihxqrxxjxtc", "Version": { "Index": 34 }, "CreatedAt": "2020-12-25T11:58:21.941243809Z", "UpdatedAt": "2020-12-25T13:12:03.62858601Z", "Spec": { "Labels": { "role": "gateway" }, "Role": "manager", "Availability": "active" }, "Description": { "Hostname": "node1", "Platform": { "Architecture": "x86_64", "OS": "linux" }, "Resources": { "NanoCPUs": 2000000000, "MemoryBytes": 4127367168 }, "Engine": { "EngineVersion": "19.03.8", "Plugins": [ { "Type": "Log", "Name": "awslogs" }, { "Type": "Log", "Name": "fluentd" }, { "Type": "Log", "Name": "gcplogs" }, { "Type": "Log", "Name": "gelf" }, { "Type": "Log", "Name": "journald" }, { "Type": "Log", "Name": "json-file" }, { "Type": "Log", "Name": "local" }, { "Type": "Log", "Name": "logentries" }, { "Type": "Log", "Name": "splunk" }, { "Type": "Log", "Name": "syslog" }, { "Type": "Network", "Name": "bridge" }, { "Type": "Network", "Name": "host" }, { "Type": "Network", "Name": "ipvlan" }, { "Type": "Network", "Name": "macvlan" }, { "Type": "Network", "Name": "null" }, { "Type": "Network", "Name": "overlay" }, { "Type": "Volume", "Name": "local" } ] }, "TLSInfo": { "TrustRoot": "******", "CertIssuerSubject": "******", "CertIssuerPublicKey": "********" } }, "Status": { "State": "ready", "Addr": "10.87.23.111" }, "ManagerStatus": { "Leader": true, "Reachability": "reachable", "Addr": "10.87.23.1" } }