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: zookeeper
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: zookeeper
template:
metadata:
labels:
app: zookeeper
spec:
containers:
- name: zookeeper
image: registry.cn-hangzhou.aliyuncs.com/qiluo-images/zookeeper:latest
ports:
- containerPort: 2181
env:
- name: ZOOKEEPER_SERVER_ID
value: "1"
- name: ZOOKEEPER_CLIENT_PORT
value: "2181"
- name: ZOOKEEPER_DATA_DIR
value: "/data"
volumeMounts:
- name: zookeeper-data
mountPath: /data
volumes:
- name: zookeeper-data
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
name: zookeeper
namespace: default
spec:
ports:
- port: 2181
targetPort: 2181
selector:
app: zookeeper
创建 zookeeper-pv.yaml
vi zookeeper-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: zookeeper-pv
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 10Gi
hostPath:
path: /mnt/data/zookeeper
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: zookeeper-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
创建 zookeeper-statefulset.yaml
vi zookeeper-statefulset.yaml
apiVersion: v1
kind: Service
metadata:
name: zookeeper
labels:
app: zookeeper
spec:
ports:
- port: 2181
name: client
- port: 2888
name: follower
- port: 3888
name: leader
clusterIP: None
selector:
app: zookeeper
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: zookeeper
spec:
serviceName: zookeeper
replicas: 3
selector:
matchLabels:
app: zookeeper
template:
metadata:
labels:
app: zookeeper
spec:
containers:
- name: zookeeper
image: registry.cn-hangzhou.aliyuncs.com/qiluo-images/zookeeper:latest
ports:
- containerPort: 2181
name: client
- containerPort: 2888
name: follower
- containerPort: 3888
name: leader
env:
- name: ZOO_MY_ID
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: ZOO_SERVERS
value: "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: data
mountPath: /data
volumeClaimTemplates:
- metadata:
name: data
spec:
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:
- ReadWriteOnce
capacity:
storage: 10Gi
hostPath:
path: /mnt/data/zookeeper
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: zookeeper-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
vi zookeeper-deployment.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: zookeeper
namespace: default
spec:
serviceName: "zookeeper"
replicas: 3
selector:
matchLabels:
app: zookeeper
template:
metadata:
labels:
app: zookeeper
spec:
containers:
- name: zookeeper
image: registry.cn-hangzhou.aliyuncs.com/qiluo-images/zookeeper:latest
ports:
- containerPort: 2181
- containerPort: 2888
- containerPort: 3888
env:
- name: ZOO_MY_ID
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: ZOO_SERVERS
value: "zookeeper-0.zookeeper:2888:3888,zookeeper-1.zookeeper:2888:3888,zookeeper-2.zookeeper:2888:3888"
volumeMounts:
- name: zookeeper-data
mountPath: /data
volumeClaimTemplates:
- metadata:
name: zookeeper-data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 10Gi
---
apiVersion: v1
kind: Service
metadata:
name: zookeeper
namespace: default
spec:
ports:
- port: 2181
targetPort: 2181
- port: 2888
targetPort: 2888
- port: 3888
targetPort: 3888
clusterIP: None
selector:
app: zookeeper
kubectl apply -f zookeeper-deployment.yaml
kubectl apply -f zookeeper-pv-pvc.yaml
这样命令简单了许多。