释放占用端口

目录

1. 找到系统当前所有的端口

2. 找到对应端口在系统中的进程 ID(PID)

3. 使用 kill -9 [PID] 命令结束进程


1. 找到系统当前所有的端口

使用 netstat 命令查找本机各端口的网络连接情况

~]netstat -nulpt

#结果如下
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:1997            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN
tcp6       0      0 :::80                   :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 ::1:25                  :::*                    LISTEN
tcp6       0      0 :::3306                 :::*                    LISTEN

这里我们要找的是 80与443 端口

2. 找到对应端口在系统中的进程 ID(PID)

依据查找到的 1997 端口找到对应进程, lsof -i :1997, 注意 : 冒号不要漏掉了

lsof -i:80

#结果如下
COMMAND    PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
tke-gatew 6360 root   10u  IPv6 6410452      0t0  TCP *:http (LISTEN)
[root@VM-0-114-centos ~]# lsof -i:443
COMMAND     PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
tke-gatew  7627 root    9u  IPv6 7717644      0t0  TCP *:https (LISTEN)
.........
->172.80.31.2:https (ESTABLISHED)
galaxy    25638 root    8u  IPv4 6276795      0t0  TCP  <机密省略>:57802-
..........

3. 使用 kill -9 [PID] 命令结束进程

通过 lsof 命令我们找到了进程的 PID: 29416,接下来就是使用 kill -9 [PID] 把进程结束就好了

kill -9 6360
kill -9 25638
kill -9 7627

到这里就 OK 了,不过为了保险起见,再次执行 netstat -tln 确认是否结束了端口占用

 前情提要

在布置k8s的时候发现容器报错如

~]kubectl get pods -A -o wide
.....
pot   nginx-ingress-nginx-ingress-85d747cfb4-9hk72   0/1   CrashLoopBackOff
.....

然后想describe查看了一下报错如下

~]kubectl describe pod -n pot nginx-ingress-nginx-ingress-85d747cfb4-9hk72
.........
 Warning  BackOff  3m14s (x613 over 137m)  kubelet  Back-off restarting failed container

最后logs查看发现端口被占用

~]# kubectl logs pods -n pot nginx-ingress-nginx-ingress-85d747cfb4-9hk72
Error from server (NotFound): pods "pods" not found
[root@VM-0-114-centos ~]# kubectl logs  -n pot nginx-ingress-nginx-ingress-85d747cfb4-9hk72
I1104 10:51:46.738108       1 main.go:169] Starting NGINX Ingress controller Version=1.6.3 GitCommit=b9378d56
2021/11/04 10:51:46 [emerg] 19#19: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2021/11/04 10:51:46 [emerg] 19#19: bind() to 0.0.0.0:443 failed (98: Address already in use)
.......
2021/11/04 10:51:46 [notice] 19#19: try again to bind() after 500ms
2021/11/04 10:51:46 [emerg] 19#19: still could not bind()
nginx: [emerg] still could not bind()

运用了以上步骤之后回复正常

pot   nginx-ingress-nginx-ingress-85d747cfb4-9hk72   1/1     Running

你可能感兴趣的:(小技巧,网络,tcp/ip,网络协议)