前言

最近因为工作原因,需要安装一个 kafka 集群,目前网络上有很多相关的教程,按着步骤来也能完成安装,只是这些教程都显得略微繁琐。因此,我写了这篇文章帮助大家快速完成 kafka 集群安装。
 

安装步骤

准备多台服务器,数量建议为奇数(如:3,5,7 等),操作系统为 CentOS 7+。
这里使用 3 台服务器作为例子,IP 分别为 192.168.1.1、192.168.1.2、192.168.1.3,修改下述脚本文件的 IP 地址,并拷贝到 3 台服务器上分别执行即可完成安装。

#!/bin/bash

# Modify the link if you want to download other version
KAFKA_DOWNLOAD_URL="https://dlcdn.apache.org/kafka/3.1.0/kafka_2.13-3.1.0.tgz" # Please use your own server ip
SERVERS=("192.168.1.1" "192.168.1.2" "192.168.1.3") ID=0 MECHINE_IP=$(hostname -i)
echo "Mechine IP: "${MECHINE_IP} LENGTH=${#SERVERS[@]} for (( i=0; i<${LENGTH}; i++ ));
do
if [ "${SERVERS[$i]}" = "${MECHINE_IP}" ]; then
ID=$((i+1))
fi
done echo "ID: "${ID} if [ "${ID}" -eq "0" ]; then
echo "Mechine IP is not matched to server list"
exit 1
fi ZOOKEEPER_CONNECT=$(printf ",%s:2181" "${SERVERS[@]}")
ZOOKEEPER_CONNECT=${ZOOKEEPER_CONNECT:1}
echo "Zookeeper Connect: "${ZOOKEEPER_CONNECT} echo "---------- Update yum ----------"
yum update -y
yum install -y wget echo "---------- Install java ----------"
yum -y install java-1.8.0-openjdk
java -version echo "---------- Create kafka user & group ----------"
groupadd -r kafka
useradd -g kafka -r kafka -s /bin/false echo "---------- Download kafka ----------"
cd /opt
wget ${KAFKA_DOWNLOAD_URL} -O kafka.tgz
mkdir -p kafka
tar -xzf kafka.tgz -C kafka --strip-components=1
chown -R kafka:kafka /opt/kafka echo "---------- Install and start zookeeper ----------"
mkdir -p /data/zookeeper
chown -R kafka:kafka /data/zookeeper
echo "${ID}" > /data/zookeeper/myid # zookeeper config
# https://zookeeper.apache.org/doc/r3.1.2/zookeeperAdmin.html#sc_configuration
cat <<EOF > /opt/kafka/config/zookeeper-cluster.properties
# the directory where the snapshot is stored.
dataDir=/data/zookeeper # the port at which the clients will connect
clientPort=2181 # setting number of connections to unlimited
maxClientCnxns=0 # keeps a heartbeat of zookeeper in milliseconds
tickTime=2000 # time for initial synchronization
initLimit=10 # how many ticks can pass before timeout
syncLimit=5 # define servers ip and internal ports to zookeeper
EOF for (( i=0; i<${LENGTH}; i++ ));
do
INDEX=$((i+1))
echo "server.${INDEX}=${SERVERS[$i]}:2888:3888" >> /opt/kafka/config/zookeeper-cluster.properties
done # zookeeper.service
cat <<EOF > /usr/lib/systemd/system/zookeeper.service
[Unit]
Description=Apache Zookeeper server (Kafka)
Documentation=http://zookeeper.apache.org
Requires=network.target remote-fs.target
After=network.target remote-fs.target [Service]
Type=simple
User=kafka
Group=kafka
ExecStart=/opt/kafka/bin/zookeeper-server-start.sh /opt/kafka/config/zookeeper-cluster.properties
ExecStop=/opt/kafka/bin/zookeeper-server-stop.sh
Restart=on-failure [Install]
WantedBy=multi-user.target
EOF systemctl daemon-reload
systemctl start zookeeper && systemctl enable zookeeper echo "---------- Install and start kafka ----------"
mkdir -p /data/kafka
chown -R kafka:kafka /data/kafka # kafka config
# https://kafka.apache.org/documentation/#configuration
cat <<EOF > /opt/kafka/config/server-cluster.properties
# The id of the broker. This must be set to a unique integer for each broker.
broker.id=${ID} # Hostname and port the broker will advertise to producers and consumers. If not set,
# it uses the value for "listeners" if configured. Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
advertised.listeners=PLAINTEXT://${MECHINE_IP}:9092 # A comma separated list of directories under which to store log files
log.dirs=/data/kafka # The default number of log partitions per topic. More partitions allow greater
# parallelism for consumption, but this will also result in more files across
# the brokers.
num.partitions=1 # The replication factor for the group metadata internal topics "__consumer_offsets" and "__transaction_state"
# For anything other than development testing, a value greater than 1 is recommended to ensure availability such as 3.
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1 # The minimum age of a log file to be eligible for deletion due to age
log.retention.hours=168 # The maximum size of a log segment file. When this size is reached a new log segment will be created.
log.segment.bytes=1073741824 # The interval at which log segments are checked to see if they can be deleted according
# to the retention policies
log.retention.check.interval.ms=300000 # Zookeeper connection string (see zookeeper docs for details).
# This is a comma separated host:port pairs, each corresponding to a zk
# server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
# You can also append an optional chroot string to the urls to specify the
# root directory for all kafka znodes.
zookeeper.connect=${ZOOKEEPER_CONNECT}/kafka # Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=60000
EOF # kafka.service
cat <<EOF > /usr/lib/systemd/system/kafka.service
[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 kafka-zookeeper.service [Service]
Type=simple
User=kafka
Group=kafka
ExecStart=/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server-cluster.properties
ExecStop=/opt/kafka/bin/kafka-server-stop.sh
Restart=on-failure [Install]
WantedBy=multi-user.target
EOF systemctl daemon-reload
systemctl start kafka && systemctl enable kafka

setup.sh

基本操作

# 启动 zookeeper
systemctl start zookeeper # 停止 zookeeper
systemctl stop zookeeper # 重启 zookeeper
systemctl restart zookeeper # 查看 zookeeper 日志
systemctl status zookeeper -l # 启动 kafka
systemctl start kafka # 停止 kafka
systemctl stop kafka # 重启 kafka
systemctl restart kafka # 查看 kafka 日志
systemctl status kafka -l

简单测试

# 进入 kafka bin 目录
cd /opt/kafka/bin/ # 创建一个 topic
kafka-topics.sh --create --topic test --partitions 3 --replication-factor 1 --bootstrap-server localhost:9092 # 查看 topic 描述
kafka-topics.sh --topic test --describe --bootstrap-server localhost:9092 # 启动生产者然后输入消息
kafka-console-producer.sh --topic test --bootstrap-server localhost:9092 # 启动消费者消费消息
kafka-console-consumer.sh --topic test --from-beginning --bootstrap-server localhost:9092 # 删除 topic
kafka-topics.sh --topic test --delete --bootstrap-server localhost:9092

脚本说明

1. 以下代码主要指定下载 kafka 的版本以及服务器 IP 列表,可根据实际情况进行调整。

# Modify the link if you want to download other version
KAFKA_DOWNLOAD_URL="https://dlcdn.apache.org/kafka/3.1.0/kafka_2.13-3.1.0.tgz" # Please use your own server ip
SERVERS=("192.168.1.1" "192.168.1.2" "192.168.1.3")

2. 以下代码主要用于生成 zookeeper id 和 kafka broker id 以及拼接 kafka 配置中的 zookeeper 连接串,通过本机 IP 与填写的 IP 列表进行匹配,如果本机 IP 等于第一个服务器 IP,则 ID为 1,等于第二个服务器 IP,则 ID为 2,等于第二个服务器 IP,则 ID为 3,以此类推;本机 IP 不在填写的 IP 列表中,则会退出安装。

ID=0

MECHINE_IP=$(hostname -i)
echo "Mechine IP: "${MECHINE_IP} LENGTH=${#SERVERS[@]} for (( i=0; i<${LENGTH}; i++ ));
do
if [ "${SERVERS[$i]}" = "${MECHINE_IP}" ]; then
ID=$((i+1))
fi
done echo "ID: "${ID} if [ "${ID}" -eq "0" ]; then
echo "Mechine IP is not matched to server list"
exit 1
fi ZOOKEEPER_CONNECT=$(printf ",%s:2181" "${SERVERS[@]}")
ZOOKEEPER_CONNECT=${ZOOKEEPER_CONNECT:1}

  

3. 更新 yum 源,并安装 wget 下载工具

yum update -y
yum install -y wget

  

4. 安装 java 8

yum -y install java-1.8.0-openjdk
java -version

  

5. 创建 kafka 用户及组

groupadd -r kafka
useradd -g kafka -r kafka -s /bin/false

  

6. 下载并解压 kafka 可执行程序

cd /opt
wget ${KAFKA_DOWNLOAD_URL} -O kafka.tgz
mkdir -p kafka
tar -xzf kafka.tgz -C kafka --strip-components=1
chown -R kafka:kafka /opt/kafka

  

7. 创建 zookeeper 目录,创建 zookeeper id

mkdir -p /data/zookeeper
chown -R kafka:kafka /data/zookeeper
echo "${ID}" > /data/zookeeper/myid

  

8. 生成 zookeeper 配置文件,详细说明可参考:https://zookeeper.apache.org/doc/r3.1.2/zookeeperAdmin.html#sc_configuration

cat <<EOF > /opt/kafka/config/zookeeper-cluster.properties
# the directory where the snapshot is stored.
dataDir=/data/zookeeper # the port at which the clients will connect
clientPort=2181 # setting number of connections to unlimited
maxClientCnxns=0 # keeps a heartbeat of zookeeper in milliseconds
tickTime=2000 # time for initial synchronization
initLimit=10 # how many ticks can pass before timeout
syncLimit=5 # define servers ip and internal ports to zookeeper
EOF for (( i=0; i<${LENGTH}; i++ ));
do
INDEX=$((i+1))
echo "server.${INDEX}=${SERVERS[$i]}:2888:3888" >> /opt/kafka/config/zookeeper-cluster.properties
done

  

9. 创建 zookeeper systemd 管理文件,启动并设置开机启动 zookeeper

cat <<EOF > /usr/lib/systemd/system/zookeeper.service
[Unit]
Description=Apache Zookeeper server (Kafka)
Documentation=http://zookeeper.apache.org
Requires=network.target remote-fs.target
After=network.target remote-fs.target [Service]
Type=simple
User=kafka
Group=kafka
ExecStart=/opt/kafka/bin/zookeeper-server-start.sh /opt/kafka/config/zookeeper-cluster.properties
ExecStop=/opt/kafka/bin/zookeeper-server-stop.sh
Restart=on-failure [Install]
WantedBy=multi-user.target
EOF systemctl daemon-reload
systemctl start zookeeper && systemctl enable zookeeper

  

10. 创建 kafka 目录

mkdir -p /data/kafka
chown -R kafka:kafka /data/kafka

  

11. 生成 kafka 配置文件,详细说明可参考:https://kafka.apache.org/documentation/#configuration

cat <<EOF > /opt/kafka/config/server-cluster.properties
# The id of the broker. This must be set to a unique integer for each broker.
broker.id=${ID} # Hostname and port the broker will advertise to producers and consumers. If not set,
# it uses the value for "listeners" if configured. Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
advertised.listeners=PLAINTEXT://${MECHINE_IP}:9092 # A comma separated list of directories under which to store log files
log.dirs=/data/kafka # The default number of log partitions per topic. More partitions allow greater
# parallelism for consumption, but this will also result in more files across
# the brokers.
num.partitions=1 # The replication factor for the group metadata internal topics "__consumer_offsets" and "__transaction_state"
# For anything other than development testing, a value greater than 1 is recommended to ensure availability such as 3.
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1 # The minimum age of a log file to be eligible for deletion due to age
log.retention.hours=168 # The maximum size of a log segment file. When this size is reached a new log segment will be created.
log.segment.bytes=1073741824 # The interval at which log segments are checked to see if they can be deleted according
# to the retention policies
log.retention.check.interval.ms=300000 # Zookeeper connection string (see zookeeper docs for details).
# This is a comma separated host:port pairs, each corresponding to a zk
# server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
# You can also append an optional chroot string to the urls to specify the
# root directory for all kafka znodes.
zookeeper.connect=${ZOOKEEPER_CONNECT}/kafka # Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=60000
EOF

  

12. 创建 kafka systemd 管理文件,启动并设置开机启动 kafka

cat <<EOF > /usr/lib/systemd/system/kafka.service
[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 kafka-zookeeper.service [Service]
Type=simple
User=kafka
Group=kafka
ExecStart=/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server-cluster.properties
ExecStop=/opt/kafka/bin/kafka-server-stop.sh
Restart=on-failure [Install]
WantedBy=multi-user.target
EOF systemctl daemon-reload
systemctl start kafka && systemctl enable kafka

  

总结

按照上述的操作,你将快速完成 kafka 集群安装,如有问题可以在文章留言。

快速安装 kafka 集群的更多相关文章

  1. Centos7.5安装kafka集群

    Tags: kafka Centos7.5安装kafka集群 Centos7.5安装kafka集群 主机环境 软件环境 主机规划 主机安装前准备 安装jdk1.8 安装zookeeper 安装kafk ...

  2. Centos安装Kafka集群

    kafka是LinkedIn开发并开源的一个分布式MQ系统,现在是Apache的一个孵化项目.在它的主页描述kafka为一个高吞吐量的分布式(能 将消息分散到不同的节点上)MQ.在这片博文中,作者简单 ...

  3. CentOS7 安装kafka集群

    1. 环境准备 JDK1.8 ZooKeeper集群(参见本人博文) Scala2.12(如果需要做scala开发的话,安装方法参见本人博文) 本次安装的kafka和zookeeper集群在同一套物理 ...

  4. RedHat6.5安装kafka集群

    版本号: Redhat6.5    JDK1.8     zookeeper-3.4.6   kafka_2.11-0.8.2.1 1.软件环境 1.3台RedHat机器,master.slave1. ...

  5. helm安装kafka集群并测试其高可用性

    介绍 Kafka是由Apache软件基金会开发的一个开源流处理平台,由Scala和Java编写.Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者在网站中的所有动作流数据. 这种动作( ...

  6. 用 Docker 快速搭建 Kafka 集群

    开源Linux 一个执着于技术的公众号 版本 •JDK 14•Zookeeper•Kafka 安装 Zookeeper 和 Kafka Kafka 依赖 Zookeeper,所以我们需要在安装 Kaf ...

  7. 安装kafka 集群 步骤

    1.下载 http://mirror.bit.edu.cn/apache/kafka/2.1.0/kafka_2.11-2.1.0.tgz 2.解压  tar -zxvf kafka_2.11-2.1 ...

  8. 安装kafka集群

    1解压tar包 tar -zxvf kafka_2.-.tgz 2.进入config目录 3.配置server.properties文件 # Licensed to the Apache Softwa ...

  9. ubuntu 16.04快速安装ceph集群

    准备工作 假设集群: 选一台作管理机 注意: ceph集群引用hostname,而非ip. 172.17.4.16 test16 #hostname必须是test16 172.17.4.17 test ...

随机推荐

  1. (第一章第四部分)TensorFlow框架之张量

    系列博客链接: (一)TensorFlow框架介绍:https://www.cnblogs.com/kongweisi/p/11038395.html (二)TensorFlow框架之图与Tensor ...

  2. js数组用法

    去面试的时候问到我一个问题,你能说出来多少种数组的方法跟用法,我当时只说出来十一个,回来以后才想起来还有很多种,现在整理一下,没有排名,想起那个写那个 1:forEach 从头遍历数组,没有返回值,有 ...

  3. 【C语言】关于单片机中断详解

    什么是中断?就是打断当前要做的事,转而去执行别的事情.比如小七我现在正在电脑前写帖子,突然老妈叫我帮她下楼拿点东西,于是我就收到了老妈给我的一个中断(可以叫做外部中断),当我去拿东西时,突然尿急(内部 ...

  4. 8、msyql性能分析工具

    性能分析工具 1服务器优化的步骤 2查询系统参数 在MySQL中,可以使用 SHOW STATUS 语句查询一些MySQL数据库服务器的性能参数.执行频率 . SHOW STATUS语句语法如下: S ...

  5. 如何用Google Drive下载超大型文件

    本文将对「如何下载Google Drive中的超大型文件?」这一问题展开探索和解决. 太长不读:直接看这里 情景与问题 在AI.系统安全等研究领域,一项研究成果的产生需要大量的数据样本进行训练和分析, ...

  6. 1.2 简单的STL案例

    文章目录 1.容器算法迭代器分离案例 2.专栏回顾:第一章到第二章2.2 1.容器算法迭代器分离案例 案例:统计某个元素在数组里出现的次数 简易版(帮助理解,无STL) #include<ios ...

  7. 使用SymPy

    最近工作的原因,需要进行一些积分运算,通过一些搜索得知了SymPy,记录一下使用历程. 1. SymPy介绍 SymPy是关于Symbolic Mathematics的Python库,它旨在成为一个功 ...

  8. Linux下mysql的彻底卸载

    1.查看mysql的安装情况 rpm -qa | grep -i mysql 2.删除上图安装的软件 rpm -ev mysql-community-libs-5.7.27-1.el6.x86_64 ...

  9. BLHeli/ BLHeli_S开源无刷电调学习记录

    BLHeli的历史轨迹:BLHeli -> BLHeli_S -> BLHeli_32,我们重点学习BLHeli_S版本. 该代码支持常规的1-2ms脉冲宽度输入,以及Oneshot125 ...

  10. mmap代替通用IO读取文件数据(curious)

    提供一份测试demo: #include <stdio.h> #include <string.h> #include <stdlib.h> #include &l ...