<p>I'm new to Kubernetes and recently I am using it to deploy hadoop. I want to do a thing that set a specific hostname to pod which is created using <code>statefulSets</code> with <code>hostNetwork = true</code>.</p>

<p>Here is my yaml config file.</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: test-bbox
  labels:
    app: test-bbox
spec:
  clusterIP: None
  selector:
    app: test-bbox
  ports:
  - name: foo
    port: 1234
    targetPort: 1234
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: test-bbox
spec:
  serviceName: "test-bbox"
  replicas: 1
  selector:
    matchLabels:
      app: test-bbox
  template:
    metadata:
      labels:
        app: test-bbox
    spec:
      hostNetwork: true
      hostname: test-hostname
      containers:
        - image: busybox
          name: busybox
          env:
          - name: MY_POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          command:
            - sleep
            - "7200"
</code></pre>

<p>As the yaml says, the attribute <code>hostnetwork</code> is set to <code>true</code>. Then the pod <code>test-bbox-0</code>'s hostname is the hostname of the Node where it is created.</p>

<p>If I set <code>hostnetwork</code> to false, the hostname is auto-generated by <code>statefulSets</code> as a format such as <code>test-bbox-0.test-bbox.default.svc.cluster.local</code>, which is just I need.</p>

<p>But in my case I need to set <code>hostnetwork</code> to <code>true</code> and at the same time to customize the hostname to the format mentioned above rather than the Node's hostname. </p>

<p>So the question is, <strong>is there any way to customize hostname for Pod ?</strong></p>

<p>Kubernetes version used : <code>1.9</code></p>

<p><strong>It is not possible</strong> to set the hostname for a Pod that is using <code>hostNetwork</code>. If you try to change the hostname in such a Pod you'll see that you are changing the hostname of the node too; this is because they are sharing the <a href="https://en.wikipedia.org/wiki/Linux_namespaces#UTS" rel="nofollow noreferrer">UTS namespace</a>, not only the <a href="https://en.wikipedia.org/wiki/Linux_namespaces#Network_(net)" rel="nofollow noreferrer">networking</a> one.</p>

<p>Pods managed by a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#stable-network-id" rel="nofollow noreferrer">StatefulSet</a> are a special case and their hostname is set my StatefulSet and it can't be configured directly. The hostname can be influenced by the name of StatefulSet itself while domainname by naming the Service appropriately:</p>

<blockquote>
  <p>Each Pod in a StatefulSet derives its hostname from the name of the StatefulSet and the ordinal of the Pod. The pattern for the constructed hostname is <code>$(statefulset name)-$(ordinal)</code>. [...] A StatefulSet can use a Headless Service to control the domain of its Pods. The domain managed by this Service takes the form: <code>$(service name).$(namespace).svc.cluster.local</code>, where <code>cluster.local</code> is the cluster domain. As each Pod is created, it gets a matching DNS subdomain, taking the form: <code>$(podname).$(governing service domain)</code>, where the governing service is defined by the <code>serviceName</code> field on the StatefulSet.</p>
</blockquote>
