In Kubernetes (k8s for short), a Service Endpoint is an abstraction that represents the network location of a service. It is an internal or external IP address that can be used to access the application services running in a Kubernetes cluster. How do you list all the Pods under a Service in Kubernetes? This article will introduce how to list all Pods under a Service using kubectl.

Prepare the environment

Before we start, we need to prepare the k8s environment. Here we use minikube to create a k8s environment and use kubectl to manage k8s resources. Let’s start by creating Nginx Pods.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: default
  labels:
    app: nginx
spec:
  containers:
  - image: nginx:latest
    name: nginx
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "500m"

In addition to creating Pods, we also use Deployment to manage Pods.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 2
      maxUnavailable: 1
  minReadySeconds: 5
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:latest
        name: nginx
        ports:
        - containerPort: 80
        resources:
          requests:
            memory: "64Mi"
            cpu: "250m"
          limits:
            memory: "128Mi"
            cpu: "500m"

Once created, we can use kubectl get pods --show-labels to see the current status of the pods.

1
2
3
4
NAME                     READY   STATUS    RESTARTS   AGE     LABELS
nginx                    1/1     Running   0          5h40m   app=nginx
nginx-6d7c8c89f7-rrszr   1/1     Running   0          5h7m    app=nginx,pod-template-hash=6d7c8c89f7
nginx-6d7c8c89f7-n2n6c   1/1     Running   0          5h7m    app=nginx,pod-template-hash=6d7c8c89f7

Finally, create two services (NodePort + Cluster) to map to Nginx Pods.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    app: nginx
  ports:
  - name: http
    protocol: TCP
    port: 3000
    targetPort: 80

---
apiVersion: v1
kind: Service
metadata:
  name: nginx-nodeport
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
  - name: http
    protocol: TCP
    port: 8080
    targetPort: 80

After the creation, we can use kubectl get svc -o wide to check the current status of the service.

1
2
3
4
NAME             TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE     SELECTOR
kubernetes       ClusterIP   10.43.0.1      <none>        443/TCP          5h49m   <none>
nginx            ClusterIP   10.43.61.137   <none>        3000/TCP         5h41m   app=nginx
nginx-nodeport   NodePort    10.43.51.4     <none>        8080:30140/TCP   5h41m   app=nginx

List all Pods under Service

List all the Endpoints of the current Service first.(kubectl get endpoints)

1
2
3
4
NAME             ENDPOINTS                                   AGE
kubernetes       192.168.5.15:6443                           5h51m
nginx-nodeport   172.17.0.6:80,172.17.0.7:80,172.17.0.9:80   5h44m
nginx            172.17.0.6:80,172.17.0.7:80,172.17.0.9:80   5h44m

As you can see from the above results, nginx maps to three Pods, and nginx-nodeport also maps to three Pods. Here we can use kubectl get endpoints nginx -o yaml to see the details.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
apiVersion: v1
kind: Endpoints
metadata:
  creationTimestamp: "2023-05-21T07:15:41Z"
  name: nginx
  namespace: default
  resourceVersion: "1164"
  uid: 686d68e6-a58b-4a12-a2d4-0f4ea88b9b56
subsets:
- addresses:
  - ip: 172.17.0.6
    nodeName: colima
    targetRef:
      kind: Pod
      name: nginx
      namespace: default
      uid: e717d901-4319-4ea6-b33d-31dff7c79549
  - ip: 172.17.0.7
    nodeName: colima
    targetRef:
      kind: Pod
      name: nginx-6d7c8c89f7-n2n6c
      namespace: default
      uid: b6e7c74c-3d1f-494e-b05d-9242e367cfe3
  - ip: 172.17.0.9
    nodeName: colima
    targetRef:
      kind: Pod
      name: nginx-6d7c8c89f7-rrszr
      namespace: default
      uid: 7528bfd6-d6dd-4557-873f-6f8ed9b9ebd4
  ports:
  - name: http
    port: 80
    protocol: TCP

How do we find the names of all the Pods from the above information? Here we can use kubectl get endpoints nginx -o jsonpath='{.subsets[*].addresses[*].targetRef.name}' to get the names of all Pods.

1
2
$ kubectl get endpoints nginx -o jsonpath='{.subsets[*].addresses[*].targetRef.name}'
nginx nginx-6d7c8c89f7-n2n6c nginx-6d7c8c89f7-rrszr

Finally, we put the above output into kubectl get pods to see the detailed pod information.

1
kubectl get pods nginx nginx-6d7c8c89f7-n2n6c nginx-6d7c8c89f7-rrszr

Use one command to complete the above steps.

1
2
3
4
kubectl get pods $(kubectl get endpoints nginx -o jsonpath='{.subsets[*].addresses[*].targetRef.name}')
# or
kubectl get endpoints nginx -o jsonpath='{.subsets[*].addresses[*].targetRef.name}' \
  | xargs kubectl get pods -o wide

Ref

  • https://blog.wu-boy.com/2023/05/list-pods-from-k8s-service/