Zookeeper和Kafka on k8s 部署

4 篇文章 0 订阅

一、概述

  • Apache ZooKeeper 是一个集中式服务,用于维护配置信息、命名、提供分布式同步和提供组服务,ZooKeeper 致力于开发和维护一个开源服务器,以实现高度可靠的分布式协调,其实也可以认为就是一个分布式数据库,只是结构比较特殊,是树状结构。官网文档:
    https://zookeeper.apache.org/doc/r3.8.0/

在这里插入图片描述

  • Kafka是最初由 Linkedin 公司开发,是一个分布式、支持分区的(partition)、多副本的(replica),基于 zookeeper 协调的分布式消息系统。官方文档:
https://kafka.apache.org/documentation/

在这里插入图片描述

二、Zookeeper on k8s 部署

在这里插入图片描述
1)添加源
部署包地址:
https://artifacthub.io/packages/helm/zookeeper/zookeeper

helm repo add bitnami https://charts.bitnami.com/bitnami
helm pull bitnami/zookeeper
tar -xf  zookeeper-10.2.1.tgz

2)修改配置
修改zookeeper/values.yaml

image:
  registry: myharbor.com
  repository: bigdata/zookeeper
  tag: 3.8.0-debian-11-r36
...

replicaCount: 3

...

service:
  type: NodePort
  nodePorts:
    #NodePort 默认范围是 30000-32767
    client: "32181"
    tls: "32182"

...

persistence:
  storageClass: "zookeeper-local-storage"
  size: "10Gi"
  # 目录需要提前在宿主机上创建
  local:
    - name: zookeeper-0
      host: "local-168-182-110"
      path: "/opt/bigdata/servers/zookeeper/data/data1"
    - name: zookeeper-1
      host: "local-168-182-111"
      path: "/opt/bigdata/servers/zookeeper/data/data1"
    - name: zookeeper-2
      host: "local-168-182-112"
      path: "/opt/bigdata/servers/zookeeper/data/data1"

...

# Enable Prometheus to access ZooKeeper metrics endpoint
metrics:
  enabled: true

添加zookeeper/templates/pv.yaml

{{- range .Values.persistence.local }}
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: {{ .name }}
  labels:
    name: {{ .name }}
spec:
  storageClassName: {{ $.Values.persistence.storageClass }}
  capacity:
    storage: {{ $.Values.persistence.size }}
  accessModes:
    - ReadWriteOnce
  local:
    path: {{ .path }}
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: kubernetes.io/hostname
              operator: In
              values:
                - {{ .host }}
---
{{- end }}

添加zookeeper/templates/storage-class.yaml

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: {{ .Values.persistence.storageClass }}
provisioner: kubernetes.io/no-provisioner

3)开始安装

# 先准备好镜像
docker pull docker.io/bitnami/zookeeper:3.8.0-debian-11-r36
docker tag docker.io/bitnami/zookeeper:3.8.0-debian-11-r36 myharbor.com/bigdata/zookeeper:3.8.0-debian-11-r36
docker push myharbor.com/bigdata/zookeeper:3.8.0-debian-11-r36

# 开始安装
helm install zookeeper ./zookeeper -n zookeeper --create-namespace

在这里插入图片描述

查看 pod 状态

kubectl get pods,svc -n zookeeper -o wide

4)测试验证

# 登录zookeeper pod
kubectl exec -it zookeeper-0 -n zookeeper -- zkServer.sh status
kubectl exec -it zookeeper-1 -n zookeeper -- zkServer.sh status
kubectl exec -it zookeeper-2 -n zookeeper -- zkServer.sh status

kubectl exec -it zookeeper-0 -n zookeeper -- bash

在这里插入图片描述

5)Prometheus 监控
Prometheus:https://prometheus.k8s.local/targets?search=zookeeper

可以通过命令查看采集数据

kubectl get --raw http://10.244.0.52:9141/metrics
kubectl get --raw http://10.244.1.101:9141/metrics
kubectl get --raw http://10.244.2.137:9141/metrics

Grafana:https://grafana.k8s.local/
账号:admin,密码通过下面命令获取

kubectl get secret --namespace grafana grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo

导入 grafana 模板,集群资源监控:10465
官方模块下载地址:https://grafana.com/grafana/dashboards/
在这里插入图片描述
6)卸载

helm uninstall zookeeper -n zookeeper

kubectl delete pod -n zookeeper `kubectl get pod -n zookeeper|awk 'NR>1{print $1}'` --force
kubectl patch ns zookeeper -p '{"metadata":{"finalizers":null}}'
kubectl delete ns zookeeper --force

三、Kafka on k8s 部署

1)添加源
部署包地址:https://artifacthub.io/packages/helm/bitnami/kafka

helm repo add bitnami https://charts.bitnami.com/bitnami
helm pull bitnami/kafka
tar -xf kafka-18.4.2.tgz

2)修改配置
修改kafka/values.yaml


image:
  registry: myharbor.com
  repository: bigdata/kafka
  tag: 3.2.1-debian-11-r16

...

replicaCount: 3

...

service:
  type: NodePort
  nodePorts:
    client: "30092"
    external: "30094"

...

externalAccess
  enabled: true
  service:
    type: NodePort
     nodePorts:
       - 30001
       - 30002
       - 30003
     useHostIPs: true

...

persistence:
  storageClass: "kafka-local-storage"
  size: "10Gi"
  # 目录需要提前在宿主机上创建
  local:
    - name: kafka-0
      host: "local-168-182-110"
      path: "/opt/bigdata/servers/kafka/data/data1"
    - name: kafka-1
      host: "local-168-182-111"
      path: "/opt/bigdata/servers/kafka/data/data1"
    - name: kafka-2
      host: "local-168-182-112"
      path: "/opt/bigdata/servers/kafka/data/data1"

...

metrics:
  kafka:
    enabled: true
    image:
      registry: myharbor.com
      repository: bigdata/kafka-exporter
      tag: 1.6.0-debian-11-r8
    jmx:
      enabled: true
      image:
      registry: myharbor.com
      repository: bigdata/jmx-exporter
      tag: 0.17.1-debian-11-r1
      annotations:
        prometheus.io/path: "/metrics"

...

zookeeper:
  enabled: false

...

externalZookeeper
  servers:
    - zookeeper-0.zookeeper-headless.zookeeper
    - zookeeper-1.zookeeper-headless.zookeeper
    - zookeeper-2.zookeeper-headless.zookeeper

添加kafka/templates/pv.yaml

{{- range .Values.persistence.local }}
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: {{ .name }}
  labels:
    name: {{ .name }}
spec:
  storageClassName: {{ $.Values.persistence.storageClass }}
  capacity:
    storage: {{ $.Values.persistence.size }}
  accessModes:
    - ReadWriteOnce
  local:
    path: {{ .path }}
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: kubernetes.io/hostname
              operator: In
              values:
                - {{ .host }}
---
{{- end }}

添加kafka/templates/storage-class.yaml

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: {{ .Values.persistence.storageClass }}
provisioner: kubernetes.io/no-provisioner

3)开始安装

# 先准备好镜像
docker pull docker.io/bitnami/kafka:3.2.1-debian-11-r16
docker tag docker.io/bitnami/kafka:3.2.1-debian-11-r16 myharbor.com/bigdata/kafka:3.2.1-debian-11-r16
docker push myharbor.com/bigdata/kafka:3.2.1-debian-11-r16

# node-export
docker pull docker.io/bitnami/kafka-exporter:1.6.0-debian-11-r8
docker tag docker.io/bitnami/kafka-exporter:1.6.0-debian-11-r8 myharbor.com/bigdata/kafka-exporter:1.6.0-debian-11-r8
docker push myharbor.com/bigdata/kafka-exporter:1.6.0-debian-11-r8

# JXM
docker.io/bitnami/jmx-exporter:0.17.1-debian-11-r1
docker tag docker.io/bitnami/jmx-exporter:0.17.1-debian-11-r1 myharbor.com/bigdata/jmx-exporter:0.17.1-debian-11-r1
docker push myharbor.com/bigdata/jmx-exporter:0.17.1-debian-11-r1

#开始安装
helm install kafka ./kafka -n kafka --create-namespace

在这里插入图片描述
查看 pod 状态

kubectl get pods,svc -n kafka -o wide

4)测试验证

# 登录zookeeper pod
kubectl exec -it kafka-0 -n kafka -- bash

1、创建 Topic(一个副本一个分区)

--create: 指定创建topic动作
--topic:指定新建topic的名称
--bootstrap-server: 指定kafka连接地址
--config:指定当前topic上有效的参数值,参数列表参考文档为: Topic-level configuration
--partitions:指定当前创建的kafka分区数量,默认为1个
--replication-factor:指定每个分区的复制因子个数,默认1个
kafka-topics.sh --create --topic test001 --bootstrap-server kafka.kafka:9092 --partitions 1 --replication-factor 1
# 查看
kafka-topics.sh --describe --bootstrap-server kafka.kafka:9092  --topic test001

2、查看 Topic 列表

kafka-topics.sh --list --bootstrap-server kafka.kafka:9092

3、生产者/消费者测试
【生产者】

kafka-console-producer.sh --broker-list kafka.kafka:9092 --topic test001

{"id":"1","name":"n1","age":"20"}
{"id":"2","name":"n2","age":"21"}
{"id":"3","name":"n3","age":"22"}

【消费者】

# 从头开始消费
kafka-console-consumer.sh --bootstrap-server kafka.kafka:9092 --topic test001 --from-beginning
# 指定从分区的某个位置开始消费,这里只指定了一个分区,可以多写几行或者遍历对应的所有分区
kafka-console-consumer.sh --bootstrap-server kafka.kafka:9092 --topic test001 --partition 0 --offset 100 --group test001

4、查看数据积压

kafka-consumer-groups.sh --bootstrap-server kafka.kafka:9092 --describe --group test001

5、删除 topic

kafka-topics.sh --delete --topic test001 --bootstrap-server kafka.kafka:9092

5)Prometheus 监控
Prometheus:https://prometheus.k8s.local/targets?search=kafka
在这里插入图片描述
可以通过命令查看采集数据

kubectl get --raw http://10.244.2.165:9308/metrics

Grafana:https://grafana.k8s.local/
账号:admin,密码通过下面命令获取

kubectl get secret --namespace grafana grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo

导入 grafana 模板,集群资源监控:11962
官方模块下载地址:https://grafana.com/grafana/dashboards/

6)卸载

helm uninstall kafka -n kafka

kubectl delete pod -n kafka `kubectl get pod -n kafka|awk 'NR>1{print $1}'` --force
kubectl patch ns kafka  -p '{"metadata":{"finalizers":null}}'
kubectl delete ns kafka  --force

zookeeper + kafka on k8s 环境部署

  • 4
    点赞
  • 8
    收藏
  • 打赏
    打赏
  • 1
    评论
<p> 推荐购买本人首页k8s架构师套餐课程,更加全面、更加优惠<br /> <p> <img src="https://img-bss.csdnimg.cn/202109180342124463.png" alt="" /><img src="https://img-bss.csdnimg.cn/202109180342248292.png" alt="" /><img src="https://img-bss.csdnimg.cn/202109180342291139.png" alt="" /><img src="https://img-bss.csdnimg.cn/202109180342378204.png" alt="" /><img src="https://img-bss.csdnimg.cn/202109180342448201.png" alt="" /><img src="https://img-bss.csdnimg.cn/202109180342534463.png" alt="" /><img src="https://img-bss.csdnimg.cn/202109180343005157.png" alt="" /> </p> <p> <img src="https://10.idqqimg.com/qqke_course_info/ajNVdqHZLLCe2d50sKQwNXBmYPjM535yG6JlRZZXK8jlooZUteSLoL3fz5icJYbDTeAVQ5HHmKZI/" /><img src="https://10.idqqimg.com/qqke_course_info/ajNVdqHZLLBzialia307kVWibHPObn3u6VAgZx5ibArebKCibSQAnCQ8VricssMILt3IRNRpuLWKicBYMg/" /><img src="https://10.idqqimg.com/qqke_course_info/ajNVdqHZLLAib1grEBvlmx5dvSeFOFJkCv4RgbKKg7iaSpP5FBtYIVg66v8B3gss6dibCIQKPBA2nI/" /><img src="https://10.idqqimg.com/qqke_course_info/ajNVdqHZLLDBNqIDBGDZHFpIWqUNS2rCtHdoasxddJYZWZk4mPOVBf7X8vxmgTaZaicdMwQb2V2U/" /><img src="https://10.idqqimg.com/qqke_course_info/ajNVdqHZLLB46TicNIgibvBuMWz3vL3JP3e4G2wVBpKGRhxQNIOww3bZG2OTicVichrqXhvF19Siab4I/" /> </p> <img src="https://img-bss.csdnimg.cn/202109180339341136.png" alt="" /><img src="https://img-bss.csdnimg.cn/202109180339479326.png" alt="" /><img src="https://img-bss.csdnimg.cn/202109180339582736.png" alt="" /><img src="https://img-bss.csdnimg.cn/202109180340071442.png" alt="" /><img src="https://img-bss.csdnimg.cn/202109180340159194.png" alt="" /><img src="https://img-bss.csdnimg.cn/202109180340413044.png" alt="" /><img src="https://img-bss.csdnimg.cn/202109180340512349.png" alt="" /><img src="https://10.idqqimg.com/qqke_course_info/ajNVdqHZLLCe2d50sKQwNXBmYPjM535yG6JlRZZXK8jlooZUteSLoL3fz5icJYbDTeAVQ5HHmKZI/" /><img src="https://10.idqqimg.com/qqke_course_info/ajNVdqHZLLBzialia307kVWibHPObn3u6VAgZx5ibArebKCibSQAnCQ8VricssMILt3IRNRpuLWKicBYMg/" /><img src="https://10.idqqimg.com/qqke_course_info/ajNVdqHZLLAib1grEBvlmx5dvSeFOFJkCv4RgbKKg7iaSpP5FBtYIVg66v8B3gss6dibCIQKPBA2nI/" /><img src="https://10.idqqimg.com/qqke_course_info/ajNVdqHZLLDBNqIDBGDZHFpIWqUNS2rCtHdoasxddJYZWZk4mPOVBf7X8vxmgTaZaicdMwQb2V2U/" /><img src="https://10.idqqimg.com/qqke_course_info/ajNVdqHZLLB46TicNIgibvBuMWz3vL3JP3e4G2wVBpKGRhxQNIOww3bZG2OTicVichrqXhvF19Siab4I/" /><br /> </p>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
评论 1

打赏作者

果子哥丶

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值