1. 程式人生 > >docker swarm如何在指定節點執行service

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。