• 已删除用户
Administrator
发布于 2023-08-03 / 3 阅读
0

K8S部署Zookeeper(集群版)

以下YAML文件从本地搭建的KubeSphere工作负载中导出,可能因关联保密字典Secret、配置字典ConfigMap、服务Service等无法直接运行,仅供参考。

kind: StatefulSet
apiVersion: apps/v1
metadata:
  name: zookeeper
  namespace: supports
  annotations:
    kubesphere.io/creator: admin
spec:
  replicas: 3
  selector:
    matchLabels:
      app: zookeeper
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: zookeeper
        what: node
      annotations:
        kubesphere.io/creator: admin
        kubesphere.io/imagepullsecrets: '{}'
        kubesphere.io/restartedAt: '2023-08-24T02:30:57.179Z'
        logging.kubesphere.io/logsidecar-config: '{}'
        prometheus.io/port: '7000'
        prometheus.io/scrape: 'true'
    spec:
      volumes:
        - name: host-time
          hostPath:
            path: /etc/localtime
            type: ''
      containers:
        - name: zookeeper
          image: 'zookeeper:3.7.0'
          command:
            - bash
            - '-x'
            - '-c'
            - >
              SERVERS=3 &&

              HOST=`hostname -s` &&

              DOMAIN=`hostname -d` &&

              CLIENT_PORT=2181 &&

              SERVER_PORT=2888 &&

              ELECTION_PORT=3888 &&

              PROMETHEUS_PORT=7000 &&

              ZOO_DATA_DIR=/mydata/zookeeper/data &&

              ZOO_DATA_LOG_DIR=/mydata/zookeeper/log &&

              {
                echo "clientPort=${CLIENT_PORT}"
                echo 'tickTime=2000'
                echo 'initLimit=300'
                echo 'syncLimit=10'
                echo 'maxClientCnxns=2000'
                echo 'maxSessionTimeout=60000000'
                echo "dataDir=${ZOO_DATA_DIR}"
                echo "dataLogDir=${ZOO_DATA_LOG_DIR}"
                echo 'autopurge.snapRetainCount=10'
                echo 'autopurge.purgeInterval=1'
                echo 'preAllocSize=131072'
                echo 'snapCount=3000000'
                echo 'leaderServes=yes'
                echo 'standaloneEnabled=false'
                echo '4lw.commands.whitelist=*'
                echo 'metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider'
                echo "metricsProvider.httpPort=${PROMETHEUS_PORT}"
              } > /conf/zoo.cfg &&

              {
                echo "zookeeper.root.logger=CONSOLE"
                echo "zookeeper.console.threshold=INFO"
                echo "log4j.rootLogger=\${zookeeper.root.logger}"
                echo "log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender"
                echo "log4j.appender.CONSOLE.Threshold=\${zookeeper.console.threshold}"
                echo "log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout"
                echo "log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n"
              } > /conf/log4j.properties &&

              echo 'JVMFLAGS="-Xms128M -Xmx4G -XX:+UseG1GC
              -XX:+CMSParallelRemarkEnabled"' > /conf/java.env &&

              if [[ $HOST =~ (.*)-([0-9]+)$ ]]; then
                  NAME=${BASH_REMATCH[1]}
                  ORD=${BASH_REMATCH[2]}
              else
                  echo "Failed to parse name and ordinal of Pod"
                  exit 1
              fi &&

              mkdir -p ${ZOO_DATA_DIR} &&

              mkdir -p ${ZOO_DATA_LOG_DIR}  &&

              export MY_ID=$((ORD+1)) &&

              echo $MY_ID > $ZOO_DATA_DIR/myid &&

              for (( i=1; i<=$SERVERS; i++ )); do
                  echo "server.$i=$NAME-$((i-1)).$DOMAIN:$SERVER_PORT:$ELECTION_PORT" >> /conf/zoo.cfg;
              done &&

              chown -Rv zookeeper "$ZOO_DATA_DIR" "$ZOO_DATA_LOG_DIR"
              "$ZOO_LOG_DIR" "$ZOO_CONF_DIR" &&

              zkServer.sh start-foreground
          ports:
            - name: client
              containerPort: 2181
              protocol: TCP
            - name: server
              containerPort: 2888
              protocol: TCP
            - name: leader-election
              containerPort: 3888
              protocol: TCP
            - name: prometheus
              containerPort: 7000
              protocol: TCP
          resources: {}
          volumeMounts:
            - name: host-time
              readOnly: true
              mountPath: /etc/localtime
          livenessProbe:
            exec:
              command:
                - bash
                - '-c'
                - >-
                  OK=$(echo ruok | nc 127.0.0.1 2181); if [[ "$OK" == "imok" ]];
                  then exit 0; else exit 1; fi
            initialDelaySeconds: 10
            timeoutSeconds: 5
            periodSeconds: 10
            successThreshold: 1
            failureThreshold: 3
          readinessProbe:
            exec:
              command:
                - bash
                - '-c'
                - >-
                  OK=$(echo ruok | nc 127.0.0.1 2181); if [[ "$OK" == "imok" ]];
                  then exit 0; else exit 1; fi
            initialDelaySeconds: 10
            timeoutSeconds: 5
            periodSeconds: 10
            successThreshold: 1
            failureThreshold: 3
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
          securityContext:
            runAsUser: 0
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      dnsPolicy: ClusterFirst
      securityContext:
        runAsUser: 1000
        fsGroup: 1000
      schedulerName: default-scheduler
  serviceName: zookeeper-headless
  podManagementPolicy: Parallel
  updateStrategy:
    type: RollingUpdate
  revisionHistoryLimit: 10