Skip to content

Kafka in Kubernetes

namespace.yaml

yaml
apiVersion: v1
kind: Namespace
metadata:
  name: common

deployment.yaml

yaml
apiVersion: v1
kind: Service
metadata:
  name: kafka
  namespace: common
spec:
  type: ClusterIP
  ports:
    - port: 9092
      name: plaintext
      targetPort: 9092
    - port: 9093
      name: controller
      targetPort: 9093
  clusterIP: None
  selector:
    app: kafka
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: kafka-config
  namespace: common
data:
  KAFKA_ENABLE_KRAFT: "yes"
  KAFKA_CFG_PROCESS_ROLES: "broker,controller"
  KAFKA_CFG_CONTROLLER_LISTENER_NAMES: "CONTROLLER"
  KAFKA_CFG_LISTENERS: "PLAINTEXT://:9092,CONTROLLER://:9093"
  KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP: "CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT"
  KAFKA_KRAFT_CLUSTER_ID: "LelM2dIFQkiUFvXCEcqRWA"
  KAFKA_CFG_CONTROLLER_QUORUM_VOTERS: "1@kafka-0.kafka:9093"
  ALLOW_PLAINTEXT_LISTENER: "yes"
  KAFKA_HEAP_OPTS: "-Xmx512M -Xms256M"
  KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE: "false"
  KAFKA_CFG_ADVERTISED_LISTENERS: "PLAINTEXT://:9092"
  KAFKA_BROKER_ID: "1"
  KAFKA_CFG_NODE_ID: "1"
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: kafka
  namespace: common
spec:
  serviceName: kafka
  replicas: 1
  selector:
    matchLabels:
      app: kafka
  template:
    metadata:
      labels:
        app: kafka
    spec:
      containers:
        - name: kafka
          image: bitnami/kafka:3.8.1
          ports:
            - containerPort: 9092
              name: plaintext
              hostPort: 9092
            - containerPort: 9093
              name: controller
              hostPort: 9093
          envFrom:
            - configMapRef:
                name: kafka-config
          resources:
            requests:
              memory: "256Mi"
              cpu: "500m"
            limits:
              memory: "512Mi"
          volumeMounts:
            - name: kafka-storage
              mountPath: /bitnami/kafka
      securityContext:
        fsGroup: 1001
        runAsUser: 0
  volumeClaimTemplates:
    - metadata:
        name: kafka-storage
        namespace: common
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: 5Gi

Kafka UI

ui-deployment.yaml

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: kafka-ui
  namespace: common
  labels:
    app: kafka-ui
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kafka-ui
  template:
    metadata:
      labels:
        app: kafka-ui
    spec:
      containers:
        - name: kafka-ui
          image: provectuslabs/kafka-ui:v0.7.2
          ports:
            - containerPort: 8080
              hostPort: 30890
          env:
            - name: KAFKA_CLUSTERS_0_NAME
              value: "dev_cluster"
            - name: KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS
              value: "kafka.common.svc.cluster.local:9092"
            - name: AUTH_TYPE
              value: "LOGIN_FORM"
            - name: SPRING_SECURITY_USER_NAME
              value: "admin"
            - name: SPRING_SECURITY_USER_PASSWORD
              value: "8d3308f1175425aafd8d758ac0b3703d" # 在这里配置密码
---
apiVersion: v1
kind: Service
metadata:
  name: kafka-ui
  labels:
    app: kafka-ui
spec:
  type: ClusterIP
  ports:
    - port: 8080
      targetPort: 8080
  clusterIP: None
  selector:
    app: kafka-ui