首页 > 其他分享 >(二)kubernetes部署kafka

(二)kubernetes部署kafka

时间:2023-07-13 16:00:22浏览次数:37  
标签:log kubernetes 部署 zookeeper kafka -- nfs override

与前面的zookeeper一样需要持久化存储,因此还是先创建PV,然后再部署kafka。

2、配置nfs

创建需要作为nfs共享的目录 mkdir /home/nfs/kafka-0

添加权限 chmod 777 /home/nfs/kafka-0

编辑配置

root@ubuntu:# vim /etc/export
# /etc/exports: the access control list for filesystems which may be exported
#               to NFS clients.  See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes       hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4        gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes  gss/krb5i(rw,sync,no_subtree_check)
#
/home/nfs/zookeeper-0 *(rw,sync,no_root_squash)
/home/nfs/zookeeper-1 *(rw,sync,no_root_squash)
/home/nfs/zookeeper-2 *(rw,sync,no_root_squash)
/home/nfs/kafka-0 *(rw,sync,no_root_squash)
/home/nfs/kafka-1 *(rw,sync,no_root_squash)
/home/nfs/kafka-2 *(rw,sync,no_root_squash)
重启服务使配置生效
root@ubuntu:# /etc/init.d/rpcbind restart
[ ok ] Restarting rpcbind (via systemctl): rpcbind.service.
root@ubuntu:# /etc/init.d/nfs-kernel-server restart
[ ok ] Restarting nfs-kernel-server (via systemctl): nfs-kernel-server.service.
root@ubuntu:# showmount -e
Export list for ubuntu:
/home/nfs/kafka-2     *
/home/nfs/kafka-1     *
/home/nfs/kafka-0     *
/home/nfs/zookeeper-2 *
/home/nfs/zookeeper-1 *
/home/nfs/zookeeper-0 *

看到以上信息就表示nfs的服务已经搭建好了。

部署kafka

kafka是一个分布式、多副本、多订阅者、分区的,基于zoopkeeper协调的分布式日志系统。其主要特点为:
1.以时间复杂度为O(1)的方式提供消息持久化能力,即使对TB级以上的数据也能保证常数时间的访问性能。
2.高吞吐率。即使在非常廉价的商用机器上也能做到单机支持每秒100K消息的传输。
3.支持kafka server间的消息分区以及分布式消费,同时保证每个partition内的消息顺序和传输。
4.同时支持离线数据处理和实时数据处理。

以下是部署kafka的pv.yaml,需要注意server的字段填充部署了nfs服务的主机ip

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-kafka-0
  namespace: storm
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs-kafka-storage
  nfs:
    path: /home/nfs/kafka-0
    server: 192.168.45.227
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-kafka-1
  namespace: storm
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs-kafka-storage
  nfs:
    path: /home/nfs/kafka-1
    server: 192.168.45.227
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-kafka-2
  namespace: storm
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs-kafka-storage
  nfs:
    path: /home/nfs/kafka-2
    server: 192.168.45.227

以下是部署kafka的yaml文件,zookeeper和kakfa都是有状态的服务,不能用Deployment来部署,因此使用StatefulSet。

---
apiVersion: v1
kind: Service
metadata:
  name: kafka-service
  namespace: storm
  labels:
    app: kafka
spec:
  type: NodePort
  ports:
  - port: 9092
    name: kafka-server
  clusterIP: None
  selector:
    app: kafka
---
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
  name: kafka-pdb
  namespace: storm
spec:
  selector:
    matchLabels:
      app: kafka
  minAvailable: 2
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: kafka
  namespace: storm
spec:
  selector:
     matchLabels:
        app: kafka
  serviceName: kafka-service
  replicas: 3
  template:
    metadata:
      labels:
        app: kafka
    spec:
      tolerations:
      - key: "travis.io/schedule-only"
        operator: "Equal"
        value: "kafka"
        effect: "NoSchedule"
      - key: "travis.io/schedule-only"
        operator: "Equal"
        value: "kafka"
        effect: "NoExecute"
        tolerationSeconds: 3600
      - key: "travis.io/schedule-only"
        operator: "Equal"
        value: "kafka"
        effect: "PreferNoSchedule"
      terminationGracePeriodSeconds: 300
      containers:
      - name: k8s-kafka
        imagePullPolicy: Always
        image: fastop/kafka:2.2.0
        resources:
          requests:
            memory: "1Gi"
            cpu: "1"
        ports:
        - containerPort: 9092
          name: server
        command:
        - sh
        - -c
        - "exec kafka-server-start.sh /opt/kafka/config/server.properties --override broker.id=${HOSTNAME##*-} \
          --override listeners=PLAINTEXT://:9092 \
          --override advertised.listeners=PLAINTEXT://$(hostname).kafka-service.storm.svc.cluster.local:9092 \
          --override zookeeper.connect=zookeeper-0.zk-inner-service.storm.svc.cluster.local:2181,zookeeper-1.zk-inner-service.storm.svc.cluster.local:2181,zookeeper-2.zk-inner-service.storm.svc.cluster.local:2181 \
          --override log.dir=/var/lib/kafka \
          --override auto.create.topics.enable=true \
          --override auto.leader.rebalance.enable=true \
          --override background.threads=10 \
          --override compression.type=producer \
          --override delete.topic.enable=false \
          --override leader.imbalance.check.interval.seconds=300 \
          --override leader.imbalance.per.broker.percentage=10 \
          --override log.flush.interval.messages=9223372036854775807 \
          --override log.flush.offset.checkpoint.interval.ms=60000 \
          --override log.flush.scheduler.interval.ms=9223372036854775807 \
          --override log.retention.bytes=-1 \
          --override log.retention.hours=168 \
          --override log.roll.hours=168 \
          --override log.roll.jitter.hours=0 \
          --override log.segment.bytes=1073741824 \
          --override log.segment.delete.delay.ms=60000 \
          --override message.max.bytes=1000012 \
          --override min.insync.replicas=1 \
          --override num.io.threads=8 \
          --override num.network.threads=3 \
          --override num.recovery.threads.per.data.dir=1 \
          --override num.replica.fetchers=1 \
          --override offset.metadata.max.bytes=4096 \
          --override offsets.commit.required.acks=-1 \
          --override offsets.commit.timeout.ms=5000 \
          --override offsets.load.buffer.size=5242880 \
          --override offsets.retention.check.interval.ms=600000 \
          --override offsets.retention.minutes=1440 \
          --override offsets.topic.compression.codec=0 \
          --override offsets.topic.num.partitions=50 \
          --override offsets.topic.replication.factor=3 \
          --override offsets.topic.segment.bytes=104857600 \
          --override queued.max.requests=500 \
          --override quota.consumer.default=9223372036854775807 \
          --override quota.producer.default=9223372036854775807 \
          --override replica.fetch.min.bytes=1 \
          --override replica.fetch.wait.max.ms=500 \
          --override replica.high.watermark.checkpoint.interval.ms=5000 \
          --override replica.lag.time.max.ms=10000 \
          --override replica.socket.receive.buffer.bytes=65536 \
          --override replica.socket.timeout.ms=30000 \
          --override request.timeout.ms=30000 \
          --override socket.receive.buffer.bytes=102400 \
          --override socket.request.max.bytes=104857600 \
          --override socket.send.buffer.bytes=102400 \
          --override unclean.leader.election.enable=true \
          --override zookeeper.session.timeout.ms=6000 \
          --override zookeeper.set.acl=false \
          --override broker.id.generation.enable=true \
          --override connections.max.idle.ms=600000 \
          --override controlled.shutdown.enable=true \
          --override controlled.shutdown.max.retries=3 \
          --override controlled.shutdown.retry.backoff.ms=5000 \
          --override controller.socket.timeout.ms=30000 \
          --override default.replication.factor=1 \
          --override fetch.purgatory.purge.interval.requests=1000 \
          --override group.max.session.timeout.ms=300000 \
          --override group.min.session.timeout.ms=6000 \
          --override inter.broker.protocol.version=2.2.0 \
          --override log.cleaner.backoff.ms=15000 \
          --override log.cleaner.dedupe.buffer.size=134217728 \
          --override log.cleaner.delete.retention.ms=86400000 \
          --override log.cleaner.enable=true \
          --override log.cleaner.io.buffer.load.factor=0.9 \
          --override log.cleaner.io.buffer.size=524288 \
          --override log.cleaner.io.max.bytes.per.second=1.7976931348623157E308 \
          --override log.cleaner.min.cleanable.ratio=0.5 \
          --override log.cleaner.min.compaction.lag.ms=0 \
          --override log.cleaner.threads=1 \
          --override log.cleanup.policy=delete \
          --override log.index.interval.bytes=4096 \
          --override log.index.size.max.bytes=10485760 \
          --override log.message.timestamp.difference.max.ms=9223372036854775807 \
          --override log.message.timestamp.type=CreateTime \
          --override log.preallocate=false \
          --override log.retention.check.interval.ms=300000 \
          --override max.connections.per.ip=2147483647 \
          --override num.partitions=4 \
          --override producer.purgatory.purge.interval.requests=1000 \
          --override replica.fetch.backoff.ms=1000 \
          --override replica.fetch.max.bytes=1048576 \
          --override replica.fetch.response.max.bytes=10485760 \
          --override reserved.broker.max.id=1000 "
        env:
        - name: KAFKA_HEAP_OPTS
          value : "-Xmx512M -Xms512M"
        - name: KAFKA_OPTS
          value: "-Dlogging.level=INFO"
        volumeMounts:
        - name: datadir
          mountPath: /var/lib/kafka
        readinessProbe:
          tcpSocket:
            port: 9092
          timeoutSeconds: 1
          initialDelaySeconds: 5
      securityContext:
        runAsUser: 1000
        fsGroup: 1000
  volumeClaimTemplates:
  - metadata:
      name: datadir
    spec:
      storageClassName: nfs-kafka-storage
      accessModes: [ "ReadWriteMany" ]
      resources:
        requests:
          storage:  20Gi

执行部署以上的yaml文件

root@ubuntu:# kubectl apply -f kafka-pv.yaml
root@ubuntu:# kubectl apply -f kafka.yaml
root@ubuntu:# kubectl -n storm get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                                   STORAGECLASS        REASON   AGE
pv-kafka-0                                 20Gi       RWX            Retain           Bound    storm/datadir-kafka-0                   nfs-kafka-storage            46s
pv-kafka-1                                 20Gi       RWX            Retain           Bound    storm/datadir-kafka-1                   nfs-kafka-storage            46s
pv-kafka-2                                 20Gi       RWX            Retain           Bound    storm/datadir-kafka-2                   nfs-kafka-storage            46s
pv-zookeeper-0                             20Gi       RWO            Retain           Bound    storm/datadir-zookeeper-2               nfs-storage                  13m44s
pv-zookeeper-1                             20Gi       RWO            Retain           Bound    storm/datadir-zookeeper-0               nfs-storage                  13m44s
pv-zookeeper-2                             20Gi       RWO            Retain           Bound    storm/datadir-zookeeper-1               nfs-storage                  13m44s
root@ubuntu:# kubectl -n storm get pod
NAME                        READY   STATUS             RESTARTS   AGE
kafka-0                     1/1     Running            0          28s
kafka-1                     1/1     Running            0          15s
kafka-2                     1/1     Running            0          8s
zookeeper-0                 1/1     Running            0          13m8s
zookeeper-1                 1/1     Running            0          13m8s
zookeeper-2                 1/1     Running            0          13m8s

在zookeeper中查看broker

root@ubuntu:# kubectl -n storm exec -it zookeeper-0 -- zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls /brokers/ids
[0, 1, 2]
[zk: localhost:2181(CONNECTED) 1] get /brokers/ids/0
{"listener_security_protocol_map":{"PLAINTEXT":"PLAINTEXT"},"endpoints":["PLAINTEXT://kafka-0.kafka-service.storm.svc.cluster.local:9092"],"jmx_port":-1,"host":"kafka-0.kafka-service.storm.svc.cluster.local","timestamp":"1689167921722","port":9092,"version":4}
cZxid = 0x10000620f
ctime = Wed Jul 12 13:18:41 UTC 2023
mZxid = 0x10000620f
mtime = Wed Jul 12 13:18:41 UTC 2023
pZxid = 0x10000620f
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x389496aa40b0035
dataLength = 260
numChildren = 0
[zk: localhost:2181(CONNECTED) 2] get /brokers/ids/1
{"listener_security_protocol_map":{"PLAINTEXT":"PLAINTEXT"},"endpoints":["PLAINTEXT://kafka-1.kafka-service.storm.svc.cluster.local:9092"],"jmx_port":-1,"host":"kafka-1.kafka-service.storm.svc.cluster.local","timestamp":"1689167899067","port":9092,"version":4}
cZxid = 0x100006189
ctime = Wed Jul 12 13:18:19 UTC 2023
mZxid = 0x100006189
mtime = Wed Jul 12 13:18:19 UTC 2023
pZxid = 0x100006189
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x189496aa354004c
dataLength = 260
numChildren = 0
[zk: localhost:2181(CONNECTED) 3] get /brokers/ids/2
{"listener_security_protocol_map":{"PLAINTEXT":"PLAINTEXT"},"endpoints":["PLAINTEXT://kafka-2.kafka-service.storm.svc.cluster.local:9092"],"jmx_port":-1,"host":"kafka-2.kafka-service.storm.svc.cluster.local","timestamp":"1689167877630","port":9092,"version":4}
cZxid = 0x1000060f3
ctime = Wed Jul 12 13:17:57 UTC 2023
mZxid = 0x1000060f3
mtime = Wed Jul 12 13:17:57 UTC 2023
pZxid = 0x1000060f3
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x289496abbc30037
dataLength = 260
numChildren = 0

可以看到3个broker都在zookeeper中注册成功了,后续可以自行进行topic的创建以及producer、comsumer的消息测试。

标签:log,kubernetes,部署,zookeeper,kafka,--,nfs,override
From: https://www.cnblogs.com/Modest-Hamilton/p/17551146.html

相关文章

  • Kubernetes轻量级日志工具Loki安装及踩坑记录
    Loki简介Loki是Grafana出品的一个轻量级日志系统,熟悉ELK的都知道ELK使用起来的成本,而且仅仅是日志检索使用ELK的话有点大材小用了。Loki8技术栈中使用了以下组件。Promtail用来将容器日志发送到Loki或者Grafana服务上的日志收集工具,该工具主要包括发现采集目标以及给日志......
  • nginx部署 vue3 同时 配置接口代理(详细)
    Vue项目配置.env文件在项目根目录下创建文件夹(.env.production)##.env.production生产环境配置VUE_APP_SYS_URL=sysapi##nginx需要用的的代理表示VUE_APP_MODE=product##模式baseUrl使用VUE_APP_SYS_URL变量代替Nginx下载部署和配置api代理Nginx下载......
  • springboot整合kafka
    一、引入依赖(kafka的版本和springboot的版本对不上的话,启动会报错,包类不存在)<dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId><version>2.5.1.RELEASE</version></de......
  • kubernetes之 存储卷
    第八部分kubernetes之存储卷脱离节点而存在共享存储。存储卷不属于容器,他属于pod缓存,宿主机,不具备真正意义上存储,宿主机退役后,存储资源随之丢失,除非宿主机上也挂载独立的卷信息。容器真正意义上的存储卷类型emptyDir:pod删除,存储内容也删除,只能当临时存储空间或缓存使用,无真正意......
  • IIS部署createWebHistory路由模式的Vue项目
    接口使用NetCore,前端使用Vue3.+网站部署目录如下api(虚拟应用程序)webindex.html需要在服务器上按照URLRewrite下载地址:https://www.iis.net/downloads/microsoft/url-rewrite安装后在网站根目录下创建“web.config”文件,内容如下<?xmlversion="1.0"encoding="UTF-8"?......
  • Jenkins远程管理K8S集群实现自动POD部署
    Jenkins远程管理K8S集群实现自动POD部署大致思路修改.kube/config文件,增加新集群的context建立隧道将集群控制端口映射到Jenkins服务器本地端口测试jenkin切换到新context是否能控制该集群新集群配置对接Harbor仓库测试helm部署podJenkins测试配置流水线自动发版1.......
  • Windows系统使用Nginx部署Vue
    Nginx是什么?Nginx(enginex)是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点开发的,因它的稳定性、丰富的功能集、简单的配置文件和低系统资源的消耗而闻名。优点速度更快、并发更高......
  • xxl-job部署
    XXL-JOB安装、配置、启动、停止教程安装xxl-jobstep1:上传step2:解压step3:初始化数据库step4:修改xxl-job配置文件step4.1:修改调度中心配置文件application.propertiesstep4.2:修改logback.xml(配置xxl-job的日志路径)step4.3:修改执行器配置文件application.propertiesstep4.4:修改logb......
  • jenkins部署及gitlab联调
    jenkins部署及gitlab联调目录jenkins部署及gitlab联调一、jenkins安装1.环境优化2.安装jdkjava环境3.下载jenkins4.启动Jenkins服务5.上个步骤执行成功后,会弹出下面的对话框,我们点击关闭即可6.Jenkins的目录介绍7.修改Jenkins密码二、Jenkins插件安装实战篇1.点击系统管理2>.......
  • HCIP-进阶实验07-高可靠性园区网部署
    HCIP-进阶实验07-高可靠性园区网部署1实验需求1.1实验拓扑1.2实验环境说明设备接口IP地址备注AR1G0/0/0172.16.1.1/24G0/0/1172.16.2.1/24loopback0202.22.2.2/24ISPSW1VLANIF10192.168.1.254/24VLANIF20192.168.2.253/24......