记一次KafKa集群部署

本文详细介绍了Zookeeper和Kafka集群的部署步骤,包括Zookeeper的JDK环境配置、版本解压、配置文件修改,特别是server配置及myid设置。接着,讲解了Kafka的安装、配置,如broker.id、log.dirs、zookeeper.connect等关键参数的设定,以及启动和检查。在部署过程中需要注意端口开放、配置正确等问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:部署Zookeeper集群

1、需要先安装jdk环境;

2、本次部署Zookeeper 用的是3.4.14版本,将安装包上传到/usr/zk/ 文件夹,解压文件使用使用如下命令

tar -zxvf  文件名

方式进行解压缩;

3、进入目录的conf目录下复制zoo_sample.cfg文件为zoo.cfg文件;zk启动默认加载的是zoo.cfg文件

--进入conf目录
cd /usr/zk/zookeeper-3.4.14/conf  
--执行复制命令
cp zoo_sample.cfg  zoo.cfg
--或者直接
mv zoo_sample.cfg  zoo.cfg

 4、修改配置文件,重点关注以下属性

      clientPort:属性zk端口

   dataDir:数据文件夹目录

   dataLogDir:日志文件夹目录

5、修改后配置如下

tickTime=2000
initLimit=10
syncLimit=5
#数据文件夹
dataDir=/tmp/zookeeper
#日志文件
dataLogDir=/usr/local/zookeeper/logs
#设置默认端口号 默认2181
clientPort=2182
#server.A=B:C:D:其中 A 数字,表示是第几号服务器. dataDir目录下必有一个myid文件,里面只存储A的值,ZK启动时读取此文件,与下面列表比较判断是哪个server
# B 是服务器 ip ;C表示与 Leader 服务器交换信息的端口 默认2888;D 表示的是进行选举时的通信端口 默认3888。
server.1=47.119.127.**:2889:3889  
server.2=0.0.0.0:2889:3889  
server.3=139.198.***.88:2889:3889 

集群部署特别注意增加以下配置

server.1=47.119.127.**:2889:3889  
server.2=0.0.0.0:2889:3889  
server.3=139.198.***.88:2889:3889 

6、我们注意到第5小步中server.1/server.2/server.31是指在dataDir文件夹下创建的myid文件内容,2和3同理在另外两台服务器中操作。

cd /tmp/zookeeper/
echo "1" >myid

 7、注意云服务器之间端口号需要放开,策略不放开的话,互相无法访问;下面贴其他两个节点的配置

#这是sever.1的配置
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
dataLogDir=/usr/local/zookeeper/logs
clientPort=2182
server.1=0.0.0.0:2889:3889  
server.2=159.75.29.***:2889:3889  
server.3=139.198.***.**:2889:3889 

#这是sever.3的配置

# 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=/tmp/zookeeper
dataLogDir=/usr/local/zookeeper/logs
# the port at which the clients will connect
clientPort=2182
# the maximum number of client connections.
# increase this if you need to handle more clients
maxClientCnxns=100
server.1=47.119.***.***:2889:3889 
server.2=159.75.**.***:2889:3889 
server.3=0.0.0.0:2889:3889

#
# 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

8、配置好后进入bin目录,执行以下命令

cd /usr/zk/zookeeper-3.4.14/bin/

./zkServer.sh start

9、查看zk状态

./zkServer.sh status

状态信息显示如下:

ZooKeeper JMX enabled by default
Using config: /usr/zk/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: leader

Mode为leader 表示主节点

其他两台执行同样命令查看启动的信息

10、当检查启动失败了可以查看bin目录下的zookeeper.out文件,查看具体报错信息

cat zookeeper.out

11、 查看myid内容

第二步:部署Kafka集群

1、先将Kafka文件包上传到/usr/kafka/或者直接wget  url 进行文件包拉取

2、修改配置文件进入到conf目录,编辑server.properties,重点关注以下属性

broker.id=3

log.dirs=/tmp/kafka-logs

zookeeper.connect=IP1:2182,IP2:2182,IP3:2182

listeners=PLAINTEXT://0.0.0.0:9092

advertised.listeners=PLAINTEXT://139.198.178.**:9092

# 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=3

############################# 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://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://139.198.178.**: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 that the server uses for receiving requests from the network and sending responses to the network
num.network.threads=3

# The number of threads that the server uses for processing requests, which may include disk I/O
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 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=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

############################# 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 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 excessive 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 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=localhost:2182
zookeeper.connect=47.119.***.**:2182,159.75.**.***:2182,139.198.***.**:2182

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


############################# Group Coordinator Settings #############################

# The following configuration specifies the time, in milliseconds, that the GroupCoordinator will delay the initial consumer rebalance.
# The rebalance will be further delayed by the value of group.initial.rebalance.delay.ms as new members join the group, up to a maximum of max.poll.interval.ms.
# The default value for this is 3 seconds.
# We override this to 0 here as it makes for a better out-of-the-box experience for development and testing.
# However, in production environments the default value of 3 seconds is more suitable as this will help to avoid unnecessary, and potentially expensive, rebalances during application startup.
group.initial.rebalance.delay.ms=0

因为集群部署了三个kafka节点,每一台需要设定对应的broker.id为相应数字即可;其他两台配置基本是一样的。在部署期间也遇到一些问题,比如端口未开放,broker.id配置错误,listeners配置错误。

3、kafka启动后台运行
 

bin/kafka-server-start.sh -daemon config/server.properties


 

到此集群部署告一段落。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值