2、Kafka集群部署

环境准备

  1. 集群规划
hadoop-100 hadoop-101 hadoop-102
zookeeper zookeeper zookeeper
kafka kafka kafka
  1. 下载jar包
    下载地址 http://kafka.apache.org/
    2、Kafka集群部署_第1张图片
    kafka下载1.png

    如上图,点download
    2、Kafka集群部署_第2张图片
    kafka下载2.png

    kafka_2.11-2.0.1.tgz,前面的数字2.11是scala的版本,2.0.1才是kafka的版本

集群部署

  1. 解压安装包
    [hadoop@hadoop-100 software]$ tar -zxvf kafka_2.11-0.11.0.0.tgz -C /opt/module/
  2. 修改压缩后的文件名称
    [hadoop@hadoop-100 module]$ mv kafka_2.11-0.11.0.0/ kafka
  3. 在/opt/module/kafka目录下创建logs文件夹
    [hadoop@hadoop-100 kafka]$ mkdir logs
  4. 修改配置文件
    即修改config/server.properties文件
    输入以下内容
#broker的全局唯一编号,不能重复
broker.id=0
#删除topic功能使能
delete.topic.enable=true
#处理网络请求的线程数量
num.network.threads=3
#用来处理磁盘IO的现成数量
num.io.threads=8
#发送套接字的缓冲区大小
socket.send.buffer.bytes=102400
#接收套接字的缓冲区大小
socket.receive.buffer.bytes=102400
#请求套接字的缓冲区大小
socket.request.max.bytes=104857600
#kafka运行日志存放的路径 
log.dirs=/opt/module/kafka/logs
#topic在当前broker上的分区个数
num.partitions=1
#用来恢复和清理data下数据的线程数量
num.recovery.threads.per.data.dir=1
#segment文件保留的最长时间,超时将被删除
log.retention.hours=168
#配置连接Zookeeper集群地址
zookeeper.connect=hadoop-100:2181,hadoop-101:2181,hadoop-102:2181
  1. 配置环境变量
    vim /etc/profile 输入以下内容
#KAFKA_HOME
export KAFKA_HOME=/opt/module/kafka
export PATH=\$PATH:$KAFKA_HOME/bin

使生效source /etc/profile

  1. 分发安装包和配置文件并生效
    sxync kafka
    xsync /etc/profile
    source /etc/profile
  2. 修改hadoop-101和hadoop-102的server.properties文件的broker.id
    分别修改为1和2
  3. 启动集群
    [hadoop@hadoop-100 bin]$ bin/kafka-server-start.sh config/server.properties &
    [hadoop@hadoop-101 bin]$ bin/kafka-server-start.sh config/server.properties &
    [hadoop@hadoop-102 bin]$ bin/kafka-server-start.sh config/server.properties &
  4. 停止集群
    [hadoop@hadoop-100 kafka]$ bin/kafka-server-stop.sh stop
    [hadoop@hadoop-101 kafka]$ bin/kafka-server-stop.sh stop
    [hadoop@hadoop-102 kafka]$ bin/kafka-server-stop.sh stop
  5. kafka群控脚本
    参考,https://www.jianshu.com/p/44dbc7f0fbb6

Kafka命令行操作

  1. 查看当前服务器中的所有topic
    [hadoop@hadoop-100 ~]$ kafka-topics.sh --zookeeper hadoop-101:2181 --list
  2. 创建topic
    [hadoop@hadoop-100 ~]$ kafka-topics.sh --zookeeper hadoop-101:2181 --create --replication-factor 3 --partitions 1 --topic first
    --topic 定义topic名
    --replication-factor 定义副本数
    --partitions 定义分区数
    [hadoop@hadoop-100 logs]kafka-topics.sh --zookeeper hadoop-102:2181 --create --replication-factor 3 --partitions 3 --topic first1 此时进入logs目录,我们可以看到,分区和副本在目录中存在 ![kafkatopic存储位置.png](https://upload-images.jianshu.io/upload_images/13274599-b8580f2f266ce848.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) [hadoop@hadoop-100 logs] kafka-topics.sh --zookeeper hadoop-102:2181 --create --replication-factor 4 --partitions 4 --topic first3
    当前可用kafka的broker为3,当想创建replication-factor为4时,报错
    brokers不够时报错.png
  3. 删除topic
    [hadoop@hadoop-100 logs]$ kafka-topics.sh --zookeeper hadoop-101 --delete --topic first1
    需要server.properties中设置delete.topic.enable=true否则只是标记删除或者直接重启
    删除不存在的topic报错
    删除不存在的topic报错.png
  4. 发送消息
    [hadoop@hadoop-100 logs]$ kafka-console-producer.sh --broker-list hadoop-101:9092 --topic first

hello
my
name
is
zhao
jun

  1. 消费消息
    [hadoop@hadoop-102 logs]$ kafka-console-consumer.sh --zookeeper hadoop-101:2181 --topic first --from-beginning
    Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].
    hello
    my
    name
    is
    zhao
    jun
  2. 查看某个Topic的详情
    [hadoop@hadoop-101 zookeeper-3.4.10]$ kafka-topics.sh --zookeeper hadoop-101 --describe --topic first

你可能感兴趣的:(2、Kafka集群部署)