首页 > 其他分享 >k8s上部署Kafka

k8s上部署Kafka

时间:2022-10-04 16:35:19浏览次数:47  
标签:name 部署 app kafka test k8s infra Kafka

一、集群部署Kafka

1.1、指定节点部署

  • 给以下节点打上标签:k8s-node01、k8s-node02、k8s-master03【也就是我们的三个节点的集群部署在这三个节点上】
[root@k8s-master01 ~]# kubectl get nodes 
NAME           STATUS   ROLES    AGE    VERSION
k8s-master01   Ready    <none>   300d   v1.19.5
k8s-master02   Ready    <none>   300d   v1.19.5
k8s-master03   Ready    <none>   300d   v1.19.5
k8s-node01     Ready    <none>   300d   v1.19.5
k8s-node02     Ready    <none>   300d   v1.19.5

# 打标签 【复用zookeeper的标签即可】
# 注意这里相当于也打了两个域【app.kubernetes.io/component、app.kubernetes.io/name】,调度的时候会用上
kubectl get nodes --show-labels
kubectl label nodes k8s-master03 app.kubernetes.io/component=zookeeper
kubectl label nodes k8s-node02 app.kubernetes.io/component=zookeeper
kubectl label nodes k8s-node01 app.kubernetes.io/component=zookeeper
kubectl label nodes k8s-master03 app.kubernetes.io/name=zookeeper
kubectl label nodes k8s-node01 app.kubernetes.io/name=zookeeper
kubectl label nodes k8s-node02 app.kubernetes.io/name=zookeeper

1.2、创建svc

#部署 Service Headless,用于Kafka间相互通信
apiVersion: v1
kind: Service
metadata:
  name: kafka-headless
  namespace: infra
  labels:
    app: kafka
spec:
  type: ClusterIP
  clusterIP: None  # 创建无头服务,如果需要对外暴露端口可自行创建service
  ports:
  - name: kafka
    port: 9092
    targetPort: kafka
  selector:
    app: kafka
---
#部署 Service,用于外部访问 Kafka
apiVersion: v1
kind: Service
metadata:
  name: kafka
  namespace: infra
  labels:
    app: kafka
spec:
  type: ClusterIP
  ports:
  - name: kafka
    port: 9092
    targetPort: kafka
  selector:
    app: kafka

1.3、kafka-pdb.yaml

  • 创建pdb,防止滚动更新的时候全部更新
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
  name: kafka-pdb
  namespace: infra
spec:
  selector:
    matchLabels:
      app: kafka
  minAvailable: 2

1.4、创建sc

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: infra-nfs-kafka
provisioner: fuseim.pri/ifs
parameters:
  archiveOnDelete: "false"   # 设置为"false"时删除PVC不会保留数据,"true"则保留数据

1.5、创建Kafka

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: kafka-test
  namespace: infra
  labels:
    app: kafka
spec:
  selector:
    matchLabels:
      app: kafka
  serviceName: kafka-headless
  podManagementPolicy: "Parallel"
  replicas: 3
  updateStrategy:
    type: "RollingUpdate"
  template:
    metadata:
      name: "kafka"
      labels:
        app: kafka
    spec:      
      securityContext:
        fsGroup: 1001
        runAsUser: 1001
      affinity:
        nodeAffinity:                                      # node亲和性
          requiredDuringSchedulingIgnoredDuringExecution:  # 硬策略,调度在app.kubernetes.io/component=zookeeper的节点中
            nodeSelectorTerms:
            - matchExpressions:
              - key: app.kubernetes.io/component
                operator: In
                values:
                  - zookeeper
        podAntiAffinity:                                    # Pod反亲和性
          preferredDuringSchedulingIgnoredDuringExecution:  # 软策略,使Pod分布在不同的节点上
          - weight: 1                                       # 权重,有多个策略通过权重控制调度
            podAffinityTerm:
              topologyKey: app.kubernetes.io/name           # 通过app.kubernetes.io/name作为域调度  
              labelSelector:
                matchExpressions:
                - key: app.kubernetes.io/component
                  operator: In
                  values:
                  - zookeeper
      containers:
      - name: kafka
        image: "docker.io/bitnami/kafka:2.3.0-debian-9-r4"
        imagePullPolicy: "IfNotPresent"
        env:
        - name: MY_POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: KAFKA_CFG_ZOOKEEPER_CONNECT
          value: "zk-headless"                      # Zookeeper Service 名称
        - name: KAFKA_PORT_NUMBER 
          value: "9092"
        - name: KAFKA_CFG_LISTENERS
          value: "PLAINTEXT://:$(KAFKA_PORT_NUMBER)"
        - name: KAFKA_CFG_ADVERTISED_LISTENERS
          value: 'PLAINTEXT://$(MY_POD_NAME).kafka-headless:$(KAFKA_PORT_NUMBER)'
        - name: ALLOW_PLAINTEXT_LISTENER
          value: "yes"
        - name: KAFKA_HEAP_OPTS
          value: "-Xmx512m -Xms512m"
        - name: KAFKA_CFG_LOGS_DIRS
          value: /opt/bitnami/kafka/data
        - name: JMX_PORT
          value: "9988"
        ports:
        - name: kafka
          containerPort: 9092
        livenessProbe:
          tcpSocket:
            port: kafka
          initialDelaySeconds: 10
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 2
        readinessProbe:
          tcpSocket:
            port: kafka
          initialDelaySeconds: 5
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 6
        volumeMounts:
        - name: data
          mountPath: /bitnami/kafka
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        storageClassName: infra-nfs-kafka     # 指定为上面创建的 storageclass
        accessModes:
          - "ReadWriteOnce"
        resources:
          requests:
            storage: 5Gi

1.6、查看Kafka部署是否调度在指定节点

[root@k8s-master01 ~]# kubectl get po -n infra  -l app=kafka -owide
NAME           READY   STATUS    RESTARTS   AGE   IP              NODE           NOMINATED NODE   READINESS GATES
kafka-test-0   1/1     Running   0          58m   10.244.195.10   k8s-master03   <none>           <none>
kafka-test-1   1/1     Running   0          57m   10.244.58.218   k8s-node02     <none>           <none>
kafka-test-2   1/1     Running   0          58m   10.244.85.210   k8s-node01     <none>           <none>

1.7、Kafka功能验证

  • 进入 kafka-0 ,创建topic test,进入生产者窗口
  • kafka消息的生产和消费正常,kafka集群正常。k8s部署kafka集群完成。
# 进入pod
[root@k8s-master01 infra]# kubectl exec -it -n infra kafka-test-0 -- bash
# 进入脚本目录
I have no name!@kafka-test-0:~ $ cd /opt/bitnami/kafka/bin/

# 创建topic
I have no name!@kafka-test-0:/opt/bitnami/kafka/bin$ kafka-topics.sh --create --topic test --zookeeper zk-test-0.zk-headless.infra.svc.cluster.local:2181,zk-test-1.zk-headless.infra.svc.cluster.local:2181,zk-test-2.zk-headless.infra.svc.cluster.local:2181 --partitions 3 --replication-factor 2
Created topic test.

# 查看topic列表
I have no name!@kafka-test-0:/opt/bitnami/kafka/bin$ kafka-topics.sh --list --zookeeper zk-test-0.zk-headless.infra.svc.cluster.local:2181,zk-test-1.zk-headless.infra.svc.cluster.local:2181,zk-test-2.zk-headless.infra.svc.cluster.local:2181
aaa
test

# 进入topic为aaa的生产者消息中心
I have no name!@kafka-test-0:/opt/bitnami/kafka/bin$ kafka-console-producer.sh --topic test --broker-list localhost:9092
>aaa   
>bbbb
>
  • 进入 kafka-1 ,进入消费者窗口
  • kafka消息的生产和消费正常,kafka集群正常。k8s部署kafka集群完成。
# 进入pod
[root@k8s-master01 infra]# kubectl exec -it -n infra kafka-test-1 -- bash

# 进入脚本目录
I have no name!@kafka-test-0:~ $ cd /opt/bitnami/kafka/bin/

# 能消费到数据,说明Kafka状态ok
I have no name!@kafka-test-1:/opt/bitnami/kafka/bin$ kafka-console-consumer.sh --topic test --bootstrap-server localhost:9092
aaa
bbbb

标签:name,部署,app,kafka,test,k8s,infra,Kafka
From: https://www.cnblogs.com/hsyw/p/16753968.html

相关文章

  • 0766-6.3.3-如何实现Kafka跨网络访问
    文档说明在使用Kafka时会遇到内外网的场景,即Kafka集群使用内网搭建,在内网和外网均有客户端需要消费Kafka的消息,同时在集群内由于使用内网环境通信,因此不必太过考虑通信的加......
  • 模型训练和部署-Iris数据集
    温馨提示:如果使用电脑查看图片不清晰,可以使用手机打开文章单击文中的图片放大查看高清原图。Fayson的github:​​https://github.com/fayson/cdhproject​​提示:代码块部分可......
  • CDSW1.4的Models功能-创建和部署模型(QuickStart)
    温馨提示:如果使用电脑查看图片不清晰,可以使用手机打开文章单击文中的图片放大查看高清原图。Fayson的github:​​https://github.com/fayson/cdhproject​​提示:代码块部分可......
  • 如何规划设置Kafka Broker的heap size
    温馨提示:如果使用电脑查看图片不清晰,可以使用手机打开文章单击文中的图片放大查看高清原图。Fayson的github:​​https://github.com/fayson/cdhproject​​提示:代码块部分可......
  • wordpress多节点部署+rsync备份图片
    基于LAMP架构搭建LB+web+mysql+nas架构,实现从web站点上传的图片自动同步(wordpress)环境:10.0.0.128apache+wordpress服务,数据库主库指向10.0.0.132,基于docker来安装,映射......
  • 0887-7.1.4-如何在CDP中为Kafka启用Kerberos认证及使用
    1.文档编写目的在CDP集群中启用了Kerberos认证,那么Kafka集群能否与Kerberos认证服务集成呢?本文主要讲述如何通过ClouderaManager为Kafka集群启用Kerberos认证及客户端配置......
  • prometheus+grafana+node-exporter部署监控系统实战
    1、在grafana中添加prometheus数据源  2、添加dashboard       ......
  • 【云原生】Presto/Trino on k8s 环境部署
    目录一、概述二、环境部署1)添加源并下载编排部署包2)构建镜像3)修改配置4)开始部署5)测试验证1、mysqlcatalog测试2、hivecatalog测试6)卸载一、概述Presto是Facebook开......
  • UML _ 部署图
    13.1部署图的概念部署图定义部署图是描述一个系统运行时的硬件节点、在这些节点上运行的软件构件将在何处物理运行以及如何彼此通信的静态视图。一般一个系统仅有一......
  • fast-data-dev 快速基于容器的kafka 环境
    fast-data-dev是由lensesio团队提供的一个快速部署kafka测试环境的工具包含的组件kafkazkschemaregistrykafkaconnect以及其他不少的connector参考资料http......