昨天介紹了kafka的機制,今天將會透過strimzi的方式配置kafka cluster,同樣的會介紹用operator的方式進行佈署,strimzi可以管理kafka的叢集、user、topic跟bridge連線,那麼就來嘗試玩看看吧。
首先創建一個kafka的namespaces
kubectl create ns strimzi
#下載yaml
wget https://strimzi.io/install/latest?namespace=strimzi
kubectl apply -f yaml -n strimzi
完成crd後就可以創建kafka cluster的instance囉
apiVersion: kafka.strimzi.io/v1beta2
kind: Kafka
metadata:
name: lab1-kafka
spec:
kafka:
version: 2.8.0
replicas: 1
listeners:
- name: plain
port: 9092
type: internal
tls: false
- name: tls
port: 9093
type: internal
tls: true
config:
offsets.topic.replication.factor: 1
transaction.state.log.replication.factor: 1
transaction.state.log.min.isr: 1
log.message.format.version: "2.8"
inter.broker.protocol.version: "2.8"
storage:
type: jbod
volumes:
- id: 0
type: persistent-claim
size: 20Gi
deleteClaim: false
template:
pod:
securityContext:
runAsUser: 0
fsGroup: 0
zookeeper:
replicas: 1
storage:
type: persistent-claim
size: 20Gi
deleteClaim: false
template:
pod:
securityContext:
runAsUser: 0
fsGroup: 0
entityOperator:
topicOperator: {}
userOperator: {}
這樣就會建出kafka囉,那就來使用看看吧
kubectl -n strimzi run kafka-producer -ti --image=quay.io/strimzi/kafka:0.25.0-kafka-2.8.0 --rm=true --restart=Never -- bin/kafka-console-producer.sh --broker-list my-cluster-kafka-bootstrap:9092 --topic my-topic
kubectl -n strimzi run kafka-consumer-2 -ti --image=quay.io/strimzi/kafka:0.25.0-kafka-2.8.0 --rm=true --restart=Never -- bin/kafka-console-consumer.sh --bootstrap-server my-cluster-kafka-bootstrap:9092 --topic my-topic --from-beginning
看起來沒什麼問題呢,那麼要從外部使用kafka的話,strimzi有開發kafka-bridge讓外部使用http或是amqp的方式連線進去使用kafka,而topic也可以用crd進行控管。