一 整体步骤
- hosts配置
- 安装配置Zookeeper
- Kafka集群搭建
二 配置Hosts
修改hosts文件
sudo vi /etc/hosts # 配置如下 127.0.0.1 localhost 192.168.0.100 master 192.168.0.101 slave1 192.168.0.102 slave2
三 安装配置Zookeeper
下载解压
# 使用wget或本地下载后传到服务器 cd /home/ sudo mkdir zookeeper cd /home/zookeeper sudo wget https://dlcdn.apache.org/zookeeper/zookeeper-3.5.9/apache-zookeeper-3.5.9-bin.tar.gz sudo tar -zxvf apache-zookeeper-3.5.9-bin.tar.gz
配置环境变量
# 修改三台主机 # sudo vi /etc/profile # 添加环境变量 export ZOOKEEPER_HOME=/home/zookeeper/apache-zookeeper-3.5.9-bin export PATH=$ZOOKEEPER_HOME/bin:$PATH source /etc/profile
创建目录
cd apache-zookeeper-3.5.9-bin sudo mkdir data sudo mkdir logs
修改配置
cd conf sudo cp zoo_sample.cfg zoo.cfg sudo vi zoo.cfg tickTime=2000 initLimit=10 syncLimit=5 dataDir=/home/zookeeper/apache-zookeeper-3.5.9-bin/data dataLogDir=/home/zookeeper/apache-zookeeper-3.5.9-bin/logs clientPort=2181 # 如果为当前主机,则修改为0.0.0.0 server.1=master:2287:3387 server.2=slave1:2287:3387 server.3=slave2:2287:3387
创建
myid
文件在各服务器节点(
master
、slave1
、slave2
)的dataDir
目录下创建名为myid
的文件,在文件第一行写上对应的 Server idmaster: echo "1" > myid slave1: echo "2" > myid slave2: echo "3" > myid
启动测试
启动顺序决定leadert,follower
# 分别在三台主机上 cd /home/zookeeper/apache-zookeeper-3.5.9-bin/bin/ zkServer.sh start # 集群验证 # 查看状态(需全部节点启动) zkServer.sh status
基本命令
# 停止 zkServer.sh stop
四 KafKa 集群搭建
下载解压
注意选择版本时,选择
Binary downloads
,不要选择Source download
!!!否则启动时会提示Could not find or load main class kafka.Kafka
,此时需要编译后才可以使用 。cd /home mkdir Kafka cd Kafka mkdir data sudo wget https://archive.apache.org/dist/kafka/3.0.0/kafka_2.12-3.0.0.tgz tar -xzf kafka_2.12-3.0.0.tgz
修改配置
cd kafka_2.12-3.0.0/config vi server.properties # 节点1 # 群中每个节点的唯一标识 broker.id=0 # 监听地址 listeners=PLAINTEXT://master:9092 # 数据的存储位置 log.dirs=/home/Kafka/data # Zookeeper连接地址 zookeeper.connect=master:2181,slave1:2181,slave2:2181 # 节点2 broker.id=1 listeners=PLAINTEXT://slave1:9092 log.dirs=/home/Kafka/data zookeeper.connect=master:2181,slave1:2181,slave2:2181 # 节点3 broker.id=2 listeners=PLAINTEXT://slave2:9092 log.dirs=/home/Kafka/data zookeeper.connect=master:2181,slave1:2181,slave2:2181
启动
# 三节点分别运行启动 cd /home/Kafka/kafka_2.12-3.0.0 bin/kafka-server-start.sh -daemon config/server.properties
测试
bin/kafka-topics.sh --create --bootstrap-server master:9092 --replication-factor 1 --partitions 1 --topic test bin/kafka-topics.sh --list --bootstrap-server master:9092 bin/kafka-topics.sh --describe --bootstrap-server master:9092 --topic test