In Kubernetes, how does one select a pod by name in a service selector? In Kubernetes, how does one select a pod by name in a service selector? kubernetes kubernetes

In Kubernetes, how does one select a pod by name in a service selector?


There is also option for you to define a service with no pod selector.and then manually map the Service to the network address and port where it’s running, by adding an Endpoint object manually.

Example for your reference :

Created two pods of type nginx

$ kubectl get all -o wideNAME            READY   STATUS    RESTARTS   AGE     IP               NODE         NOMINATED NODE   READINESS GATESpod/nginx-one   1/1     Running   0          4m56s   192.168.58.199   k8s-node02   <none>           <none>pod/nginx-two   1/1     Running   0          4m50s   192.168.85.193   k8s-node01   <none>           <none>NAME                        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE     SELECTORservice/kubernetes          ClusterIP   10.96.0.1       <none>        443/TCP   29m     <none>

Create two service using below yamls, Note no Pod selector field used on yaml below

service1.yaml

apiVersion: v1kind: Servicemetadata:  name: nginx-one-servicespec:  ports:    - protocol: TCP      port: 80

service2.yaml

apiVersion: v1kind: Servicemetadata:  name: nginx-two-servicespec:  ports:    - protocol: TCP      port: 80$ kubectl get svcNAME                TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGEkubernetes          ClusterIP   10.96.0.1       <none>        443/TCP   32mnginx-one-service   ClusterIP   10.102.230.78   <none>        80/TCP    7m16snginx-two-service   ClusterIP   10.98.86.67     <none>        80/TCP    6m56s

Describe the service and no end point are mapped since we gave no selector.

ubuntu@k8s-master:~$ kubectl describe service nginx-one-serviceName:              nginx-one-serviceNamespace:         defaultLabels:            <none>Annotations:       <none>Selector:          <none>Type:              ClusterIPIP:                10.102.230.78Port:              <unset>  80/TCPTargetPort:        80/TCPEndpoints:         <none>Session Affinity:  NoneEvents:            <none>ubuntu@k8s-master:~$ kubectl describe service nginx-two-serviceName:              nginx-two-serviceNamespace:         defaultLabels:            <none>Annotations:       <none>Selector:          <none>Type:              ClusterIPIP:                10.98.86.67Port:              <unset>  80/TCPTargetPort:        80/TCPEndpoints:         <none>Session Affinity:  NoneEvents:            <none>

Now you can choose to map the end point manually using below yamls.

endpoint1.yaml

apiVersion: v1kind: Endpointsmetadata:  name: nginx-one-servicesubsets:  - addresses:      - ip: 192.168.85.193    ports:      - port: 80

endpoint2.yaml

apiVersion: v1kind: Endpointsmetadata:  name: nginx-two-servicesubsets:  - addresses:      - ip: 192.168.85.193    ports:      - port: 80

Now get endpoint on creation

$ kubectl get endpointsNAME                ENDPOINTS             AGEkubernetes          131.160.188.46:6443   35mnginx-one-service   192.168.58.199:80     5m30snginx-two-service   192.168.85.193:80     4m59s

and list the servie and endpoint should be mapped as below

ubuntu@k8s-master:~$ kubectl describe service nginx-one-serviceName:              nginx-one-serviceNamespace:         defaultLabels:            <none>Annotations:       <none>Selector:          <none>Type:              ClusterIPIP:                10.102.230.78Port:              <unset>  80/TCPTargetPort:        80/TCPEndpoints:         192.168.58.199:80Session Affinity:  NoneEvents:            <none>ubuntu@k8s-master:~$ kubectl describe service nginx-two-serviceName:              nginx-two-serviceNamespace:         defaultLabels:            <none>Annotations:       <none>Selector:          <none>Type:              ClusterIPIP:                10.98.86.67Port:              <unset>  80/TCPTargetPort:        80/TCPEndpoints:         192.168.85.193:80Session Affinity:  NoneEvents:            <none>


I think you are using StatefulSet for controlling Pods.If so, you can use label statefulset.kubernetes.io/pod-name to select pods in a service.

For illustration:

apiVersion: v1kind: Servicemetadata:  name: generator-agent-service-1  labels:    app: agent-servicespec:  type: NodePort  ports:  - port: 8085    protocol: TCP  selector:    statefulset.kubernetes.io/pod-name: generator-agent-pod-1