• 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
    
  • 相关阅读:
    【译】常用网络端口号列表
    使用Simian进行重复代码检测
    使用GCOV进行代码覆盖率统计
    AFL Fuzz安装及完成一次简单的模糊测试
    数据可视化概述
    完成下方的 which_date() 函数,并返回某一起始时间后特定一段时间的日期
    linux用户不在sudoers文件中
    linux /lib64/libc.so.6: version `GLIBC_2.17′ not found
    web api 2.0 上传文件超过4M时,出现404错误
    Centos7 编译安装 Nginx Mariadb Asp.net Core2 (实测 笔记 Centos 7.7 + Openssl 1.1.1d + Mariadb 10.3.7 + Nginx 1.16.1 + Asp.net. Core 2 )
  • 原文地址:https://www.cnblogs.com/klvchen/p/15838832.html
Copyright © 2020-2023  润新知