Convert Kafka docker run command to Kubernetes

3/15/2020

The Kubernetes yaml below creates resources as expected and there are no errors/restarts. External requests to the kafka broker at localhost:80005 hang indefinitely. My understanding is that the docker notion of a "network" is achieved in Kubernetes via services. In this case, I have a NodePort service which brings traffic into the cluster and then a ClusterIp port which handles communication between containers. Based on the non-Kubernetes, docker run version of this Zookeeper/Kafka configuration, I suspect my issue lies somewhere in my service and/or advertised listeners. The internet is replete with docker compose files and helm charts but I don't seem to be able to find a combination of configurations that work.

Kafka/Zookeeper YAML:

# Zookeeper
apiVersion: apps/v1
kind: Deployment
metadata:
  name: zookeeper-deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: zookeeper-app
  template:
    metadata:
      labels:
        app: zookeeper-app
    spec:
      containers:
        - name: zookeeper-app
          image: confluentinc/cp-zookeeper:5.4.1
          ports:
            - containerPort: 2181
          env:
          - name: ZOOKEEPER_TICK_TIME
            value: "2000"
          - name: ZOOKEEPER_CLIENT_PORT
            value: "2181"
---
apiVersion: v1
kind: Service
metadata:
  name: zk-cluster-svc
  labels:
    app: zk
spec:
  type: ClusterIP
  selector:
    app: zookeeper-app
  ports:
  - port: 2181
    targetPort: 2181
    protocol: TCP
---
# Kafka
apiVersion: apps/v1
kind: Deployment
metadata:
  name: kafka-deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kafka-app
  template:
    metadata:
      labels:
        app: kafka-app
    spec:
      containers:
        - name: kafka-app
          image: confluentinc/cp-kafka:5.4.1
          ports:
            - containerPort: 9092
            - containerPort: 29092
          env:
          - name: KAFKA_BROKER_ID
            value: "1"
          - name: KAFKA_ZOOKEEPER_CONNECT
            value: "zk-cluster-svc:2181"
          - name: KAFKA_ADVERTISED_LISTENERS
            value: "INSIDE://kafka-cluster-svc:29092,OUTSIDE://localhost:30005"
          - name: KAFKA_LISTENER_SECURITY_PROTOCOL_MAP
            value: "INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT"
          - name: KAFKA_INTER_BROKER_LISTENER_NAME
            value: "INSIDE"
          - name: KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR
            value: "1"
---
apiVersion: v1
kind: Service
metadata:
  name: kafka-service
  labels:
    app: kafka-service
spec:
  type: NodePort
  ports:
  - port: 9092
    nodePort: 30005
    protocol: TCP
  selector:
    app: kafka-app
---
apiVersion: v1
kind: Service
metadata:
  name: kafka-cluster-svc
  labels:
    app: kafka-app
spec:
  selector:
    app: kafka-app
  ports:
  - port: 29092
    targetPort: 29092
    protocol: TCP

^ This is based on a working docker run version of these containers:

docker network create kafzoonet
docker run --name app-zookeeper -d --network="kafzoonet" --env ZOOKEEPER_TICK_TIME=2000 --env ZOOKEEPER_CLIENT_PORT=2181 confluentinc/cp-zookeeper:5.4.1
docker run --name app-kafka -p 30005:9092 -d --network="kafzoonet" --env KAFKA_BROKER_ID=1 --env KAFKA_ZOOKEEPER_CONNECT="app-zookeeper:2181" --env KAFKA_ADVERTISED_LISTENERS="PLAINTEXT://app-kafka:29092,PLAINTEXT_HOST://localhost:30005" --env KAFKA_LISTENER_SECURITY_PROTOCOL_MAP="PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT" --env KAFKA_INTER_BROKER_LISTENER_NAME=PLAINTEXT --env KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 confluentinc/cp-kafka:5.4.1

Are the services configured correctly for a single Kafka node? It appears that Kafka is able to reach Zookeeper but for some reason when a client connects, it isn't getting the correct listener echoed back. Unfortunately I don't have any error messages to support this as the client hangs and neither the Kafka or Zookeeper pods log anything.

-- user2864874
apache-kafka
apache-zookeeper
docker
kubernetes
kubernetes-service

0 Answers