k8s 搭建 zookeeper集群

zookeeper-cluster.yaml文件

yaml 复制代码
# https://github.com/bitnami/containers/blob/main/bitnami

# 1. zk-cluster的无头svc
---
kind: Service
apiVersion: v1
metadata:
  name: zk-cluster-headless
  namespace: hgjg-common
  labels:
    app: zk-cluster-headless
  annotations:
    kubesphere.io/creator: admin
spec:
  ports:
    - name: client
      protocol: TCP
      port: 2181
      targetPort: 2181
    - name: server
      protocol: TCP
      port: 2888
      targetPort: 2888
    - name: leader-election
      protocol: TCP
      port: 3888
      targetPort: 3888
    - name: adminserver
      protocol: TCP
      port: 8080
      targetPort: 8080
  selector:
    app: zk-cluster
  clusterIP: 10.233.13.70
  clusterIPs:
    - 10.233.13.70
  type: ClusterIP
  sessionAffinity: None
  ipFamilies:
    - IPv4
  ipFamilyPolicy: SingleStack
  internalTrafficPolicy: Cluster

# 2. zk的StatefulSet
---
kind: StatefulSet
apiVersion: apps/v1
metadata:
  name: zk-cluster
  namespace: hgjg-common
  labels:
    app: zk-cluster
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: >
      {"apiVersion":"apps/v1","kind":"StatefulSet","metadata":{"annotations":{"kubesphere.io/creator":"admin","kubesphere.io/description":"zookeeper集群"},"labels":{"app":"zk-cluster"},"name":"zk-cluster","namespace":"hgjg-common"},"spec":{"persistentVolumeClaimRetentionPolicy":{"whenDeleted":"Retain","whenScaled":"Retain"},"podManagementPolicy":"OrderedReady","replicas":3,"revisionHistoryLimit":10,"selector":{"matchLabels":{"app":"zk-cluster"}},"serviceName":"zk-cluster-headless","template":{"metadata":{"annotations":{"cni.projectcalico.org/ipv4pools":"[\"default-ipv4-ippool\"]","kubesphere.io/creator":"admin","kubesphere.io/imagepullsecrets":"{\"container-o7sjfa\":\"register-addr\"}","kubesphere.io/restartedAt":"2025-01-13T05:11:56.755Z","logging.kubesphere.io/logsidecar-config":"{}"},"creationTimestamp":null,"labels":{"app":"zk-cluster"}},"spec":{"affinity":{"podAntiAffinity":{"preferredDuringSchedulingIgnoredDuringExecution":[{"podAffinityTerm":{"labelSelector":{"matchExpressions":[{"key":"app","operator":"In","values":["zk-cluster"]}]},"topologyKey":"kubernetes.io/hostname"},"weight":100}]}},"containers":[{"command":["sh","-c","export
      ZOO_SERVER_ID=$((${HOSTNAME##*-}+1))\nexport
      ZOO_SERVERS='zk-cluster-sts-0.zk-cluster-headless:2888:3888,zk-cluster-sts-1.zk-cluster-headless:2888:3888,zk-cluster-sts-2.zk-cluster-headless:2888:3888'\nexec
      /opt/bitnami/scripts/zookeeper/entrypoint.sh
      /opt/bitnami/scripts/zookeeper/run.sh\n"],"env":[{"name":"TZ","value":"Asia/Shanghai"},{"name":"ALLOW_ANONYMOUS_LOGIN","value":"yes"},{"name":"JVMFLAGS","value":"-Xmx1g"}],"image":"registry.flow.cn/library/bitnami_zookeeper:3.8.4","imagePullPolicy":"Always","livenessProbe":{"failureThreshold":3,"httpGet":{"path":"/commands/ruok","port":"adminserver","scheme":"HTTP"},"initialDelaySeconds":10,"periodSeconds":5,"successThreshold":1,"timeoutSeconds":1},"name":"container-o7sjfa","ports":[{"containerPort":2181,"name":"client","protocol":"TCP"},{"containerPort":2888,"name":"server","protocol":"TCP"},{"containerPort":3888,"name":"leader-election","protocol":"TCP"},{"containerPort":8080,"name":"adminserver","protocol":"TCP"}],"readinessProbe":{"failureThreshold":3,"httpGet":{"path":"/commands/ruok","port":"adminserver","scheme":"HTTP"},"initialDelaySeconds":10,"periodSeconds":5,"successThreshold":1,"timeoutSeconds":1},"resources":{"limits":{"cpu":"1","memory":"1536Mi"},"requests":{"cpu":"500m","memory":"512Mi"}},"terminationMessagePath":"/dev/termination-log","terminationMessagePolicy":"File","volumeMounts":[{"mountPath":"/bitnami/zookeeper","name":"zk-cluster-pvc"}]}],"dnsPolicy":"ClusterFirst","imagePullSecrets":[{"name":"register-addr"}],"restartPolicy":"Always","schedulerName":"default-scheduler","securityContext":{},"serviceAccount":"default","serviceAccountName":"default","terminationGracePeriodSeconds":30}},"updateStrategy":{"rollingUpdate":{"partition":0},"type":"RollingUpdate"},"volumeClaimTemplates":[{"apiVersion":"v1","kind":"PersistentVolumeClaim","metadata":{"creationTimestamp":null,"name":"zk-cluster-pvc","namespace":"hgjg-common"},"spec":{"accessModes":["ReadWriteOnce"],"resources":{"requests":{"storage":"10Gi"}},"storageClassName":"local","volumeMode":"Filesystem"},"status":{"phase":"Pending"}}]}}
    kubesphere.io/creator: admin
    kubesphere.io/description: zookeeper集群
spec:
  replicas: 3
  selector:
    matchLabels:
      app: zk-cluster
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: zk-cluster
      annotations:
        cni.projectcalico.org/ipv4pools: '["default-ipv4-ippool"]'
        kubesphere.io/creator: admin
        kubesphere.io/imagepullsecrets: '{"container-o7sjfa":"register-addr"}'
        kubesphere.io/restartedAt: '2025-01-13T05:11:56.755Z'
        logging.kubesphere.io/logsidecar-config: '{}'
    spec:
      containers:
        - name: container-o7sjfa
          image: 'registry.flow.cn/library/bitnami_zookeeper:3.8.4'
          command:
            - sh
            - '-c'
            - >
              export ZOO_SERVER_ID=$((${HOSTNAME##*-}+1))

              export
              ZOO_SERVERS='zk-cluster-0.zk-cluster-headless:2888:3888,zk-cluster-1.zk-cluster-headless:2888:3888,zk-cluster-2.zk-cluster-headless:2888:3888'

              exec /opt/bitnami/scripts/zookeeper/entrypoint.sh
              /opt/bitnami/scripts/zookeeper/run.sh
          ports:
            - name: client
              containerPort: 2181
              protocol: TCP
            - name: server
              containerPort: 2888
              protocol: TCP
            - name: leader-election
              containerPort: 3888
              protocol: TCP
              # 可选zk的http接口
            - name: adminserver
              containerPort: 8080
              protocol: TCP
          env:
            - name: TZ
              value: Asia/Shanghai
             # 允许匿名登录
            - name: ALLOW_ANONYMOUS_LOGIN
              value: 'yes'
              # zk的堆内存设置,这里是1g
            - name: JVMFLAGS
              value: "-Xms1g -Xmx1g"
          resources:
            limits:
              cpu: '1'
              memory: 1536Mi  # 要比上面VMFLAGS变量设置的要高,避免OOM
            requests:
              cpu: 500m
              memory: 512Mi
          volumeMounts:
            - name: zk-cluster-pvc
            - # 镜像默认zk的数据目录
              mountPath: /bitnami/zookeeper
          # https://github.com/bitnami/containers/blob/main/bitnami/zookeeper/3.8/debian-12/Dockerfile
          # ZOO_SERVER_ID: 基于主机名截取id然后+1作为zk实例的id
          # ZOO_SERVERS: 实例列表
           # 探针
          livenessProbe:
            httpGet:
              path: /commands/ruok
              port: adminserver
              scheme: HTTP
            initialDelaySeconds: 10
            timeoutSeconds: 1
            periodSeconds: 5
            successThreshold: 1
            failureThreshold: 3
          readinessProbe:
            httpGet:
              path: /commands/ruok
              port: adminserver
              scheme: HTTP
            initialDelaySeconds: 10
            timeoutSeconds: 1
            periodSeconds: 5
            successThreshold: 1
            failureThreshold: 3
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: Always
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      dnsPolicy: ClusterFirst
      serviceAccountName: default
      serviceAccount: default
      securityContext: {}
      imagePullSecrets:
        - name: register-addr
       # 反亲和的软策略,尽量不要调度到同一个node
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 100
              podAffinityTerm:
                labelSelector:
                  matchExpressions:
                    - key: app
                      operator: In
                      values:
                        - zk-cluster
                topologyKey: kubernetes.io/hostname
      schedulerName: default-scheduler
  volumeClaimTemplates:
    - kind: PersistentVolumeClaim
      apiVersion: v1
      metadata:
        name: zk-cluster-pvc
        namespace: hgjg-common
        creationTimestamp: null
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 10Gi
        storageClassName: local
        volumeMode: Filesystem
      status:
        phase: Pending
  serviceName: zk-cluster-headless
  podManagementPolicy: OrderedReady
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      partition: 0
  revisionHistoryLimit: 10
  persistentVolumeClaimRetentionPolicy:
    whenDeleted: Retain
    whenScaled: Retain

创建资源,本例创建hgjg-common命名空间

bash 复制代码
kubectl -n hgjg-common apply -f zookeeper-cluster.yaml 

查看资源

bash 复制代码
[root@master1 ~]# kubectl get pod -A | grep zk
hgjg-common                    zk-cluster-0                                                   1/1     Running     0             6h35m
hgjg-common                    zk-cluster-1                                                   1/1     Running     0             6h35m
hgjg-common                    zk-cluster-2                                                   1/1     Running     0             6h35m
kube-system                    coredns-77b659488f-zk6zn                                       1/1     Running     0             47d
kubesphere-system              ks-console-6d75cbd66f-zk2zl                                    1/1     Running     0             52d
[root@master1 ~]# 
bash 复制代码
[root@master1 ~]# kubectl -n hgjg-common exec -it zk-cluster-0 -- zkServer.sh status
/opt/bitnami/java/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/bitnami/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
[root@master1 ~]# kubectl -n hgjg-common exec -it zk-cluster-1 -- zkServer.sh status
/opt/bitnami/java/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/bitnami/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
[root@master1 ~]# kubectl -n hgjg-common exec -it zk-cluster-2 -- zkServer.sh status
/opt/bitnami/java/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/bitnami/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader
[root@master1 ~]# 
相关推荐
晒足以百八十8 小时前
大数据技术实训:Zookeeper集群配置
大数据·分布式·zookeeper
小马爱打代码11 小时前
Zookeeper特性与节点数据类型详解
分布式·zookeeper·云原生
jimiStephen14 小时前
zookeeper-配置
分布式·zookeeper·debian
一张假钞14 小时前
Zookeeper概览
分布式·zookeeper·云原生
jimiStephen14 小时前
zookeeper 基本原理-单机模式、集群模式
分布式·zookeeper·云原生
昌sit!14 小时前
k8s故障 ImagePullBackOff状态排错
云原生·容器·kubernetes
刘什么洋啊Zz14 小时前
K8S--配置存活、就绪和启动探针
云原生·容器·kubernetes
福大大架构师每日一题14 小时前
1.3 k8s-上部署第一个应用程序
云原生·容器·kubernetes
年薪丰厚14 小时前
K8S集群常用命令
docker·容器·kubernetes