k8s kafka集群 连接不上_在K8s下部署Kafka集群

说明

今天来记录一下在K8s下部署Kafka集群,本以为K8s发展到今天已经有很多人踩过坑了,安装个Kafka问题也不大,可自己还是遇到了坑o(╥﹏╥)o,由于我的K8s集群是1master多worker,所以可能多master的情况下配置也不一样,请酌情查看。

快速开始

master_ip:192.168.128.52

先创建一个命名空间,之后全在这操作

kubectl create namespace zookeeper

Zookeeper集群的安装

创建zookeeper-svc.yaml

apiVersion: v1

kind: Service

metadata:

name: zoo1

labels:

app: zookeeper-1

spec:

ports:

- name: client

port: 2181

protocol: TCP

- name: follower

port: 2888

protocol: TCP

- name: leader

port: 3888

protocol: TCP

selector:

app: zookeeper-1

---

apiVersion: v1

kind: Service

metadata:

name: zoo2

labels:

app: zookeeper-2

spec:

ports:

- name: client

port: 2181

protocol: TCP

- name: follower

port: 2888

protocol: TCP

- name: leader

port: 3888

protocol: TCP

selector:

app: zookeeper-2

---

apiVersion: v1

kind: Service

metadata:

name: zoo3

labels:

app: zookeeper-3

spec:

ports:

- name: client

port: 2181

protocol: TCP

- name: follower

port: 2888

protocol: TCP

- name: leader

port: 3888

protocol: TCP

selector:

app: zookeeper-3

创建zookeeper-deployment.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

name: zookeeper-deployment-1

spec:

replicas: 1

selector:

matchLabels:

app: zookeeper-1

name: zookeeper-1

template:

metadata:

labels:

app: zookeeper-1

name: zookeeper-1

spec:

containers:

- name: zoo1

image: zookeeper

imagePullPolicy: IfNotPresent

ports:

- containerPort: 2181

env:

- name: ZOO_MY_ID

value: "1"

- name: ZOO_SERVERS

value: "server.1=0.0.0.0:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181"

---

apiVersion: apps/v1

kind: Deployment

metadata:

name: zookeeper-deployment-2

spec:

replicas: 1

selector:

matchLabels:

app: zookeeper-2

name: zookeeper-2

template:

metadata:

labels:

app: zookeeper-2

name: zookeeper-2

spec:

containers:

- name: zoo2

image: zookeeper

imagePullPolicy: IfNotPresent

ports:

- containerPort: 2181

env:

- name: ZOO_MY_ID

value: "2"

- name: ZOO_SERVERS

value: "server.1=zoo1:2888:3888;2181 server.2=0.0.0.0:2888:3888;2181 server.3=zoo3:2888:3888;2181"

---

apiVersion: apps/v1

kind: Deployment

metadata:

name: zookeeper-deployment-3

spec:

replicas: 1

selector:

matchLabels:

app: zookeeper-3

name: zookeeper-3

template:

metadata:

labels:

app: zookeeper-3

name: zookeeper-3

spec:

containers:

- name: zoo3

image: zookeeper

imagePullPolicy: IfNotPresent

ports:

- containerPort: 2181

env:

- name: ZOO_MY_ID

value: "3"

- name: ZOO_SERVERS

value: "server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=0.0.0.0:2888:3888;2181"

部署zookeeper

kubectl apply -f zookeeper-svc.yaml -n zookeeper

kubectl apply -f zookeeper-deployment.yaml -n zookeeper

Kafka集群的安装

创建kafka-svc.yaml

apiVersion: v1

kind: Service

metadata:

name: kafka-service-1

labels:

app: kafka-service-1

spec:

type: NodePort

ports:

- port: 9092

name: kafka-service-1

targetPort: 9092

nodePort: 30901

protocol: TCP

selector:

app: kafka-service-1

---

apiVersion: v1

kind: Service

metadata:

name: kafka-service-2

labels:

app: kafka-service-2

spec:

type: NodePort

ports:

- port: 9092

name: kafka-service-2

targetPort: 9092

nodePort: 30902

protocol: TCP

selector:

app: kafka-service-2

---

apiVersion: v1

kind: Service

metadata:

name: kafka-service-3

labels:

app: kafka-service-3

spec:

type: NodePort

ports:

- port: 9092

name: kafka-service-3

targetPort: 9092

nodePort: 30903

protocol: TCP

selector:

app: kafka-service-3

创建kafka-deployment.yaml(注意修改我尖括号的地方)

查看CLUSTER-IP可以使用kubectl get svc -n zookeeper,注意KAFKA_ADVERTISED_LISTENERS变量,我就是在这踩的坑,发现这里不写的话命令行能用,但使用java程序去连接会因为没有代理而直接连接内网地址,最终导致连接不上node而出错(你会发现程序会去直接连接CLUSTER-IP)

apiVersion: apps/v1

kind: Deployment

metadata:

name: kafka-deployment-1

spec:

replicas: 1

selector:

matchLabels:

name: kafka-service-1

template:

metadata:

labels:

name: kafka-service-1

app: kafka-service-1

spec:

containers:

- name: kafka-1

image: wurstmeister/kafka

imagePullPolicy: IfNotPresent

ports:

- containerPort: 9092

env:

- name: KAFKA_ADVERTISED_PORT

value: "9092"

- name: KAFKA_ADVERTISED_HOST_NAME

value: - name: KAFKA_ZOOKEEPER_CONNECT

value: zoo1:2181,zoo2:2181,zoo3:2181

- name: KAFKA_BROKER_ID

value: "1"

- name: KAFKA_CREATE_TOPICS

value: mytopic:2:1

- name: KAFKA_ADVERTISED_LISTENERS

value: PLAINTEXT://:30901

- name: KAFKA_LISTENERS

value: PLAINTEXT://0.0.0.0:9092

---

apiVersion: apps/v1

kind: Deployment

metadata:

name: kafka-deployment-2

spec:

replicas: 1

selector:

matchLabels:

name: kafka-service-2

template:

metadata:

labels:

name: kafka-service-2

app: kafka-service-2

spec:

containers:

- name: kafka-2

image: wurstmeister/kafka

imagePullPolicy: IfNotPresent

ports:

- containerPort: 9092

env:

- name: KAFKA_ADVERTISED_PORT

value: "9092"

- name: KAFKA_ADVERTISED_HOST_NAME

value: - name: KAFKA_ZOOKEEPER_CONNECT

value: zoo1:2181,zoo2:2181,zoo3:2181

- name: KAFKA_BROKER_ID

value: "2"

- name: KAFKA_ADVERTISED_LISTENERS

value: PLAINTEXT://:30902

- name: KAFKA_LISTENERS

value: PLAINTEXT://0.0.0.0:9092

---

apiVersion: apps/v1

kind: Deployment

metadata:

name: kafka-deployment-3

spec:

replicas: 1

selector:

matchLabels:

name: kafka-service-3

template:

metadata:

labels:

name: kafka-service-3

app: kafka-service-3

spec:

containers:

- name: kafka-3

image: wurstmeister/kafka

imagePullPolicy: IfNotPresent

ports:

- containerPort: 9092

env:

- name: KAFKA_ADVERTISED_PORT

value: "9092"

- name: KAFKA_ADVERTISED_HOST_NAME

value: - name: KAFKA_ZOOKEEPER_CONNECT

value: zoo1:2181,zoo2:2181,zoo3:2181

- name: KAFKA_BROKER_ID

value: "3"

- name: KAFKA_ADVERTISED_LISTENERS

value: PLAINTEXT://:30903

- name: KAFKA_LISTENERS

value: PLAINTEXT://0.0.0.0:9092

一样的部署

kubectl apply -f kafka-svc.yaml -n zookeeper

kubectl apply -f kafka-deployment.yaml -n zookeeper

测试

命令行

你可以进入任意一个pod然后使用命令行进行kafka的操作,常用的命令和目录我放下面了

kubectl exec -it kafka-deployment-1-xxxxxxxxxxx -n zookeeper /bin/bash

cd cd opt/kafka

# 查看topics

bin/kafka-topics.sh --list --zookeeper :2181

# 手动创建主题

bin/kafka-topics.sh --create --zookeeper :2181,:2181,:2181 --topic test --partitions 3 --replication-factor 1

# 写(CTRL+D结束写内容)

bin/kafka-console-producer.sh --broker-list :9092,:9092,:9092 --topic test

# 读(CTRL+C结束读内容)

bin/kafka-console-consumer.sh --bootstrap-server :9092 --topic test --from-beginning

Java测试

我也创建了一个简单的测试项目用于测试Kafka连接->https://github.com/Yunlingfly/springboot-kafka,注意修改yml里的bootstrap-servers,启动后访问http://localhost:8088/send即可发送消息并看到消费者打印了一个UUID

k8s kafka集群 连接不上_在K8s下部署Kafka集群_第1张图片

你可能感兴趣的:(k8s,kafka集群,连接不上)