CentOS 7 安装 Zookeeper + Kafka 集群

目录

介绍

Kafka是由Apache软件基金会开发的一个开源流处理平台,由Scala和Java编写。

Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据。

安装 kafka 集群,需要把集群状态保存在 zookeeper 上,所以需要先安装 zookeeper

环境准备

主机名系统IP地址zk 端口kafka 版本kafka 端口
node01Centos7.5172.16.1.1121812.129092
node02Centos7.5172.16.1.1221812.129092
node03Centos7.5172.16.1.1321812.129092

zookeeper 集群

zookeeper 的集群安装之前文章已经介绍过, CentOS 7 Zookeeper 介绍 及 集群安装,本次也同样使用该环境;

kafka 集群安装

下载地址:

下面的操作,需要在三台机器上郡

[ ~]# cd /opt/soft/
[ soft]# tar xf kafka_2.12-2.2.1.tgz 
[ soft]# mv kafka_2.12-2.2.1 /opt/kafka-2.12
[ soft]# ln -s /opt/kafka-2.12 /opt/kafka
[ soft]# ls -ld /opt/kafka*
lrwxrwxrwx 1 root root 15 Mar  3 16:03 /opt/kafka -> /opt/kafka-2.12
drwxr-xr-x 6 root root 89 May 14  2019 /opt/kafka-2.12

服务管理脚本

cat > /etc/systemd/system/kafka.service <<EOF
[Unit]
Description=Apache Kafka server (broker)
Documentation=http://kafka.apache.org/documentation.html
Requires=network.target remote-fs.target
After=network.target remote-fs.target zookeeper.service
[Service]
Type=simple
User=kafka
Group=kafka
Environment=JAVA_HOME=/opt/jdk1.8.0_192
ExecStart=/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties
ExecStop=/opt/kafka/bin/kafka-server-stop.sh
[Install]
WantedBy=multi-user.target
EOF

配置

需要注意,三个服务配置文件 server.properties 中的 broker.idlisteners 需要根据服务器做相应的修改 不能相同,否则集群会报错,要注意修改。

cd /opt/kafka/config/

cp server.properties server.properties.default

cat > server.properties <<EOF
broker.id=1
listeners=PLAINTEXT://172.16.1.11:9092
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/opt/kafka-2.12/data
num.partitions=30
num.recovery.threads.per.data.dir=2
offsets.topic.replication.factor=3
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.flush.interval.messages=10000
log.flush.interval.ms=1000
log.retention.hours=144
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=172.16.1.11:2181,172.16.1.12:2181,172.16.1.13:2181
zookeeper.connection.timeout.ms=6000
group.initial.rebalance.delay.ms=0
EOF

启动测试

注意:修改好 配置文件server.properties 中的 broker.idlisteners 不同之后,相继启动三个服务;

systemctl enable kafka
systemctl start kafka
systemctl status kafka

● kafka.service - Apache Kafka server (broker)
   Loaded: loaded (/etc/systemd/system/kafka.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-02-20 21:29:28 CST; 1 weeks 4 days ago
     Docs: http://kafka.apache.org/documentation.html
  Process: 30768 ExecStop=/opt/kafka-2.12/bin/kafka-server-stop.sh (code=exited, status=0/SUCCESS)
 Main PID: 30784 (java)
   CGroup: /system.slice/kafka.service
           └─30784 /opt/jdk1.8.0_192/bin/java -Xmx2G -Xms2G -server -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:+ExplicitGCInvokesConcurrent -Djava.awt...