Multi-broker Kafka on Kubernetes how to set KAFKA_ADVERTISED_HOST_NAME

若如初见. 提交于 2020-12-08 07:59:11

问题


My current Kafka deployment file with 3 Kafka brokers looks like this:

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

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:

zookeeper-0.zookeeper-headless.default.svc.cluster.local:2181

Broker IDs are generated based on the pod name:

- name: BROKER_ID_COMMAND
          value: "hostname | awk -F '-' '{print $2}'"

Result:

kafka-0 = 0
kafka-1 = 1
kafka-2 = 2

However, In order to use the Kubedns names for the Kafka brokers:

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

I need to be able to set the KAFKA_ADVERTISED_HOST_NAME variable to the above FQDN values based on the name of the pod.

Currently I have the variable set to the name of the pod:

- name: KAFKA_ADVERTISED_HOST_NAME
   valueFrom:
      fieldRef:
        fieldPath: metadata.name

Result:

KAFKA_ADVERTISED_HOST_NAME=kafka-0
KAFKA_ADVERTISED_HOST_NAME=kafka-1
KAFKA_ADVERTISED_HOST_NAME=kafka-2

But somehow I would need to append the rest of the DNS name.

Is there a way I could set the DNS value directly?

Something like that:

- name: KAFKA_ADVERTISED_HOST_NAME
       valueFrom:
          fieldRef:
            fieldPath: kubedns.name

回答1:


I managed to solve the problem with a command field inside the pod definition:

    command:
    - sh
    - -c
    - "export KAFKA_ADVERTISED_HOST_NAME=$(hostname).kafka-headless.default.svc.cluster.local &&
       start-kafka.sh"

This runs a shell command which exports the advertised hostname environment variable based on the hostname value.




回答2:


- name: MY_POD_NAME
  valueFrom:
    fieldRef:
      fieldPath: metadata.name 
- name: KAFKA_ZOOKEEPER_CONNECT
  value: zook-zookeeper.zook.svc.cluster.local:2181
- name: KAFKA_PORT_NUMBER
  value: "9092"
- name: KAFKA_LISTENERS
  value: SASL_SSL://:$(KAFKA_PORT_NUMBER)
- name: KAFKA_ADVERTISED_LISTENERS
  value: SASL_SSL://$(MY_POD_NAME).kafka-kafka-headless.kafka.svc.cluster.local:$(KAFKA_PORT_NUMBER)

The above config would create your FQDN. You should be able to see those names in your Kafka logs when Kafka server starts.

NOTE: Kubernetes allows you to reference environment variables using the syntax $(VARIABLE)



来源:https://stackoverflow.com/questions/47516355/multi-broker-kafka-on-kubernetes-how-to-set-kafka-advertised-host-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!