由于Kafka运行需要zookeeper配合,zookeeper需要运行在JVM上,所以需要安装JDK,zookeeper。Kafka 从2.0.0版本开始就不再支持 JDK7 及以下版本,就以 CentOS 7 64位 JDK8 为例
1、下载ZooKeeper
wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar.gz
2、解压到/usr/local/
mkdir -p /usr/local/zookeeper3.4
tar -zxf zookeeper-3.4.12.tar.gz -C /usr/local/zookeeper3.4 --strip-components=1 #--strip-components选项表示从目录级别上去除指定的前缀,以实现更加控制解压的效果
3、配置环境变量
vi /etc/profile
export ZOOKEEPER_HOME=/usr/local/zookeeper3.4/
export PATH=$ZOOKEEPER_HOME/bin:$PATH
source /etc/profile
4、修改zookeeper3.4的配置文件
cd /usr/local/zookeeper3.4/conf/
cp zoo_sample.cfg zoo.cfg
vi zoo.cfg
tickTime=2000 #ZooKeeper服务器心跳时间,单位为ms
initLimit=10 #允许follower连接并同步到leader的初始化连接时间,以tickTime的倍数来表示
syncLimit=5 #leader与follower心跳检测最大容忍时间,响应超过syncLimit*tickTime,leader认为follower"死掉",从服务器列表中删除follower
dataDir=/data/zookeeper/data #数据目录
dataLogDir=/data/zookeeper/data/log #日志目录
clientPort=2181 #ZooKeeper对外服务端口
5、创建目录zookeeper3.4及存放服务器编号
mkdir -p /data/zookeeper/data/log
cd /data/zookeeper/data
touch myid
echo 0 > myid
6、启动、查看zookeeper3.4
cd /usr/local/zookeeper3.4/bin/
zkServer.sh start #启动zookeeper
zkServer.sh status #查看zookeeper状态
zkServer.sh stop #停止zookeeper
zkServer.sh restart #重启zookeeper
7、设置开机启动
vi /lib/systemd/system/zookeeper.service
[Unit]
Description=Zookeeper service
After=network.target
[Service]
Type=simple
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/jdk/1.8.0_391/bin"
User=root
Group=root
ExecStart=/usr/local/kafka2.12/bin/zookeeper-server-start.sh /usr/local/kafka2.12/config/zookeeper.properties
ExecStop=/usr/local/kafka2.12/bin/zookeeper-server-stop.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl enable zookeeper.service #加入开机启动
systemctl start zookeeper.service #启动
systemctl stop zookeeper.service #停止
systemctl status zookeeper.service #状态
8、开放端口
firewall-cmd --zone=public --add-port=2181/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-ports
9、测试zookeeper配置开机启动是否成功
netstat -lntup
jps
centos7安装Kafka单节点环境部署之ZooKeeper安装与配置已经结束,快去试试吧