kubernetes runtime_error ai_generated true

E: Unable to listen on port 8080: Listeners failed to create with the following errors: [unable to create listener: Error listen tcp4 127.0.0.1:8080: bind: address already in use]

ID: kubernetes/port-forward-connection-refused

Also available as: JSON · Markdown · 中文
92%Fix Rate
88%Confidence
1Evidence
2023-03-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
kubernetes 1.25 active
kubectl 1.26 active
kubectl 1.27 active

Root Cause

Local port (e.g., 8080) is already occupied by another process, preventing kubectl port-forward from binding.

generic

中文

本地端口(例如 8080)已被其他进程占用,阻止了 kubectl port-forward 的绑定。

Official Documentation

https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/

Workarounds

  1. 95% success Identify the process using the port (e.g., 8080) and terminate it: `lsof -i :8080` then `kill -9 <PID>`. Alternatively, use `fuser -k 8080/tcp`.
    Identify the process using the port (e.g., 8080) and terminate it: `lsof -i :8080` then `kill -9 <PID>`. Alternatively, use `fuser -k 8080/tcp`.
  2. 90% success Use a different local port that is not in use: `kubectl port-forward pod/my-pod 9090:80` (maps local 9090 to pod's 80). Verify with `netstat -tuln | grep 9090`.
    Use a different local port that is not in use: `kubectl port-forward pod/my-pod 9090:80` (maps local 9090 to pod's 80). Verify with `netstat -tuln | grep 9090`.

中文步骤

  1. 识别占用端口(例如 8080)的进程并终止:`lsof -i :8080` 然后 `kill -9 <PID>`。或者使用 `fuser -k 8080/tcp`。
  2. 使用未被占用的不同本地端口:`kubectl port-forward pod/my-pod 9090:80`(将本地 9090 映射到 pod 的 80)。使用 `netstat -tuln | grep 9090` 验证。

Dead Ends

Common approaches that don't work:

  1. 95% fail

    Restarting the command doesn't release the port; the local process still holds it.

  2. 30% fail

    If the new port is also in use, the same error occurs. Requires manual verification.

  3. 90% fail

    Elevated privileges don't free the port; the underlying socket is still bound.