歡迎訪問我的GitHub
這里分類和匯總了欣宸的全部原創(chuàng)(含配套源碼):https://github.com/zq2599/blog_demos
本篇概覽
- 本篇是《strimzi實戰(zhàn)》系列的第二篇,前文完成了介紹和準備工作,是時候體驗strimzi的核心功能了:發(fā)送和接受kafka消息,為了完成這個目標,本篇會按照如下步驟開始實戰(zhàn):
- 在kubernetes環(huán)境部署strimzi,這里面包含兩個步驟:首先是將各類資源創(chuàng)建好,然后再啟動strmzi
- 驗證基本功能:發(fā)送和接受kafka消息,這里面有兩種操作(注意,是兩種里面二選一,不是兩步):如果您的kubernetes環(huán)境有pv,就可以選擇使用pv的操作步驟,如果您沒有pv,就選擇不用pv的操作步驟
- 刪除操作
- 接下來開始實戰(zhàn)
部署
- 創(chuàng)建namespace
kubectl create namespace kafka
- 部署角色、權(quán)限、CRD等資源
kubectl create -f 'https://strimzi.io/install/latest?namespace=kafka' -n kafka
啟動
- 接下來的啟動操作,根據(jù)您的實際情況,有兩種可選
- 第一種:如果您的k8s環(huán)境已經(jīng)準備好了pv,請執(zhí)行以下命令完成部署,strimzi會通過pvc去申請使用pv,這樣就算pod有問題被刪除重建了,kafka消息的數(shù)據(jù)也不會丟失
kubectl apply -f https://strimzi.io/examples/latest/kafka/kafka-persistent-single.yaml -n kafka
- 第二種,如果您的k8s環(huán)境還沒有準備好pv,請執(zhí)行以下命令完成部署,這樣創(chuàng)建的kafka服務也能正常使用,只不過所有數(shù)據(jù)都存在pod中,一旦pod被刪除,數(shù)據(jù)就找不回來了
kubectl apply -f https://strimzi.io/examples/latest/kafka/kafka-ephemeral-single.yaml -n kafka
- 以上兩種方式只要選擇一種去執(zhí)行即可,執(zhí)行完命令后,需要等待鏡像下載和服務創(chuàng)建,尤其是鏡像下載,實測真的慢啊,我用騰訊云服務器大約等了七八分鐘
[root@VM-12-12-centos ~]# kubectl get pod -n kafka
NAME READY STATUS RESTARTS AGE
strimzi-cluster-operator-566948f58c-h2t6g 0/1 ContainerCreating 0 16m
- 等到operator的pod運行起來后,就該創(chuàng)建zookeeper的pod了,繼續(xù)等鏡像下載...
[root@VM-12-12-centos ~]# kubectl get pods -n kafka
NAME READY STATUS RESTARTS AGE
my-cluster-zookeeper-0 0/1 ContainerCreating 0 7m59s
my-cluster-zookeeper-1 0/1 ContainerCreating 0 7m59s
my-cluster-zookeeper-2 0/1 ContainerCreating 0 7m59s
strimzi-cluster-operator-566948f58c-h2t6g 1/1 Running 0 24m
- 如下圖紅色箭頭所指,顯示正在拉取zookeeper鏡像
- 等到zookeeper的pod創(chuàng)建完成后,終于輪到主角登場了:開始kafka的pod創(chuàng)建,最后,來個全家福,如下所示,一套具備基本功能的kafka環(huán)境
[root@VM-12-12-centos ~]# kubectl get pods -n kafka
NAME READY STATUS RESTARTS AGE
my-cluster-entity-operator-66598599fc-sskcx 3/3 Running 0 73s
my-cluster-kafka-0 1/1 Running 0 96s
my-cluster-zookeeper-0 1/1 Running 0 14m
my-cluster-zookeeper-1 1/1 Running 0 14m
my-cluster-zookeeper-2 1/1 Running 0 14m
strimzi-cluster-operator-566948f58c-h2t6g 1/1 Running 0 30m
基本操作:收發(fā)消息
- strimzi部署已經(jīng)OK,現(xiàn)在收發(fā)消息試試,看kafka基本功能是否正常
- 接下來的操作需要兩個控制臺窗口,一個用于發(fā)消息,一個用于收消息
- 在發(fā)消息的窗口輸入以下命令,就會創(chuàng)建名為my-topic的topic,并且進入發(fā)送消息的模式
kubectl -n kafka \
run kafka-producer \
-ti \
--image=quay.io/strimzi/kafka:0.32.0-kafka-3.3.1 \
--rm=true \
--restart=Never \
-- bin/kafka-console-producer.sh --bootstrap-server my-cluster-kafka-bootstrap:9092 --topic my-topic
- 在收消息的窗口輸入以下命令,就會進入消費消息的模式,topic是my-topic
kubectl -n kafka \
run kafka-consumer \
-ti \
--image=quay.io/strimzi/kafka:0.32.0-kafka-3.3.1 \
--rm=true \
--restart=Never \
-- bin/kafka-console-consumer.sh --bootstrap-server my-cluster-kafka-bootstrap:9092 --topic my-topic --from-beginning
-
然后,在發(fā)送消息的窗口輸入一些文字后再回車,消息就會發(fā)送出去,如下圖,左側(cè)紅框顯示一共發(fā)送了四次消息,最后一次是空字符串,右側(cè)黃框顯示成功收到四條消息
-
如果您的kubernetes環(huán)境是按照《快速搭建云原生開發(fā)環(huán)境(k8s+pv+prometheus+grafana)》的方法來部署的,現(xiàn)在就能通過grafana看到命名空間kafka下面的資源了,如下圖
-
另外,如果您使用了pv,還可以關(guān)注一下pv的使用情況,如下圖,kafka的zookeeper的數(shù)據(jù)都改為外部存儲了,數(shù)據(jù)不會因為pod問題而丟失
-
不過由于我們還沒有將strimzi的監(jiān)控配置好,現(xiàn)在還看不到kafka業(yè)務相關(guān)的指標情況,只能從k8s維度去查看pod的基本指標,這些會在后面的章節(jié)補齊文章來源:http://www.zghlxwxcb.cn/news/detail-712133.html
刪除操作
- 如果需要把strimzi從kubernetes環(huán)境刪除,執(zhí)行以下操作即可:
- 如果您使用了pv,就執(zhí)行以下命令完成刪除
kubectl delete -f https://strimzi.io/examples/latest/kafka/kafka-persistent-single.yaml -n kafka \
&& kubectl delete -f 'https://strimzi.io/install/latest?namespace=kafka' -n kafka \
&& kubectl delete namespace kafka
- 如果您沒有使用pv,就執(zhí)行以下命令完成刪除
kubectl delete -f https://strimzi.io/examples/latest/kafka/kafka-ephemeral-single.yaml -n kafka \
&& kubectl delete -f 'https://strimzi.io/install/latest?namespace=kafka' -n kafka \
&& kubectl delete namespace kafka
- 再去檢查所有pod,已看不到strimzi的痕跡
[root@VM-12-12-centos ~]# kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
calico-apiserver calico-apiserver-67b7856948-bg2wh 1/1 Running 0 6d2h
calico-apiserver calico-apiserver-67b7856948-fz64n 1/1 Running 0 6d2h
calico-system calico-kube-controllers-78687bb75f-z2r7m 1/1 Running 0 6d2h
calico-system calico-node-l6nmw 1/1 Running 0 6d2h
calico-system calico-typha-b46ff96f6-qqzxb 1/1 Running 0 6d2h
calico-system csi-node-driver-lv2g2 2/2 Running 0 6d2h
kafka my-cluster-entity-operator-66598599fc-fz7wx 3/3 Running 0 4m57s
kafka my-cluster-kafka-0 1/1 Running 0 5m22s
kafka my-cluster-zookeeper-0 1/1 Running 0 5m48s
kafka strimzi-cluster-operator-566948f58c-pj45s 1/1 Running 0 6m15s
kube-system coredns-78fcd69978-57r7x 1/1 Running 0 6d2h
kube-system coredns-78fcd69978-psjcs 1/1 Running 0 6d2h
kube-system etcd-vm-12-12-centos 1/1 Running 0 6d2h
kube-system kube-apiserver-vm-12-12-centos 1/1 Running 0 6d2h
kube-system kube-controller-manager-vm-12-12-centos 1/1 Running 0 6d2h
kube-system kube-proxy-x8nhg 1/1 Running 0 6d2h
kube-system kube-scheduler-vm-12-12-centos 1/1 Running 0 6d2h
local-path-storage local-path-provisioner-55d894cf7f-mpd2n 1/1 Running 0 3d21h
monitoring alertmanager-main-0 2/2 Running 0 24h
monitoring alertmanager-main-1 2/2 Running 0 24h
monitoring alertmanager-main-2 2/2 Running 0 24h
monitoring blackbox-exporter-6798fb5bb4-4hmf7 3/3 Running 0 24h
monitoring grafana-d9c6954b-qts2s 1/1 Running 0 24h
monitoring kube-state-metrics-5fcb7d6fcb-szmh9 3/3 Running 0 24h
monitoring node-exporter-4fhb6 2/2 Running 0 24h
monitoring prometheus-adapter-7dc46dd46d-245d7 1/1 Running 0 24h
monitoring prometheus-adapter-7dc46dd46d-sxcn2 1/1 Running 0 24h
monitoring prometheus-k8s-0 2/2 Running 0 24h
monitoring prometheus-k8s-1 2/2 Running 0 24h
monitoring prometheus-operator-7ddc6877d5-d76wk 2/2 Running 0 24h
tigera-operator tigera-operator-6f669b6c4f-t8t9h 1/1 Running 0 6d2h
- 不過,對于pv來說,由于使用的策略是Retain,因此還會繼續(xù)存在
- 至此,strimzi基本功能實戰(zhàn)已經(jīng)完成,咱們知道了如何快速部署strimzi和收發(fā)消息,感受到operator給我們帶來的便利,接下來的文章,還會有更多簡單的操作,更多精彩的功能等著咱們?nèi)L試,歡迎您繼續(xù)關(guān)注欣宸原創(chuàng),咱們一起學習共同進步
歡迎關(guān)注博客園:程序員欣宸
學習路上,你不孤單,欣宸原創(chuàng)一路相伴...文章來源地址http://www.zghlxwxcb.cn/news/detail-712133.html
到了這里,關(guān)于strimzi實戰(zhàn)之二:部署和消息功能初體驗的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!