飞道的博客

CentOS7安装zookeeper+kafka

366人阅读  评论(0)

一、环境介绍

操作系统:centos7
Kafka版本:2.4.1
JDK版本:jdk1.8.0_231
Zookeeper版本:3.5.8
三台服务器:192.168.3.100,192.168.3.101,192.168.3.102

在kafka每个版本的文档查看与zookeeper的版本对应关系,比如kafka2.4.1版本http://kafka.apache.org/24/documentation.html

二、安装zookeeper

       从目前的最新版本3.5.5开始,带有bin名称的包才是我们想要的下载可以直接使用的里面有编译后的二进制的包,而之前的普通的tar.gz的包里面是只是源码的包无法直接使用

1、开放端口

[root@localhost jvm]# firewall-cmd --add-port=2181/tcp --permanent
success
[root@localhost jvm]# firewall-cmd --add-port=2888/tcp --permanent
success
[root@localhost jvm]# firewall-cmd --add-port=3888/tcp --permanent
success
[root@localhost jvm]# firewall-cmd --reload
success
[root@localhost jvm]# firewall-cmd --query-port=2181/tcp
yes
[root@localhost jvm]# firewall-cmd --query-port=2888/tcp
yes
[root@localhost jvm]# firewall-cmd --query-port=3888/tcp
yes

2、下载安装文件

https://zookeeper.apache.org/releases.html


放到/usr/mysoft目录,解压
tar -zxf apache-zookeeper-3.5.8.tar.gz

3、配置zookeeper

分别在三台服务器上,进入zookeeper所在目录

[root@localhost home]# cd apache-zookeeper-3.5.6/
[root@localhost apache-zookeeper-3.5.6]# ls
bin        conf                       ivysettings.xml  LICENSE.txt  owaspSuppressions.xml  README.md             zookeeper-assembly  zookeeper-contrib  zookeeper-it    zookeeper-recipes
build.xml  excludeFindBugsFilter.xml  ivy.xml          NOTICE.txt   pom.xml                README_packaging.txt  zookeeper-client    zookeeper-docs     zookeeper-jute  zookeeper-server
[root@localhost apache-zookeeper-3.5.6]# cd conf
[root@localhost conf]# ls
configuration.xsl  log4j.properties  zoo_sample.cfg
[root@localhost conf]# cp  zoo_sample.cfg  zoo.cfg
[root@localhost conf]# ls
configuration.xsl  log4j.properties  zoo.cfg  zoo_sample.cfg
[root@localhost conf]# vi zoo.cfg

修改zoo.cfg文件,增加如下内容
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/Data/zookeeper
clientPort=2181
server.1=192.168.3.100:2888:3888
server.2=192.168.3.101:2888:3888
server.3=192.168.3.102:2888:3888

注意:要注释掉之前的dataDir。配置项不能重复定义,key(加粗部分的数字)必须唯一,否则启动有问题。

配置参数说明:
       tickTime:客户端与服务器或者服务器与服务器之间每个tickTime时间就会发送一次心跳。通过心跳不仅能够用来监听机器的工作状态,还可以通过心跳来控制Flower跟Leader的通信时间,默认2秒
       initLimit:集群中的follower服务器(F)与leader服务器(L)之间初始连接时能容忍的最多心跳数(tickTime的数量)。
       syncLimit:集群中flower服务器(F)跟leader(L)服务器之间的请求和答应最多能容忍的心跳数。
       dataDir:该属性对应的目录是用来存放myid信息跟一些版本,日志,跟服务器唯一的ID信息等。
       clientPort:客户端连接的接口,客户端连接zookeeper服务器的端口,zookeeper会监听这个端口,接收客户端的请求访问!这个端口默认是2181。
       service.N=YYY:A:B
N:代表服务器编号(也就是myid里面的值)
YYY:服务器地址
A:表示 Flower 跟 Leader的通信端口,简称服务端内部通信的端口(默认2888)
B:表示 是选举端口(默认是3888)

完整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=/Data/zookeeper
# 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.3.100:2888:3888
server.2=192.168.3.101:2888:3888
server.3=192.168.3.102:2888:3888

4、创建zookeeper目录和myid文件

分别在三台服务器上操作。

[root@localhost conf]# mkdir -pv /Data/zookeeper
mkdir: created directory ‘/Data’
mkdir: created directory ‘/Data/zookeeper’ 
[root@localhost conf]# echo "1" > /Data/zookeeper/myid

       myid文件,里面的内容为数字,用于标识主机,如果这个文件没有的话,zookeeper无法启动。
       特别注意:myid必须与zoo.cfg里配置的一致。
       如下图,192.168.3.100的myid必须是1,以此类推。如果与zoo.cfg不一致则zookeeper无法启动

5、启动zookeeper

[root@localhost bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/apache-zookeeper-3.5.6/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

查看状态。可以看到192.168.3.101是leader
192.168.3.102

[root@localhost bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/apache-zookeeper-3.5.6/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower

192.168.3.101

[root@localhost bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/apache-zookeeper-3.5.6/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader

192.168.3.100

[root@localhost bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/apache-zookeeper-3.5.6/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower

三、安装kafka

1、下载安装文件

从下面地址下载
http://kafka.apache.org/downloads#2.4.1

3、开放端口

要开放9092端口。分别在三台服务器执行

[root@localhost jvm]# firewall-cmd --add-port=9092/tcp --permanent
success
[root@localhost jvm]# firewall-cmd --reload
success
[root@localhost jvm]# firewall-cmd --query-port=9092/tcp
yes

3、修改kafka配置文件

分别在三台服务器执行

[root@localhost]# cd kafka/config/
[root@localhost]# cp server.properties server.properties-bak
[root@localhost]# vi server.properties 
broker.id=1  #唯一,与myid保持一致
listeners=PLAINTEXT://192.168.3.100:9092  #修改为本机地址
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/Data/kafka-logs #数据目录,kafka-logs会自动采集
num.partitions=3
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=192.168.3.100:2181,192.168.3.101:2181,192.168.3.102:2181  #zokeeper集群地址,以","为分割
zookeeper.connection.timeout.ms=6000
group.initial.rebalance.delay.ms=0

注意:如果修改了broker.id=1的值,需要清空log.dirs目录下的所有文件并重启kafka

完整server.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.

# 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://192.168.3.102: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 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=/Data/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 for 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=192.168.3.100:2181,192.168.3.101:2181,192.168.3.102:2181

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


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

4、启动kafka

启动kafka后台运行

[root@localhost]# cd /home/kafka_2.12-2.4.1/bin
[root@localhost bin]# ./kafka-server-start.sh -daemon ../config/server.properties

如下图就是启动正常

如果启动异常,可以查看kafka的logs目录日志定位


转载:https://blog.csdn.net/reachyu/article/details/106873402
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场