kafka安装

1. 准备工作

1.1 安装包

#zookeeper必须是bin包

wget https://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.6.1/apache-zookeeper-3.6.1-bin.tar.gz

wget http://archive.apache.org/dist/kafka/2.5.0/kafka_2.12-2.5.0.tgz

1.2 准备至少3台主机(centos系统)

主机名
IP地址
硬件配置
kafka-1 192.168.3.40 4CPU、8G内存、100G存储
kafka-2 192.168.3.41 4CPU、8G内存、100G存储
kafka-3 192.168.3.42 4CPU、8G内存、100G存储

2. 安装JAVA环境

scp root@192.168.3.23:/home/work/software/jdk-8u251-linux-x64.tar.gz /home/work/software/
tar -zxvf apache-zookeeper-3.6.1-bin.tar.gz
mv jdk1.8.0_251/ /opt/java1.8
vi /etc/profile
export JAVA_HOME=/opt/java1.8
export JRE_HOME=$JAVA_HOME/jre
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

保存
source /etc/profile

3. 搭建zookeeper集群

tar -zxvf apache-zookeeper-3.6.1.tar.gz
mv apache-zookeeper-3.6.1-bin /home/work/zookeeper

vim /etc/profile

export ZK_HOME=/home/work/zookeeper
export PATH=$ZK_HOME/bin:$PATH

保存
source /etc/profile

打开zookeeper配置文件

cp /home/work/zookeeper/conf/zoo_sample.cfg /home/work/zookeeper/conf/zoo.cfg
mkdir -p /data/zookeeper

修改zookeeper配置文件

vim /home/work/zookeeper/conf/zoo.cfg

#修改数据文件夹路径
dataDir=/data/zookeeper
#在文件末尾添加

server.1=192.168.3.40:2888:3888
server.2=192.168.3.41:2888:3888
server.3=192.168.3.42:2888:3888

#其它不变

#修改日志目录

mkdir -p /data/zookeeper/log
vi /home/work/zookeeper/conf/log4j.properties
zookeeper.log.dir=/data/zookeeper/log

创建myid文件,在myid文件中添加本机的 server ID,在本例中对应关系如下

主机名
IP地址
zookeeper
myid
kafka-1 192.168.3.40 server.1 1
kafka-2 192.168.3.41 server.2 2
kafka-3 192.168.3.42 server.3 3

所以,在kafka-1中执行下面命令

echo "1" > /data/zookeeper/myid  #kafka-1主机myid

在kafka-2中执行下面命令

echo "2" > /data/zookeeper/myid #kafka-2主机myid

在kafka-3中执行下面命令

echo "3" > /data/zookeeper/myid  #kafka-3主机myid

在每台电脑上启动zookeeper

 
cat << EOF > /etc/systemd/system/zookeeper.service
[Unit]
Description=zookeeper.service
After=network.target
[Service]
Type=forking
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/java1.8/bin"
ExecStart=/home/work/zookeeper/bin/zkServer.sh start
ExecStop=/home/work/zookeeper/bin/zkServer.sh stop
ExecReload=/home/work/zookeeper/bin/zkServer.sh restart
[Install]
WantedBy=multi-user.target
EOF
#设置java环境
vi /home/work/zookeeper/bin/zkEnv.sh
JAVA_HOME=/opt/java1.8
 
systemctl daemon-reload
systemctl start zookeeper
systemctl enable zookeeper

#查看zk状态
bin/zkServer.sh status

4. 搭建kafka集群

解压文件

tar -zxvf kafka_2.12-2.5.0.tgz 

重命名文件夹为zookeeper

mv kafka_2.12-2.5.0 /home/work/kafka

配置kafka环境变量,首先打开profile文件

vim /etc/profile

按i进入编辑模式,在文件末尾添加kafka环境变量

#set kafka environment

export KAFKA_HOME=/home/work/kafka
export PATH=${KAFKA_HOME}/bin:$PATH

保存文件后,让该环境变量生效

source /etc/profile
mkdir -p /data/kafka/logs

在kafka-1主机中修改server.properties配置文件

打开配置文件

vim /home/work/kafka/config/server.properties

修改配置如下(IP地址应该根据实际情况填写)

broker.id=1
log.dirs=/data/kafka/logs
listeners=PLAINTEXT://192.168.3.40:9092
zookeeper.connect=192.168.3.40:2181,192.168.3.41:2181,192.168.3.42:2181


在kafka-2主机中修改server.properties配置文件

打开配置文件

vim /home/work/kafka/config/server.properties

修改配置如下(IP地址应该根据实际情况填写)

broker.id=2
log.dirs=/data/kafka/logs
listeners=PLAINTEXT://192.168.3.41:9092
zookeeper.connect=192.168.3.40:2181,192.168.3.41:2181,192.168.3.42:2181


在kafka-3主机中修改server.properties配置文件

打开配置文件

vim /home/work/kafka/config/server.properties

修改配置如下(IP地址应该根据实际情况填写)

broker.id=3
log.dirs=/data/kafka/logs
listeners=PLAINTEXT://192.168.3.42:9092
zookeeper.connect=192.168.3.40:2181,192.168.3.41:2181,192.168.3.42:2181


启动kafka(要确保zookeeper已启动)

在每台主机上分别启动kafka

cat <<EOF >/etc/systemd/system/kafka.service
[Unit]
Description=Apache Kafka server (broker)
After=network.target  zookeeper.service
 
[Service]
Type=simple
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/java1.8/bin"
User=root
Group=root
ExecStart=/home/work/kafka/bin/kafka-server-start.sh /home/work/kafka/config/server.properties
ExecStop=/home/work/kafka/bin/kafka-server-stop.sh
Restart=on-failure
 
[Install]
WantedBy=multi-user.target
EOF
systemctl start kafka
systemctl enable kafka

 每台机器bin/kafka-run-class.sh里面第一行添加JMX_PORT=9988

在其中一台虚拟机(192.168.3.40)创建topic

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

查看创建的topic信息

/home/work/kafka/bin/kafka-topics.sh --describe --zookeeper 192.168.3.40:2181 --topic test-topic
posted @ 2020-06-24 11:50  暗痛  阅读(239)  评论(0编辑  收藏  举报