写在前面
- kafka依赖zookeeper,请先安装zookeeper:centos7 安装zookeeper集群
- kafka集群规划:3个节点,端口分别是9092、9093、9094
1. 下载并解压安装包
kafka官方地址:http://kafka.apache.org/downloads
下载安装包到任意文件夹,本文下载文件夹是:/home/soft
# 下载安装包,没有wget指令,可以执行yum install -y wget安装
wget http://apache.01link.hk/kafka/2.2.0/kafka_2.12-2.2.0.tgz
# 解压到当前目录
tar -zxvf kafka_2.12-2.2.0.tgz
2. 安装目录结构
本文的安装根目录是/home/app/kafka
# 创建目录结构
mkdir -p kafka/909{2,3,4}/logs
# 复制kafka安装程序文件到各个节点目录下
cp -r kafka_2.12-2.2.0 /home/app/kafka/9092/
cp -r kafka_2.12-2.2.0 /home/app/kafka/9093/
cp -r kafka_2.12-2.2.0 /home/app/kafka/9094/
最终目录结构如下:
# 查目录结构
[root@localhost app]# tree -L 3
└── kafka
├── 9092
│ ├── kafka_2.12-2.2.0
│ └── logs
├── 9093
│ ├── kafka_2.12-2.2.0
│ └── logs
└── 9094
├── kafka_2.12-2.2.0
└── logs
3. server.properties配置文件
修改每个节点下kafka_2.12-2.2.0/config/server.properties配置文件
以下只列出需要修改的配置项,其他保持默认即可:
- 节点1:
[root@localhost kafka]# vim 9092/kafka_2.12-2.2.0/config/server.properties
broker.id=1
listeners=PLAINTEXT://10.30.2.222:9092
advertised.listeners=PLAINTEXT://10.30.2.222:9092
log.dirs=/home/app/kafka/9092/logs
zookeeper.connect=10.30.2.222:2181,10.30.2.222:2182,10.30.2.222:2183
- 节点2:
[root@localhost kafka]# vim 9093/kafka_2.12-2.2.0/config/server.properties
broker.id=2
listeners=PLAINTEXT://10.30.2.222:9093
advertised.listeners=PLAINTEXT://10.30.2.222:9093
log.dirs=/home/app/kafka/9093/logs
zookeeper.connect=10.30.2.222:2181,10.30.2.222:2182,10.30.2.222:2183
- 节点3:
[root@localhost kafka]# vim 9094/kafka_2.12-2.2.0/config/server.properties
broker.id=2
listeners=PLAINTEXT://10.30.2.222:9094
advertised.listeners=PLAINTEXT://10.30.2.222:9094
log.dirs=/home/app/kafka/9094/logs
zookeeper.connect=10.30.2.222:2181,10.30.2.222:2182,10.30.2.222:2183
4. consumer.properties配置文件
每个节点的kafka_2.12-2.2.0/config/server.properties配置文件,只需修改:
bootstrap.servers=10.30.2.222:9092,10.30.2.222:9093,10.30.2.222:9094
5. 配置防火墙
firewall-cmd --zone=public --add-port=9092/tcp --permanent
firewall-cmd --zone=public --add-port=9093/tcp --permanent
firewall-cmd --zone=public --add-port=9094/tcp --permanent
firewall-cmd --reload
6. 启动集群
# 节点1
cd /home/app/kafka/9092/kafka_2.12-2.2.0/bin/
./kafka-server-start.sh -daemon ../config/server.properties
# 节点2
cd /home/app/kafka/9093/kafka_2.12-2.2.0/bin/
./kafka-server-start.sh -daemon ../config/server.properties
# 节点3
cd /home/app/kafka/9094/kafka_2.12-2.2.0/bin/
./kafka-server-start.sh -daemon ../config/server.properties
检查是否启动成功,可以使用jps命令,因为kafka是用java启动的。
可以看到3个节点都启动了:
[root@localhost bin]# jps | grep Kafka
8404 Kafka
8759 Kafka
9402 Kafka
7. 测试
创建一个test主题(topic)
[root@localhost bin]# ./kafka-topics.sh --create --zookeeper 10.30.2.222:2181,10.30.2.222:2182,10.30.2.222:2183 --replication-factor 3 --partitions 3 --topic test
Created topic test.
连接生产者,并发送数据
[root@localhost bin]# ./kafka-console-producer.sh --broker-list 10.30.2.222:9092,10.30.2.222:9093,10.30.2.222:9094 --topic test
>1
>2222
>333
>
新开一个ssh会话,连接消费者:
[root@localhost bin]# ./kafka-console-consumer.sh --bootstrap-server 10.30.2.222:9092 --topic test --from-beginning
1
2222
333
测试成功,kafka集群搭建大功告成!
8. kafka-manager安装
下载解压kafka-manager
wget https://github.com/yahoo/kafka-manager/archive/2.0.0.2.tar.gz
tar -zxvf 2.0.0.2.tar.gz
安装sbt工具,因为kafka-manager需要用sbt进行编译
curl https://bintray.com/sbt/rpm/rpm > bintray-sbt-rpm.repo
mv bintray-sbt-rpm.repo /etc/yum.repos.d/
yum install -y sbt
sbt -version
编译
cd kafka-manager-2.0.0.2
./sbt clean dist
编译时间会比较长,因为需要下载很多依赖包,慢慢等吧。。。最后成功的话可以看到:
[success] All package validations passed
[info] Your package is ready in /home/soft/kafka-manager-2.0.0.2/target/universal/kafka-manager-2.0.0.2.zip
[success] Total time: 869 s, completed May 5, 2019 2:53:45 PM
编译完成后,会生成./target/universal/kafka-manager-2.0.0.2.zip文件,解压之:
unzip kafka-manager-2.0.0.2.zip
mv kafka-manager-2.0.0.2 /home/app/kafka/
配置
[root@localhost kafka-manager-2.0.0.2]# cd /home/app/kafka/kafka-manager-2.0.0.2/
[root@localhost kafka-manager-2.0.0.2]# vim conf/application.conf
# kafka-manager.zkhosts="kafka-manager-zookeeper:2181"
# 填写自己的kafka使用的zookeeper集群地址
kafka-manager.zkhosts="10.30.2.222:2181,10.30.2.222:2182,10.30.2.222:2183"
启动kafka-manager
nohup bin/kafka-manager -Dconfig.file=conf/application.conf -Dhttp.port=9090 &
firewall-cmd --zone=public --add-port=9090/tcp --permanent
firewall-cmd --reload
浏览器打开:http://10.30.2.222:9090/,添加cluster即可: