kafka安装和配置

以0.9.0.1版本kafka为例,介绍kafka的安装、配置步骤,以及如何开启jmx等。

下载

从官网http://kafka.apache.org/downloads选择合适版本下载,解压即可。
为了以后升级方便,可以创建一个软链,ln -s kafka-0.9.0.1 kafka,如以后升级到0.10.1.1,只需改变软链指向即可。使用软链另一个好处是

kafka配置

修改conf/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
listeners=PLAINTEXT://:9092
auto.create.topics.enable=true
auto.leader.rebalance.enable=true
controlled.shutdown.enable=true
delete.topic.enable=true
kafka.http.metrics.host=0.0.0.0
kafka.http.metrics.port=24042
log.segment.bytes=106954752
max.connections.per.ip=10
message.max.bytes=5000000
min.insync.replicas=1
replica.fetch.max.bytes=10000000
replica.lag.max.messages=4000
unclean.leader.election.enable=false
zookeeper.session.timeout.ms=6000
log.retention.bytes=-1
port=9092
# 不主动创建topic时,默认使用的备份因子,partition数量,过期时间
default.replication.factor=2
num.partitions=6
log.retention.hours=168
log.roll.hours=168
# kafka数据目录,不是kafka自身日志目录
log.dirs=/data/kafka/data
# kafka连接的zk地址,各个broker配置一致
zookeeper.connect=192.168.10.1:2181,192.168.10.2:2181,192.168.10.3:2181
# 各个broker配置不一致的地方
advertised.host.name=192.168.11.1
broker.id=0

kafka自身日志目录默认是${kafka.dir}/logs,可以在bin/kafa-server-start.sh中export LOG_DIR修改,也可以直接修改bin/kafka-run-class.sh的以下部分。

1
2
3
4
# Log directory to use
if [ "x$LOG_DIR" = "x" ]; then
LOG_DIR="$base_dir/logs"
fi

jvm配置

配置xmx,xms,直接修改bin/kafa-server-start.sh

1
2
3
if [ "x$KAFKA_HEAP_OPTS" = "x" ]; then
export KAFKA_HEAP_OPTS="-Xmx4G -Xms4G"
fi

需要修改更多jvm参数,如gc等,修改bin/kafka-run-class.sh

jmx配置

可以在bin/kafa-server-start.sh中export KAFKA_JMX_OPTSKAFKA_JMX_OPTS,也可以直接修改bin/kafka-run-class.sh

1
2
3
4
5
6
7
8
9
10
11
# JMX settings
if [ -z "$KAFKA_JMX_OPTS" ]; then
KAFKA_JMX_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false "
fi
# JMX port to use
JMX_PORT=9393
JMX_RMI_PORT=8033
if [ $JMX_PORT ]; then
KAFKA_JMX_OPTS="$KAFKA_JMX_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT -Dcom.sun.management.jmxremote.rmi.port=$JMX_RMI_PORT "
fi

由于jmx会在jmxremote.port之外,另外随机使用一个端口作为jmxremote.rmi.port,当机器打开iptables而此端口不在iptables放行端口中时,就会连接不了jmx。
所以注意jmxremote.portjmxremote.rmi.port都需要配置,并且在iptables中放行。

参考Stack Overflow: How to activate JMX on my JVM for access with jconsole?