ZooKeeper 服务
服务类型: 无头服务(clusterIP: None),这是 StatefulSet(有状态集)必需的配置。
端口:
2181 (客户端): 用于客户端连接。
2888 (跟随者): 用于 ZooKeeper 服务器之间的连接。
3888 (领导者): 用于领导者选举。
选择器: 选择带有 app: zookeeper 标签的 Pod。
ZooKeeper 有状态集 (StatefulSet)
副本数: 3(用于 ZooKeeper 的法定人数,保证集群可用性)。
服务名称: zookeeper(关联到上面的无头服务)。
环境变量:
ZOO_MY_ID: 根据 Pod 名称动态分配,例如 zookeeper-0、zookeeper-1、zookeeper-2。
ZOO_SERVERS: 定义集群中的服务器,确保它们可以相互通信。
数据卷挂载:
将 data 卷挂载到 /data 目录,存储 ZooKeeper 的数据。
持久化存储
PersistentVolume: 虽然配置中没有显示,但通常会通过 PersistentVolume 或动态存储供应配置持久存储。
VolumeClaimTemplates: 每个 ZooKeeper Pod 都有自己的 10Gi 存储,确保在 Pod 重启时数据不会丢失。
额外注意事项:
高可用性: 这个配置设计用于生产环境,具备高可用性和数据持久性。
动态 DNS: StatefulSet 中的 Pod 会有稳定的 DNS 名称,例如 zookeeper-0.zookeeper、zookeeper-1.zookeeper、zookeeper-2.zookeeper。
拉取镜像
docker pull registry.cn-hangzhou.aliyuncs.com/qiluo-images/zookeeper:latest
vi zookeeper-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: zookeepernamespace: default
spec:replicas: 1selector:matchLabels:app: zookeepertemplate:metadata:labels:app: zookeeperspec:containers:- name: zookeeperimage: registry.cn-hangzhou.aliyuncs.com/qiluo-images/zookeeper:latestports:- containerPort: 2181env:- name: ZOOKEEPER_SERVER_IDvalue: "1"- name: ZOOKEEPER_CLIENT_PORTvalue: "2181"- name: ZOOKEEPER_DATA_DIRvalue: "/data"volumeMounts:- name: zookeeper-datamountPath: /datavolumes:- name: zookeeper-dataemptyDir: {}
---
apiVersion: v1
kind: Service
metadata:name: zookeepernamespace: default
spec:ports:- port: 2181targetPort: 2181selector:app: zookeeper
创建 zookeeper-pv.yaml
vi zookeeper-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:name: zookeeper-pv
spec:accessModes:- ReadWriteOncecapacity:storage: 10GihostPath:path: /mnt/data/zookeeper
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: zookeeper-pvc
spec:accessModes:- ReadWriteOnceresources:requests:storage: 10Gi
创建 zookeeper-statefulset.yaml
vi zookeeper-statefulset.yaml
apiVersion: v1
kind: Service
metadata:name: zookeeperlabels:app: zookeeper
spec:ports:- port: 2181name: client- port: 2888name: follower- port: 3888name: leaderclusterIP: Noneselector:app: zookeeper
---
apiVersion: apps/v1
kind: StatefulSet
metadata:name: zookeeper
spec:serviceName: zookeeperreplicas: 3selector:matchLabels:app: zookeepertemplate:metadata:labels:app: zookeeperspec:containers:- name: zookeeperimage: registry.cn-hangzhou.aliyuncs.com/qiluo-images/zookeeper:latestports:- containerPort: 2181name: client- containerPort: 2888name: follower- containerPort: 3888name: leaderenv:- name: ZOO_MY_IDvalueFrom:fieldRef:fieldPath: metadata.name- name: ZOO_SERVERSvalue: "server.1=zookeeper-0.zookeeper:2888:3888;2181 server.2=zookeeper-1.zookeeper:2888:3888;2181 server.3=zookeeper-2.zookeeper:2888:3888;2181"volumeMounts:- name: datamountPath: /datavolumeClaimTemplates:- metadata:name: dataspec:accessModes: [ "ReadWriteOnce" ]resources:requests:storage: 10Gi
第二种方法
PersistentVolume 和 PersistentVolumeClaim
首先,定义 PersistentVolume 和 PersistentVolumeClaim,这样 ZooKeeper 的数据可以持久化。
vi zookeeper-pv-pvc.yaml
apiVersion: v1
kind: PersistentVolume
metadata:name: zookeeper-pv
spec:accessModes:- ReadWriteOncecapacity:storage: 10GihostPath:path: /mnt/data/zookeeper
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: zookeeper-pvc
spec:accessModes:- ReadWriteOnceresources:requests:storage: 10Gi
vi zookeeper-deployment.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:name: zookeepernamespace: default
spec:serviceName: "zookeeper"replicas: 3selector:matchLabels:app: zookeepertemplate:metadata:labels:app: zookeeperspec:containers:- name: zookeeperimage: registry.cn-hangzhou.aliyuncs.com/qiluo-images/zookeeper:latestports:- containerPort: 2181- containerPort: 2888- containerPort: 3888env:- name: ZOO_MY_IDvalueFrom:fieldRef:fieldPath: metadata.name- name: ZOO_SERVERSvalue: "zookeeper-0.zookeeper:2888:3888,zookeeper-1.zookeeper:2888:3888,zookeeper-2.zookeeper:2888:3888"volumeMounts:- name: zookeeper-datamountPath: /datavolumeClaimTemplates:- metadata:name: zookeeper-dataspec:accessModes: ["ReadWriteOnce"]resources:requests:storage: 10Gi
---
apiVersion: v1
kind: Service
metadata:name: zookeepernamespace: default
spec:ports:- port: 2181targetPort: 2181- port: 2888targetPort: 2888- port: 3888targetPort: 3888clusterIP: Noneselector:app: zookeeper
kubectl apply -f zookeeper-deployment.yamlkubectl apply -f zookeeper-pv-pvc.yaml
这样命令简单了许多。