Docker - Service

2017-10-03  本文已影响0人  peter_yzq

这篇文章主要介绍Docker中的Service
参考:https://docs.docker.com/get-started/part3/#about-services

在分布式应用中,一个应用提供的不同的功能模块称为Service
例如:一个视频分享网站,可能会包含若干个服务,一个是用于存储应用数据的数据库服务,一个是用于对上传的录像片段进行转码的服务和一个用于前端的服务等。

一个Service中只运行着一个镜像,但是它定义了镜像运行方式,如镜像运行时应该使用哪一个端口,需要运行多少个镜像才能满足这个service需要处理的请求容量。扩大一个service的规模意味着增加运行着那个镜像的容器和更多的计算机资源。
在Docker中可以使用docker-compose.yml文件来定义一个service的运行和规模。
docker-compose.yml 文件

version: "3"
services:
  web:
    # image 替换成在Docker-Container中定义的镜像路径
    image: peter/get-started:part2
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "4010:85"
    networks:
      - webnet
networks:
  webnet:

docker-compose.yml 文件相关参数的解释

开始实验:
a: 运行 docker swarm init --advertise-addr 192.168.99.100(有多张网卡的情况下需要指定一张网卡的ip)

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

b: 运行 docker stack deploy -c docker-compose.yml getstartedlab

Creating network getstartedlab_webnet
Creating service getstartedlab_web

c:查看服务:

MacBook-Pro:Service root# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                     PORTS
qvd1gv0as4a9        getstartedlab_web   replicated          0/5                 peter/get-started:part2   *:4010->85/tcp
MacBook-Pro:Service root# docker service ps qvd1gv0as4a9
ID                  NAME                  IMAGE                     NODE                DESIRED STATE       CURRENT STATE           ERROR               PORTS
14sm4xrqau4o        getstartedlab_web.1   peter/get-started:part2   default             Running             Running 5 minutes ago                       
8hwmin9n42ur        getstartedlab_web.2   peter/get-started:part2   default             Running             Running 5 minutes ago                       
zge7k7tf9ysi        getstartedlab_web.3   peter/get-started:part2   default             Running             Running 5 minutes ago                       
xf29evcz9ls9        getstartedlab_web.4   peter/get-started:part2   default             Running             Running 5 minutes ago                       
3ac4smbofa2l        getstartedlab_web.5   peter/get-started:part2   default             Running             Running 5 minutes ago   

通过curl http://192.168.99.100:4010 命令刷新页面可以看到实例的id在不断的变化。(负载均衡的作用)。

curl http://192.168.99.100:4010
<h3>Hello World!</h3><b>Hostname:</b> e09afafac661<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>yangzhiqiongs-MacBook-Pro:Service root# curl http://192.168.99.100:4010
<h3>Hello World!</h3><b>Hostname:</b> bfc863fbda38<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>yangzhiqiongs-MacBook-Pro:Service root# curl http://192.168.99.100:4010
<h3>Hello World!</h3><b>Hostname:</b> 0f46c60f34f6<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>yangzhiqiongs-MacBook-Pro:Service root# curl http://192.168.99.100:4010
<h3>Hello World!</h3><b>Hostname:</b> c9430749a933<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>yangzhiqiongs-MacBook-Pro:Service root# curl http://192.168.99.100:4010
<h3>Hello World!</h3><b>Hostname:</b> 9dbccd314306<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>yangzhiqiongs-MacBook-Pro:Service root# curl http://192.168.99.100:4010
<h3>Hello World!</h3><b>Hostname:</b> e09afafac661<br/><b>Visits:</b> <i>cannot connect to R

修改 replicas的值为10.

重新部署应用
docker stack deploy -c docker-compose.yml getstartedlab

通过命令:docker service ls,可以查看到现在容器的数量已经变成10个。

ID                  NAME                MODE                REPLICAS            IMAGE                     PORTS
qvd1gv0as4a9        getstartedlab_web   replicated          9/10                peter/get-started:part2   *:4010->85/tcp

删除swarm中运行的实例:
docker stack rm getstartedlab

docker stack rm getstartedlab
Removing service getstartedlab_web
Removing network getstartedlab_webnet

离开swarm:
docker swarm leave --force

其它:
查看容器id的命令。
docker container ls -q

上一篇下一篇

猜你喜欢

热点阅读