当前位置: 首页 > article >正文

Docker Swarm集群配置与使用

机器准备

  1. 安装好Linux,关闭防火墙,关闭 selinux,可以只配置1个NAT网卡,可参考:Vmware下安装Rocky Linux9.4

  2. 安装好Docker,可参考:Rocky Linux9下安装Docker和卸载Docker

  3. 使用第一台Linux机器完整克隆得到三台机器。

修改ip地址

分别修改三台机器的ip地址

查看网卡配置文件
[root@localhost ~]# ls /etc/NetworkManager/system-connections/
ens33.nmconnection
​
修改网卡,如果有多张网卡,修改NAT类型的那张网卡
[root@localhost ~]# vi /etc/NetworkManager/system-connections/ens33.nmconnection 

修改ip地址,每台机器的ip要不一样

第一台机器

[ipv4]
address1=192.168.193.151/24,192.168.193.2
dns=114.114.114.114;8.8.8.8;
method=manual

第二台机器

[ipv4]
address1=192.168.193.152/24,192.168.193.2
dns=114.114.114.114;8.8.8.8;
method=manual

第三台机器

[ipv4]
address1=192.168.193.153/24,192.168.193.2
dns=114.114.114.114;8.8.8.8;
method=manual

修改主机名称

第一台机器

[root@localhost ~]# hostnamectl set-hostname swarm-manager01

第二台机器

[root@localhost ~]# hostnamectl set-hostname swarm-worker01

第三台机器

[root@localhost ~]# hostnamectl set-hostname swarm-worker02

修改/etc/hosts映射ip和主机名

三台机器均执行

[root@localhost ~]# vi /etc/hosts

三台机器均添加如下内容

192.168.193.151 swarm-manager01 m1
192.168.193.152 swarm-worker01 w1
192.168.193.153 swarm-worker02 w2

三台机器均重启机器生效

[root@localhost ~]# reboot

 

集群规划

集群机器swarm-manager01swarm-worker01swarm-worker02
集群角色managerworkerworker

 

Docker Swarm集群配置

初始化manger

[root@swarm-manager01 ~]# docker swarm init
Swarm initialized: current node (rcmlknoggnz81dfd5b5k778o7) is now a manager.
​
To add a worker to this swarm, run the following command:
​
    docker swarm join --token SWMTKN-1-3do5k5ownszjyyql3nznnmndft3ws7bzclhb4hwj2h8v3s325j-0i8z9k57p7hk141srhemsci4e 192.168.193.151:2377
​
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
​
[root@swarm-manager01 ~]#

根据输出日志提示,找到如下内容,并复制添加worker的命令:

添加worker命令如下:
docker swarm join --token SWMTKN-1-3do5k5ownszjyyql3nznnmndft3ws7bzclhb4hwj2h8v3s325j-0i8z9k57p7hk141srhemsci4e 192.168.193.151:2377
注意:--token后面的值每个人的会不一样,一定要复制自己执行命令输出日志中的docker swarm join命令。
​
添加manager命令如下:
docker swarm join-token manager

添加worker

在worker角色的机器,执行上一步从日志中复制得到的添加worker命令

swarm-worker01机器

[root@swarm-worker01 ~]# docker swarm join --token SWMTKN-1-3do5k5ownszjyyql3nznnmndft3ws7bzclhb4hwj2h8v3s325j-0i8z9k57p7hk141srhemsci4e 192.168.193.151:2377
This node joined a swarm as a worker.

swarm-worker02机器

[root@swarm-worker02 ~]# docker swarm join --token SWMTKN-1-3do5k5ownszjyyql3nznnmndft3ws7bzclhb4hwj2h8v3s325j-0i8z9k57p7hk141srhemsci4e 192.168.193.151:2377
This node joined a swarm as a worker.

提示:

如果找不到token,使用如下命令查看token

[root@swarm-manager01 ~]# docker swarm join-token worker
To add a worker to this swarm, run the following command:
​
 docker swarm join --token SWMTKN-1-3do5k5ownszjyyql3nznnmndft3ws7bzclhb4hwj2h8v3s325j-0i8z9k57p7hk141srhemsci4e 192.168.193.151:2377

验证信息​

docker info看到Swarm: active及Swarm相关信息为正常

[root@swarm-manager01 ~]# docker info
Client: Docker Engine - Community
 Version:    27.3.1
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.17.1
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.29.7
    Path:     /usr/libexec/docker/cli-plugins/docker-compose
​
Server:
 Containers: 1
  Running: 0
  Paused: 0
  Stopped: 1
 Images: 1
 Server Version: 27.3.1
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: active
  NodeID: rcmlknoggnz81dfd5b5k778o7
  Is Manager: true
  ClusterID: h5pz39obu2gnufvdkm4masczp
  Managers: 1
  Nodes: 3
  Data Path Port: 4789
  Orchestration:
   Task History Retention Limit: 5
  Raft:
   Snapshot Interval: 10000
   Number of Old Snapshots to Retain: 0
   Heartbeat Tick: 1
   Election Tick: 10
  Dispatcher:
   Heartbeat Period: 5 seconds
  CA Configuration:
   Expiry Duration: 3 months
   Force Rotate: 0
  Autolock Managers: false
  Root Rotation In Progress: false
  Node Address: 192.168.193.151
  Manager Addresses:
   192.168.193.151:2377
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7f7fdf5fed64eb6a7caf99b3e12efcf9d60e311c
 runc version: v1.1.14-0-g2c9f560
 init version: de40ad0
 Security Options:
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 5.14.0-427.37.1.el9_4.x86_64
 Operating System: Rocky Linux 9.4 (Blue Onyx)
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 3.543GiB
 Name: swarm-manager01
 ID: dbd0ed83-4c48-4b00-9892-951baaed7707
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://public.ecr.aws/
  https://dockerhub.timeweb.cloud/
  https://docker.m.daocloud.io/
  https://kfp63jaj.mirror.aliyuncs.com/
  https://docker.mirrors.ustc.edu.cn/
  https://mirror.iscas.ac.cn/
  https://docker.registry.cyou/
  https://docker-cf.registry.cyou/
  https://dockercf.jsdelivr.fyi/
  https://docker.jsdelivr.fyi/
  https://dockertest.jsdelivr.fyi/
  https://mirror.aliyuncs.com/
  https://dockerproxy.com/
  https://mirror.baidubce.com/
  https://docker.nju.edu.cn/
  https://docker.mirrors.sjtug.sjtu.edu.cn/
  https://docker.rainbond.cc/
 Live Restore Enabled: false
​
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
[root@swarm-manager01 ~]# 

上面docker info命令输出内容较多,可以用如下命令过滤出swarm相关信息,如下

[root@swarm-manager01 ~]# docker info -f '{{.Swarm}}'
{rcmlknoggnz81dfd5b5k778o7 192.168.193.151 active true  [{rcmlknoggnz81dfd5b5k778o7 192.168.193.151:2377}] 3 1 0xc0003989a0 []}
​

查看节点信息

[root@swarm-manager01 ~]# docker node ls
ID                            HOSTNAME          STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
rcmlknoggnz81dfd5b5k778o7 *   swarm-manager01   Ready     Active         Leader           27.3.1
cl5d8lcbsr63gr6tc4k0z7wg0     swarm-worker01    Ready     Active                          27.3.1
vbsdoepbteh83v2lq58jbdraa     swarm-worker02    Ready     Active                          27.3.1
​

看到AVAILABILITY均为Active,其中swarm-manager01机器是Leader

节点下线

[root@swarm-manager01 ~]# docker node update --availability drain swarm-worker01
swarm-worker01
[root@swarm-manager01 ~]# docker node ls
ID                            HOSTNAME          STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
rcmlknoggnz81dfd5b5k778o7 *   swarm-manager01   Ready     Active         Leader           27.3.1
cl5d8lcbsr63gr6tc4k0z7wg0     swarm-worker01    Ready     Drain                           27.3.1
vbsdoepbteh83v2lq58jbdraa     swarm-worker02    Ready     Active                          27.3.1
​

节点上线

[root@swarm-manager01 ~]# docker node update --availability active swarm-worker01
swarm-worker01
[root@swarm-manager01 ~]# docker node ls
ID                            HOSTNAME          STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
rcmlknoggnz81dfd5b5k778o7 *   swarm-manager01   Ready     Active         Leader           27.3.1
cl5d8lcbsr63gr6tc4k0z7wg0     swarm-worker01    Ready     Active                          27.3.1
vbsdoepbteh83v2lq58jbdraa     swarm-worker02    Ready     Active                          27.3.1
​

删除worker节点

[root@swarm-manager01 ~]# docker node rm -f swarm-worker02
swarm-worker02
[root@swarm-manager01 ~]# docker node ls
ID                            HOSTNAME          STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
rcmlknoggnz81dfd5b5k778o7 *   swarm-manager01   Ready     Active         Leader           27.3.1
cl5d8lcbsr63gr6tc4k0z7wg0     swarm-worker01    Ready     Active                          27.3.1
​

再次加入worker节点

查看token
[root@swarm-manager01 ~]# docker swarm join-token worker
To add a worker to this swarm, run the following command:
​
    docker swarm join --token SWMTKN-1-3do5k5ownszjyyql3nznnmndft3ws7bzclhb4hwj2h8v3s325j-0i8z9k57p7hk141srhemsci4e 192.168.193.151:2377
​
[root@swarm-manager01 ~]#
​
在需要加入的worker节点运行如下命令
[root@swarm-worker02 ~]# docker swarm join --token SWMTKN-1-3do5k5ownszjyyql3nznnmndft3ws7bzclhb4hwj2h8v3s325j-0i8z9k57p7hk141srhemsci4e 192.168.193.151:2377
Error response from daemon: This node is already part of a swarm. Use "docker swarm leave" to leave this swarm and join another one.
​
先执行自动离开
[root@swarm-worker02 ~]# docker swarm leave
Node left the swarm.
​
重新加入集群
[root@swarm-worker02 ~]# docker swarm join --token SWMTKN-1-3do5k5ownszjyyql3nznnmndft3ws7bzclhb4hwj2h8v3s325j-0i8z9k57p7hk141srhemsci4e 192.168.193.151:2377
This node joined a swarm as a worker.
​
​
在manager主机执行
[root@swarm-manager01 ~]# docker node ls
ID                            HOSTNAME          STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
rcmlknoggnz81dfd5b5k778o7 *   swarm-manager01   Ready     Active         Leader           27.3.1
cl5d8lcbsr63gr6tc4k0z7wg0     swarm-worker01    Ready     Active                          27.3.1
ywp8ooinwiua72af3psojepjr     swarm-worker02    Ready     Active                          27.3.1
​
​
​

将 worker 提升为 manager

docker node promote <主机或ID>

操作

[root@swarm-manager01 ~]# docker node ls
ID                            HOSTNAME          STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
rcmlknoggnz81dfd5b5k778o7 *   swarm-manager01   Ready     Active         Leader           27.3.1
cl5d8lcbsr63gr6tc4k0z7wg0     swarm-worker01    Ready     Active                          27.3.1
ywp8ooinwiua72af3psojepjr     swarm-worker02    Ready     Active                          27.3.1
​
[root@swarm-manager01 ~]# docker node promote swarm-worker01
Node swarm-worker01 promoted to a manager in the swarm.
​
[root@swarm-manager01 ~]# docker node ls
ID                            HOSTNAME          STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
rcmlknoggnz81dfd5b5k778o7 *   swarm-manager01   Ready     Active         Leader           27.3.1
cl5d8lcbsr63gr6tc4k0z7wg0     swarm-worker01    Ready     Active         Reachable        27.3.1
ywp8ooinwiua72af3psojepjr     swarm-worker02    Ready     Active                          27.3.1
​
[root@swarm-manager01 ~]# docker node promote ywp8ooinwiua72af3psojepjr
Node ywp8ooinwiua72af3psojepjr promoted to a manager in the swarm.
​
[root@swarm-manager01 ~]# docker node ls
ID                            HOSTNAME          STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
rcmlknoggnz81dfd5b5k778o7 *   swarm-manager01   Ready     Active         Leader           27.3.1
cl5d8lcbsr63gr6tc4k0z7wg0     swarm-worker01    Ready     Active         Reachable        27.3.1
ywp8ooinwiua72af3psojepjr     swarm-worker02    Ready     Active         Reachable        27.3.1
​

当Leader的Manager出故障时,Reachable的主机就通过一定机制(Raft算法)当选为Leader

Leader机器关机
[root@swarm-manager01 ~]# init 0
​
worker01机器执行命令,看到worker01变为了Leader
[root@swarm-worker01 ~]# docker node ls
ID                            HOSTNAME          STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
rcmlknoggnz81dfd5b5k778o7     swarm-manager01   Unknown   Active         Unreachable      27.3.1
cl5d8lcbsr63gr6tc4k0z7wg0 *   swarm-worker01    Unknown   Active         Leader           27.3.1
ywp8ooinwiua72af3psojepjr     swarm-worker02    Unknown   Active         Reachable        27.3.1
​
当manager01再次开机,查看节点状态
[root@swarm-manager01 ~]# docker node ls
ID                            HOSTNAME          STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
rcmlknoggnz81dfd5b5k778o7 *   swarm-manager01   Ready     Active         Reachable        27.3.1
cl5d8lcbsr63gr6tc4k0z7wg0     swarm-worker01    Ready     Active         Leader           27.3.1
ywp8ooinwiua72af3psojepjr     swarm-worker02    Ready     Active         Reachable        27.3.1
​

Manager节点降级为worker

docker node demote <主机或ID>

操作过程

[root@swarm-manager01 ~]# docker node ls
ID                            HOSTNAME          STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
rcmlknoggnz81dfd5b5k778o7 *   swarm-manager01   Ready     Active         Reachable        27.3.1
cl5d8lcbsr63gr6tc4k0z7wg0     swarm-worker01    Ready     Active         Leader           27.3.1
ywp8ooinwiua72af3psojepjr     swarm-worker02    Ready     Active         Reachable        27.3.1
​
​
[root@swarm-manager01 ~]# docker node demote swarm-worker01
Manager swarm-worker01 demoted in the swarm.
​
[root@swarm-manager01 ~]# docker node ls
ID                            HOSTNAME          STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
rcmlknoggnz81dfd5b5k778o7 *   swarm-manager01   Ready     Active         Leader           27.3.1
cl5d8lcbsr63gr6tc4k0z7wg0     swarm-worker01    Ready     Active                          27.3.1
ywp8ooinwiua72af3psojepjr     swarm-worker02    Ready     Active         Reachable        27.3.1
​
[root@swarm-manager01 ~]# docker node demote swarm-worker02
Manager swarm-worker02 demoted in the swarm.
​
[root@swarm-manager01 ~]# docker node ls
ID                            HOSTNAME          STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
rcmlknoggnz81dfd5b5k778o7 *   swarm-manager01   Ready     Active         Leader           27.3.1
cl5d8lcbsr63gr6tc4k0z7wg0     swarm-worker01    Ready     Active                          27.3.1
ywp8ooinwiua72af3psojepjr     swarm-worker02    Ready     Active                          27.3.1
​

Docker Swarm集群的使用

部署服务

部署服务
[root@swarm-manager01 ~]# docker service create --replicas 1 --name nginxtest -p 80:80 nginx 
16usw3u7rpzz2n8trlx139k1k
overall progress: 0 out of 1 tasks 
overall progress: 0 out of 1 tasks 
overall progress: 0 out of 1 tasks 
overall progress: 1 out of 1 tasks 
1/1: running   [==================================================>] 
verify: Service 16usw3u7rpzz2n8trlx139k1k converged
​
查看镜像,只有1台机器有nginx镜像
[root@swarm-manager01 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    d2c94e258dcb   17 months ago   13.3kB
​
[root@swarm-worker01 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
nginx         <none>    7f553e8bbc89   4 days ago      192MB
hello-world   latest    d2c94e258dcb   17 months ago   13.3kB
​
[root@swarm-worker02 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    d2c94e258dcb   17 months ago   13.3kB
​
​

查看服务列表

在manager机器执行如下命令
[root@swarm-manager01 ~]# docker service ls
ID             NAME        MODE         REPLICAS   IMAGE          PORTS
16usw3u7rpzz   nginxtest   replicated   1/1        nginx:latest   *:80->80/tcp
​

访问服务

使用swarm集群任意一台机器的主机名:端口,都可以访问到服务内容

​
[root@swarm-manager01 ~]# curl swarm-manager01:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
​
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
​
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
​
[root@swarm-manager01 ~]# curl swarm-worker01:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
​
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
​
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@swarm-manager01 ~]# curl swarm-worker02:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
​
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
​
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@swarm-manager01 ~]# curl nginxtest:80
curl: (6) Could not resolve host: nginxtest
[root@swarm-manager01 ~]# 
​

显示详细信息

docker service inspect --pretty <service-id || service-name> 
[root@swarm-manager01 ~]# docker service inspect --pretty nginxtest
​
ID:     16usw3u7rpzz2n8trlx139k1k
Name:       nginxtest
Service Mode:   Replicated
 Replicas:  1
Placement:
UpdateConfig:
 Parallelism:   1
 On failure:    pause
 Monitoring Period: 5s
 Max failure ratio: 0
 Update order:      stop-first
RollbackConfig:
 Parallelism:   1
 On failure:    pause
 Monitoring Period: 5s
 Max failure ratio: 0
 Rollback order:    stop-first
ContainerSpec:
 Image:     nginx:latest@sha256:d2eb56950b84efe34f966a2b92efb1a1a2ea53e7e93b94cdf45a27cf3cd47fc0
 Init:      false
Resources:
Endpoint Mode:  vip
Ports:
 PublishedPort = 80
  Protocol = tcp
  TargetPort = 80
  PublishMode = ingress 
​
[root@swarm-manager01 ~]# 
​

查看哪些节点正在运行服务

docker service ps <service-id || service-name> 
​
[root@swarm-manager01 ~]# docker service ps nginxtest
ID             NAME              IMAGE          NODE              DESIRED STATE   CURRENT STATE             ERROR                              PORTS
8386rrtz7333   nginxtest.1       nginx:latest   swarm-worker01    Running         Running 12 minutes ago   
lm8o4xs3wbag    \_ nginxtest.1   nginx:latest   swarm-manager01   Shutdown        Rejected 13 minutes ago   "No such image: nginx:latest@s…"   
ogm99z0w9q40    \_ nginxtest.1   nginx:latest   swarm-worker01    Shutdown        Shutdown 12 minutes ago  
​
​

服务扩容

​
服务扩容
[root@swarm-manager01 ~]# docker service scale nginxtest=3
nginxtest scaled to 3
overall progress: 3 out of 3 tasks 
1/3: running   [==================================================>] 
2/3: running   [==================================================>] 
3/3: running   [==================================================>] 
verify: Service nginxtest converged 
​
查看进程
[root@swarm-manager01 ~]# docker service ps nginxtest
ID             NAME              IMAGE          NODE              DESIRED STATE   CURRENT STATE                ERROR                              PORTS
8386rrtz7333   nginxtest.1       nginx:latest   swarm-worker01    Running         Running 16 minutes ago  
lm8o4xs3wbag    \_ nginxtest.1   nginx:latest   swarm-manager01   Shutdown        Rejected 16 minutes ago      "No such image: nginx:latest@s…"   
ogm99z0w9q40    \_ nginxtest.1   nginx:latest   swarm-worker01    Shutdown        Shutdown 16 minutes ago   
qsfgvj08ztfp   nginxtest.2       nginx:latest   swarm-worker02    Running         Running 35 seconds ago  
sa1wnrlun0v0   nginxtest.3       nginx:latest   swarm-manager01   Running         Running about a minute ago   
​
​
查看镜像,三台机器都有nginx镜像
[root@swarm-manager01 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
nginx         <none>    7f553e8bbc89   4 days ago      192MB
hello-world   latest    d2c94e258dcb   17 months ago   13.3kB
​
[root@swarm-worker01 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
nginx         <none>    7f553e8bbc89   4 days ago      192MB
hello-world   latest    d2c94e258dcb   17 months ago   13.3kB
​
[root@swarm-worker02 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
nginx         <none>    7f553e8bbc89   4 days ago      192MB
hello-world   latest    d2c94e258dcb   17 months ago   13.3kB
​
访问测试,访问curl m1:80   curl w1:80  curl w2:80 均可访问到
[root@swarm-worker02 ~]# curl w2:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
​
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
​
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
​

服务缩容

[root@swarm-manager01 ~]# docker service scale nginxtest=1
nginxtest scaled to 1
overall progress: 1 out of 1 tasks 
1/1: running   [==================================================>] 
verify: Service nginxtest converged 
​
[root@swarm-manager01 ~]# docker service ps nginxtest
ID             NAME              IMAGE          NODE              DESIRED STATE   CURRENT STATE             ERROR                              PORTS
8386rrtz7333   nginxtest.1       nginx:latest   swarm-worker01    Running         Running 23 minutes ago   
lm8o4xs3wbag    \_ nginxtest.1   nginx:latest   swarm-manager01   Shutdown        Rejected 23 minutes ago   "No such image: nginx:latest@s…"   
ogm99z0w9q40    \_ nginxtest.1   nginx:latest   swarm-worker01    Shutdown        Shutdown 23 minutes ago  
​

删除服务

docker service rm <service-id || service-name>
​
[root@swarm-manager01 ~]# docker service rm nginxtest
nginxtest
[root@swarm-manager01 ~]# docker service ls
ID        NAME      MODE      REPLICAS   IMAGE     PORTS
​
​
​

删除不需要的镜像

# 缩容和删除服务,均不会删除下载的镜像
[root@swarm-manager01 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
nginx         <none>    7f553e8bbc89   4 days ago      192MB
hello-world   latest    d2c94e258dcb   17 months ago   13.3kB
​
​
[root@swarm-manager01 ~]# docker rmi 7f5
[root@swarm-worker01 ~]# docker rmi 7f5
[root@swarm-worker02 ~]# docker rmi 7f5

滚动更新服务

部署一个nginx服务,版本号为1.26.2,然后升级版本为1.27.2。

部署一个nginx:1.26.2服务
[root@swarm-manager01 ~]# docker service create --replicas 3 --name nginxtestupd --update-delay 10s -p 80:80 nginx:1.26.2
79s8sd9pv1pqpqxk0tk7w7rvs
overall progress: 3 out of 3 tasks 
1/3: running   [==================================================>] 
2/3: running   [==================================================>] 
3/3: running   [==================================================>] 
verify: Service 79s8sd9pv1pqpqxk0tk7w7rvs converged 
​
该标志配置对服务任务或任务集的更新之间的时间延迟。你可以将时间描述为秒数、分钟数或小时数的组合   --update-delay   
​
[root@swarm-manager01 ~]# docker service inspect --pretty nginxtestupd
​
ID:     79s8sd9pv1pqpqxk0tk7w7rvs
Name:       nginxtestupd
Service Mode:   Replicated
 Replicas:  3
Placement:
UpdateConfig:
 Parallelism:   1
 Delay:     10s
 On failure:    pause
 Monitoring Period: 5s
 Max failure ratio: 0
 Update order:      stop-first
RollbackConfig:
 Parallelism:   1
 On failure:    pause
 Monitoring Period: 5s
 Max failure ratio: 0
 Rollback order:    stop-first
ContainerSpec:
 Image:     nginx:1.26.2@sha256:e489771d6058fca1d9bd57757b0136383c454989a868c62cded241ae1513e47f
 Init:      false
Resources:
Endpoint Mode:  vip
Ports:
 PublishedPort = 80
  Protocol = tcp
  TargetPort = 80
  PublishMode = ingress 
​
[root@swarm-manager01 ~]#

滚动更新镜像

docker service update --image nginx:1.27.2 nginxtestupd
​
[root@swarm-manager01 ~]# docker service update --image nginx:1.27.2 nginxtestupd
nginxtestupd
overall progress: 3 out of 3 tasks 
1/3: running   [==================================================>] 
2/3: running   [==================================================>] 
3/3: running   [==================================================>] 
verify: Service nginxtestupd converged 
​
[root@swarm-manager01 ~]# docker service inspect --pretty nginxtestupd
​
ID:     79s8sd9pv1pqpqxk0tk7w7rvs
Name:       nginxtestupd
Service Mode:   Replicated
 Replicas:  3
UpdateStatus:
 State:     completed
 Started:   About a minute ago
 Completed: 5 seconds ago
 Message:   update completed
Placement:
UpdateConfig:
 Parallelism:   1
 Delay:     10s
 On failure:    pause
 Monitoring Period: 5s
 Max failure ratio: 0
 Update order:      stop-first
RollbackConfig:
 Parallelism:   1
 On failure:    pause
 Monitoring Period: 5s
 Max failure ratio: 0
 Rollback order:    stop-first
ContainerSpec:
 Image:     nginx:1.27.2@sha256:d2eb56950b84efe34f966a2b92efb1a1a2ea53e7e93b94cdf45a27cf3cd47fc0
 Init:      false
Resources:
Endpoint Mode:  vip
Ports:
 PublishedPort = 80
  Protocol = tcp
  TargetPort = 80
  PublishMode = ingress 
​
[root@swarm-manager01 ~]# 
​
删除服务
[root@swarm-manager01 ~]# docker service rm nginxtestupd
nginxtestupd
[root@swarm-manager01 ~]# docker service ls
ID        NAME      MODE      REPLICAS   IMAGE     PORTS
​
​

完成!enjoy it!


http://www.kler.cn/a/378696.html

相关文章:

  • RV1126-SDK学习之OSD实现原理
  • PHP常量
  • 原来大佬的测试用例都是这样写的...
  • 【Python】数据清洗与特征工程:使用Python的Feature-engine库
  • 操作系统(10) (并发(2)------基于软件/硬件/操作系统层面解决两个进程之间的临界区问题/抢占式/非抢占式内核)
  • ChatGPT 和 RAG(检索增强生成)的区别;ChatGPT 和 RAG 的联系
  • 基于知识中台的智能法律咨询服务:革新法律服务的新篇章
  • sicp每日一题[2.65]
  • 【D3.js in Action 3 精译_039】4.3 D3 面积图的绘制方法及其边界标签的添加
  • RTP和RTCP的详细介绍及其C代码示例
  • UG NX二次开发(C#)-UFun-创建草图和草图曲线
  • Redis设计与实现 学习笔记 第十四章 服务器
  • RSTP的工作过程
  • CentOS 9 Stream 上安装 Redis
  • 从事人工智能相关岗位需要具备哪些技能?
  • 交叉编译工具链命名规则、以及如何生成交叉编译工具链步骤
  • bash: git: command not found
  • SpringBoot源码(四):run() 方法解析(一)
  • 微服务架构面试内容整理-微服务与传统单体架构的区别
  • 在麒麟V10上下载pycharm
  • Pinctrl子系统中client端设备树相关数据结构介绍和解析
  • 【双目视觉标定】——1原理与实践
  • XSS跨站脚本攻击的实现原理及讲解
  • 第三百零八节 Log4j教程 - Log4j日志到数据库
  • 江协科技STM32学习- P35 硬件I2C读写MPU6050
  • NFTScan Site:以蓝标认证与高级项目管理功能赋能 NFT 项目