kafka集群搭建

kafka集群搭建

  • 环境准备
    • host配置
    • 安装zookeeper集群
      • 配置相应myid
    • kafka集群搭建
      • 更新kafka配置
    • 问题排查
      • 集群不生效问题(停掉某个节点后,kafka某个topic服务失效)
      • session close问题

环境准备

认证配置参见 kafka认证配置

搭建之前先做个简短说明。最近一个项目一直在使用kafka和redis,因单节点已完成且运行良好,为了进一步巩固系统稳定性,所以需要更新为kafka集群和redis集群,此处重点说下kafka集群搭建过程记录及相关问题思考
已下载kafka和zookeeper,如没有则需自行下载,本项目使用为zookeeper-3.6.1 和 kafka_2.13-3.0.0,操作系统为centos
防火墙相关配置

firewalld-cmd --state
systemctl stop firewalld.service
systemctl disable firewalld.service

注意各节点防火墙端口号是否放行,否则集群启动会失败

host配置

增加zookeeper及kafka节点域名配置:
cd /etc/hosts 文件 增加如下

192.168.0.201 casic.zookeeper1.com
192.168.0.213 casic.zookeeper2.com
192.168.0.214 casic.zookeeper3.com

192.168.0.201 casic.kafka1.com
192.168.0.213 casic.kafka2.com
192.168.0.214 casic.kafka3.com

安装zookeeper集群

  • 解压文件夹
    tar -ZXVF apache-zookeeper-3.6.1-bin.tar.gz
  • 修改zoo.cfg
    cp zoo_sample.cfg 改为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.
dataDir=/root/app/zk_data
# 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
server.1=casic.zookeeper1.com:2888:3888
server.2=casic.zookeeper2.com:2888:3888
server.3=casic.zookeeper3.com:2888:3888

2888端口号是zookeeper服务之间通信的端口
3888端口号是leader选举接口,zk节点最好为奇数,防止脑裂
配置可视化管理

admin.enableServer = true
admin.serverPort = 8888

配置相应myid

对应dataDir下创建myid文件,与 server. 后面的数字对应。如上则配置为 1 2 3

执行启动命令

./zkServer.sh start
  • tickTime:CS通信心跳数
    Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
    tickTime以毫秒为单位。
    tickTime:该参数用来定义心跳的间隔时间,zookeeper的客户端和服务端之间也有和web开发里类似的session的概念,而zookeeper里最小的session过期时间就是tickTime的两倍。
  • initLimit:LF初始通信时限
    集群中的follower服务器(F)与leader服务器(L)之间 初始连接 时能容忍的最多心跳数(tickTime的数量)
  • syncLimit:LF同步通信时限
    集群中的follower服务器(F)与leader服务器(L)之间 请求和应答 之间能容忍的最多心跳数(tickTime的数量)

kafka集群搭建

  • 解压文件夹
    tar -ZXVF kafka_2.13-3.0.0.tgz
  • 修改server.properties文件
# Licensed to the Apache Software Foundation (ASF) under one or more
# limitations under the License.
############################# Server Basics #############################
broker.id=0

############################# 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
listeners=SASL_PLAINTEXT://0.0.0.0: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
advertised.listeners=SASL_PLAINTEXT://casic.kafka1.com:9092
advertised.host.name=casic.kafka1.com
# 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
listener.security.protocol.map=SASL_PLAINTEXT:SASL_PLAINTEXT
security.inter.broker.protocol=SASL_PLAINTEXT
sasl.mechanism.inter.broker.protocol=PLAIN
sasl.enabled.mechanisms=PLAIN
allow.everyone.if.no.acl.found=true
auto.create.topics.enable=true
super.users=User:admin

# The number of threads that the server uses for receiving requests from the network and sending responses to the network
num.network.threads=9
# The number of threads that the server uses for processing requests, which may include disk I/O
num.io.threads=16
# 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 separated 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=500

# 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

############################# Internal Topic Settings  #############################
# The replication factor for the group metadata internal topics "__consumer_offsets" and "__transaction_state"
# For anything other than development testing, a value greater than 1 is recommended to ensure availability such as 3.
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1


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

# 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 unless the remaining
# segments 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=casic.zookeeper1.com:2181,casic.zookeeper2.com:2181,casic.zookeeper3.com:2181

# Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=18000
group.initial.rebalance.delay.ms=0

更新kafka配置

broker.id=0 每个节点需变更

问题排查

集群不生效问题(停掉某个节点后,kafka某个topic服务失效)

具体查看zk中相关topic中partitions中的isr节点是否分布到至少一个节点以上

session close问题

2022-01-26 10:53:43,236 [myid:0] - WARN  [NIOWorkerThread-9:NIOServerCnxn@373] - Close of session 0x0
java.io.EOFException
        at java.io.DataInputStream.readFully(DataInputStream.java:197)
        at java.io.DataInputStream.readLong(DataInputStream.java:416)
        at org.apache.jute.BinaryInputArchive.readLong(BinaryInputArchive.java:100)
        at org.apache.zookeeper.proto.ConnectRequest.deserialize(ConnectRequest.java:88)
        at org.apache.zookeeper.server.ZooKeeperServer.processConnectRequest(ZooKeeperServer.java:1323)
        at org.apache.zookeeper.server.NIOServerCnxn.readConnectRequest(NIOServerCnxn.java:419)
        at org.apache.zookeeper.server.NIOServerCnxn.readPayload(NIOServerCnxn.java:180)
        at org.apache.zookeeper.server.NIOServerCnxn.doIO(NIOServerCnxn.java:339)
        at org.apache.zookeeper.server.NIOServerCnxnFactory$IOWorkRequest.doWork(NIOServerCnxnFactory.java:522)
        at org.apache.zookeeper.server.WorkerService$ScheduledWorkRequest.run(WorkerService.java:154)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)

zookeeper 有个节点重启后一直报close of session 0x0 的错误,停掉已连接的客户端即可
可以参见 [kafka连接](http://t.zoukankan.com/qi-yuan-008-p-13912143.html)

你可能感兴趣的:(kafka,zookeeper,分布式)