其实网络上有很多Kubernetes部署zookeeper集群的文章,搜了好多总觉得每个文章都回拉下一点东西,所以弄好之后把这个yaml文件发到这里,希望有帮到大家。
这里着重说几点:
1、在containers中如果加command字段可能会造成容器直接认为运行结束退出,所以需要初始化容器的情况下,可以先initcontainers来处理,初始化完成后再正常启动。containers和initcontainers的顺序感觉应该没有影响;
2、如果初始化容器是需要在挂在的pvc中进行操作,那么就需要在initcontainers和containers中都进行volumeMounts操作,我一开始也以为只要在initcontainers中进行了挂在就不需要再containers中再挂载了;
3、因为集群启动zookeeper的时候需要指定myid,由于kubernetes启动的zookeeper一般是从zookeeper-0开始,但是集群中的myid一般是从1开始,所以在初始化文件时候可能还需要一个简单的运算赋值。
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: zookeeper
spec:
selector:
matchLabels:
app: zookeeper
serviceName: "zookeeper"
replicas: 3
template:
metadata:
labels:
app: zookeeper
spec:
terminationGracePeriodSeconds: 30
containers:
- name: zookeeper
image: <your-image>
imagePullPolicy: Always
ports:
- containerPort: 2181
name: client
- containerPort: 2888
name: server
- containerPort: 3888
name: leader-election
volumeMounts:
- name: data
mountPath: /opt/zookeeper/data
- name: config
mountPath: /conf
readOnly: true
resources: # 设置资源限制
limits:
cpu: "1"
memory: "1Gi"
requests:
cpu: "1"
memory: "1Gi"
initContainers:
- name: init-myid
image: <your-image>
command: ["sh", "-c", "myid=`echo $(hostname | awk -F'-' '{print $NF}')` && mkdir -pv /opt/zookeeper/logs && if [ ! -f /opt/zookeeper/data/myid ]; then echo $myid && echo $(($myid + 1)) > /opt/zookeeper/data/myid; fi"]
volumeMounts:
- name: data
mountPath: /opt/zookeeper/data
- name: config
mountPath: /conf
readOnly: true
volumes:
- name: config
configMap:
name: zookeeper-config
items:
- key: zoo.cfg
path: zoo.cfg
- key: log4j.properties
path: log4j.properties
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "nfs-storage"
resources:
requests:
storage: 1Gi