与前面的zookeeper一样需要持久化存储,因此还是先创建PV,然后再部署kafka。
2、配置nfs
创建需要作为nfs共享的目录 mkdir /home/nfs/kafka-0
添加权限 chmod 777 /home/nfs/kafka-0
编辑配置
root@ubuntu:# vim /etc/export
# /etc/exports: the access control list for filesystems which may be exported
# to NFS clients. See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4 gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes gss/krb5i(rw,sync,no_subtree_check)
#
/home/nfs/zookeeper-0 *(rw,sync,no_root_squash)
/home/nfs/zookeeper-1 *(rw,sync,no_root_squash)
/home/nfs/zookeeper-2 *(rw,sync,no_root_squash)
/home/nfs/kafka-0 *(rw,sync,no_root_squash)
/home/nfs/kafka-1 *(rw,sync,no_root_squash)
/home/nfs/kafka-2 *(rw,sync,no_root_squash)
重启服务使配置生效
root@ubuntu:# /etc/init.d/rpcbind restart
[ ok ] Restarting rpcbind (via systemctl): rpcbind.service.
root@ubuntu:# /etc/init.d/nfs-kernel-server restart
[ ok ] Restarting nfs-kernel-server (via systemctl): nfs-kernel-server.service.
root@ubuntu:# showmount -e
Export list for ubuntu:
/home/nfs/kafka-2 *
/home/nfs/kafka-1 *
/home/nfs/kafka-0 *
/home/nfs/zookeeper-2 *
/home/nfs/zookeeper-1 *
/home/nfs/zookeeper-0 *
看到以上信息就表示nfs的服务已经搭建好了。
部署kafka
kafka是一个分布式、多副本、多订阅者、分区的,基于zoopkeeper协调的分布式日志系统。其主要特点为:
1.以时间复杂度为O(1)的方式提供消息持久化能力,即使对TB级以上的数据也能保证常数时间的访问性能。
2.高吞吐率。即使在非常廉价的商用机器上也能做到单机支持每秒100K消息的传输。
3.支持kafka server间的消息分区以及分布式消费,同时保证每个partition内的消息顺序和传输。
4.同时支持离线数据处理和实时数据处理。
以下是部署kafka的pv.yaml,需要注意server的字段填充部署了nfs服务的主机ip
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-kafka-0
namespace: storm
spec:
capacity:
storage: 20Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs-kafka-storage
nfs:
path: /home/nfs/kafka-0
server: 192.168.45.227
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-kafka-1
namespace: storm
spec:
capacity:
storage: 20Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs-kafka-storage
nfs:
path: /home/nfs/kafka-1
server: 192.168.45.227
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-kafka-2
namespace: storm
spec:
capacity:
storage: 20Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs-kafka-storage
nfs:
path: /home/nfs/kafka-2
server: 192.168.45.227
以下是部署kafka的yaml文件,zookeeper和kakfa都是有状态的服务,不能用Deployment来部署,因此使用StatefulSet。
---
apiVersion: v1
kind: Service
metadata:
name: kafka-service
namespace: storm
labels:
app: kafka
spec:
type: NodePort
ports:
- port: 9092
name: kafka-server
clusterIP: None
selector:
app: kafka
---
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
name: kafka-pdb
namespace: storm
spec:
selector:
matchLabels:
app: kafka
minAvailable: 2
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: kafka
namespace: storm
spec:
selector:
matchLabels:
app: kafka
serviceName: kafka-service
replicas: 3
template:
metadata:
labels:
app: kafka
spec:
tolerations:
- key: "travis.io/schedule-only"
operator: "Equal"
value: "kafka"
effect: "NoSchedule"
- key: "travis.io/schedule-only"
operator: "Equal"
value: "kafka"
effect: "NoExecute"
tolerationSeconds: 3600
- key: "travis.io/schedule-only"
operator: "Equal"
value: "kafka"
effect: "PreferNoSchedule"
terminationGracePeriodSeconds: 300
containers:
- name: k8s-kafka
imagePullPolicy: Always
image: fastop/kafka:2.2.0
resources:
requests:
memory: "1Gi"
cpu: "1"
ports:
- containerPort: 9092
name: server
command:
- sh
- -c
- "exec kafka-server-start.sh /opt/kafka/config/server.properties --override broker.id=${HOSTNAME##*-} \
--override listeners=PLAINTEXT://:9092 \
--override advertised.listeners=PLAINTEXT://$(hostname).kafka-service.storm.svc.cluster.local:9092 \
--override zookeeper.connect=zookeeper-0.zk-inner-service.storm.svc.cluster.local:2181,zookeeper-1.zk-inner-service.storm.svc.cluster.local:2181,zookeeper-2.zk-inner-service.storm.svc.cluster.local:2181 \
--override log.dir=/var/lib/kafka \
--override auto.create.topics.enable=true \
--override auto.leader.rebalance.enable=true \
--override background.threads=10 \
--override compression.type=producer \
--override delete.topic.enable=false \
--override leader.imbalance.check.interval.seconds=300 \
--override leader.imbalance.per.broker.percentage=10 \
--override log.flush.interval.messages=9223372036854775807 \
--override log.flush.offset.checkpoint.interval.ms=60000 \
--override log.flush.scheduler.interval.ms=9223372036854775807 \
--override log.retention.bytes=-1 \
--override log.retention.hours=168 \
--override log.roll.hours=168 \
--override log.roll.jitter.hours=0 \
--override log.segment.bytes=1073741824 \
--override log.segment.delete.delay.ms=60000 \
--override message.max.bytes=1000012 \
--override min.insync.replicas=1 \
--override num.io.threads=8 \
--override num.network.threads=3 \
--override num.recovery.threads.per.data.dir=1 \
--override num.replica.fetchers=1 \
--override offset.metadata.max.bytes=4096 \
--override offsets.commit.required.acks=-1 \
--override offsets.commit.timeout.ms=5000 \
--override offsets.load.buffer.size=5242880 \
--override offsets.retention.check.interval.ms=600000 \
--override offsets.retention.minutes=1440 \
--override offsets.topic.compression.codec=0 \
--override offsets.topic.num.partitions=50 \
--override offsets.topic.replication.factor=3 \
--override offsets.topic.segment.bytes=104857600 \
--override queued.max.requests=500 \
--override quota.consumer.default=9223372036854775807 \
--override quota.producer.default=9223372036854775807 \
--override replica.fetch.min.bytes=1 \
--override replica.fetch.wait.max.ms=500 \
--override replica.high.watermark.checkpoint.interval.ms=5000 \
--override replica.lag.time.max.ms=10000 \
--override replica.socket.receive.buffer.bytes=65536 \
--override replica.socket.timeout.ms=30000 \
--override request.timeout.ms=30000 \
--override socket.receive.buffer.bytes=102400 \
--override socket.request.max.bytes=104857600 \
--override socket.send.buffer.bytes=102400 \
--override unclean.leader.election.enable=true \
--override zookeeper.session.timeout.ms=6000 \
--override zookeeper.set.acl=false \
--override broker.id.generation.enable=true \
--override connections.max.idle.ms=600000 \
--override controlled.shutdown.enable=true \
--override controlled.shutdown.max.retries=3 \
--override controlled.shutdown.retry.backoff.ms=5000 \
--override controller.socket.timeout.ms=30000 \
--override default.replication.factor=1 \
--override fetch.purgatory.purge.interval.requests=1000 \
--override group.max.session.timeout.ms=300000 \
--override group.min.session.timeout.ms=6000 \
--override inter.broker.protocol.version=2.2.0 \
--override log.cleaner.backoff.ms=15000 \
--override log.cleaner.dedupe.buffer.size=134217728 \
--override log.cleaner.delete.retention.ms=86400000 \
--override log.cleaner.enable=true \
--override log.cleaner.io.buffer.load.factor=0.9 \
--override log.cleaner.io.buffer.size=524288 \
--override log.cleaner.io.max.bytes.per.second=1.7976931348623157E308 \
--override log.cleaner.min.cleanable.ratio=0.5 \
--override log.cleaner.min.compaction.lag.ms=0 \
--override log.cleaner.threads=1 \
--override log.cleanup.policy=delete \
--override log.index.interval.bytes=4096 \
--override log.index.size.max.bytes=10485760 \
--override log.message.timestamp.difference.max.ms=9223372036854775807 \
--override log.message.timestamp.type=CreateTime \
--override log.preallocate=false \
--override log.retention.check.interval.ms=300000 \
--override max.connections.per.ip=2147483647 \
--override num.partitions=4 \
--override producer.purgatory.purge.interval.requests=1000 \
--override replica.fetch.backoff.ms=1000 \
--override replica.fetch.max.bytes=1048576 \
--override replica.fetch.response.max.bytes=10485760 \
--override reserved.broker.max.id=1000 "
env:
- name: KAFKA_HEAP_OPTS
value : "-Xmx512M -Xms512M"
- name: KAFKA_OPTS
value: "-Dlogging.level=INFO"
volumeMounts:
- name: datadir
mountPath: /var/lib/kafka
readinessProbe:
tcpSocket:
port: 9092
timeoutSeconds: 1
initialDelaySeconds: 5
securityContext:
runAsUser: 1000
fsGroup: 1000
volumeClaimTemplates:
- metadata:
name: datadir
spec:
storageClassName: nfs-kafka-storage
accessModes: [ "ReadWriteMany" ]
resources:
requests:
storage: 20Gi
执行部署以上的yaml文件
root@ubuntu:# kubectl apply -f kafka-pv.yaml
root@ubuntu:# kubectl apply -f kafka.yaml
root@ubuntu:# kubectl -n storm get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv-kafka-0 20Gi RWX Retain Bound storm/datadir-kafka-0 nfs-kafka-storage 46s
pv-kafka-1 20Gi RWX Retain Bound storm/datadir-kafka-1 nfs-kafka-storage 46s
pv-kafka-2 20Gi RWX Retain Bound storm/datadir-kafka-2 nfs-kafka-storage 46s
pv-zookeeper-0 20Gi RWO Retain Bound storm/datadir-zookeeper-2 nfs-storage 13m44s
pv-zookeeper-1 20Gi RWO Retain Bound storm/datadir-zookeeper-0 nfs-storage 13m44s
pv-zookeeper-2 20Gi RWO Retain Bound storm/datadir-zookeeper-1 nfs-storage 13m44s
root@ubuntu:# kubectl -n storm get pod
NAME READY STATUS RESTARTS AGE
kafka-0 1/1 Running 0 28s
kafka-1 1/1 Running 0 15s
kafka-2 1/1 Running 0 8s
zookeeper-0 1/1 Running 0 13m8s
zookeeper-1 1/1 Running 0 13m8s
zookeeper-2 1/1 Running 0 13m8s
在zookeeper中查看broker
root@ubuntu:# kubectl -n storm exec -it zookeeper-0 -- zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls /brokers/ids
[0, 1, 2]
[zk: localhost:2181(CONNECTED) 1] get /brokers/ids/0
{"listener_security_protocol_map":{"PLAINTEXT":"PLAINTEXT"},"endpoints":["PLAINTEXT://kafka-0.kafka-service.storm.svc.cluster.local:9092"],"jmx_port":-1,"host":"kafka-0.kafka-service.storm.svc.cluster.local","timestamp":"1689167921722","port":9092,"version":4}
cZxid = 0x10000620f
ctime = Wed Jul 12 13:18:41 UTC 2023
mZxid = 0x10000620f
mtime = Wed Jul 12 13:18:41 UTC 2023
pZxid = 0x10000620f
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x389496aa40b0035
dataLength = 260
numChildren = 0
[zk: localhost:2181(CONNECTED) 2] get /brokers/ids/1
{"listener_security_protocol_map":{"PLAINTEXT":"PLAINTEXT"},"endpoints":["PLAINTEXT://kafka-1.kafka-service.storm.svc.cluster.local:9092"],"jmx_port":-1,"host":"kafka-1.kafka-service.storm.svc.cluster.local","timestamp":"1689167899067","port":9092,"version":4}
cZxid = 0x100006189
ctime = Wed Jul 12 13:18:19 UTC 2023
mZxid = 0x100006189
mtime = Wed Jul 12 13:18:19 UTC 2023
pZxid = 0x100006189
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x189496aa354004c
dataLength = 260
numChildren = 0
[zk: localhost:2181(CONNECTED) 3] get /brokers/ids/2
{"listener_security_protocol_map":{"PLAINTEXT":"PLAINTEXT"},"endpoints":["PLAINTEXT://kafka-2.kafka-service.storm.svc.cluster.local:9092"],"jmx_port":-1,"host":"kafka-2.kafka-service.storm.svc.cluster.local","timestamp":"1689167877630","port":9092,"version":4}
cZxid = 0x1000060f3
ctime = Wed Jul 12 13:17:57 UTC 2023
mZxid = 0x1000060f3
mtime = Wed Jul 12 13:17:57 UTC 2023
pZxid = 0x1000060f3
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x289496abbc30037
dataLength = 260
numChildren = 0
可以看到3个broker都在zookeeper中注册成功了,后续可以自行进行topic的创建以及producer、comsumer的消息测试。
标签:log,kubernetes,部署,zookeeper,kafka,--,nfs,override From: https://www.cnblogs.com/Modest-Hamilton/p/17551146.html