3.Docker-服务(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.

分布式程序中,应用的不同部分被称作服务。例如影片分享网站,可能有存储数据服务,影片转码服务,前端服务等等。
这些服务实际上是“产品中的容器”,一个服务只执行一个软件,但是需要考虑以什么方式运行:选择使用什么端口,根据服务的需要选择运行多少副本等等。扩展服务时改变软件的容器实例数量,以此给服务分配更多的计算资源。
幸运的是在Docker平台下只需要简单的写一个docker-compose.yml文件来定义、运行、扩展服务。

1.创建 docker-compose.yml

version: "3"
services:
  web:
    image: gaojingyuan/testrepo:v1
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "80:80"
    networks:
      - webnet
networks:
  webnet:

文件配置解释:

  • 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 the CPU (across all cores), and 50MB of RAM.

名为web的服务跑5个上述镜像,限制每个镜像最多用10%的cpu,50MB的内存

  • Immediately restart containers if one fails.

一旦失败立即重启

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

映射web服务的端口到宿主机的80端口

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

通过名为webnet的负载均衡共享80端口(在内部,这些容器用临时的端口映射到80端口)

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

用默认设置定义webnet网络(它是一个负载均衡覆盖网络)

注:覆盖网络就是应用层网络,它是面向应用层的,不考虑或很少考虑网络层,物理层的问题。详细说来,覆盖网络是指建立在另一个网络上的网络。该网络中的结点可以看作通过虚拟或逻辑链路而连接起来的。虽然在底层有很多条物理链路,但是这些虚拟或逻辑链路都与路径一一对应。例如:许多P2P网络就是覆盖网络,因为它运行在互连网的上层。覆盖网络允许对没有 IP地址标识的目的主机路由信息,例如:Freenet 和DHT(分布式哈希表)可以路由信息到一个存储特定文件的结点,而这个结点的IP地址事 先并不知道。覆盖网络被认为是一条用来改善互连网路由的途径,例如通过QOS(服务质量)保障来实现高质量的流媒体。

2.运行

2.1 集群初始化

docker swarm init 启动蜂群模式 
控制台输出:

Swarm initialized: current node (m17i3idaej14894yvlimto6pr) is now a manager.

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

docker swarm join --token SWMTKN-1-597khckt07uawn08qg62e4qbobb0vszlkoodf8qzejohn1wuqn-6m8kcym10w7zf41zhp32y325a 192.168.65.2:2377

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

2.2 发布服务

docker stack deploy -c docker-compose.yml getstartedlab
控制台输出:

Creating network getstartedlab_webnet

Creating service getstartedlab_web

2.3 查看服务

docker service ls
控制台输出:

ID NAME MODE REPLICAS IMAGE PORTS

o0fklnad8n1d getstartedlab_web replicated 5/5 gaojingyuan/testrepo:v1 *:80->80/tcp

2.4 查看服务进程

示例:docker service ps 尖括号中间service表示它的ID

docker service ps o0fklnad8n1d
控制台输出:
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS

3tvfgmoal009 getstartedlab_web.1 gaojingyuan/testrepo:v1 moby Running Running 6 minutes ago

uk39j4zinpv4 getstartedlab_web.2 gaojingyuan/testrepo:v1 moby Running Running 6 minutes ago

5rscy4ulz09a getstartedlab_web.3 gaojingyuan/testrepo:v1 moby Running Running 6 minutes ago

7e2i142lz3mg getstartedlab_web.4 gaojingyuan/testrepo:v1 moby Running Running 6 minutes ago

ps4yzw5k9z2q getstartedlab_web.5 gaojingyuan/testrepo:v1 moby Running Running 6 minutes ago

2.5 通过任务ID查容器ID

docker inspect --format='{{.Status.ContainerStatus.ContainerID}}' <task>

2.6 通过容器ID查任务ID

docker inspect --format="{{index .Config.Labels \"com.docker.swarm.task.id\"}}" <container>

2.7 查看所有容器

docker container ls -q
控制台输出:
989efc8812a5

38d07f166b48

9dab6a401ce5

9b98583cd7a8

a54105710e39

2.8 访问localhost

再次访问

可见页面中Hostname已经变了,5个container轮询

3 重启
修改docker-compose.yml 例如修改replicas为10
再运发布服务命令

docker stack deploy -c docker-compose.yml getstartedlab
控制台输出
Updating service getstartedlab_web (id: o0fklnad8n1do2v0fisrc4kpl)

再次查看所有容器

docker container ls -q
控制台输出
131f38ee8844
097c3526d6e4
aadfe3e18df5
83e37e0951ac
88944ca651f5
989efc8812a5
38d07f166b48
9dab6a401ce5
9b98583cd7a8
a54105710e39

可以看到无需关闭stack或者已有的容器,而且已有容器的ID仍然得以保留

4 关闭

docker stack rm getstartedlab 移除节点 但是swarm仍然运行
docker swarm leave --force 移除swarm,目的是使下次运行时docker swarm init命令不会报错,方便脚本管理

备注:

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、付费专栏及课程。

余额充值