1. 程式人生 > 其它 >helm部署zookeeper+kafka叢集

helm部署zookeeper+kafka叢集

技術標籤:kuberneteszookeeper+kafka

helm部署zookeeper+kafka叢集

系統環境

一個k8s叢集,安裝helm,實現動態PV供給(非必須),並且設定為預設storageclass
k8s叢集資訊

[[email protected] ~]# kubectl get node -A
NAME     STATUS   ROLES    AGE   VERSION
master   Ready    <none>   52d   v1.17.11
node1    Ready    <
none> 52d v1.17.11 node2 Ready <none> 52d v1.17.11 node3 Ready <none> 52d v1.17.11

動態PV(後端為nfs儲存)

[[email protected] ~]# kubectl get storageclass
NAME                    PROVISIONER   RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
nfs-storage (
default) nfs-storage Retain Immediate false 31h

helm版本

[[email protected] ~]# helm version
version.BuildInfo{Version:"v3.4.1", GitCommit:"c4e74854886b2efe3321e185578e6db9be0a6e29", GitTreeState:"clean", GoVersion:"go1.14.11"
}

下載zookeeper kafka的helm包

新增bitnami倉庫

helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo add ali-incubator https://aliacs-app-catalog.oss-cn-hangzhou.aliyuncs.com/charts-incubator/

列出新增的helm倉庫

helm repo list
NAME         	URL                                                                      
bitnami      	https://charts.bitnami.com/bitnami                                       
ali-incubator	https://aliacs-app-catalog.oss-cn-hangzhou.aliyuncs.com/charts-incubator/

查詢helm包

helm search repo zookeeper
NAME             		CHART VERSION		APP VERSION	DESCRIPTION                                       
bitnami/zookeeper	6.0.0        			3.6.2      			A centralized service for maintaining configura...
bitnami/kafka    	12.2.4       			2.6.0      			Apache Kafka is a distributed streaming platform.

下載helm包到本地,線上安裝可不用下載

[[email protected] zk]# helm pull bitnami/zookeeper
[[email protected] zk]# helm pull bitnami/kafka
[[email protected] zk]# clear
[[email protected] zk]# ls
kafka-12.2.4.tgz  zookeeper-6.0.0.tgz

安裝zookeeper kafka

安裝方法參考bitnami官網:https://docs.bitnami.com/tutorials/deploy-scalable-kafka-zookeeper-cluster-kubernetes/
線上安裝

安裝zookeeper,可通過-n namaspace新增名稱空間,因不暴露在公網,關閉了認證(--set auth.enabled=false),並允許匿名訪問,設定zookeeper副本為3
helm install zookeeper bitnami/zookeeper   --set replicaCount=3   --set auth.enabled=false   --set allowAnonymousLogin=true
安裝kafka,取消自動建立zookeeper,使用剛剛建立的zookeeper,制定zookeeper的服務名稱,
helm install kafka bitnami/kafka   --set zookeeper.enabled=false   --set replicaCount=3  --set externalZookeeper.servers=zookeeper

離線安裝
解壓下載好的tar包

[[email protected] zk]# tar -zxf zookeeper-6.0.0.tgz 
[[email protected] zk]# tar -zxf kafka-12.2.4.tgz
修改values.yaml,主要修改倉庫地址以及儲存storageclass(kafka的配置檔案類似)
[[email protected] zookeeper]# vim values.yaml
image:
  registry: docker.io
  repository: bitnami/zookeeper
  tag: 3.6.2-debian-10-r58
persistence:
  ## A manually managed Persistent Volume and Claim
  ## If defined, PVC must be created manually before volume will be bound
  ## The value is evaluated as a template
  ##
  # existingClaim:
  enabled: true		#測試環境可設定為false
  # storageClass: "-"	#未配置預設動態PV,可開啟註釋,並寫入storageClass的名稱

建立名稱空間

[[email protected] zk]# kubectl create ns zk
namespace/zk created
[[email protected] zk]# kubectl get ns
NAME                   STATUS   AGE
default                Active   52d
kube-public            Active   52d
kube-system            Active   52d
kubernetes-dashboard   Active   51d
zk                     Active   6s

安裝zookeeper

[[email protected] zk]# helm install zookeeper -n zk --set replicaCount=3  --set auth.enabled=false --set allowAnonymousLogin=true /root/zk/zookeeper/
NAME: zookeeper
LAST DEPLOYED: Thu Jan 28 17:40:11 2021
NAMESPACE: zk
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
** Please be patient while the chart is being deployed **

ZooKeeper can be accessed via port 2181 on the following DNS name from within your cluster:

    zookeeper.zk.svc.cluster.local

To connect to your ZooKeeper server run the following commands:

    export POD_NAME=$(kubectl get pods --namespace zk -l "app.kubernetes.io/name=zookeeper,app.kubernetes.io/instance=zookeeper,app.kubernetes.io/component=zookeeper" -o jsonpath="{.items[0].metadata.name}")
    kubectl exec -it $POD_NAME -- zkCli.sh

To connect to your ZooKeeper server from outside the cluster execute the following commands:

    kubectl port-forward --namespace zk svc/zookeeper 2181:2181 &
    zkCli.sh 127.0.0.1:2181
[[email protected] zk]# kubectl get pod -n zk 
NAME          READY   STATUS    RESTARTS   AGE
zookeeper-0   1/1     Running   0          24s
zookeeper-1   1/1     Running   0          23s
zookeeper-2   1/1     Running   0          23s

安裝kafka

[[email protected] zk]# helm install kafka -n zk --set zookeeper.enabled=false --set replicaCount=3  --set externalZookeeper.servers=zookeeper /root/zk/kafka/
NAME: kafka
LAST DEPLOYED: Thu Jan 28 17:41:21 2021
NAMESPACE: zk
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
** Please be patient while the chart is being deployed **

Kafka can be accessed by consumers via port 9092 on the following DNS name from within your cluster:

    kafka.zk.svc.cluster.local

Each Kafka broker can be accessed by producers via port 9092 on the following DNS name(s) from within your cluster:

    kafka-0.kafka-headless.zk.svc.cluster.local:9092
    kafka-1.kafka-headless.zk.svc.cluster.local:9092
    kafka-2.kafka-headless.zk.svc.cluster.local:9092

To create a pod that you can use as a Kafka client run the following commands:

    kubectl run kafka-client --restart='Never' --image 10.41.11.100/lh/bitnami/kafka:2.6.0-debian-10-r78 --namespace zk --command -- sleep infinity
    kubectl exec --tty -i kafka-client --namespace zk -- bash

    PRODUCER:
        kafka-console-producer.sh \
            
            --broker-list kafka-0.kafka-headless.zk.svc.cluster.local:9092,kafka-1.kafka-headless.zk.svc.cluster.local:9092,kafka-2.kafka-headless.zk.svc.cluster.local:9092 \
            --topic test

    CONSUMER:
        kafka-console-consumer.sh \
            
            --bootstrap-server kafka.zk.svc.cluster.local:9092 \
            --topic test \
            --from-beginning
[[email protected] zk]# kubectl get pod -n zk 
NAME          READY   STATUS    RESTARTS   AGE
kafka-0       1/1     Running   0          21s
kafka-1       1/1     Running   0          21s
kafka-2       1/1     Running   0          21s
zookeeper-0   1/1     Running   0          92s
zookeeper-1   1/1     Running   0          91s
zookeeper-2   1/1     Running   0          91s

驗證kafka與zookeeper;日誌中有以下資訊
kubectl logs -f -n zk kafka-0
在這裡插入圖片描述

測試叢集

進入kafka的pod建立一個topic

[[email protected] zk]# kubectl exec -it -n zk kafka-0 bash
I have no name!@kafka-0:/$ kafka-topics.sh --create --zookeeper zookeeper:2181 --replication-factor 1 --partitions 1 --topic testtopic

啟動一個消費者

I have no name!@kafka-0:/$ kafka-console-consumer.sh --bootstrap-server kafka:9092 --topic testtopic

在這裡插入圖片描述
新開一個視窗,進入kafka的pod,啟動一個生產者,輸入訊息;在消費者端可以收到訊息

[[email protected] zk]# kubectl exec -it -n zk kafka-0 bash 
I have no name!@kafka-0:/$ kafka-console-producer.sh --bootstrap-server kafka:9092 --topic mytopic

在這裡插入圖片描述

解除安裝應用

[[email protected] zk]# helm uninstall kafka -n zk
release "kafka" uninstalled
[[email protected] zk]# helm uninstall zookeeper -n zk
release "zookeeper" uninstalled
[[email protected] zk]# kubectl delete pvc,pv -n zk --all