Published on

Working Out Which Kubernetes Pod Ports Are Clashing


Today a pod on one of our clusters was stuck in a crashloop back off. When looking at the logs for the pod we got an error similar to Cannot use port 80, port already in use. Ok cool so it was clear why it was failing but which other pod was trying to use the same port?

Googling this did not return any useful results. I then remembered if I get pods and output them as yamls I can then see which containerPort they were using. The following query does exactly that for a given namespace:

kubectl get pods -n yourNamespace -o yaml | grep -i containerport

Which outputs similar to the below:

      - containerPort: 8080
      - containerPort: 8080
      - containerPort: 443
      - containerPort: 18080

With a bit of tweaking a script can be written for this to output which ports are for which pod. If you first ran this command without including the last -o yaml you can easily see what order the pods are output in and tie that back to the ports in the second command.