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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值