Docker Swarm - 節(jié)點(diǎn)標(biāo)簽與服務(wù)約束

坐我旁邊的妹子

多節(jié)點(diǎn) Swarm 集群下,可能節(jié)點(diǎn)的配置不同(比如 CPU、內(nèi)存等),部署著不同類型的服務(wù)(比如 Web服務(wù)、Job服務(wù)等),當(dāng)這些服務(wù)以 Service 或者 Stack 的形式部署到集群,默認(rèn)情況下會(huì)隨機(jī)分配到各個(gè)節(jié)點(diǎn)。不同類型的服務(wù)對(duì)服務(wù)器需求的資源是不同的,為了更合理的利用服務(wù)器資源,我們可能希望某些服務(wù)能夠部署到指定的服務(wù)器上。另外一種場(chǎng)景,Swarm 集群中的節(jié)點(diǎn)跨機(jī)房,為了內(nèi)部服務(wù)間通信更快,我們可能希望關(guān)聯(lián)比較密切的服務(wù)能夠部署到同一機(jī)房的節(jié)點(diǎn)上。那么,如何做到呢?

很簡(jiǎn)單,先給節(jié)點(diǎn)添加標(biāo)簽,然后服務(wù)發(fā)布時(shí)添加限制條件即可!

Node Label 管理

示例集群信息:

docker@node1:~$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
axr4zun8u1es8ytizjpt3zlnw *   node1               Ready               Active              Leader              18.03.0-ce
vdip2js7tfflxv0smj6wdw0bv     node2               Ready               Active                                  18.03.0-ce
vi17ametnwd58297z6nlcl2o0     node3               Ready               Active                                  18.03.0-ce

※ 添加標(biāo)簽

docker node update --label-add role=web node1

※ 查看標(biāo)簽

docker node inspect node1
[
    {
        "ID": "axr4zun8u1es8ytizjpt3zlnw",
        "Version": {
            "Index": 476
        },
        "CreatedAt": "2018-07-19T03:50:02.734603631Z",
        "UpdatedAt": "2018-07-30T06:37:04.465194614Z",
        "Spec": {
            "Labels": {
                "role": "web"          # 人工添加的標(biāo)簽
            },
            "Role": "manager",
            "Availability": "active"
        }
       # 省略
    }
]

※ 刪除標(biāo)簽

docker node update --label-rm role node1

服務(wù)部署條件約束

※ Service 方式

docker service create \
  --name nginx_2 \
  --constraint 'node.labels.role == web' \
  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==web
           restart_policy:
             condition: on-failure
             max_attempts: 3

constraints 為數(shù)組,填寫多個(gè)約束時(shí),它們之間的關(guān)系是 AND。

條件約束補(bǔ)充

constraints 可以匹配 node 標(biāo)簽和 engine 標(biāo)簽,engine.labels 適用于 Docker Engine 標(biāo)簽,如操作系統(tǒng),驅(qū)動(dòng)程序等,node.labels 適用于上述人為添加到節(jié)點(diǎn)的。

node attribute matches example
node.id Node ID node.id==2ivku8v2gvtg4
node.hostname Node hostname node.hostname!=node-2
node.role Node role node.role==manager
node.labels user defined node labels node.labels.security==high
engine.labels Docker Engine's labels engine.labels.operatingsystem==ubuntu 14.04
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容