Search code examples
kubernetesserviceselector

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


I am looking to spin up a specific number of pods that are independent and not load balanced. (The intent is to use these to send and receive certain traffic to and from some external endpoint.) The way I am planning to do this is to create the pods explicitly (yaml snippet as below)

    apiVersion: v1
    kind: Pod
    metadata:
      name: generator-agent-pod-1
      labels:
        app: generator-agent
        version: v1
    spec:
      containers:
        ...

(In this, the name will be auto-generated as generator-agent-pod-1, generator-agent-pod-2, etc.)

I am then looking to create one service per pod: so essentially, there'll be a generator-agent-service-1, generator-agent-service-2, etc., and so I can then use the service to be able to reach the pod from outside.

I now have two questions: 1. In the service, how do I select a specific pod by name (instead of by labels)? something equivalent to:

apiVersion: v1
kind: Service
metadata:
  name: generator-agent-service-1
  labels:
    app: agent-service
spec:
  type: NodePort
  ports:
  - port: 8085
    protocol: TCP
  selector:
    metadata.name: generator-agent-pod-1

(This service does not get any endpoints, so the selector is incorrect, I suppose.)

  1. Is there a better way to approach this problem (Kubernetes or otherwise)?

Thanks!


Solution

  • 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 wide
    NAME            READY   STATUS    RESTARTS   AGE     IP               NODE         NOMINATED NODE   READINESS GATES
    pod/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     SELECTOR
    service/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: v1
    kind: Service
    metadata:
      name: nginx-one-service
    spec:
      ports:
        - protocol: TCP
          port: 80
    

    service2.yaml

    apiVersion: v1
    kind: Service
    metadata:
      name: nginx-two-service
    spec:
      ports:
        - protocol: TCP
          port: 80
    
    
    $ kubectl get svc
    NAME                TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
    kubernetes          ClusterIP   10.96.0.1       <none>        443/TCP   32m
    nginx-one-service   ClusterIP   10.102.230.78   <none>        80/TCP    7m16s
    nginx-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-service
    Name:              nginx-one-service
    Namespace:         default
    Labels:            <none>
    Annotations:       <none>
    Selector:          <none>
    Type:              ClusterIP
    IP:                10.102.230.78
    Port:              <unset>  80/TCP
    TargetPort:        80/TCP
    Endpoints:         <none>
    Session Affinity:  None
    Events:            <none>
    
    ubuntu@k8s-master:~$ kubectl describe service nginx-two-service
    Name:              nginx-two-service
    Namespace:         default
    Labels:            <none>
    Annotations:       <none>
    Selector:          <none>
    Type:              ClusterIP
    IP:                10.98.86.67
    Port:              <unset>  80/TCP
    TargetPort:        80/TCP
    Endpoints:         <none>
    Session Affinity:  None
    Events:            <none>
    

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

    endpoint1.yaml

    apiVersion: v1
    kind: Endpoints
    metadata:
      name: nginx-one-service
    subsets:
      - addresses:
          - ip: 192.168.85.193
        ports:
          - port: 80
    

    endpoint2.yaml

    apiVersion: v1
    kind: Endpoints
    metadata:
      name: nginx-two-service
    subsets:
      - addresses:
          - ip: 192.168.85.193
        ports:
          - port: 80
    

    Now get endpoint on creation

    $ kubectl get endpoints
    NAME                ENDPOINTS             AGE
    kubernetes          131.160.188.46:6443   35m
    nginx-one-service   192.168.58.199:80     5m30s
    nginx-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-service
    Name:              nginx-one-service
    Namespace:         default
    Labels:            <none>
    Annotations:       <none>
    Selector:          <none>
    Type:              ClusterIP
    IP:                10.102.230.78
    Port:              <unset>  80/TCP
    TargetPort:        80/TCP
    Endpoints:         192.168.58.199:80
    Session Affinity:  None
    Events:            <none>
    
    ubuntu@k8s-master:~$ kubectl describe service nginx-two-service
    Name:              nginx-two-service
    Namespace:         default
    Labels:            <none>
    Annotations:       <none>
    Selector:          <none>
    Type:              ClusterIP
    IP:                10.98.86.67
    Port:              <unset>  80/TCP
    TargetPort:        80/TCP
    Endpoints:         192.168.85.193:80
    Session Affinity:  None
    Events:            <none>