<p>My current Kafka deployment file with 3 Kafka brokers looks like this:</p>

<pre><code>apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: kafka
spec:
  selector:
    matchLabels:
      app: kafka
  serviceName: kafka-headless
  replicas: 3
  updateStrategy:
    type: RollingUpdate
  podManagementPolicy: Parallel
  template:
    metadata:
      labels:
        app: kafka
    spec:
      containers:
      - name: kafka-instance
        image: wurstmeister/kafka
        ports:
        - containerPort: 9092
        env:
        - name: KAFKA_ADVERTISED_PORT
          value: "9092"
        - name: KAFKA_ADVERTISED_HOST_NAME
          valueFrom:
              fieldRef:
                fieldPath: metadata.name
        - name: KAFKA_ZOOKEEPER_CONNECT
          value: "zookeeper-0.zookeeper-headless.default.svc.cluster.local:2181,\
                  zookeeper-1.zookeeper-headless.default.svc.cluster.local:2181,\
                  zookeeper-2.zookeeper-headless.default.svc.cluster.local:2181"
        - name: BROKER_ID_COMMAND
          value: "hostname | awk -F '-' '{print $2}'"
        - name: KAFKA_CREATE_TOPICS
          value: hello:2:1
        volumeMounts:
        - name: data
          mountPath: /var/lib/kafka/data
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 50Gi
</code></pre>

<p>This creates 3 Kafka brokers as a Stateful Set and connects to the Zookeeper cluster using the Kubedns service with FQDN (Fully Qualified Domain Names) such as: </p>

<pre><code>zookeeper-0.zookeeper-headless.default.svc.cluster.local:2181
</code></pre>

<p>Broker IDs are generated based on the pod name:</p>

<pre><code>- name: BROKER_ID_COMMAND
          value: "hostname | awk -F '-' '{print $2}'"
</code></pre>

<p>Result:</p>

<pre><code>kafka-0 = 0
kafka-1 = 1
kafka-2 = 2
</code></pre>

<p>However, In order to use the Kubedns names for the Kafka brokers:</p>

<pre><code>kafka-0.kafka-headless.default.svc.cluster.local:9092
kafka-1.kafka-headless.default.svc.cluster.local:9092
kafka-2.kafka-headless.default.svc.cluster.local:9092
</code></pre>

<p>I need to be able to set the <code>KAFKA_ADVERTISED_HOST_NAME</code> variable to the above FQDN values based on the name of the pod.</p>

<p>Currently I have the variable set to the name of the pod:</p>

<pre><code>- name: KAFKA_ADVERTISED_HOST_NAME
   valueFrom:
      fieldRef:
        fieldPath: metadata.name
</code></pre>

<p>Result:</p>

<pre><code>KAFKA_ADVERTISED_HOST_NAME=kafka-0
KAFKA_ADVERTISED_HOST_NAME=kafka-1
KAFKA_ADVERTISED_HOST_NAME=kafka-2
</code></pre>

<p>But somehow I would need to append the rest of the DNS name.</p>

<p>Is there a way I could set the DNS value directly? </p>

<p>Something like that:</p>

<pre><code>- name: KAFKA_ADVERTISED_HOST_NAME
       valueFrom:
          fieldRef:
            fieldPath: kubedns.name
</code></pre>

<p>I managed to solve the problem with a command field inside the pod definition:</p>

<pre><code>    command:
    - sh
    - -c
    - "export KAFKA_ADVERTISED_HOST_NAME=$(hostname).kafka-headless.default.svc.cluster.local &amp;&amp;
       start-kafka.sh"
</code></pre>

<p>This runs a shell command which exports the advertised hostname environment variable based on the <code>hostname</code> value.</p>
