一、创建一个服务
1. 查看创建服务命令
[root@localhost ~]# docker service --help Usage: docker service COMMAND Manage services Commands: create Create a new service # 创建新服务 inspect Display detailed information on one or more services # 检查显示一个或多个服务的详细信息 logs Fetch the logs of a service or task # 日志获取服务或任务的日志 ls List services # ls列表服务 ps List the tasks of one or more services # ps列出一个或多个服务的任务 rm Remove one or more services # rm删除一个或多个服务 rollback Revert changes to a service's configuration # 回滚还原对服务配置的更改 scale Scale one or multiple replicated services # 扩展一个或多个复制服务 update Update a service # 更新服务 Run 'docker service COMMAND --help' for more information on a command.
[root@localhost ~]# docker service create --help Usage: docker service create [OPTIONS] IMAGE [COMMAND] [ARG...] # 用法:docker 服务 创建[选项]图像[命令][参数…] Create a new service Options:
-p, --publish port Publish a port as a node port # 将端口发布为节点端口
-u, --user string Username or UID (format: <name|uid>[:<group|gid>]) # 指定用户名
--with-registry-auth Send registry authentication details to swarm agents
-w, --workdir string Working directory inside the container #指定容器内工作目录
2. 创建一个服务
创建的swarm服务启动命令和docker run 差不多
[root@localhost ~]# docker service create -p 8081:80 --name my-nginx nginx ubq32058ujwjrvlp8n7uqk22f overall progress: 1 out of 1 tasks 1/1: running [==================================================>] verify: Service converged
3. 查看启动的docker服务及列表
[root@localhost ~]# docker service ps my-nginx ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS fbtc2jpqfm0g my-nginx.1 nginx:latest localhost.localdomain Running Running 6 minutes ago
[root@localhost ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS ubq32058ujwj my-nginx replicated 1/1 nginx:latest *:8081->80/tcp
4. 查看启动服务的详细信息
[root@localhost ~]# docker service inspect my-nginx [ { "ID": "ubq32058ujwjrvlp8n7uqk22f", "Version": { "Index": 54 }, "CreatedAt": "2021-03-04T02:17:57.18776573Z", "UpdatedAt": "2021-03-04T02:17:57.196303716Z", "Spec": { "Name": "my-nginx", "Labels": {}, "TaskTemplate": { "ContainerSpec": { "Image": "nginx:latest@sha256:f3693fe50d5b1df1ecd315d54813a77afd56b0245a404055a946574deb6b34fc", "Init": false, "StopGracePeriod": 10000000000, "DNSConfig": {}, "Isolation": "default" }, "Resources": { "Limits": {}, "Reservations": {} }, "RestartPolicy": { "Condition": "any", "Delay": 5000000000, "MaxAttempts": 0 }, "Placement": { "Platforms": [ { "Architecture": "amd64", "OS": "linux" }, { "OS": "linux" }, { "OS": "linux" }, { "Architecture": "arm64", "OS": "linux" }, { "Architecture": "386", "OS": "linux" }, { "Architecture": "mips64le", "OS": "linux" }, { "Architecture": "ppc64le", "OS": "linux" }, { "Architecture": "s390x", "OS": "linux" } ] }, "ForceUpdate": 0, "Runtime": "container" }, "Mode": { "Replicated": { "Replicas": 1 # 副本数量 } }, "UpdateConfig": { "Parallelism": 1, "FailureAction": "pause", "Monitor": 5000000000, "MaxFailureRatio": 0, "Order": "stop-first" }, "RollbackConfig": { "Parallelism": 1, "FailureAction": "pause", "Monitor": 5000000000, "MaxFailureRatio": 0, "Order": "stop-first" }, "EndpointSpec": { "Mode": "vip", "Ports": [ { "Protocol": "tcp", "TargetPort": 80, "PublishedPort": 8081, "PublishMode": "ingress" } ] } }, "Endpoint": { "Spec": { "Mode": "vip", "Ports": [ { "Protocol": "tcp", "TargetPort": 80, "PublishedPort": 8081, "PublishMode": "ingress" } ] }, "Ports": [ { "Protocol": "tcp", "TargetPort": 80, "PublishedPort": 8081, "PublishMode": "ingress" } ], "VirtualIPs": [ { "NetworkID": "q24fl2e3wkw7zhdbu2a3trzpz", "Addr": "10.0.0.8/24" } ] } } ]
以上【Mode块】显示,我们的副本数现在只有一个,因为现在就启动了一个服务
5. 查看服务在Swarm集群中哪台服务器上
Manager集群节点状态查看:
[root@localhost ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION 2giiey95hal5fv2rgfmx3souu localhost.localdomain Down Active 20.10.5 65ohc7a6u28qlanpb2tygpd8k localhost.localdomain Ready Active 20.10.5 bbksnbwq0aq96ap3z6s1znk09 * localhost.localdomain Ready Active Reachable 20.10.5 ww14ifp4ki9gyw1gdhvzgbzff localhost.localdomain Ready Active Leader 20.10.5 xttcuf3iu5cvirxwwiydi3808 localhost.localdomain Ready Active Reachable 20.10.5
现在docker-1、docker-3、docker-4 为Manager主节点,docker-2 为worker从节点
docker-1 查看:
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker-2 查看:
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fcdfee3f3953 nginx:latest "/docker-entrypoint.…" 29 minutes ago Up 29 minutes 80/tcp my-nginx.1.fbtc2jpqfm0gld0g7itavm0r1
docker-3 查看:
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker-4 查看:
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
以上显示,服务已经被分配到了docker-2 worker从节点运行
二、弹性,动态扩缩容
方法一:
1. 集群服务扩缩容
[root@localhost ~]# docker service update --help Usage: docker service update [OPTIONS] SERVICE Update a service Options:
-q, --quiet Suppress progress output
--replicas uint Number of tasks # 副本的任务数量
--replicas-max-per-node uint Maximum number of tasks per node (default 0 = unlimited)
[root@localhost ~]# docker service update --replicas 5 my-nginx
可以根据环境需求,进行动态扩缩服务数量,比如:10,50,100,.......... 等
[root@localhost ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS ubq32058ujwj my-nginx replicated 5/5 nginx:latest *:8081->80/tcp
2. 再次查看my-nginx服务详细信息
[root@localhost ~]# docker service inspect my-nginx { "ID": "ubq32058ujwjrvlp8n7uqk22f", "Version": { "Index": 61 }, # 此处副本数已经改成5 "Mode": { "Replicated": { "Replicas": 5 } },
3. 4台服务器上服务分布
docker-1
docker-2
docker-3
docker-4
方法二:
1. 查看命令使用方法
[root@localhost ~]# docker service --help
Usage: docker service COMMAND
Manage services
Commands:
create Create a new service # 创建新服务
inspect Display detailed information on one or more services # 检查显示一个或多个服务的详细信息
logs Fetch the logs of a service or task # 日志获取服务或任务的日志
ls List services # ls列表服务
ps List the tasks of one or more services # ps列出一个或多个服务的任务
rm Remove one or more services # rm删除一个或多个服务
rollback Revert changes to a service's configuration # 回滚还原对服务配置的更改
scale Scale one or multiple replicated services # 扩展一个或多个复制服务
update Update a service # 更新服务
Run 'docker service COMMAND --help' for more information on a command.
[root@localhost ~]# docker service scale --help Usage: docker service scale SERVICE=REPLICAS [SERVICE=REPLICAS...]
2. 动态扩缩容服务
将服务副本数量扩容到10个
[root@localhost ~]# docker service scale my-nginx=10
my-nginx scaled to 10
overall progress: 10 out of 10 tasks
1/10: running
2/10: running
3/10: running
4/10: running
5/10: running
6/10: running
7/10: running
8/10: running
9/10: running
10/10: running
verify: Service converged
3. 查看副本数量
[root@localhost ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS ubq32058ujwj my-nginx replicated 10/10 nginx:latest *:8081->80/tcp
4. 服务器上的副本分布
docker-1
docker-2
docker-3
docker-4
5. 如果想要减少副本数量,执行:
[root@localhost ~]# docker service scale my-nginx=5
my-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@localhost ~]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
ubq32058ujwj my-nginx replicated 5/5 nginx:latest *:8081->80/tcp
结论:
以上两种方式均可实现动态扩缩容,方法二更简单一些!
三、 访问测试
以上得知,服务,在集群任意节点中都可以访问!服务可以有多个副本动态扩缩容实现高可用!
在客户端看来,Swarm集群就是一个整体!
概念:
1. docker run 容器启动!不具有扩缩容功能! 2. docker service 服务!具有扩缩容功能,滚动更新!