入門-service-第二層

前提

  • dockers version:1.13或者更高(17.03.1-ce)
  • 安裝Docker Compose

理解service

在一個分布式應用中,不同的部分都被稱為service。

services實際上是containers in production。一個service啟動一個image,它規(guī)定了image的運行方式(使用哪些端口,需要多少個container的復制集才能滿足service等)。

docker-compose.yml文件可以很簡單的定義、運行、規(guī)劃services

docker-compose.yml

可以把該文件保存在任何位置。確保image已經上傳。

version: "3"
services:
  web:
    # replace username/repo:tag with your name and image details
    image: username/repository:tag
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "80:80"
    networks:
      - webnet
networks:
  webnet:
  • registry拉取image
  • 運行5個image的實例作為服務,稱為web,限制每個使用最多10%的CPU和50M的內存
  • 如果停止運行立即重新啟動container
  • 映射本地80端口到web的80端口
  • 指導webcontainers通過負載均衡網絡webnet共享80端口。(在內部,containers will publish to web’s port 80 at an ephemeral port)
  • 使用默認設置定義webnet網絡

運行新的負債均衡的app

docker swarm init

# app name: getstartedlab
docker stack deploy -c docker-compose.yml getstartedlab

# See a list of the five containers you just launched
docker stack ps getstartedlab

Scale(規(guī)劃) the app

可以修改replicas,保存,重新運行docker stack deploy:

docker stack deploy -c docker-compose.yml getstartedlab

Docker will do an in-place update, no need to tear the stack down first or kill any containers.

摧毀app和swarm

docker stack rm getstartedlab

This removes the app, but our one-node swarm is still up and running (as shown by docker node ls). Take down the swarm with docker swarm leave --force.

docker stack ls              # List all running applications on this Docker host
docker stack deploy -c <composefile> <appname>  # Run the specified Compose file
docker stack services <appname>       # List the services associated with an app
docker stack ps <appname>   # List the running containers associated with an app
docker stack rm <appname>                             # Tear down an application
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容