OpenEuler22.04配置zookeeper+kafka三节点集群
- 1. 基础环境
- 2. 部署zookeeper集群
- 2.1 下载、解压
- 2.2 配置
- 2.3 配置zookeeper服务
- 3. 配置kafka集群
- 3.1 下载解压
- 3.2 配置
- 3.3 配置服务
- 3.4 kafka常用命令
- 4. 其它
感觉OpenEuler的操作方式跟centos区别不大,下面的操作在centos下也可行
1. 基础环境
三主机节点IP:
192.168.8.10
192.168.8.11
192.168.8.12
配置一些所需的基础软件
yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel wget curl lrzsz bash-completion
2. 部署zookeeper集群
下载:apache-zookeeper-3.8.4-bin.tar.gz
下载链接地址:https://zookeeper.apache.org/releases.html
2.1 下载、解压
cd /tmp/
wget https://www.apache.org/dyn/closer.lua/zookeeper/zookeeper-3.8.4/apache-zookeeper-3.8.4-bin.tar.gz
tar -xvf apache-zookeeper-3.8.4-bin.tar.gz
mv apache-zookeeper-3.8.4-bin /usr/local/
cd /usr/local/
ln -sv apache-zookeeper-3.8.4-bin zookeeper
cd /usr/local/zookeeper/conf/
2.2 配置
简单配置
mkdir -pv /usr/local/zookeeper/data
mkdir -pv /usr/local/zookeeper/logs
# 复制配置文件
cp zoo_sample.cfg zoo.cfg
vim zoo.cfg
配置文件内容如下:
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper/data
dataLogDir=/usr/local/zookeeper/logs
clientPort=2181
server.1=192.168.8.10:2888:3888
server.2=192.168.8.11:2888:3888
server.3=192.168.8.12:2888:3888
分别在3台主机上修改myid
/usr/local/zookeeper/data/myid#server1 192.168.8.10 执行
echo "1" > /usr/local/zookeeper/data/myid
#server2 192.168.8.11 执行
echo "2" > /usr/local/zookeeper/data/myid
#server3 192.168.8.12 执行
echo "3" > /usr/local/zookeeper/data/myid
2.3 配置zookeeper服务
使用systemd管理服务
vim /usr/lib/systemd/system/zookeeper.service
服务配置文件如下
[Unit]
Description=zookeeper server daemon
After=zookeeper.target[Service]
Type=forking
ExecStart=/usr/local/zookeeper/bin/zkServer.sh start
ExecReload=/usr/local/zookeeper/bin/zkServer.sh stop && sleep 2 && /usr/local/zookeeper/bin/zkServer.sh start
ExecStop=/usr/local/zookeeper/bin/zkServer.sh stop
Restart=always[Install]WantedBy=multi-user.target
配置完成后启动服务
systemctl start zookeeper
查看启动状态
systemctl status zookeeper
/usr/local/zookeeper/bin/zkServer.sh status
设置开机启动
systemctl enable zookeeper
3. 配置kafka集群
3.1 下载解压
下载:kafka_2.12-3.8.1.tgz
下载页面:https://kafka.apache.org/downloads.html
cd /tmp/
wget https://dlcdn.apache.org/kafka/3.8.1/kafka_2.12-3.8.1.tgz
tar -xvf kafka_2.12-3.8.1.tgz
mv kafka_2.12-3.8.1 /usr/local/
cd /usr/local/
ln -sv kafka_2.12-3.8.1 kafka
mkdir -pv /data/kafka/data/logs
3.2 配置
简单配置
cd /usr/local/kafka/config/
vim server.properties
第一台:192.168.8.10配置如下:
host.name=192.168.8.10
listeners=PLAINTEXT://192.168.8.10:9092
advertised.listeners=PLAINTEXT://192.168.8.10:9092log.dirs=/data/kafka/data/logs
zookeeper.connect=192.168.8.10:2181,192.168.8.11:2181,192.168.8.12:2181log.retention.hours=48
log.cleaner.enable=true
第2台:192.168.8.11配置如下:
host.name=192.168.8.11
listeners=PLAINTEXT://192.168.8.11:9092
advertised.listeners=PLAINTEXT://192.168.8.11:9092log.dirs=/data/kafka/data/logs
zookeeper.connect=192.168.8.10:2181,192.168.8.11:2181,192.168.8.12:2181
第3台:192.168.8.12配置如下:
host.name=192.168.8.12
listeners=PLAINTEXT://192.168.8.12:9092
advertised.listeners=PLAINTEXT://192.168.8.12:9092log.dirs=/data/kafka/data/logs
zookeeper.connect=192.168.8.10:2181,192.168.8.11:2181,192.168.8.12:2181
3.3 配置服务
使用systemd管理服务
vim /usr/lib/systemd/system/kafka.service
服务配置文件如下
[Unit]
Description=kafka server daemon
After=kafka.target[Service]Type=forking
ExecStart=/usr/local/kafka/bin/kafka-server-start.sh -daemon /usr/local/kafka/config/server.properties
ExecReload=/usr/local/kafka/bin/kafka-server-stop.sh && sleep 2 && /usr/local/kafka/bin/kafka-server-start.sh -daemon /usr/local/kafka/config/server.properties
ExecStop=/usr/local/kafka/bin/kafka-server-stop.sh
Restart=always[Install]
WantedBy=multi-user.target
配置完成后启动服务
systemctl start kafka
查看启动状态
systemctl status kafka
设置开机启动
systemctl enable kafka
3.4 kafka常用命令
创建一个topic,test
bin/kafka-topics.sh --create --bootstrap-server 192.168.8.12:9092 --replication-factor 2 --partitions 3 --topic test
查看一个topic情况
./bin/kafka-topics.sh --describe --bootstrap-server 192.168.8.12:9092 --topic test
修改一个topic
bin/kafka-topics.sh --alter --bootstrap-server 192.168.8.12:9092 --topic test --partitions 8
创建topic成功,查看topic列表
bin/kafka-topics.sh --list --bootstrap-server 192.168.8.12:9092
生产者producer测试发送消息
bin/kafka-console-producer.sh --broker-list 192.168.8.12:9092 --topic test
用另一个shell窗口,消费者consumer测试接收消息
bin/kafka-console-consumer.sh --bootstrap-server 192.168.8.12:9092 --topic test --from-beginning
能够正常收发信息,说明安装成功了
创建项目用的topic
bin/kafka-topics.sh --create --bootstrap-server 192.168.8.12:9092 --replication-factor 1 --partitions 1 --topic test-project
bin/kafka-topics.sh --create --bootstrap-server 192.168.8.12:9092 --replication-factor 1 --partitions 10 --topic test-project
查看topic消息消费情况
bin/kafka-consumer-groups.sh --bootstrap-server 192.168.8.12:9092 --list
查看topic消息组消费情况
bin/kafka-consumer-groups.sh --describe --bootstrap-server 192.168.8.12:9092 --group test-consumer
4. 其它
注意: 上面的配置都是不带用户认证的,只适合本机或局域网访问。如果考虑到安全性,主要添加主机防火墙和用户认证。