Kafka NodePort服务在群集外无法访问

问题描述 投票:0回答:1

我一直在尝试使用Helm charts部署Kafka。所以我为Kafka pod定义了NodePort服务。我检查了控制台Kafka生产者和消费者使用相同的主机和端口 - 他们正常工作。但是,当我创建Spark应用程序作为数据使用者而Kafka作为生产者时,他们无法连接到Kafka service0。我使用minikube ip(而不是node ip)作为主机和服务NodePort端口。虽然,在Spark日志中,我看到NodePort服务解析了端点,并且发现了代理作为pods和端口的代理:

INFO AbstractCoordinator: [Consumer clientId=consumer-1, groupId=avro_data] Discovered group coordinator 172.17.0.20:9092 (id: 2147483645 rack: null)
INFO ConsumerCoordinator: [Consumer clientId=consumer-1, groupId=avro_data] Revoking previously assigned partitions []
INFO AbstractCoordinator: [Consumer clientId=consumer-1, groupId=avro_data] (Re-)joining group
WARN NetworkClient: [Consumer clientId=consumer-1, groupId=avro_data] Connection to node 2147483645 (/172.17.0.20:9092) could not be established. Broker may not be available.
INFO AbstractCoordinator: [Consumer clientId=consumer-1, groupId=avro_data] Group coordinator 172.17.0.20:9092 (id: 2147483645 rack: null) is unavailable or invalid, will attempt rediscovery
WARN NetworkClient: [Consumer clientId=consumer-1, groupId=avro_data] Connection to node 2 (/172.17.0.20:9092) could not be established. Broker may not be available.
WARN NetworkClient: [Consumer clientId=consumer-1, groupId=avro_data] Connection to node 0 (/172.17.0.12:9092) could not be established. Broker may not be available.

如何改变这种行为?

NodePort服务定义如下所示:

kind: Service
apiVersion: v1
metadata:
  name: kafka-service
spec:
  selector:
    app: cp-kafka
    release: my-confluent-oss
  ports:
    - protocol: TCP
      targetPort: 9092
      port: 32400
      nodePort: 32400
  type: NodePort

Spark消费者配置:

def kafkaParams() = Map[String, Object](
  "bootstrap.servers" -> "192.168.99.100:32400",
  "schema.registry.url" -> "http://192.168.99.100:8081",
  "key.deserializer" -> classOf[StringDeserializer],
  "value.deserializer" -> classOf[KafkaAvroDeserializer],
  "group.id" -> "avro_data",
  "auto.offset.reset" -> "earliest",
  "enable.auto.commit" -> (false: java.lang.Boolean)
)

Kafka制作人配置:

  props.put("bootstrap.servers", "192.168.99.100:32400")
  props.put("client.id", "avro_data")
  props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
  props.put("value.serializer", "io.confluent.kafka.serializers.KafkaAvroSerializer")
  props.put("schema.registry.url", "http://192.168.99.100:32500")

Kafka的所有K8服务:

NAME                                     TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)             AGE
kafka-service                            NodePort       10.99.113.234    <none>        32400:32400/TCP     6m34s
kubernetes                               ClusterIP      10.96.0.1        <none>        443/TCP             27d
my-confluent-oss-cp-kafka                ClusterIP      10.100.156.108   <none>        9092/TCP            102m
my-confluent-oss-cp-kafka-connect        ClusterIP      10.99.78.89      <none>        8083/TCP            102m
my-confluent-oss-cp-kafka-headless       ClusterIP      None             <none>        9092/TCP            102m
my-confluent-oss-cp-kafka-rest           ClusterIP      10.100.152.109   <none>        8082/TCP            102m
my-confluent-oss-cp-ksql-server          ClusterIP      10.96.249.202    <none>        8088/TCP            102m
my-confluent-oss-cp-schema-registry      ClusterIP      10.109.27.45     <none>        8081/TCP            102m
my-confluent-oss-cp-zookeeper            ClusterIP      10.102.182.90    <none>        2181/TCP            102m
my-confluent-oss-cp-zookeeper-headless   ClusterIP      None             <none>        2888/TCP,3888/TCP   102m
schema-registry-service                  NodePort       10.103.100.64    <none>        32500:32500/TCP     33m
zookeeper-np                             NodePort       10.98.180.130    <none>        32181:32181/TCP     53m
apache-spark kubernetes apache-kafka kubernetes-helm
1个回答
2
投票

当我试图从外面访问在minikube上运行的kafka经纪人(cp-helm-chart)时,我遇到了类似的问题。

我在这里如何解决它。在从本地存储库使用helm install安装之前。

  1. 在此文件https://github.com/confluentinc/cp-helm-charts/blob/master/charts/cp-kafka/values.yaml内编辑
  2. 搜索nodeport:并将其enabled字段更改为true。 nodeport: 启用:true
  3. 通过删除#取消注释这两行: “advertised.listeners”:| - EXTERNAL:// $ {HOST_IP}:$((31090 + $ {KAFKA_BROKER_ID}))
  4. 用minikube ip替换$ {HOST_IP}(在cmd中输入minikube ip以检索你的k8s主机ip,例如:196.169.99.100)
  5. 用代理id替换$ {KAFKA_BROKER_ID}(如果只有一个代理正在运行,那么它默认为0)
  6. 最后它看起来像这样: “advertised.listeners”:| - 外部://196.169.99.100:31090

现在,您可以通过将bootstrap.servers指向196.169.99.100:31090来从外部访问在k8s集群中运行的kafka代理

© www.soinside.com 2019 - 2024. All rights reserved.