如何修改一个已经发布的service的暴露的端口号?
今天要说的这个,其实非常的简单,就是说······
如果你现在已经在swarm集群中,部署了一个service,这个service对外暴露的端口号是8080,怎么将它修改为80端口?
比如,在集群中有个nginx的service,对外提供服务的端口是8080
[root@nccztsjb-node-01 ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS xi0du71dqcou nginx replicated 3/3 172.20.58.152/middleware/nginx:1.21.4 *:8080->80/tcp [root@nccztsjb-node-01 ~]# netstat -ntlp |grep 8080 tcp6 0 0 :::8080 :::* LISTEN 16278/dockerd [root@nccztsjb-node-01 ~]#
现在,要将这个端口改成80端口,也就是说,对外提供服务的端口从8080变成了80了
其实,修改也是非常的简单的,就像下面这样:
docker service update --publish-rm published=8080,target=80 --publish-add published=80,target=80 nginx
执行这个命令,端口更新的过程如下:
[root@nccztsjb-node-01 ~]# docker service update --publish-rm published=8080,target=80 --publish-add published=80,target=80 nginx nginx overall progress: 3 out of 3 tasks 1/3: running [==================================================>] 2/3: running [==================================================>] 3/3: running [==================================================>] verify: Service converged
代表更新成功了,再来看下service现在的对外端口和各个主机上监听的端口
[root@nccztsjb-node-01 ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS 08tg8sy9nb1b nginx replicated 3/3 172.20.58.152/middleware/nginx:1.21.4 *:80->80/tcp [root@nccztsjb-node-01 ~]# netstat -ntlp | grep 80 tcp 0 0 127.0.0.1:6012 0.0.0.0:* LISTEN 18580/sshd: root@pt tcp6 0 0 ::1:6012 :::* LISTEN 18580/sshd: root@pt tcp6 0 0 :::80 :::* LISTEN 16278/dockerd [root@nccztsjb-node-01 ~]#
到这里你看到,service监听的端口已经从8080变成了80了。
这个是怎么实现的,看下更新service的参数,然后你就明白了。
- --publish-rm published=8080,target=80,这个参数是把原来8080到80的映射给删除了
- --publish-add published=80,target=80,这个参数是把80映射到了主机的80
其实,这个端口的调整也是非常的简单的,主要就是对2个参数的灵活的应用
- --publish-rm published=8080,target=80
- --publish-add published=80,target=80
OK,今天的主题就到这里,任何的疑问,欢迎留言······