使用过kafka的童鞋应该都知道,kafka的安装是比较简单的,尤其跟hadoop,storm这类相比。本文就主要介绍kafka集群的安装和配置方法。

安装环境

本人在安装kafka集群时,因为集群已经配置好了CDH的环境,在CDH环境安装kafka就变得非常简单。集群环境如下:

Name Value
Java版本 java 1.6.0_31
Linux版本 Centos 6.6
CDH版本 CDH 5.4.0
zookeeper版本 Zookeeper 3.4.5-cdh5.4.0

安装Kafka

下载相应kafka版本

这是kafka的官网Download地址,我们安装的kafka版本为2.10-0.8.1.1,就选择下载kafka-0.8.1.1-src.tgz.

安装

这个安装就是解压对应的压缩文件:

1
tar -zxvf kafka-0.8.1.1-src.tgz

配置

这里的配置主要是broker的配置,修改kafka-0.8.1.1-src/config/server.properties文件,参数意义,重要的设置参数,在下面的配置文件我们会加以说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 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.id=1 #broker的标识,id为正数,kafka集群内不能重复,推荐用ip地址设置

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

# The port the socket server listens on
port=9092 #侦听的相应端口,Producer或Consumer在此端口建立连接

# Hostname the broker will bind to. If not set, the server will bind to all interfaces
host.name=192.168.80.1 #指定broke绑定的网络接口地址

# Hostname the broker will advertise to producers and consumers. If not set, it uses the
# value for "host.name" if configured. Otherwise, it will use the value returned from
# java.net.InetAddress.getCanonicalHostName().
#advertised.host.name=<hostname routable by clients>

# The port to publish to ZooKeeper for clients to use. If this is not set,
# it will publish the same port that the broker binds to.
#advertised.port=<port accessible by clients>

# The number of threads handling network requests
num.network.threads=2 #处理网络请求的线程数

# The number of threads doing disk I/O
num.io.threads=8 #磁盘读写的线程数

# The send buffer (SO_SNDBUF) used by the socket server
socket.send.buffer.bytes=1048576 #节点端口使用的发送缓存大小

# The receive buffer (SO_RCVBUF) used by the socket server
socket.receive.buffer.bytes=1048576 #节点端口使用的接收缓存大小

# The maximum size of a request that the socket server will accept (protection against OOM)
socket.request.max.bytes=104857600 #节点端口能接收一个请求的最大大小


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

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

log.dirs=/tmp/kafka-logs,/hdfs/data1/tmp/kafka-logs #日志文件保存的目录,一台broker上可以设置多个

# 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.
num.partitions=2 #此值越大将导致各个Server上同步时需要的延迟越高

############################# 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
#log.flush.interval.messages=10000 #partition的buffer中,message达到阈值时,将flush到磁盘

# The maximum amount of time a message can sit in a log before we force a flush
#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
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.
#log.retention.bytes=1073741824 #logs目录下保存信息的最大大小

# The maximum size of a log segment file. When this size is reached a new log segment will be created.
log.segment.bytes=536870912 #保存的一个segment file的大小

# The interval at which log segments are checked to see if they can be deleted according
# to the retention policies
log.retention.check.interval.ms=60000

# By default the log cleaner is disabled and the log retention policy will default to just delete segments after their retention expires.
# If log.cleaner.enable=true is set the cleaner will be enabled and individual logs can then be marked for log compaction.
log.cleaner.enable=false

############################# 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.connect=192.168.80.1:2181,192.168.80.2:2181,192.168.80.3:2181 #连接的zookeeper对应的IP和端口

# Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=1000000

配置好以上信息之后,单个节点的kafka环境也就配置好了,同样的我们在其他节点也这样安装配置即可。

最后,再补充一点,经过我之前对kafka速度的测试,发现这上面Socket Server Settings下的几个参数对于速度的提升比较重要(brokerconfigs),根据服务器的配置情况,可以适当增大一些参数,比如我用的集群可以设置为下面这样:

  • num.network.threads:20
  • num.io.threads:8
  • socket.send.buffer.bytes:1048576
  • socket.receive.buffer.bytes:1073741824
  • socket.request.max.buytes:1073741824