Docker学习笔记(四)Service

       最简单的情况下,我们的应用运行在一个container中,能够提供的服务和性能非常有限。在实际项目中,应用大多是一般由大量的service组成,比如鉴权、告警等等。当然,每个service也可以有多个instance,来达到高性能和高可用性的目的。

       在docker中部署应用时,每个service有自己的image,并且一个service可以启动多个container,service中的单个container叫作task。docker通过yml配置来定义service的生命周期,简化service的部署,下面使用笔记三中build的image来演示service的基本应用。

Note

本文demo参考docker官方文档,有条件的朋友建议直接看原文Link:https://docs.docker.com/get-started/part3/#recap-and-cheat-sheet-optional

hello-service demo

创建service-hello.yml文件

version: "3"
services:
  web:
    # replace username/repo:tag with your name and image details
    image: hebostary/gohead:demo1
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "4000:80"
    networks:
      - webnet
networks:
  webnet:

部署service

docker swarm init #first deploy need
docker stack deploy -c docker-service.yml hello-service

查询servservice及container

[root@izuf682lz6444cynn96up0z service]# docker service ls
ID            NAME               MODE        REPLICAS  IMAGE
dpwa2lyvnrly  hello-service_web  replicated  5/5       hebostary/gohead:demo1
[root@izuf682lz6444cynn96up0z service]# docker service ps hello-service_web
ID            NAME                 IMAGE                   NODE                     DESIRED STATE  CURRENT STATE           ERROR  PORTS
ywf3ge6efoh0  hello-service_web.1  hebostary/gohead:demo1  izuf682lz6444cynn96up0z  Running        Running 40 seconds ago         
v4djymxdqopb  hello-service_web.2  hebostary/gohead:demo1  izuf682lz6444cynn96up0z  Running        Running 40 seconds ago         
j6qfm5k170n1  hello-service_web.3  hebostary/gohead:demo1  izuf682lz6444cynn96up0z  Running        Running 40 seconds ago         
g4t1etgfja8v  hello-service_web.4  hebostary/gohead:demo1  izuf682lz6444cynn96up0z  Running        Running 40 seconds ago         
sou1glk6ygtw  hello-service_web.5  hebostary/gohead:demo1  izuf682lz6444cynn96up0z  Running        Running 40 seconds ago         
[root@izuf682lz6444cynn96up0z service]# docker container ls
CONTAINER ID        IMAGE                                                                                      COMMAND             CREATED              STATUS              PORTS               NAMES
0043de2a9c83        hebostary/gohead@sha256:969c4f12d7c1d9e3f167498e1779aceefc631a158ec4e18730d16f5602569d03   "python app.py"     About a minute ago   Up About a minute   80/tcp              hello-service_web.2.v4djymxdqopb4ib9ilto9xvhm
e483dd3fcce9        hebostary/gohead@sha256:969c4f12d7c1d9e3f167498e1779aceefc631a158ec4e18730d16f5602569d03   "python app.py"     About a minute ago   Up About a minute   80/tcp              hello-service_web.3.j6qfm5k170n12k60huesg1lpl
377dc8046ba9        hebostary/gohead@sha256:969c4f12d7c1d9e3f167498e1779aceefc631a158ec4e18730d16f5602569d03   "python app.py"     About a minute ago   Up About a minute   80/tcp              hello-service_web.1.ywf3ge6efoh0mvxxg09gfgjle
32834e797374        hebostary/gohead@sha256:969c4f12d7c1d9e3f167498e1779aceefc631a158ec4e18730d16f5602569d03   "python app.py"     About a minute ago   Up About a minute   80/tcp              hello-service_web.5.sou1glk6ygtwdjphmdwnf2c5w
682f529a98ab        hebostary/gohead@sha256:969c4f12d7c1d9e3f167498e1779aceefc631a158ec4e18730d16f5602569d03   "python app.py"     About a minute ago   Up About a minute   80/tcp              hello-service_web.4.g4t1etgfja8vf8bzgxcfv3s2f

note:前面部署的service的名字是hello-service_web。hello-service实际上是stack,这个概念在后面会做描述。

[root@izuf682lz6444cynn96up0z service]# docker stack ls
NAME           SERVICES
hello-service  1

访问http://ip:4000

Hello World!

Hostname: 32834e797374
Visits: cannot connect to Redis, counter disabled

每次访问返回的Hostname几乎都是不一样的,这个hostname就是container的ID。这也说明,hello-service_web的5个instance共同分担了多次请求的处理,有loadbalance的过程。

增加instance(scale)

将yml中的replicas修改成6,然后再次deploy,看到docker只是add了一个新的container,并没有去修改原有的5个container。[root@izuf682lz6444cynn96up0z service]# docker stack deploy -c docker-service.yml hello-service Updating service hello-service_web (id: dpwa2lyvnrly17fz9ey3itba0)

删除service

[root@izuf682lz6444cynn96up0z service]# docker stack rm hello-service
Removing service hello-service_web
Removing network hello-service_webnet
[root@izuf682lz6444cynn96up0z service]# docker swarm leave --force
Node left the swarm.

用到的命令

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章