docker学习笔记--Services

本文详细介绍了如何使用Docker平台定义、运行和扩展服务。通过编写docker-compose.yml文件,可以控制容器实例的数量、资源限制、重启策略及网络映射等。文章展示了从初始化Swarm到部署、检查和管理服务的全过程。

https://docs.docker.com/get-started/part3/

About services

In a distributed application, different pieces of the app are called “services”. For example, if you imagine a video sharing site, it probably includes a service for storing application data in a database, a service for video transcoding in the background after a user uploads something, a service for the front-end, and so on.

Services are really just “containers in production.” A service only runs one image, but it codifies the way that image runs—what ports it should use, how many replicas of the container should run so the service has the capacity it needs, and so on. Scaling a service changes the number of container instances running that piece of software, assigning more computing resources to the service in the process.

Luckily it’s very easy to define, run, and scale services with the Docker platform -- just write a docker-compose.yml file.

Your first docker-compose.yml file 

编写service的配置文件:

version: "3"
services:
  web:
    image: dingxiaotong/friendlyhello:v1
    deploy:
      replica: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "4000:80"
    networks:
      - webnet
  networks:
    webnet:

This docker-compose.yml file tells Docker to do the following:

  • Pull the image we uploaded in step 2 from the registry.

  • Run 5 instances of that image as a service called web, limiting each one to use, at most, 10% of a single core of CPU time (this could also be e.g. “1.5” to mean 1 and half core for each), and 50MB of RAM.

    replica: 5 表示这个services允许5个 dingxiaotong/friendlyhello:v1 镜像运行

  • Immediately restart containers if one fails.

  • Map port 4000 on the host to web’s port 80.

  • Instruct web’s containers to share port 80 via a load-balanced network called webnet. (Internally, the containers themselves publish to web’s port 80 at an ephemeral port.)

  • Define the webnet network with the default settings (which is a load-balanced overlay network).

PS E:\MY_CODE\所有test文件\docker_test> cat docker-compose.yml
version: "3"
services:
  web:
    image: dingxiaotong/friendlyhello:v1
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "4000:80"
    networks:
      - webnet
networks:
  webnet:

PS E:\MY_CODE\所有test文件\docker_test> docker swarm init
Swarm initialized: current node (kq4mt6gy179yu3e4jorbmja9r) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-60gl70wvitrwdzaoyitguz3x1all4jczxjpxqphexacw16bw68-39316wtrdl4osgy1lx5x3l6bs 192.168.65.3:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

PS E:\MY_CODE\所有test文件\docker_test> docker stack deploy -c docker-compose.yml dxt00app
Creating network dxt00app_webnet
Updating service dxt00app_web (id: rsdmmvtk357cdvovu03snvrbr)
PS E:\MY_CODE\所有test文件\docker_test> docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                           PORTS
rsdmmvtk357c        dxt00app_web        replicated          5/5                 dingxiaotong/friendlyhello:v1   *:4000->80/tcp

dxt00app_web service包含了5个Tasks:

PS E:\MY_CODE\所有test文件\docker_test> docker service ps dxt00app_web
ID                  NAME                 IMAGE                           NODE                    DESIRED STATE       CURRENT STATE            ERROR               PORTS
l6eawr29kqnh        dxt00app_web.1       dingxiaotong/friendlyhello:v1   linuxkit-00155dc5b603   Running             Running 3 minutes ago
ohhw2k949sbr         \_ dxt00app_web.1   dingxiaotong/friendlyhello:v1   linuxkit-00155dc5b603   Shutdown            Shutdown 3 minutes ago
km2hi68d13tp        dxt00app_web.2       dingxiaotong/friendlyhello:v1   linuxkit-00155dc5b603   Running             Running 3 minutes ago
v582gi8c83rb        dxt00app_web.3       dingxiaotong/friendlyhello:v1   linuxkit-00155dc5b603   Running             Running 3 minutes ago
pwpd7wmfxgc5        dxt00app_web.4       dingxiaotong/friendlyhello:v1   linuxkit-00155dc5b603   Running             Running 3 minutes ago
xf3jgpuyhhyd        dxt00app_web.5       dingxiaotong/friendlyhello:v1   linuxkit-00155dc5b603   Running

可以看到现在有5个Tasks正在运行: 

PS E:\MY_CODE\所有test文件\docker_test> docker ps
CONTAINER ID        IMAGE                           COMMAND             CREATED             STATUS              PORTS               NAMES
3efad9c51832        dingxiaotong/friendlyhello:v1   "python app.py"     8 minutes ago       Up 8 minutes        80/tcp              dxt00app_web.1.l6eawr29kqnhqywomxeqg8r6g
17d624f47753        dingxiaotong/friendlyhello:v1   "python app.py"     8 minutes ago       Up 8 minutes        80/tcp              dxt00app_web.2.km2hi68d13tpqcj4rla2jvyis
c9c2f912a68d        dingxiaotong/friendlyhello:v1   "python app.py"     8 minutes ago       Up 8 minutes        80/tcp              dxt00app_web.5.xf3jgpuyhhydz59wsl46fbhir
a6b013b0b8b2        dingxiaotong/friendlyhello:v1   "python app.py"     8 minutes ago       Up 8 minutes        80/tcp              dxt00app_web.4.pwpd7wmfxgc5n79r52phksve2
c1e1da38cbce        dingxiaotong/friendlyhello:v1   "python app.py"     8 minutes ago       Up 8 minutes        80/tcp              dxt00app_web.3.v582gi8c83rb7sti67t9tgsid

Summary

To recap, while typing docker run is simple enough, the true implementation of a container in production is running it as a service. Services codify a container’s behavior in a Compose file, and this file can be used to scale, limit, and redeploy our app. Changes to the service can be applied in place, as it runs, using the same command that launched the service:docker stack deploy.

总的来说,Service可以通过编辑Compose file来部署Containers的行为

Some commands to explore at this stage:

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值