kafka在k8s下最大的坑是访问通讯问题,折腾了4天,今早灵感来解决了
仓库地址https://github.com/liangguohun/dynamic-pvc.git
目录 elk\kafka
RC
核心在于/var/run/docker.sock 挂载 及使用status.hostIP 宿主机ip
apiVersion: v1
kind: ReplicationController
metadata:
name: kafka-rc
spec:
replicas: 1
selector:
name: kafka-pod
template:
metadata:
labels:
name: kafka-pod
spec:
# hostNetwork: true
# dnsPolicy: ClusterFirstWithHostNet
hostAliases:
# - ip: "192.168.1.5"
# hostnames:
# - "fuck.kafka"
containers:
- name: kafka
image: 192.168.1.4:5000/liangguohun/kafka:1.0
imagePullPolicy: IfNotPresent
env:
- name: TZ
value: Asia/Shanghai
- name: KAFKA_ADVERTISED_HOST_NAME
valueFrom:
fieldRef:
fieldPath: status.hostIP
- name: ZOOKEEPER_DATADIR
value: "/workspace/data"
ports:
- name: kafka
containerPort: 9092
volumeMounts:
- mountPath: /tmp/kafka-logs
name: log-data
- mountPath: /var/app/config/server.properties
name: kafka-conf
subPath: server.properties
- mountPath: /var/run/docker.sock
name: connect
restartPolicy: "Always"
volumes:
- name: connect
hostPath:
path: /var/run/docker.sock
- name: log-data
persistentVolumeClaim:
claimName: kafka-claim
- name: kafka-conf
configMap:
name: kafka-conf
SVC
之前一直通讯不ok最后发现是 selector cp 改的不匹配
apiVersion: v1
kind: Service
metadata:
name: kafka
labels:
name: kafka
spec:
type: NodePort
ports:
- name: kafka
port: 9092
targetPort: kafka
nodePort: 9092
selector:
name: kafka-pod
server.properies
相应的属性 在此修改
broker.id=0
listeners=PLAINTEXT://:9092
listener.security.protocol.map=PLAINTEXT:PLAINTEXT
zookeeper.connect=192.168.1.4:2181
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/tmp/kafka-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
#log.flush.interval.messages=10000
#log.flush.interval.ms=1000
log.retention.hours=168
#log.retention.bytes=1073741824
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connection.timeout.ms=6000
group.initial.rebalance.delay.ms=0
注
advertised.listeners
根据rc配置在env.sh 去掉注释的配置自动补上
dataDir 要与zk配置的路径匹配