Is there a command to check which pods have a service applied

6/30/2020

I have made a service.yaml and have created the service.

kind: Service
apiVersion: v1
metadata:
  name: cass-operator-service
spec:
  type: LoadBalancer
  ports:
    - port: 9042
      targetPort: 9042
  selector:
    name: cass-operator

Is there a way to check on which pods the service has been applied?

I want that using the above service, I connect to a cluster in Google Cloud running Kubernetes/Cassandra on external_ip/port (9042 port). But using the above service, I am not able to.

kubectl get svc shows

NAME                    TYPE           CLUSTER-IP     EXTERNAL-IP     PORT(S)          AGE
cass-operator-service   LoadBalancer   10.51.247.82   34.91.214.233   9042:31902/TCP   73s

So probably the service is listening on 9042 but is forwarding to pods in 31902. I want both ports to be 9042. Is it possible to do?

-- Manu Chadha
kubernetes

2 Answers

6/30/2020

You can get pods by querying with selector like following

kubectl get pods -l name=cass-operator

You can also list all pods which are serving traffic behind a kubernetes service by running

kubectl get ep <service name> -o=jsonpath='{.subsets[*].addresses[*].ip}' | tr ' ' '\n' | xargs -I % kubectl get pods -o=name --field-selector=status.podIP=%
-- hoque
Source: StackOverflow

6/30/2020

The best way is follow labels and selectros

Your pod have a label section, and the service use it in the selector section, some examples in:

https://kubernetes.io/docs/concepts/services-networking/service/

You can find the selectors of your service with:

kubectl describe svc cass-operator-service

You can list your labels with:

kubectl get pods --show-labels
-- V&#237;ctor Oriol
Source: StackOverflow