Search code examples
kuberneteskubectl

I can find the service I want in k8, but I don't know its pod name, how can I find out the pod name of the service?


I can get the service using k get svc, but I don't know which pod it lives, I can't tell it from the pod names I have, how can I find out the related pod of my service?


Solution

  • When you define a service you also define the selector for the particular service.

    apiVersion: v1
    kind: Service
    metadata:
      name: my-service
    spec:
      selector:
        app: MyApp
      ports:
        - protocol: TCP
          port: 80
          targetPort: 9376
    

    In this example my-service defines selector as app:MyApp
    This service will look for a pod with the same label selector. The controller for the Service selector continuously scans for Pods that match its selector, and then POSTs any updates to an Endpoint object also named “my-service”. You can get the list of Pods this service is routing traffic to
    kubectl get po -l app:MyApp
    If you are not getting any pod with this command then its obvious that the service is not able to find any pod that contains the label selector. And yes services can exist without Pods as well.See doc