1:运行kafka需要依赖jdk环境所以我们这里先安装jdk环境,我这里采用apt方式安装,这样安装完不需要手动配置jdk环境变量比较便捷
apt-get update #更新源
apt-get install openjdk-8-jdk #安装jdk环境
java -version #测试jdk是否安装成功
2:添加host ip映射到/etc/hosts,同时填写你的本机ip 可不做此步骤
vi /etc/hosts #将你的ip地址填进去 kafka 192.168.X.X
3:下载zookeeper并安装
zookeeper下载地址
tar -zxvf apache-zookeeper-3.6.1-bin.tar.gz #解压文件
4:进入zookeeper中conf下复制zoo_sample.cfg 配置文件并修改配置,创建myid
cd /apache-zookeeper-3.6.1-bin/conf #进入zookeeper配置目录
cp zoo_sample.cfg zoo.cfg #复制zoo_sample.cfg到zoo.cfg
3个配置文件分别如下所示
第一台
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/root/apache-zookeeper-3.6.1-bin/data1
dataLogDir=/root/apache-zookeeper-3.6.1-bin/log1
# the port at which the clients will connect
clientPort=2181
server.1=172.16.30.25:2888:3888
server.2=172.16.30.45:2888:3888
server.3=172.16.30.65:2888:3888
完成后在/root/apache-zookeeper-3.6.1-bin/ 目录下创建data1、log1,然后在data1中创建文件myid文件且myid内容分别输入对应上面配置 sercer. 的的数字即可第一台机器所以myid文件中输入1,用来标识当前主机
第二台
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
# the port at which the clients will connect
dataDir=/root/apache-zookeeper-3.6.1-bin/data2
dataLogDir=/root/apache-zookeeper-3.6.1-bin/log2
# the port at which the clients will connect
clientPort=2181
server.1=172.16.30.25:2888:3888
server.2=172.16.30.45:2888:3888
server.3=172.16.30.65:2888:3888
完成后在/root/apache-zookeeper-3.6.1-bin/ 目录下创建data2、log2,然后在data1中创建文件myid文件且myid内容分别输入对应上面配置 sercer. 的的数字即可第一台机器所以myid文件中输入2,用来标识当前主机
第三台
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/root/apache-zookeeper-3.6.1-bin/data3
dataLogDir=/root/apache-zookeeper-3.6.1-bin/log3
# the port at which the clients will connect
clientPort=2181
server.1=172.16.30.25:2888:3888
server.2=172.16.30.45:2888:3888
server.3=172.16.30.65:2888:3888
完成后在/root/apache-zookeeper-3.6.1-bin/ 目录下创建data3、log3,然后在data1中创建文件myid文件且myid内容分别输入对应上面配置 sercer. 的的数字即可第一台机器所以myid文件中输入3,用来标识当前主机
5:下载安装kafka
kafka下载地址
wget http://mirrors.shuosc.org/apache/kafka/1.0.0/kafka_2.12-2.6.0.tgz #或者下载kafka
tar -zxvf kafka_2.12-2.6.0.tgz #解压文件
6:分别进入3台机器kafka_2.12-2.6.0/config/配置文件目录复制配置文件并修改,将zookeeper节点和kafka节点关联起来行成伪集群
cd kafka_2.12-2.6.0/config #进入kafka配置文件目录
cp server.properties server1.properties #第一台
cp server.properties server2.properties #第二台
cp server.properties server3.properties #第三台
server1.properties #以下是server1.properties 里面的配置信息修改
broker.id=1
port=9092
listeners=PLAINTEXT://172.16.30.25:9092
zookeeper.connect=172.16.30.25:2181,172.16.30.45:2181,172.16.30.65:2181
server2.properties #以下是server2.properties 里面的配置信息修改
broker.id=2
port=9092
listeners=PLAINTEXT://172.16.30.45:9092
zookeeper.connect=172.16.30.25:2181,172.16.30.45:2181,172.16.30.65:2181
server3.properties #以下是server3.properties 里面的配置信息修改
broker.id=3
port=9092
listeners=PLAINTEXT://172.16.30.65:9092
zookeeper.connect=172.16.30.25:2181,172.16.30.45:2181,172.16.30.65:2181
保存退出即可
7:启动zookeeper和kafka
cd /apache-zookeeper-3.6.1-bin/bin #分别进入3台机器zookeeper的bin目录执行
./zkServer.sh start ../conf/zoo.cfg #执行启动命令
./zkServer.sh status ../conf/zoo.cfg #查看集群状态
cd ../../kafka_2.12-2.6.0/bin #进入到kafka的bin目录
#3台机器分别执行
./kafka-server-start.sh ../config/server1.properties & #第一台
./kafka-server-start.sh ../config/server2.properties & #第二台
./kafka-server-start.sh ../config/server3.properties & #第三台
zookeeper和kafka运行成功分别如图
8:检查kafka是否启动成功
netstat -an | grep 9092 #9092 9093 9094三个端口均处在listening状态,如果某个端口未关闭,则需要重新启动控制端口为该端口的kafka进程
jps |grep -i kafka #或者用java的jps查看kafka