Docker 入門筆記 3 - Service

本筆記參考 https://docs.docker.com/get-started/part3/

  • Containers
  • Services
  • Swarms
  • Stacks

什麼是Services

分佈式應用中,一個應用的不同部分被稱爲“服務“,一個服務只運行一個image, 它規定了image的運行方式,包括使用哪個端口,容器應運行多少個副本,分配的計算資源等等。

接下來我們將之前製作的image部署成多個副本,擴展我們的應用並實現load-balance

docker-compose.yml

docker-compose.yml 文件用於指定生產環境中 docker container應如何工作。

爲之前的image 創建docker-compose.yml

$ cat docker-compose.yml 
version: "3"
services:
  web:       #service called web
    image: misterchi/repositorytest:hello  #Pull the image 
    deploy:
      replicas: 5     #five instances
      resources:
        limits:
          cpus: "0.1"  #one instance at most 10% of the CPU
          memory: 50M  #one instance at most 50M memory
      restart_policy:
        condition: on-failure   #Immediately restart containers if one fails
    ports:
      - "80:80"   #map host port 80 with service port 80
    networks:
      - webnet  #share 80 via a load-balanced network called webnet
networks:
  webnet:   #use webnet as default network

運行app

#創建swarm集羣,默認當前node爲管理節點

docker swarm init

#根據 docker-compose.yml 部署一個stack,並給它起個名字
#stack就是一組有關聯的服務的組合,可以編排在一起,一起管理

docker stack deploy -c docker-compose.yml myClusterApp

現在這個服務在主機上用我們的image運行了5個實例

$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                            PORTS
iqpurp9syc3t        myClusterApp_web    replicated          5/5                 misterchi/repositorytest:hello   *:80->80/tcp

或者

$ docker service ps myClusterApp_web
ID                  NAME                 IMAGE                            NODE                DESIRED STATE       CURRENT STATE           ERROR               PORTS
1xst4gighgh0        myClusterApp_web.1   misterchi/repositorytest:hello   chic-X450LD         Running             Running 7 minutes ago                       
lh35uqoxsoja        myClusterApp_web.2   misterchi/repositorytest:hello   chic-X450LD         Running             Running 7 minutes ago                       
cz2awa9lrywo        myClusterApp_web.3   misterchi/repositorytest:hello   chic-X450LD         Running             Running 7 minutes ago                       
ilbiihzuofvw        myClusterApp_web.4   misterchi/repositorytest:hello   chic-X450LD         Running             Running 7 minutes ago                       
n4sxpk0l5zx0        myClusterApp_web.5   misterchi/repositorytest:hello   chic-X450LD         Running             Running 7 minutes ago

也可以按照stack來查看

$ docker stack ps myClusterApp

服務的每個container被稱爲一個task, 我們可以這樣列出所有的容器實例

docker container ls

測試

curl -4 http://localhost

關閉app

#從swarm stack中關閉app

$ docker stack rm myClusterApp
Removing service myClusterApp_web
Removing network myClusterApp_webnet

#退出swarm stack
$ docker swarm leave --force

常用命令

docker stack ls                                            # List stacks or apps
docker stack deploy -c <composefile> <appname>  # Run the specified Compose file
docker service ls                 # List running services associated with an app
docker service ps <service>                  # List tasks associated with an app
docker inspect <task or container>                   # Inspect task or container
docker container ls -q                                      # List container IDs
docker stack rm <appname>                             # Tear down an application
docker swarm leave --force      # Take down a single node swarm from the manager
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章