• K8S 部署 Kafka 集群(集群外部可调用)


    部署 zookeeper 集群

    mkdir -p /data/yaml/kube-system/zookeeper
    cd /data/yaml/kube-system/zookeeper
    
    cat svc.yaml
    apiVersion: v1
    kind: Service
    metadata:
      name: zookeeper-hs
      namespace: kube-system
      labels:
        app: zookeeper
    spec:
      ports:
      - port: 2888
        name: server
      - port: 3888
        name: leader-election
      clusterIP: None
      selector:
        app: zookeeper
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: zookeeper
      namespace: kube-system
      labels:
        app: zookeeper
    spec:
      ports:
      - port: 2181
        name: zookeeper-client
      selector:
        app: zookeeper
    
    kubectl apply -f svc.yaml
    
    cat sts.yaml
    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: zookeeper
      namespace: kube-system
    spec:
      selector:
        matchLabels:
          app: zookeeper
      serviceName: zookeeper-hs
      replicas: 3
      updateStrategy:
        type: RollingUpdate
      podManagementPolicy: OrderedReady
      template:
        metadata:
          labels:
            app: zookeeper
        spec:
          affinity:
            podAntiAffinity:
              requiredDuringSchedulingIgnoredDuringExecution:
                - labelSelector:
                    matchExpressions:
                      - key: "app"
                        operator: In
                        values:
                          - zookeeper
                  topologyKey: "kubernetes.io/hostname"
          containers:
          - name: zookeeper
            imagePullPolicy: Always
            image: "fastop/zookeeper:3.4.10"
            command:
              - sh
              - -c
              - "start-zookeeper \
                      --servers=3 \
                      --data_dir=/var/lib/zookeeper/data \
                      --data_log_dir=/var/lib/zookeeper/data/log \
                      --conf_dir=/opt/zookeeper/conf \
                      --client_port=2181 \
                      --election_port=3888 \
                      --server_port=2888 \
                      --tick_time=2000 \
                      --init_limit=10 \
                      --sync_limit=5 \
                      --heap=512M \
                      --max_client_cnxns=60 \
                      --snap_retain_count=3 \
                      --purge_interval=12 \
                      --max_session_timeout=40000 \
                      --min_session_timeout=4000 \
                      --log_level=INFO"
            ports:
              - containerPort: 2181
                name: client
              - containerPort: 2888
                name: server
              - containerPort: 3888
                name: leader-election
            volumeMounts:
            - name: zookeeper-data
              mountPath: /var/lib/zookeeper
            - mountPath: /etc/localtime
              readOnly: true 
              name: time-data
          securityContext:
            runAsUser: 1000
            fsGroup: 1000
          volumes: 
          - name: time-data 
            hostPath: 
              path: /usr/share/zoneinfo/Asia/Shanghai
      volumeClaimTemplates:
      - metadata:
          name: zookeeper-data
          labels:
            app: zk
        spec:
          accessModes: [ "ReadWriteOnce" ]
          storageClassName: ceph-storage
          resources:
            requests:
              storage: 10Gi
    
    kubectl apply -f sts.yaml
    

    部署 kafka 集群

    mkdir -p /data/yaml/kube-system/kafka
    cd /data/yaml/kube-system/kafka
    
    cat svc.yaml 
    apiVersion: v1
    kind: Service
    metadata:
      name: kafka-svc
      namespace: kube-system
      labels:
        app: kafka
    spec:
      ports:
      - port: 9092
        name: server
      clusterIP: None
      selector:
        app: kafka
    
    kubectl apply -f svc.yaml 
    
    cat svc-nodeport-0.yaml 
    apiVersion: v1
    kind: Service
    metadata:
      name: kafka-0
      namespace: kube-system
      labels:
        app: kafka
    spec:
      ports:
      - port: 9092
        targetPort: 9092
        nodePort: 30092
        name: server
      type: NodePort
      selector:
        statefulset.kubernetes.io/pod-name: kafka-0
    
    kubectl apply -f svc-nodeport-0.yaml
    
    cat svc-nodeport-1.yaml 
    apiVersion: v1
    kind: Service
    metadata:
      name: kafka-1
      namespace: kube-system
      labels:
        app: kafka
    spec:
      ports:
      - port: 9092
        targetPort: 9092
        nodePort: 30093
        name: server
      type: NodePort
      selector:
        statefulset.kubernetes.io/pod-name: kafka-1
    
    kubectl apply -f svc-nodeport-1.yaml
    
    cat svc-nodeport-2.yaml 
    apiVersion: v1
    kind: Service
    metadata:
      name: kafka-2
      namespace: kube-system
      labels:
        app: kafka
    spec:
      ports:
      - port: 9092
        targetPort: 9092
        nodePort: 30094
        name: server
      type: NodePort
      selector:
        statefulset.kubernetes.io/pod-name: kafka-2
    
    kubectl apply -f svc-nodeport-2.yaml
    
    cat sts.yaml
    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: kafka
      namespace: kube-system
    spec:
      selector:
        matchLabels:
          app: kafka
      serviceName: kafka
      replicas: 3
      updateStrategy:
        type: RollingUpdate
      podManagementPolicy: OrderedReady
      template:
        metadata:
          labels:
            app: kafka
        spec:
          affinity:
            podAntiAffinity:
              requiredDuringSchedulingIgnoredDuringExecution:
                - labelSelector:
                    matchExpressions:
                      - key: "app"
                        operator: In
                        values:
                          - kafka
                  topologyKey: "kubernetes.io/hostname"
          containers:
          - name: kafka
            command:
              - bash
              - -ec
              - |
                HOSTNAME=`hostname -s`
                if [[ $HOSTNAME =~ (.*)-([0-9]+)$ ]]; then
                  ORD=${BASH_REMATCH[2]}
                  PORT=$((ORD + 30092))
                  export KAFKA_CFG_ADVERTISED_LISTENERS="PLAINTEXT://192.168.1.106:$PORT"
                else
                  echo "Failed to get index from hostname $HOST"
                  exit 1
                fi
                exec /entrypoint.sh /run.sh
            image: "bitnami/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"
              - name: KAFKA_CFG_OFFSETS_TOPIC_REPLICATION_FACTOR
                value: "3"
              - name: KAFKA_CFG_TRANSACTION_STATE_LOG_MIN_ISR
                value: "3"
              - name: KAFKA_CFG_TRANSACTION_STATE_LOG_REPLICATION_FACTOR
                value: "3"
              - name: KAFKA_HEAP_OPTS
                value: "-Xmx6g -Xms6g"
              - name: KAFKA_CFG_MESSAGE_MAX_BYTES
                value: "100000000"
              - name: KAFKA_CFG_LOG_RETENTION_HOURS
                value: "24"
              - name: KAFKA_CFG_MAX_PARTITION_FETCH_BYTES
                value: "200000000"
            ports:
              - containerPort: 9092
            volumeMounts:
              - name: kafka-data
                mountPath: /bitnami
              - mountPath: /etc/localtime
                readOnly: true 
                name: time-data
          securityContext:
            runAsUser: 1000
            fsGroup: 1000
          volumes: 
          - name: time-data 
            hostPath: 
              path: /usr/share/zoneinfo/Asia/Shanghai
      volumeClaimTemplates:
      - metadata:
          name: kafka-data
        spec:
          accessModes: [ "ReadWriteOnce" ]
          storageClassName: ceph-storage
          resources:
            requests:
              storage:  40G
    
    kubectl apply -f sts.yaml
    
  • 相关阅读:
    Educational Codeforces Round 51 (Rated for Div. 2)
    Kruskal重构树入门
    编译原理词法分析
    java.lang.String内部结构的变化
    android 世界各国英文简写代码 资源文件
    openCV python 安装
    解读30个提高Web程序执行效率的好经验
    从认知盈余说起,也谈分享精神
    STL set multiset map multimap unordered_set unordered_map example
    [置顶] 【Git入门之一】Git是神马?
  • 原文地址:https://www.cnblogs.com/klvchen/p/15838832.html
Copyright © 2020-2023  润新知