docker swarm如何在指定節點運行service

Swarm環境中怎麼指定某個容器在指定的宿主上運行呢? #

每個 Docker Host 建立時都可以通過 --label 指定其 Docker Daemon 的標籤,比如:

$ docker daemon \
  --label com.example.environment="production" \
  --label com.example.storage="ssd"


  • 注意,上面的配置參數應該配置在 docker daemon 的配置文件裏,如 docker.service,而不是簡單的命令行執行……


如果是配置文件啓動的需要這樣寫:(ubuntu)

DOCKER_OPTS="$DOCKER_OPTS --label com.example.environment='production' "


然後運行容器時,使用環境變量約束調度即可。可以使用 Compose 文件的 environment 配置,也可以使用 docker run 的 -e 環境變量參數。下面以 Compose 配置文件爲例:

version: "2"
services:
 mongodb:
 image: mongo:latest
 environment:
 "constraint:com.example.storage==ssd"


這樣這個 mongodb 的服務就會運行在標記爲 com.example.storage="ssd" 的宿主上運行。


還可以在啓動的時候使用 --constraint 參數進行限制,此方法效果比使用變量效果要好,經過試驗變量效果會失效的現象太多,按照上述在 Dockerd 上配置 label 後啓動服務的命令如:

$ docker service create --name xxx --constraint engine.labels.com.example.environment==production xxx

把其餘參數補齊即可啓動在使用這個 production 標籤的節點上。

$ docker service create --name xxx --constraint node.hostname==swarm1 xxx

可以使用集羣節點的名稱來啓動,省去了配置 label。

Main Reference: https://wiki.shileizcc.com/pages/viewpage.action?pageId=10911914

https://blog.lab99.org/post/docker-2016-07-14-faq.html

  docker原理: http://www.csdn.net/article/2015-08-21/2825511

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章