Kafka集群部署

Posted 技术实践分享

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kafka集群部署相关的知识,希望对你有一定的参考价值。

KAFKA集群部署



1 软硬件环境

Linux环境:centos6.7-64x

Kafka版本:kafka_2.12-0.10.2.0

Kafka节点:kafka01,kafka02,kafka03

Zookeeper节点:zookeeper01,zookeeper02,zookeeper03


2 安装

(1)Zookeeper安装

Zookeeper安装并启动(此处忽略安装步骤)

(2)Kakfa安装

分别上传Kafka安装包kafka_2.12-0.10.2.0.tgz至kafka01,kafka02,kafka03三台机器的/opt目录下,解压并重命名。

注:在三台机器上做同样操作

[root@kafka01 opt]# tar -zxvf kafka_2.12-0.10.2.0.tgz

··· ···

[root@kafka01 opt]# mv kafka_2.12-0.10.2.0 kafka

[root@kafka01 opt]# rm kafka_2.12-0.10.2.0.tgz


3 配置环境变量

注:在三台机器上做同样操作

[root@kafka01 opt]# vim /etc/profile

export JAVA_HOME=/opt/jdk

export JRE_HOME=/opt/jdk/jre

export HADOOP_HOME=/opt/hadoop

export ZOOKEEPER_HOME=/opt/zookeeper

export KAFKA_HOME=/opt/kafka

export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$ZOOKEEPER_HOME/bin:$KAFKA_HOME/bin

export

CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$HADOOP_HOME/lib:$ZOOKEEPER_HOME/lib:$KAFKA_HOME/lib


4 参数配置

注:修改kafka Server配置文档,在kafka01,kafka02,kafka03上做同样操作

[root@kafka01 ~]# cd /opt/kafka/config

[root@kafka01 config]# vi server.properties

# Licensed to the Apache Software Foundation (ASF) under one or more

# contributor license agreements.  See the NOTICE file distributed with

# this work for additional information regarding copyright ownership.

# The ASF licenses this file to You under the Apache License, Version 2.0

# (the "License"); you may not use this file except in compliance with

# the License.  You may obtain a copy of the License at

#

#    http://www.apache.org/licenses/LICENSE-2.0

#

# Unless required by applicable law or agreed to in writing, software

# distributed under the License is distributed on an "AS IS" BASIS,

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

# See the License for the specific language governing permissions and

# limitations under the License.


# see kafka.server.KafkaConfig for additional details and defaults


############################# Server Basics #############################


# The id of the broker. This must be set to a unique integer for each broker.

#broker在kafka集群中的唯一标识,必须为唯一的整数:kafka01标识设置为0,kafka02标识设置为1,kafka03标识设置为2

broker.id=0


# Switch to enable topic deletion or not, default value is false

#是否启用删除Topic的操作,默认为false

#delete.topic.enable=true


########################### Socket Server Settings ############################


# The address the socket server listens on. It will get the value returned from

# java.net.InetAddress.getCanonicalHostName() if not configured.

#   FORMAT:

#     listeners = listener_name://host_name:port

#   EXAMPLE:

#     listeners = PLAINTEXT://your.host.name:9092

#broker的监听IP/主机名:端口

#listeners=PLAINTEXT://:9092


# Hostname and port the broker will advertise to producers and consumers. If not set,

# it uses the value for "listeners" if configured.  Otherwise, it will use the value

# returned from java.net.InetAddress.getCanonicalHostName().

#advertised.listeners=PLAINTEXT://your.host.name:9092


# Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details

#listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL


# The number of threads handling network requests

#用于接收并处理网络请求的线程数,默认为3。其内部实现是采用Selector模型。启动一个线程作为Acceptor来负责建立连接,再配合启动num.network.threads个线程来轮流负责从Sockets里读取请求,一般无需改动,除非上下游并发请求量过大。

num.network.threads=3


# The number of threads doing disk I/O

# 处理磁盘I/O的线程数高峰期可能有些io等待,因此配置需要大些配置线程数量为cpu核数2倍,最大不超过3

num.io.threads=8


# The send buffer (SO_SNDBUF) used by the socket server

# socket的发送缓冲区,socket的调优参数SO_SNDBUFF

socket.send.buffer.bytes=102400


# The receive buffer (SO_RCVBUF) used by the socket server

# Socket接收缓冲区 ,socket的调优参数SO_RCVBUFF

socket.receive.buffer.bytes=102400


# The maximum size of a request that the socket server will accept (protection against OOM)

# socket请求的最大字节数。为了防止内存溢出,message.max.bytes必然要小于socket.request.max.bytes,会被topic创建时的指定参数覆盖socket.request.max.bytes=104857600


############################# Log Basics #############################


# A comma seperated list of directories under which to store log files

#用于存储日志文件,多个用逗号分隔

log.dirs=/tmp/kafka-logs


# The default number of log partitions per topic. More partitions allow greater

# parallelism for consumption, but this will also result in more files across

# the brokers.

#kafka分区默认数量

num.partitions=1


# The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.

# This value is recommended to be increased for installations with data dirs located in RAID array.

#每个数据目录的线程数,用于启动时的日志恢复和关闭时的刷新。

num.recovery.threads.per.data.dir=1


############################# Log Flush Policy #############################


# Messages are immediately written to the filesystem but by default we only fsync() to sync

# the OS cache lazily. The following configurations control the flush of data to disk.

# There are a few important trade-offs here:

#    1. Durability: Unflushed data may be lost if you are not using replication.

#    2. Latency: Very large flush intervals may lead to latency spikes when the flush does occur as there will be a lot of data to flush.

#    3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to exceessive seeks.

# The settings below allow one to configure the flush policy to flush data after a period of time or

# every N messages (or both). This can be done globally and overridden on a per-topic basis.


# The number of messages to accept before forcing a flush of data to disk

# 在强制将数据刷新到磁盘之前要接受的消息数,如下:每当producer写入10000条消息时,刷新数据到磁盘

#log.flush.interval.messages=10000


# The maximum amount of time a message can sit in a log before we force a flush

# 消息在强制刷新到磁盘之前可以日志中存留的最长时间。如下:每隔1秒,刷数据到磁盘

#log.flush.interval.ms=1000


########################### Log Retention Policy ###########################


# The following configurations control the disposal of log segments. The policy can

# be set to delete segments after a period of time, or after a given size has accumulated.

# A segment will be deleted whenever *either* of these criteria are met. Deletion always happens

# from the end of the log.


# The minimum age of a log file to be eligible for deletion due to age

# 当kafka server的被写入海量消息后,会生成很多数据文件,且占用大量磁盘空间,如果不及时清理,可能磁盘空间不够用,kafka默认是保留7天。建议保留三天,也可以根据磁盘是否充裕设置。

log.retention.hours=168


# A size-based retention policy for logs. Segments are pruned from the log as long as the remaining

# segments don't drop below log.retention.bytes. Functions independently of log.retention.hours.

# 控制日志数据存储的最大字节数,如果超过这个界限,会根据pplicy处理数据

#log.retention.bytes=1073741824


# The maximum size of a log segment file. When this size is reached a new log segment will be created.

# 控制日志segment文件的大小,超出该大小则追加到一个新的日志segment文件中(-1表示没有限制)段文件配置1GB,有利于快速回收磁盘空间,重启kafka加载也会加快;如果文件过小,则文件数量比较多,kafka启动时是单线程扫描目录(log.dir)下所有数据文件

log.segment.bytes=1073741824


# The interval at which log segments are checked to see if they can be deleted according

# to the retention policies

# 日志片段文件的检查周期,查看它们是否达到了删除策略的设置(log.retention.hours或log.retention.bytes)

log.retention.check.interval.ms=300000


############################# Zookeeper #############################


# Zookeeper connection string (see zookeeper docs for details).

# This is a comma separated host:port pairs, each corresponding to a zk

# server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".

# You can also append an optional chroot string to the urls to specify the

# root directory for all kafka znodes.

#指定zookeeper集群,必须和broker使用同样的zookeeper配置

zookeeper.connect=zookeeper01:2181,zookeeper02:2181zookeeper03:2181


# Timeout in ms for connecting to zookeeper

# 客户端等待和zookeeper建立连接的最大时间,默认为6000ms,如果设置太小,容易超时,设置过大,影响效率。

zookeeper.connection.timeout.ms=6000


5 测试

注:Kafka 的运行依赖 Zookeeper,启动 Kafka 前要保证 Zookeeper 服务已经启动

(1)启动kafka

注:在kafka01,kafka02,kafka03上做同样操作

[root@kafka01 ~]# cd /opt/kafka/bin

[root@kafka01 bin]# ./kafka-server-start.sh ../config/server.properties &

(2)创建Topic

[root@kafka01 ~]# cd /opt/kafka/bin

[root@kafka01 bin]# ./kafka-topics.sh --zookeeper zookeeper01:2181,zookeeper02:2181,zookeeper03:2181 --create --topic test --partitions 3 --replication-factor 1

(3)查询所有Topic

[root@kafka01 ~]# cd /opt/kafka/bin

[root@kafka01 bin]# ./kafka-topics.sh --zookeeper zookeeper01:2181,zookeeper02:2181,zookeeper03:2181 --list

(4)查询Topic详情

[root@kafka01 ~]# cd /opt/kafka/bin

[root@kafka01 bin]# ./kafka-topics.sh --zookeeper zookeeper01:2181,zookeeper02:2181,zookeeper03:2181 --describe --topic test

(5)测试发布

[root@kafka01 ~]# cd /opt/kafka/bin

[root@kafka01 bin]# ./kafka-console-producer.sh --broker-list kafka01:9092,kafka02:9092,kafka03:9092 --topic test

测试发布

(6)测试消费

[root@kafka02 ~]# cd /opt/kafka/bin

[root@kafka02 bin]# ./kafka-console-consumer.sh --zookeeper zookeeper01:2181,zookeeper02:2181,zookeeper03:2181 --topic test

测试发布

以上是关于Kafka集群部署的主要内容,如果未能解决你的问题,请参考以下文章

Kafka快速入门——Kafka集群部署

Kafka(四)集群之kafka

kafka集群的部署及kafka监控工具

Kafka-集群部署

kafka集群部署步骤

zookeeper集群+kafka集群 部署