<p>I have followed the directives of this web site <a href="https://dzone.com/articles/how-to-deploy-apache-kafka-with-kubernetes" rel="nofollow noreferrer">How To Deploy Apache Kafka With Kubernetes (DZone)</a> to deploy a <em>Kafka</em> broker with its <em>Zookeeper</em>. (I've only changed the namespaces names).</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Service
metadata:
  labels:
    app: zookeeper-service
  name: zookeeper-service
  namespace: ecoemploi
spec:
  type: NodePort
  ports:
    - name: zookeeper-port
      port: 2181
      nodePort: 30181
      targetPort: 2181
  selector:
    app: zookeeper
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: zookeeper
  name: zookeeper
  namespace: ecoemploi
spec:
  replicas: 1
  selector:
    matchLabels:
      app: zookeeper
  template:
    metadata:
      labels:
        app: zookeeper
    spec:
      containers:
        - image: bitnami/zookeeper:latest
          imagePullPolicy: IfNotPresent
          name: zookeeper
          ports:
            - containerPort: 2181
</code></pre>
<p>But to make it working, the writers of that solution do wait for the cluster to start a first time,<br />
then copy the IP address of <code>zookeeper-service</code></p>
<pre class="lang-bash prettyprint-override"><code>kubectl get services -n kafka
NAME               TYPE      CLUSTER-IP     EXTERNAL-IP PORT(S)        AGE
zookeeper-service  NodePort  10.100.69.243  &lt;none&gt;      2181:30181/TCP 3m4s
</code></pre>
<p>and paste it into the <code>kafka-broker</code> deployment file:</p>
<pre class="lang-yaml prettyprint-override"><code>[...]
    spec:
      hostname: kafka-broker
      containers:
        - env:
            - name: KAFKA_BROKER_ID
              value: &quot;1&quot;
            - name: KAFKA_ZOOKEEPER_CONNECT
              value: 10.100.69.243:2181
            - name: KAFKA_LISTENERS
              value: PLAINTEXT://:9092
            - name: KAFKA_ADVERTISED_LISTENERS
              value: PLAINTEXT://kafka-broker:9092
[...]
</code></pre>
<p>Then re-applying <code>kafka-broker</code> makes it working.</p>
<p>I'm using this solution, but in a <em>Vagrant</em> box it isn't convenient: each time I'm recreating my vagrant, that IP changes...</p>
<p>Is there a solution to get the Cluster IP of that <code>zookeeper-service</code> and prepare the correct <code>KAFKA_ZOOKEEPER_CONNECT</code> environment variable for <code>kafka-broker</code> automatically?</p>

Marc Le Bihan
<p>You can the service name, which will be the same each time K8s DNS will take care of it.</p>
<pre><code>- name: KAFKA_ZOOKEEPER_CONNECT
  value: zookeeper-service
</code></pre>
<p><strong>K8s</strong> work that way, internal communication happens over the <strong>service name</strong> use the <strong>IP</strong> if required else <strong>ClusterIP</strong> in nature may changes <strong>IP</strong> with new deployment of <strong>service</strong> or any changes.</p>
<p>DNS resolution will be taken care by Kubernetes(coredns, kubedns), so we just mention the <strong>service-name</strong>.</p>
<p>Here is example for ref : <a href="https://kubernetes.io/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume/" rel="nofollow noreferrer">https://kubernetes.io/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume/</a></p>
<p>Wordpess connecting to MySQL database with <strong>MySQL</strong> service name <strong>wordpress-mysql</strong> variable <strong>WORDPRESS_DB_HOST</strong></p>
