Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 672|回复: 0

kubernetes statefulset kafka 部署后, 外部访问超时问题解决

[复制链接]
  • TA的每日心情
    奋斗
    6 天前
  • 签到天数: 803 天

    [LV.10]以坛为家III

    2053

    主题

    2111

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    726482
    发表于 2021-7-10 06:39:50 | 显示全部楼层 |阅读模式

    k8s 内部的kafka要映射到外网,直接把 kafka 通过 expose 把pod 映射成服务,使用nodeport 连接,出现超时问题,

     



    解决思路:

        1.  查看zk中,kafka的注册信息,PLAINTEXT://kafka-0.kafka-hs.sy-platform-demo.svc.cluster.local.:9093", 而应用代码中,必须使用这个域名+端口进行连接,由于k8s使用30000-32000,端口对外映射,所以9093端口必须修改

     

     

     

      2.  kafka.yaml文件修改端口

    apiVersion: v1
    kind: Service
    metadata:
      name: kafka-hs
      namespace: sy-platform-demo
      labels:
        app: kafka
    spec:
      ports:
      - port: 31902
        name: server
      clusterIP: None
      selector:
        app: kafka
    
    
    ---
    apiVersion: v1
    kind: Service
    metadata:
      namespace: sy-platform-demo
      name: kafka-cs
      labels:
        app: kafka
    spec:
      ports:
      - port: 31902
        name: client
      selector:
        app: kafka
    
    ---
    apiVersion: policy/v1beta1
    kind: PodDisruptionBudget
    metadata:
      namespace: sy-platform-demo
      name: kafka-pdb
    spec:
      selector:
        matchLabels:
          app: kafka
      maxUnavailable: 1
    ---
    apiVersion: apps/v1beta1
    kind: StatefulSet
    metadata:
      namespace: sy-platform-demo
      name: kafka
    spec:
      serviceName: kafka-hs
      replicas: 3
      podManagementPolicy: Parallel
      updateStrategy:
          type: RollingUpdate
      template:
        metadata:
          labels:
            app: kafka
        spec:
          affinity:
            podAntiAffinity:
              requiredDuringSchedulingIgnoredDuringExecution:
                - labelSelector:
                    matchExpressions:
                      - key: "app"
                        operator: In
                        values: 
                        - kafka
                  topologyKey: "kubernetes.io/hostname"
            podAffinity:
              preferredDuringSchedulingIgnoredDuringExecution:
                 - weight: 1
                   podAffinityTerm:
                     labelSelector:
                        matchExpressions:
                          - key: "app"
                            operator: In
                            values: 
                            - zk
                     topologyKey: "kubernetes.io/hostname"
          terminationGracePeriodSeconds: 300
          containers:
          - name: k8skafka
            imagePullPolicy: Always
            image: 192.168.200.10/fengjian/kafka_2.11-0.10.1.1:20190326
            resources:
              requests:
                memory: "3Gi"
                cpu: 2
            ports:
            - containerPort: 31902
              name: server
            command:
            - sh
            - -c
            - "exec /data/kafka/bin/kafka-server-start.sh /data/kafka/config/server.properties --override broker.id=${HOSTNAME##*-} \
              --override listeners=PLAINTEXT://:31902 \
              --override zookeeper.connect=zk-cs.sy-platform-demo.svc.cluster.local:2181 \
              --override request.timeout.ms=60000 \
              --override session.timeout.ms=150000 \
              --override heartbeat.interval.ms=50000"
            env:
            - name: KAFKA_HEAP_OPTS
              value : "-Xmx1G -Xms1G"
            - name: KAFKA_OPTS
              value: "-Dlogging.level=INFO"
            volumeMounts:
            - name: datadir
              mountPath: /export
            readinessProbe:
              tcpSocket:
                port: 31902
              initialDelaySeconds: 30
              periodSeconds: 10
      volumeClaimTemplates:
      - metadata:
          name: datadir
          annotations:
            volume.beta.kubernetes.io/storage-class: "ceph-rbd-provisioner"
        spec:
          accessModes: [ "ReadWriteOnce" ]
          resources:
            requests:
              storage: 30Gi

     

     

      3. 对pod打标签,然后映射成服务

    kubectl label pod kafka-0 kafkaInst=0 -n sy-platform-demo
    kubectl expose pod kafka-0 --port=31902 --target-port=31902 --name=kafka-0 --selector=kafkaInst=0 --type=NodePort -n sy-platform-demo

     

      4. 修改kafka-0 的nodeport 端口号

    kubectl edit  service kafka-0 -n sy-platform-demo

     

       5. 外部测试机修改host,写入对应关系

    [root@node2 logs]# cat /etc/hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    192.168.20.171        master1    etcd1
    192.168.20.172        master2 etcd2
    192.168.20.173        master3    etcd3
    192.168.20.174        node1
    192.168.20.175        node2
    192.168.20.176        node3
    192.168.20.177        node4
    192.168.20.178        node5
    192.168.20.174        kafka-0.kafka-hs.senyint.svc.cluster.local.
    192.168.20.174        zk-cs

     

      6. 修改代码文件,写入域名

    abcd.queue.internal.notify.kafka.producer.bootstrap.servers=kafka-0.kafka-hs.senyint.svc.cluster.local.:30010
    abcd..queue.zkAddress=zk-cs:31097

     

      7. 遗留问题

    目前只映射了一个kafka pod, 如果全部映射,那么需要修改端口后,保持kafka-0 ---kafka-2 的端口号不同

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-11-23 16:21 , Processed in 2.050800 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表