1

I have created a Kubernetes StatefulSet. There are three pods in the StatefulSet with names mysql-0, mysql-1, and mysql-2 each with a single container.

If I "log" onto the container in pod mysql-1 and type hostname I get the response mysql-1. However, what I want is for the container to think its hostname is mysql-1.example.com. In other words, force the hostname to have the domain appended.

How do I get the StatefulSet to make this happen?

user35042
  • 2,601
  • 10
  • 32
  • 57

1 Answers1

0

As per http://man7.org/linux/man-pages/man7/hostname.7.html:

Valid characters for hostnames are ASCII(7) letters from a to z, the digits from 0 to 9, and the hyphen (-). A hostname may not start with a hyphen.

Above is also true for subdomains. Here is a regex used for validation: [a-z0-9]([-a-z0-9]*[a-z0-9])?

However, you can use hostname: and subdomain: fields in a pod definition, as described here

A ReplicaSet with above fields would look like this:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: test-replica-set
  labels:
    app: guestbook
    tier: frontend
spec:
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      hostname: my-hostname
      subdomain: my-subdomain
      containers:
      - image: busybox:1.28
        command:
          - sleep
          - "3600"
        name: busybox1
        env:
        - name: HOSTNAME
          value: "whathever.domain.com"

This works like so:

> kubectl exec test-replica-set-pz2kk -it -- hostname
my-hostname

You can also retrieve DNS domain name adding the -d option, or FQDN name using the -f option. This will yield K8s domain names for your pods as described here.

You can also inject environmental variable to a pod and put your info there, as I did in the example above.

> kubectl exec test-replica-set-pz2kk -it -- sh
/ # echo $HOSTNAME
whathever.domain.com
MWZ
  • 128
  • 5
  • Since a StatefulSet sets the hostname based on the name and a counter, how can you use the `hostname` parameter in a StatefulSet to set the internal hostname as I want? – user35042 Jul 19 '19 at 13:49
  • StatefulSet sets pod name. `hostname` field sets what is being presented to container by container runtime. You can't have a hostname that includes a dot (full stop). – MWZ Jul 19 '19 at 14:06