当前位置:   article > 正文

k8s单节点Kafka-29_kubernetes部署单机版kafka的yaml

kubernetes部署单机版kafka的yaml
cat > zk.yaml <<EOF
apiVersion: v1
kind: Service
metadata:
  labels:
    app: zookeeper-service
  name: zookeeper-service
spec:
  ports:
  - name: zookeeper-port
    port: 2181
    targetPort: 2181
  selector:
    app: zookeeper
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: zookeeper
  name: zookeeper
spec:
  replicas: 1
  selector:
    matchLabels:
      app: zookeeper
  template:
    metadata:
      labels:
        app: zookeeper
    spec:
      containers:
      - image: wurstmeister/zookeeper
        imagePullPolicy: IfNotPresent
        name: zookeeper
        ports:
        - containerPort: 2181
EOF
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
cat > kafka.yaml <<EOF
apiVersion: v1
kind: Service
metadata:
  name: kafka-service
  labels:
    app: kafka
spec:
  type: NodePort
  ports:
  - port: 9092
    name: kafka-port
    targetPort: 9092
    nodePort: 30092
    protocol: TCP
  selector:
    app: kafka

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: kafka
  labels:
    app: kafka
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kafka
  template:
    metadata:
      labels:
        app: kafka
    spec:
      containers:
      - name: kafka
        image: wurstmeister/kafka
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 9092
        env:
        - name: KAFKA_ADVERTISED_PORT
          value: "9092"
        - name: KAFKA_ADVERTISED_HOST_NAME
          value: "kafka-service" #[kafka的service的clusterIP]
        - name: KAFKA_ZOOKEEPER_CONNECT
          value: zookeeper-service:2181
        - name: KAFKA_BROKER_ID
          value: "1"
EOF
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • .创建后,需要对服务进行测试
[root@master240 asd]# kubectl get svc && kubectl get pods
NAME                TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kafka-service       NodePort    10.106.153.152   <none>        9092:30092/TCP   11h
kubernetes          ClusterIP   10.96.0.1        <none>        443/TCP          3d19h
zookeeper-service   ClusterIP   10.96.240.113    <none>        2181/TCP         11h
NAME                         READY   STATUS    RESTARTS   AGE
kafka-6d5d8884fc-qmq68       1/1     Running   0          11h
zookeeper-75fd7fdb96-6dh6f   1/1     Running   0          11h
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 进入容器
kubectl exec -it [Kafka的pod名称]  -- bash

[root@master240 asd]# kubectl exec -it kafka-6d5d8884fc-qmq68 -- bash
bash-5.1# kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic jbjb
>11111111111111
>2222222222222
>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 另外打开一个linux终端,执行相同的命令进入容器或者外部执行以下命令。这次将这个终端作为消费者。注意,上面的博客中写的创建消费者的方法在新版的Kafka中已经改变,需要执行下面的命令:
[root@master240 ~]# kubectl exec -it kafka-6d5d8884fc-qmq68 -- kafka-console-consumer.sh --bootstrap-server 127.0.0.1:9092 --topic jbjb --from-beginning
jbjbbbjb
11111111111111
2222222222222
#之后,在生产者输入信息,查看消费者是否能够接收到。如果接收到,说明运行成功
  • 1
  • 2
  • 3
  • 4
  • 5
  • 最后,还可以执行下面的命令以测试列出所有的消息主题:
[root@master240 ~]# kubectl exec -it kafka-6d5d8884fc-qmq68 -- kafka-topics.sh --list --zookeeper zookeeper-service:2181 
__consumer_offsets
jbjb
test
#注意,有时需要用Kafka的端口,有时需要用Zookeeper的端口,应注意区分。
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/864168
推荐阅读
相关标签
  

闽ICP备14008679号