从在Kubernetes上运行的Spring Boot应用程序访问kafka

时间:2019-03-11 08:21:10

标签: kubernetes apache-kafka spring-kafka

我在kubernetes集群中运行了一个kafka安装。我有一个运行春季启动应用程序的Pod,该应用程序使用默认的bootstrap.servers(localhost:9092)配置,而不是传入的配置(bootstrap.kafka.svc.cluster.local:9092)。然后,由于kafka未在本地主机上运行,​​因此pod无法启动。

这是我的春季靴子配置

spring:
  kafka:
    consumer:
      group-id: spring-template
      auto-offset-reset: earliest
      bootstrap-servers: "bootstrap.kafka.svc.cluster.local:9092"
    producer:
      bootstrap-servers: "bootstrap.kafka.svc.cluster.local:9092"
    bootstrap-servers: "bootstrap.kafka.svc.cluster.local:9092"

在启动时设置使用者时,将从上述配置正确传递组ID和自动偏移量重置。但是,bootstrap-servers配置不是,并且根据以下日志,应用程序实际上正在使用localhost:9092

2019-03-11 07:34:36.826  INFO 1 --- [  restartedMain] o.a.k.clients.consumer.ConsumerConfig    : ConsumerConfig values:
        auto.commit.interval.ms = 5000
        auto.offset.reset = earliest
        bootstrap.servers = [localhost:9092]
        check.crcs = true
        client.id =
        connections.max.idle.ms = 540000
        default.api.timeout.ms = 60000
        enable.auto.commit = true
        exclude.internal.topics = true
        fetch.max.bytes = 52428800
        fetch.max.wait.ms = 500
        fetch.min.bytes = 1
        group.id = spring-template
        heartbeat.interval.ms = 3000
        interceptor.classes = []
        internal.leave.group.on.close = true
        isolation.level = read_uncommitted
        key.deserializer = class org.apache.kafka.common.serialization.StringDeserializer
        max.partition.fetch.bytes = 1048576
        max.poll.interval.ms = 300000
        max.poll.records = 500
        metadata.max.age.ms = 300000
        metric.reporters = []
        metrics.num.samples = 2
        metrics.recording.level = INFO
        metrics.sample.window.ms = 30000
        partition.assignment.strategy = [class org.apache.kafka.clients.consumer.RangeAssignor]
        receive.buffer.bytes = 65536
        reconnect.backoff.max.ms = 1000
        reconnect.backoff.ms = 50
        request.timeout.ms = 30000
        retry.backoff.ms = 100
        sasl.client.callback.handler.class = null
        sasl.jaas.config = null
        sasl.kerberos.kinit.cmd = /usr/bin/kinit
        sasl.kerberos.min.time.before.relogin = 60000
        sasl.kerberos.service.name = null
        sasl.kerberos.ticket.renew.jitter = 0.05
        sasl.kerberos.ticket.renew.window.factor = 0.8
        sasl.login.callback.handler.class = null
        sasl.login.class = null
        sasl.login.refresh.buffer.seconds = 300
        sasl.login.refresh.min.period.seconds = 60
        sasl.login.refresh.window.factor = 0.8
        sasl.login.refresh.window.jitter = 0.05
        sasl.mechanism = GSSAPI
        security.protocol = PLAINTEXT
        send.buffer.bytes = 131072
        session.timeout.ms = 10000
        ssl.cipher.suites = null
        ssl.enabled.protocols = [TLSv1.2, TLSv1.1, TLSv1]
        ssl.endpoint.identification.algorithm = https
        ssl.key.password = null
        ssl.keymanager.algorithm = SunX509
        ssl.keystore.location = null
        ssl.keystore.password = null
        ssl.keystore.type = JKS
        ssl.protocol = TLS
        ssl.provider = null
        ssl.secure.random.implementation = null
        ssl.trustmanager.algorithm = PKIX
        ssl.truststore.location = null
        ssl.truststore.password = null
        ssl.truststore.type = JKS
        value.deserializer = class org.apache.kafka.common.serialization.StringDeserializer

2019-03-11 07:34:36.942  INFO 1 --- [  restartedMain] o.a.kafka.common.utils.AppInfoParser     : Kafka version : 2.0.1
2019-03-11 07:34:36.945  INFO 1 --- [  restartedMain] o.a.kafka.common.utils.AppInfoParser     : Kafka commitId : fa14705e51bd2ce5
2019-03-11 07:34:37.149  WARN 1 --- [  restartedMain] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-1, groupId=spring-template] Connection to node -1 could not be established. Broker may not be available.

我有一个名为bootstrap的kubernetes服务,它在kubernetes集群中的命名空间kafka中运行。这是日志文件的一个片段。为什么Spring Boot应用程序无法获取已配置的bootstrap.servers配置

1 个答案:

答案 0 :(得分:0)

有时候,实际上是您的Kafka Kubernetes部署有问题。

请问我能看到您针对Kafka部署的部署YAML / JSON文件吗?

它应该像这样:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-kafka
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kafka
      id: "0"
  template:
    metadata:
      labels:
        app: kafka
        id: "0"
    spec:
      containers:
        - name: kafka
          image: wurstmeister/kafka:latest
          ports:
            - containerPort: 30035
          env:
            - name: KAFKA_ADVERTISED_HOST_NAME
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP

            - name: KAFKA_ADVERTISED_PORT
              value: "9092"

            - name: KAFKA_HEAP_OPTS
              value: -Xms320m

            - name: KAFKA_ZOOKEEPER_CONNECT
              value: "zookeeper:2181"

---
apiVersion: v1
kind: Service
metadata:
  name: api-kafka
  namespace: default
  labels:
    name: kafka
spec:
  ports:
    - port: 9092
      name: kafka-port
      protocol: TCP
  selector:
    app: kafka
    id: "0"

  type: NodePort

重点是:

- name: KAFKA_ADVERTISED_HOST_NAME
  valueFrom:
      fieldRef:
          fieldPath: status.podIP

确保您的配置文件正确,并且可以解决与Kubernetes部署中的连接和公布的主机名有关的所有问题