How can I expose the name of a pod as hostname using a headless service?

191 views Asked by At

I have a headless kubernetes service.

apiVersion: v1
kind: Service
metadata:
  name: api-server-headless
  labels:
    {{- include "api-server.labels" . | nindent 4 }}
    app.kubernetes.io/version: {{ .Values.image.tag }}
    app.kubernetes.io/name: api-server-headless
spec:
  type: ClusterIP
  clusterIP: None
  ports:
  - name: http
    port: 80
    targetPort: 8080
    protocol: TCP
  selector:
    {{- include "api-server.selectorLabels" . | nindent 4 }}
    app.kubernetes.io/name: api-server

It does resolve the ip addresses of all replicas of my api-server pods. What I am looking for, is that I also can resolve the hostnames (name of the pod). This, I could not accomplish yet.

I've seen that the generated EndpointSlices of the headless service do only contain the IP address and no hostname.

addressType: IPv4
apiVersion: discovery.k8s.io/v1
endpoints:
  - addresses:
      - 10.240.4.100
    conditions:
      ready: true
      serving: true
      terminating: false
    nodeName: aks-nodepool27-xxxxxx
    targetRef:
      kind: Pod
      name: api-server-5f58f8764d-nr5ln
      namespace: app
      uid: 625dd52e-73a4-41c9-938f-2ac5ac8f5972
  - addresses:
      - 10.240.4.64
    conditions:
      ready: true
      serving: true
      terminating: false
    nodeName: aks-nodepool27-xxxxx
    targetRef:
      kind: Pod
      name: api-server-5f58f8764d-nt74m
      namespace: app
      uid: 7fb188cb-4876-45e9-b816-ba73c38a82b0

Ass far as I understand, I would want to have the hostname: generated with the value of the pod name.

nslookup on a pod in the same namespace returns the following:

root@dnsutils:/# nslookup api-server-headless
Server:         10.0.0.10
Address:        10.0.0.10#53

Name:   api-server-headless.app.svc.cluster.local
Address: 10.240.4.100
Name:   api-server-headless.app.svc.cluster.local
Address: 10.240.4.64

root@dnsutils:/# nslookup 10.240.4.100
Server:         10.0.0.10
Address:        10.0.0.10#53

100.4.240.10.in-addr.arpa       name = 10-240-4-100.api-server-headless.app.svc.cluster.local.
100.4.240.10.in-addr.arpa       name = 10-240-4-100.api-server.app.svc.cluster.local.

How can I expose the name of a pod as its hostname using a headless service?

0

There are 0 answers