k8s搭建zookeeper和kafka的yaml
时间: 2023-08-16 21:29:28 浏览: 200
基于k8s的zookeeper集群
以下是使用Kubernetes搭建Zookeeper和Kafka集群的YAML文件示例:
Zookeeper YAML文件示例:
```yaml
apiVersion: v1
kind: Service
metadata:
name: zk-headless
labels:
app: zookeeper
spec:
clusterIP: None
selector:
app: zookeeper
ports:
- name: server
port: 2888
targetPort: 2888
- name: leader-election
port: 3888
targetPort: 3888
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: zk
spec:
selector:
matchLabels:
app: zookeeper
serviceName: zk-headless
replicas: 3
template:
metadata:
labels:
app: zookeeper
spec:
containers:
- name: zk
image: zookeeper:3.5.6
ports:
- containerPort: 2181
name: client
- containerPort: 2888
name: server
- containerPort: 3888
name: leader-election
env:
- name: ZOO_MY_ID
value: "0"
- name: ZOO_SERVERS
value: "zk-0.zk-headless.default.svc.cluster.local:2888:3888;z
k-1.zk-headless.default.svc.cluster.local:2888:3888;zk-2.zk-headless.default.svc.cluster.local:2888:3888"
volumeMounts:
- name: datadir
mountPath: /data
readinessProbe:
exec:
command:
- sh
- -c
- "echo mntr | nc localhost 2181 | grep zk_version"
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
exec:
command:
- sh
- -c
- "echo mntr | nc localhost 2181 | grep zk_server_state | grep -q standalone || exit 1"
initialDelaySeconds: 15
periodSeconds: 20
volumes:
- name: datadir
persistentVolumeClaim:
claimName: datadir
volumeClaimTemplates:
- metadata:
name: datadir
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
```
Kafka YAML文件示例:
```yaml
apiVersion: v1
kind: Service
metadata:
name: kafka-headless
labels:
app: kafka
spec:
clusterIP: None
selector:
app: kafka
ports:
- name: port-0
port: 9092
targetPort: 9092
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: kafka
spec:
selector:
matchLabels:
app: kafka
serviceName: kafka-headless
replicas: 3
template:
metadata:
labels:
app: kafka
spec:
containers:
- name: kafka
image: wurstmeister/kafka:2.12-2.3.0
env:
- name: KAFKA_ADVERTISED_HOST_NAME
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: KAFKA_ZOOKEEPER_CONNECT
value: "zk-headless.default.svc.cluster.local:2181"
- name: KAFKA_BROKER_ID
valueFrom:
fieldRef:
fieldPath: metadata.uid
- name: KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR
value: "3"
- name: KAFKA_NUM_PARTITIONS
value: "3"
- name: KAFKA_HEAP_OPTS
value: "-Xmx1G -Xms1G"
ports:
- containerPort: 9092
name: port-0
volumeMounts:
- name: datadir
mountPath: /kafka
livenessProbe:
exec:
command:
- sh
- -c
- "/opt/kafka/bin/kafka-broker-api-versions.sh --bootstrap-server localhost:9092"
initialDelaySeconds: 60
periodSeconds: 10
volumes:
- name: datadir
persistentVolumeClaim:
claimName: datadir
volumeClaimTemplates:
- metadata:
name: datadir
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 5Gi
```
请注意,这些示例假定您已经创建了Kubernetes的PersistentVolumeClaim资源以供Zookeeper和Kafka使用,并且已经将它们的名称设置为datadir。如果您还没有这些资源,请先创建它们。
阅读全文