apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: kafka
  namespace: kafka
spec:
  selector:
    matchLabels:
      app: kafka
  serviceName: kafka
  replicas: 6
  updateStrategy:
    type: RollingUpdate
  podManagementPolicy: OrderedReady
  template:
    metadata:
      labels:
        app: kafka
    spec:
      imagePullSecrets:  # 私有仓库拉取镜像的secret，必须和服务在同一个命名空间下
        - name: test
      containers:
      - name: kafka
        command:
          - bash
          - -ec
          - |
            HOSTNAME=`hostname -s`
            if [[ $HOSTNAME =~ (.*)-([0-9]+)$ ]]; then
              ORD=${BASH_REMATCH[2]}
              PORT=$((ORD + 32010))
              PORT1=$((ORD + 32016))
              export KAFKA_CFG_ADVERTISED_LISTENERS="PLAINTEXT://140.249.248.70:$PORT"
              export JMX_PORT="$PORT1"
            else
              echo "Failed to get index from hostname $HOST"
              exit 1
            fi
            exec /entrypoint.sh /run.sh
        imagePullPolicy: IfNotPresent
        image: "kafka:2"
        env:
          - name: ALLOW_PLAINTEXT_LISTENER
            value: "yes"
          - name: KAFKA_CFG_ZOOKEEPER_CONNECT
            value: "zookeeper-0.zookeeper-hs:2181,zookeeper-1.zookeeper-hs:2181,zookeeper-2.zookeeper-hs:2181,zookeeper-3.zookeeper-hs:2181,zookeeper-4.zookeeper-hs:2181,zookeeper-5.zookeeper-hs:2181"
          - name: KAFKA_CFG_OFFSETS_TOPIC_REPLICATION_FACTOR
            value: "6"
          - name: KAFKA_CFG_TRANSACTION_STATE_LOG_MIN_ISR
            value: "6"
          - name: KAFKA_CFG_TRANSACTION_STATE_LOG_REPLICATION_FACTOR
            value: "6"
        ports:
          - name: inside
            containerPort: 9092
          - name: jmx
            containerPort: 5555
        resources:
          requests:
            cpu: 1500m
            memory: 2048Mi
          limits:
            cpu: 1500m
            memory: 3072Mi
        volumeMounts:
          - name: kafka-data
            mountPath: /bitnami
      securityContext:
        runAsUser: 1000
        fsGroup: 1000
  volumeClaimTemplates:
  - metadata:
      name: kafka-data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: managed-nfs-storage-master03
      resources:
        requests:
          storage: 10Gi