如何在kubernetes上启动kafka

时间:2019-07-17 13:45:19

标签: kubernetes apache-kafka

我的目标是从kubernetes上的多个分区开始一个kafka主题。 为此,我部署了以下.yml文件:

apiVersion: v1
kind: Namespace
metadata:
  name: kafka

---

apiVersion: v1
kind: Pod
metadata:
  name: kubernetes-kafka
  namespace: kafka
  labels:
    k8s-app: kubernetes-kafka
spec:
  containers:
    - name: zookeeper
      image: zookeeper
      env:
      - name: ZOO_MY_ID
        value: "1"


    - name: kafka
      image: wurstmeister/kafka
      env:
      - name: KAFKA_ADVERTISED_HOST_NAME
        value: "kubernetes-cluster.nt"
      - name: KAFKA_ADVERTISED_PORT
        value: 30001
      - name: KAFKA_ZOOKEEPER_CONNECT
        value: "localhost:2181"
      - name: KAFKA_BROKER_ID
        value: "1"
      - name: KAFKA_CREATE_TOPICS
        value: write:20:1

---

apiVersion: v1
kind: Service
metadata:
  name: kubernetes-kafka
  namespace: kafka
spec:
  type: NodePort
  selector:
    k8s-app: kubernetes-kafka
  ports:
  - name: zk-client
    port: 2181
    protocol: TCP
  - name: zk-follower
    port: 2888
    protocol: TCP
  - name: zk-election
    port: 3888
    protocol: TCP
  - name: zk-admin
    port: 8080
    protocol: TCP
  - name: kafka-client
    port: 9092
    nodePort: 30001
    protocol: TCP

我希望这段代码可以在kafka上创建一个kubernetes服务器,可以通过write来访问其host:port = kubernetes-cluster.nt:30001主题

但是,尽管kubernetes服务和pod已启动(kubectl get pods --all-namespaceskubectl get services --all-namespaces命令列出了名称为kubernetes-kafka的条目),但未创建kafka主题:

kafkacat -b kubernetes-cluster.nt:30001 -L

应列出所有主题,返回创建0个主题:

Metadata for all topics (from broker 1: kubernetes-cluster.nt:30001/1):
 1 brokers:
   broker 1 at kubernetes-cluster.nt:30001
 0 topics:

我在做什么错

2 个答案:

答案 0 :(得分:0)

确保Kafka外部端口和k8s服务nodePort一致,其他服务调用k8s-service:nodeport。写这个config_kafka_in_kubernetes,希望对你有帮助!

答案 1 :(得分:0)

我将使用运算符在Kubernetes中运行Kafka。 我会推荐Strimzi kafka operator