Search code examples
kuberneteskubectl

Pods not found while using kubectl port-forward


I want to forward the ports

kubectl port-forward ...

But for this I need to find out the name of pods, I run the command

kubectl -n main_sp get pods

Getting a list:

NAME                              READY   STATUS    RESTARTS   AGE
main-ms-hc-78469b74c-7lfdh         1/1     Running   0          13h

I'm trying

kubectl port-forward main-ms-hc-78469b74c-7lfdh 8080:80

and I get

Error from server (NotFound): pods "main-ms-hc-78469b74c-7lfdh" not found

What am I doing wrong?


Solution

  • You need to mention the namespace too while using port-forward:

    $ kubectl port-forward -n main_sp main-ms-hc-78469b74c-7lfdh 8080:80
    

    To port-forward a pod:

    $ kubectl port-forward -n <namespace> <pod-name> <local-port>:<target-port>
    

    To port-forward a pod via service name:

    $ kubectl port-forward -n <namespace> svc/<servic-name> <local-port>:<target-port>