前言
文章主要介绍以docker容器的方式部署kafka集群。
环境说明
- 三台虚拟机:centos7.2 docker1.12.6(192.168.180.42,192.168.180.45,192.168.180.46)
- zookeeper镜像:zookeeper:3.4.9
- kafka镜像:wurstmeister/kafka:0.10.2.0
- kafka-manager镜像:kafka-manager:alpine
zookeeper部署
- zookeeper配置文件(zoo.cfg)
# 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.
# zookeeper镜像中设定会将zookeeper的data与dataLog分别映射到/data, /datalog
# 本质上,这个配置文件是为zookeeper的容器所用,容器中路径的配置与容器所在的宿主机上的路径是有区别的,要区分清楚。
dataDir=/data
dataLogDir=/datalog
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.1=192.168.180.42:2888:3888
server.2=192.168.180.45:2888:3888
server.3=192.168.180.46:2888:3888
上述配置文件中的server.x,数字x对应到data/myid文件中的值。三台机器x的值分别就是1,2,3。参数详细说明请参考官网文档。
-
关键目录存储结构
1./kafka_cluster/zookeeper/conf
2./kafka_cluster/zookeeper/data
3./kafka_cluster/zookeeper/datalog
启动zookeeper容器(三台机器均执行)
docker run -tid --name=zookeeper --restart=always --net=host -v /kafka_cluster/zookeeper/conf:/conf -v /kafka_cluster/zookeeper/data:/data -v /kafka_cluster/zookeeper/datalog:/datalog zookeeper:3.4.9
1.--net=host: 容器与主机共享同一Network Namespace,即容器与网络看到的是相同的网络视图(host模式存在一定的风险,对安全要求很高的生产环境最好不要用host模式,应考虑除此之外的其他几种模式)
2.-v: 指定主机到容器的目录映射关系
这样就以容器的方式启动了zookeeper的服务,可以通过 "docker exec -it zookeeper bash" 命令进入容器中进行一些操作,例如查看服务启动是否正常。也可以通过查看2181端口是否被监听判断zookeeper的服务是否运行
kafka部署
- kafka配置文件(server.properties)
# The id of the broker. This must be set to a unique integer for each broker.
broker.id=0
# Switch to enable topic deletion or not, default value is 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
#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(最好设置为cpu核数+1)
num.network.threads=3
# The number of threads doing disk I/O(最好设置为cpu核数的2倍值)
num.io.threads=8
# The send buffer (SO_SNDBUF) used by the socket server
socket.send.buffer.bytes=102400
# The receive buffer (SO_RCVBUF) used by the socket server
socket.receive.buffer.bytes=102400
# 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
# 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=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
#log.flush.interval.messages=10000
# 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 due to age
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.
#log.retention.bytes=1073741824
# The maximum size of a log segment file. When this size is reached a new log segment will be created.
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.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.connect=localhost:2181
# Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=6000
详细的参数配置说明请参考官方文档,参数不仅可以通过上述文件的方式来配置,也可以通过容器环境变量的方式来配置,这里结合两种方式使用。
-
关键目录存储结构
1./kafka_cluster/kafka/config
2./kafka_cluster/kafka/data
3./kafka_cluster/kafka/logs
启动kafka容器(三台机器均执行)
docker run -itd --name=kafka --restart=always --net=host -v /etc/hosts:/etc/hosts -v /kafka_cluster/kafka/data:/kafka -v /kafka_cluster/kafka/config:/opt/kafka/config -v /kafka_cluster/kafka/logs:/opt/kafka/logs -e KAFKA_ADVERTISED_HOST_NAME=192.168.180.42 -e JMX_PORT=9999 -e HOST_IP=127.0.0.1 -e KAFKA_ADVERTISED_PORT=9092 -e KAFKA_ZOOKEEPER_CONNECT=127.0.0.1:2181 -e KAFKA_BROKER_ID=1 -e KAFKA_HEAP_OPTS="-Xmx6G -Xms6G" wurstmeister/kafka:0.10.2.0
1.KAFKA_ADVERTISED_HOST_NAME、KAFKA_BROKER_ID的值要结合每台机器自身设置
2./etc/hosts文件中最好配置ip与hostname的映射关系,否则会报出如下错误"Error: Exception thrown by the agent : java.net.MalformedURLException: Local host name unknown: java.net.UnknownHostException: node0: node0: System error
"
3.通过-e 指定的环境变量与在server.properties中配置的选项其效果是一样的
4.配置文件中的选项若要通过环境变量来指定,方式为:如broker.id对应KAFKA_BROKER_ID,类似的log.dirs对应KAFKA_LOG_DIRS
5.KAFKA_HEAP_OPTS="-Xmx6G -Xms6G"指java堆内存大小的设置,6G大小是kafka官网给出的数值,此数值要结合机器的内存大小给出。超过6G的内存,可以设置为6G;若机器的内存低于6G而设置6G,则会报错。
5.启动成功后,可以通过"docker logs kafka"命令查看日志
部署监控工具kafka-manager
- 启动kafka-manager容器
docker run -itd --restart=always --name=kafka-manager -p 9000:9000 -e ZK_HOSTS="192.168.180.42:2181" sheepkiller/kafka-manager:alpine
1.ZK_HOSTS:ZooKeeper访问地址(需指定机器的ip,localhost:2181或127.0.0.1:2181均会报 "java.net.ConnectException: Connection refused" 异常)
-
访问192.168.180.42:9000地址
通过这个web界面,可以创建kafka集群与topic,操作topic的partition,监控topic的行为等。