• 部署服务
    • 新建服务
    • 查看服务
    • 删除服务

    部署服务

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

    新建服务

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

    1. $ docker service create --replicas 3 -p 80:80 --name nginx nginx:1.13.7-alpine

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

    查看服务

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

    1. $ docker service ls
    2. ID NAME MODE REPLICAS IMAGE PORTS
    3. kc57xffvhul5 nginx replicated 3/3 nginx:1.13.7-alpine *:80->80/tcp

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

    1. $ docker service ps nginx
    2. ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
    3. pjfzd39buzlt nginx.1 nginx:1.13.7-alpine swarm2 Running Running about a minute ago
    4. hy9eeivdxlaa nginx.2 nginx:1.13.7-alpine swarm1 Running Running about a minute ago
    5. 36wmpiv7gmfo nginx.3 nginx:1.13.7-alpine swarm3 Running Running about a minute ago

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

    1. $ docker service logs nginx
    2. nginx.3.36wmpiv7gmfo@swarm3 | 10.255.0.4 - - [25/Nov/2017:02:10:30 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:58.0) Gecko/20100101 Firefox/58.0" "-"
    3. nginx.3.36wmpiv7gmfo@swarm3 | 10.255.0.4 - - [25/Nov/2017:02:10:30 +0000] "GET /favicon.ico HTTP/1.1" 404 169 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:58.0) Gecko/20100101 Firefox/58.0" "-"
    4. nginx.3.36wmpiv7gmfo@swarm3 | 2017/11/25 02:10:30 [error] 5#5: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 10.255.0.4, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.99.102"
    5. nginx.1.pjfzd39buzlt@swarm2 | 10.255.0.2 - - [25/Nov/2017:02:10:26 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:58.0) Gecko/20100101 Firefox/58.0" "-"
    6. nginx.1.pjfzd39buzlt@swarm2 | 10.255.0.2 - - [25/Nov/2017:02:10:27 +0000] "GET /favicon.ico HTTP/1.1" 404 169 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:58.0) Gecko/20100101 Firefox/58.0" "-"
    7. nginx.1.pjfzd39buzlt@swarm2 | 2017/11/25 02:10:27 [error] 5#5: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 10.255.0.2, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.99.101"

    删除服务

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

    1. $ docker service rm nginx