How to access pods without services in Kubernetes

后端 未结 4 1876
攒了一身酷
攒了一身酷 2021-01-02 12:20

I was wondering how pods are accessed when no service is defined for that specific pod. If it\'s through the environment variables, how does the cluster retrieve these?

4条回答
  •  迷失自我
    2021-01-02 13:07

    Just for debugging purposes, you can forward a port from your machine to one in the pod:

    kubectl port-forward POD_NAME HOST_PORT:POD_PORT
    

    If you have to access it from anywhere, you should use services, but you got to have a deployment created

    Create deployment

    kubectl create -f https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/service/networking/run-my-nginx.yaml
    

    Expose the deployment with a NodePort service

    kubectl expose deployment deployment/my-nginx --type=NodePort --name=nginx-service
    

    Then list the services and get the port of the service

    kubectl get services | grep nginx-service
    

提交回复
热议问题