远程访问kubernetes内部运行的Kafka

zf2sa74q  于 2021-06-04  发布在  Kafka
关注(0)|答案(3)|浏览(465)

我有一个单节点kafka代理在单节点kubernetes环境的pod中运行。我用这张图片来描述Kafka:https://hub.docker.com/r/wurstmeister/kafka
Kafka版本=1.1.0
kubernetes群集正在服务器上的vm中运行。vm在活动接口ens32上具有以下ip- 192.168.3.102 Kafka.亚马尔

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  namespace: casb-deployment
  name: kafkaservice
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: kafkaservice
    spec:
      hostname: kafkaservice
      containers:
      - name: kafkaservice
        imagePullPolicy: IfNotPresent
        image: wurstmeister/kafka:1.1.0
        env:
         - name: KAFKA_BROKER_ID
           value: "1"

# - name: KAFKA_ADVERTISED_HOST_NAME

# value: "kafkaservice"

         - name: KAFKA_LISTENER_SECURITY_PROTOCOL_MAP
           value: "INTERNAL_PLAINTEXT:PLAINTEXT,EXTERNAL_PLAINTEXT:PLAINTEXT"
         - name: KAFKA_LISTENERS
           value: "INTERNAL_PLAINTEXT://0.0.0.0:9092,EXTERNAL_PLAINTEXT://0.0.0.0:9093"
         - name: KAFKA_ADVERTISED_LISTENERS
           value: "INTERNAL_PLAINTEXT://kafkaservice:9092,EXTERNAL_PLAINTEXT://192.168.3.102:9093"
         - name: KAFKA_INTER_BROKER_LISTENER_NAME
           value: "INTERNAL_PLAINTEXT"
         - name: KAFKA_CREATE_TOPICS
           value: "topic-1:100:1,topic-2:1:1"  
         - name: KAFKA_ZOOKEEPER_CONNECT
           value: "zookeeper:2181"
        ports: 
        - name: port9092
          containerPort: 9092
        - name: port9093
          containerPort: 9093
        volumeMounts:
        - mountPath: /kafka/kafka-logs-kafkaservice
          name: kafka-volume
      volumes:
      - name: kafka-volume
        hostPath:
          path: /home/volume/kafka-logs

---

apiVersion: v1
kind: Service
metadata:
  namespace: casb-deployment
  name: kafkaservice
  labels:
    app: kafkaservice
spec:
  selector:
    app: kafkaservice
  ports:
  - name: port9092
    port: 9092
    targetPort: 9092
    protocol: TCP

---

apiVersion: v1
kind: Service
metadata:
  namespace: casb-deployment
  name: kafkaservice-external
  labels:
    app: kafkaservice-external
spec:
  selector:
    app: kafkaservice
  ports:
  - name: port9093
    port: 9093
    protocol: TCP
    nodePort: 30035
  type: NodePort

我可以从本地机器ping vm,即kubernetes节点 ping 192.168.3.102 我正在使用nodeport公开服务。
我也可以远程登录 telnet 192.168.3.102 30035 它给出了:

Trying 192.168.3.102...
Connected to 192.168.3.102.
Escape character is '^]'.

我尝试从本地计算机运行kafka控制台consumer and producer:
consumer:bin/kafka-console-consumer.sh--引导服务器192.168.3.102:30035--主题1--从头开始
输出:

[2019-09-25 12:30:40,716] WARN [Consumer clientId=consumer-1, groupId=console-consumer-20551] Connection to node 1 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)

制作人:

bin/kafka-console-producer.sh --broker-list 192.168.3.102:30035 --topic topic-1

输出:

[2019-09-25 12:32:07,958] WARN [Producer clientId=console-producer] Connection to node 1 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)

网络状态:

netstat -tulpn | grep 30035
tcp6       0      0 :::30035                :::*                    LISTEN      113545/kube-proxy

我试着运行一个基于python的消费者,即kafka python==1.4.2,它给了我以下日志:

[2019-09-25T12:15:39+0500] INFO kafka.client Bootstrapping cluster metadata from [('192.168.3.102', 30035, <AddressFamily.AF_INET: 2>)]
[2019-09-25T12:15:39+0500] INFO kafka.conn <BrokerConnection node_id=bootstrap host=192.168.3.102:30035 <connecting> [IPv4 ('192.168.3.102', 30035)]>: connecting to 192.168.3.102:30035 [('192.168.3.102', 30035) IPv4]
[2019-09-25T12:15:39+0500] INFO kafka.conn <BrokerConnection node_id=bootstrap host=192.168.3.102:30035 <connecting> [IPv4 ('192.168.3.102', 30035)]>: Connection complete.
[2019-09-25T12:15:39+0500] INFO kafka.client Bootstrap succeeded: found 1 brokers and 26 topics.
[2019-09-25T12:15:39+0500] INFO kafka.conn <BrokerConnection node_id=bootstrap host=192.168.3.102:30035 <connected> [IPv4 ('192.168.3.102', 30035)]>: Closing connection. 
[2019-09-25T12:15:39+0500] INFO kafka.conn <BrokerConnection node_id=1 host=192.168.3.102:9093 <connecting> [IPv4 ('192.168.3.102', 9093)]>: connecting to 192.168.3.102:9093 [('192.168.3.102', 9093) IPv4]
[2019-09-25T12:15:39+0500] INFO kafka.conn Probing node 1 broker version
[2019-09-25T12:15:39+0500] ERROR kafka.conn Connect attempt to <BrokerConnection node_id=1 host=192.168.3.102:9093 <connecting> [IPv4 ('192.168.3.102', 9093)]> returned error 111. Disconnecting.
[2019-09-25T12:15:39+0500] INFO kafka.conn <BrokerConnection node_id=1 host=192.168.3.102:9093 <connecting> [IPv4 ('192.168.3.102', 9093)]>: Closing connection. ConnectionError: 111 ECONNREFUSED
[2019-09-25T12:15:40+0500] INFO kafka.conn <BrokerConnection node_id=1 host=192.168.3.102:9093 <connecting> [IPv4 ('192.168.3.102', 9093)]>: connecting to 192.168.3.102:9093 [('192.168.3.102', 9093) IPv4]
[2019-09-25T12:15:40+0500] ERROR kafka.conn Connect attempt to <BrokerConnection node_id=1 host=192.168.3.102:9093 <connecting> [IPv4 ('192.168.3.102', 9093)]> returned error 111. Disconnecting.
[2019-09-25T12:15:40+0500] INFO kafka.conn <BrokerConnection node_id=1 host=192.168.3.102:9093 <connecting> [IPv4 ('192.168.3.102', 9093)]>: Closing connection. ConnectionError: 111 ECONNREFUSED
[2019-09-25T12:15:40+0500] INFO Activity URL collector Exception in activity url collector: NoBrokersAvailable

从日志上看,似乎是建立了联系。

<connecting> [IPv4 ('192.168.3.102', 30035)]>: Connection complete.
Bootstrap succeeded: found 1 brokers and 26 topics.

但后来断开了。
请帮助我找出我遗漏了什么,以及如何解决这个问题。谢谢。

frebpwbc

frebpwbc1#

我也会建议斯特里姆齐为Kafka在Kubernetes。对于外部访问,这篇文章救了我https://developers.redhat.com/blog/2019/06/11/accessing-apache-kafka-in-strimzi-part-4-load-balancers/. 我的配置看起来像:

apiVersion: kafka.strimzi.io/v1beta1
kind: Kafka
metadata:
  name: my-cluster
spec:
  kafka:
    version: 2.4.0
    replicas: 1
    listeners:
      plain: {}
      tls: {}
      external:
        type: loadbalancer
        tls: false
    config:
      offsets.topic.replication.factor: 1
      transaction.state.log.replication.factor: 1
      transaction.state.log.min.isr: 1
      log.message.format.version: "2.4"
    storage:
      type: ephemeral
  zookeeper:
    replicas: 1
    storage:
      type: ephemeral
  entityOperator:
    topicOperator: {}
    userOperator: {}

这是用来检索ip的

kubectl get service my-cluster-kafka-external-bootstrap -o=jsonpath='{.status.loadBalancer.ingress[0].ip}{"\n"}'
pod7payv

pod7payv2#

您正在引导到端口30035,并获得初始连接,然后为后续连接返回播发的端口9093,而不是30035。
您需要nodeport和公布的nodeport相同,或者至少两者都是外部可路由的。如果代码在主机上运行,那么您还需要在vm上进行端口转发
注:合流或斯特里姆齐 Helm 图表存在设置Kafka在Kubernetes

wxclj1h5

wxclj1h53#

在aws/digitalocean/gce/azure中的kubernetes上简单配置kafka/zookeeper,外部访问:
https://github.com/stanislavko/k8s_digitalocean_kafka
您可以通过常规二进制协议从aws/do/gce外部连接到kafka。连接为纯文本或sasl\u纯文本(用户名/密码)。
kafka集群是statefolset,所以您可以轻松地扩展集群。

相关问题