6

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!

Srinivas
  • 83
  • 1
  • 6
  • simplest solution will be to add the additional name field under labels as well and use the name label as selector refer example in below comment. – DT. Jan 31 '20 at 13:39
  • `apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: name: nginx1 name: nginx1 spec: containers: - image: nginx name: nginx1` – DT. Jan 31 '20 at 13:41

2 Answers2

7

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: v1
kind: Service
metadata:
  name: generator-agent-service-1
  labels:
    app: agent-service
spec:
  type: NodePort
  ports:
  - port: 8085
    protocol: TCP
  selector:
    statefulset.kubernetes.io/pod-name: generator-agent-pod-1
DoroCoder
  • 71
  • 1
  • 2
  • 2
    Really like this answer. I would like to know how to view all of these options. I.E (statefulset.kubernetes.io/*) (pod.kubernetes.io/*) – Lon Kaut Dec 28 '21 at 20:03
4

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>
DT.
  • 3,351
  • 2
  • 18
  • 32
  • 2
    Thanks! @D.T., your answer also works, of course; I was also looking to see if there was a way to select based on metadata other than labels. Much appreciated!! – Srinivas Feb 01 '20 at 05:29