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