kafka(一):kafka集群部署(kafka+zk模式)

3,747 阅读5分钟

kafka集群部署(kafka+zk模式)

「这是我参与11月更文挑战的第15天,活动详情查看:2021最后一次更文挑战

kafka+zk模式

1. 集群规划

如果没有物理机,也可以弄3台虚拟机。ubuntu系统不会安装的话可以自己百度哦,这里就不细说了。

作者用的是虚拟机,下面是3台电脑的配置

主机名ip地址broker.idmyid
kafka1192.168.56.10701
kafka2192.168.56.10812
kafka3192.168.56.10923

注:环境篇 一一 Virtualbox+Vagrant安装Centos7

2. 下载安装包

官网下载

image.png

wget https://dlcdn.apache.org/kafka/3.0.0/kafka_2.13-3.0.0.tgz

3. kafka集群搭建

  1. 解压安装包指定目录

tar -zxvf kafka_2.13-3.0.0.tgz -C /app

  1. 创建数据文件夹

cd kafka_2.13-3.0.0/ & mkdir /app/kafka_2.13-3.0.0/logs

  1. 修改配置文件server.properties

vi config/server.properties

#advertised_listeners` 是对外暴露的服务端口,真正建立连接用的是 `listeners`,集群要改为自己的iP
advertised.listeners=PLAINTEXT://192.168.56.107:9092
#broker 集群中的全局唯一编号,不能重复
broker.id=0
#删除 topic 功能
delete.topic.enable=true
#自动创建topic,false:生产者发送信息到已存在topic才没有报错
auto.create.topics.enable = false
#处理网络请求的线程数量
num.network.threads=3
#用来处理磁盘 IO 的现成数量
num.io.threads=8
#发送套接字的缓冲区大小
socket.send.buffer.bytes=102400
#接收套接字的缓冲区大小
socket.receive.buffer.bytes=102400
#请求套接字的缓冲区大小
socket.request.max.bytes=104857600
#修改kafka的日志目录和zookeeper数据目录,因为这两项默认放在tmp目录,而tmp目录中内容会随重启而丢失
log.dirs=/app/kafka_2.13-3.0.0/logs
#topic 在当前 broker 上的分区个数.分区数量一般与broker保持一致
num.partitions=3
#用来恢复和清理 data 下数据的线程数量
num.recovery.threads.per.data.dir=1
#segment 文件保留的最长时间,超时将被删除
log.retention.hours=168
#配置连接 Zookeeper 集群地址,新版自带Zookeeper
zookeeper.connect=192.168.56.107:2181,192.168.56.108:2181,192.168.56.109:2181

注:

  • 如果kafaka启动时加载的配置文件中server.properties没有配置delete.topic.enable=true,那么此时的删除并不是真正的删除,而是把topic标记为:marked for deletion
  • kafka如何彻底删除topic及数据
  • advertised.listeners 集群中不一样
  • broker.id 集群中不一样
  1. 配置环境变量

注:其他两台服务器相同操作,broker.id在集群中要唯一

4. 配置自带的zk

  1. 创建文件夹 快照日志的存储路径 mkdir /app/zookeeper/data -p

事物日志的存储路径 mkdir /app/zookeeper/datalog -p

  1. 修改配置文件zookeeper.properties

vi /app/kafka_2.13-3.0.0/config/zookeeper.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.
# the directory where the snapshot is stored.、
#快照日志的存储路径
dataDir=/app/zookeeper/data
# the port at which the clients will connect
#这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。修改他的端口改大点
clientPort=2181
# disable the per-ip limit on the number of connections since this is a non-production config
maxClientCnxns=0
# Disable the adminserver by default to avoid port conflicts.
# Set the port to something non-conflicting if choosing to enable this
admin.enableServer=false
# admin.serverPort=8080

####################自定义####################
#这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
tickTime=2000
#这个配置项是用来配置 Zookeeper 接受客户端(这里所说的客户端不是用户连接 Zookeeper 服务器的客户端,
#而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。当已经超 过 5个心跳的时间(也就是 tickTime)
#长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 5*2000=10 秒
initLimit=10
#这个配置项标识 Leader 与Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是5*2000=10秒
syncLimit=5

#事物日志的存储路径,如果不配置这个那么事物日志会默认存储到dataDir制定的目录,这样会严重影响zk的性能,当zk吞吐量较大的时候,产生的事物日志、快照日志太多
dataLogDir=/app/zookeeper/datalog


server.1=192.168.56.107:2888:3888
server.2=192.168.56.108:2888:3888
server.3=192.168.56.109:2888:3888
#server.1 这个1是服务器的标识也可以是其他的数字, 表示这个是第几号服务器,用来标识服务器,这个标识要写到快照目录下面myid文件里
#192.168.7.107为集群里的IP地址,第一个端口是master和slave之间的通信端口,默认是2888,第二个端口是leader选举的端口,集群刚启动的时候选举或者leader挂掉之后进行新的选举的端口默认是3888
  1. 在配置dataDir路径下创建myid文件
服务器创建命令
192.168.56.107echo "1" > /app/zookeeper/data/myid
192.168.56.108echo "2" > /app/zookeeper/data/myid
192.168.56.109echo "3" > /app/zookeeper/data/myid

注:

  • myid文件和server.myid 在快照目录下存放的标识本台服务器的文件,他是整个zk集群用来发现彼此的一个重要标识。
  • 其他两台服务器相同操作,myid文件内容在集群中要唯一

5. jdk安装并配置环境变量

注:

6. kafka环境变量配置

新建一个配置 sudo vi /etc/profile.d/kafka.sh

#KAFKA_HOME
export KAFKA_HOME=/app/kafka_2.13-3.0.0
export PATH=$PATH:$KAFKA_HOME/bin

刷新配置 source /etc/profile

7. 启动集群

  1. 先启动zookeeper
bin/zookeeper-server-start.sh -daemon config/zookeeper.properties
nohup bin/zookeeper-server-start.sh config/zookeeper.properties > log/zookeeper/zookeeper.log 2>1 &
  1. 再启动kafka
bin/kafka-server-start.sh -daemon config/server.properties
nohup bin/kafka-server-start.sh config/server.properties > zklog/kafka.log 2>1 &

依次在 kafka1、kafka2、kafka3 节点上启动

8. 关闭集群

依次在 kafka1、kafka2、kafka3 节点上关闭 kafka

bin/kafka-server-stop.sh stop