Add namespace to kubernetes hostname

3/21/2017

Currently my pods are all named "some-deployment-foo-bar" which does not help me track down issues when an error is reported with just the hostname.

So I want "$POD_NAMESPACE.$POD_NAME" as hostname.

I tried pod.beta.kubernetes.io/hostname: "foo" but that only sets an absolute name ... and subdomain did not work ...

The only other solution I saw was using a wrapper script that modifies the hostname and then executes the actual command ... which is pretty hacky and adds overhead ot every container.

Any way of doing this nicely?

current config is:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: foo
  labels:
    project: foo
spec:
  selector:
    matchLabels:
      project: foo
  template:
    metadata:
      name: foo
      labels:
        project: foo
    spec:
      containers:
      - image: busybox
        name: foo
-- grosser
kubernetes

1 Answer

3/21/2017

PodSpec has a subdomain field, which can be used to specify the Pod’s subdomain. This field value takes precedence over the pod.beta.kubernetes.io/subdomain annotation value.

There is more information here, below is an example.

    apiVersion: v1
    kind: Service
    metadata:
      name: default-subdomain
    spec:
      selector:
        name: busybox
      clusterIP: None
      ports:
        - name: foo # Actually, no port is needed.
          port: 1234 
          targetPort: 1234
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: busybox1
      labels:
        name: busybox
    spec:
      hostname: busybox-1
      subdomain: default-subdomain
      containers:
      - image: busybox
        command:
          - sleep
          - "3600"
        name: busybox
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: busybox2
      labels:
        name: busybox
    spec:
      hostname: busybox-2
      subdomain: default-subdomain
      containers:
      - image: busybox
        command:
          - sleep
          - "3600"
        name: busybox
-- Simon I
Source: StackOverflow