部署服务

我们使用 docker service 命令来管理 Swarm 集群中的服务,该命令只能在管理节点运行。

新建服务

现在我们在上一节创建的 Swarm 集群中运行一个名为 nginx 服务。

$ docker service create --replicas 3 -p 80:80 --name nginx nginx:1.19.0-alpine

参数说明:

  • --replicas:service运行的实例个数
  • --name: 命名service为nginx

现在我们使用浏览器,输入任意节点 IP ,即可看到 nginx 默认页面。

查看服务

使用 docker service ls 来查看当前 Swarm 集群运行的服务。

[root@node1 ~]$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                 PORTS
ryhf4p7z48tc        nginx               replicated          3/3                 nginx:1.19.0-alpine   *:80->80/tcp

使用 docker service ps 来查看某个服务的详情。

[root@node1 ~]$ docker service ps nginx
ID                  NAME                IMAGE                 NODE                DESIRED STATE       CURRENT STATE           ERROR               PORTS
ibgbksjwo1kx        nginx.1             nginx:1.19.0-alpine   node1               Running             Running 3 minutes ago                       
g46e327d7j9g        nginx.2             nginx:1.19.0-alpine   node2               Running             Running 3 minutes ago 
uyxa5ye0061k        nginx.3             nginx:1.19.0-alpine   node3               Running             Running 3 minutes ago

使用 docker service logs 来查看某个服务的日志。

[root@node1 ~]$ docker service logs nginx
nginx.2.g46e327d7j9g@node1    | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx.2.g46e327d7j9g@node1    | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx.2.g46e327d7j9g@node1    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx.2.g46e327d7j9g@node1    | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
nginx.2.g46e327d7j9g@node1    | 10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
nginx.2.g46e327d7j9g@node1    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx.2.g46e327d7j9g@node1    | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx.2.g46e327d7j9g@node1    | 10.0.0.2 - - [28/Jun/2020:07:39:37 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36" "-"
nginx.2.g46e327d7j9g@node1    | 2020/06/28 07:39:37 [error] 29#29: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 10.0.0.2, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "172.16.35.10", referrer: "http://172.16.35.10/"
nginx.2.g46e327d7j9g@node1    | 10.0.0.2 - - [28/Jun/2020:07:39:37 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://172.16.35.10/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36" "-"
nginx.1.ibgbksjwo1kx@node2    | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx.1.ibgbksjwo1kx@node2    | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx.1.ibgbksjwo1kx@node2    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx.1.ibgbksjwo1kx@node2    | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
nginx.1.ibgbksjwo1kx@node2    | 10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
nginx.1.ibgbksjwo1kx@node2    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx.1.ibgbksjwo1kx@node2    | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx.1.ibgbksjwo1kx@node2    | 10.0.0.3 - - [28/Jun/2020:07:39:43 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36" "-"
nginx.1.ibgbksjwo1kx@node2    | 2020/06/28 07:39:43 [error] 29#29: *2 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 10.0.0.3, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "172.16.35.11", referrer: "http://172.16.35.11/"
nginx.1.ibgbksjwo1kx@node2    | 10.0.0.3 - - [28/Jun/2020:07:39:43 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://172.16.35.11/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36" "-"

service缩容与扩容

我们可以使用 docker service scale 对一个服务运行的容器数量进行伸缩。

当业务处于高峰期时,我们需要扩展服务运行的容器数量。

[root@node1 ~]$ docker service scale nginx=5
nginx scaled to 5
overall progress: 5 out of 5 tasks 
1/5: running   [==================================================>] 
2/5: running   [==================================================>] 
3/5: running   [==================================================>] 
4/5: running   [==================================================>] 
5/5: running   [==================================================>] 
verify: Service converged

当业务平稳时,我们需要减少服务运行的容器数量。

[root@node1 ~]$ docker service scale nginx=2
nginx scaled to 2
overall progress: 2 out of 2 tasks 
1/2: running   [==================================================>] 
2/2: running   [==================================================>] 
verify: Service converged

删除service

使用 docker service rm 来从 Swarm 集群移除某个服务。

[root@node1 ~]$ docker service rm nginx
nginx

results matching ""

    No results matching ""