最簡單的情況下,我們的應用運行在一個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