尚硅谷大数据技术之Kafka第2章 Kafka集群部署

2.1 环境准备

2.1.1 集群规划

hadoop102           hadoop103           hadoop104html

zk                          zk                          zklinux

kafka                     kafka                    kafkaapache

2.1.2 jar下载

http://kafka.apache.org/downloads.html缓存

2.1.3 虚拟机准备

1准备3虚拟机服务器

2配置ip地址网络

尚硅谷大数据技术之修改成静态IPsession

 

3配置主机名称app

尚硅谷大数据技术之修改主机名异步

 

43主机分别关闭防火墙socket

 

[root@hadoop102 atguigu]# chkconfig iptables off

 

[root@hadoop103 atguigu]# chkconfig iptables off

 

[root@hadoop104 atguigu]# chkconfig iptables off

 

2.1.4 安装jdk

尚硅谷大数据技术之安装jdk

 

2.1.5 安装Zookeeper

 

0)集群规划

 

hadoop102hadoop103hadoop104三个节点上部署Zookeeper

 

1解压安装

 

1)解压zookeeper安装包到/opt/module/目录下

 

[atguigu@hadoop102 software]$ tar -zxvf zookeeper-3.4.10.tar.gz -C /opt/module/

 

2)在/opt/module/zookeeper-3.4.10/这个目录下建立zkData

 

mkdir -p zkData

 

3)重命名/opt/module/zookeeper-3.4.10/conf这个目录下的zoo_sample.cfgzoo.cfg

 

mv zoo_sample.cfg zoo.cfg

 

2)配置zoo.cfg文件

 

1)具体配置

 

dataDir=/opt/module/zookeeper-3.4.10/zkData

 

增长以下配置

 

#######################cluster##########################

 

server.2=hadoop102:2888:3888

 

server.3=hadoop103:2888:3888

 

server.4=hadoop104:2888:3888

 

2)配置参数解读

 

Server.A=B:C:D

 

A是一个数字,表示这个是第几号服务器;

 

B是这个服务器的ip地址;

 

C是这个服务器与集群中的Leader服务器交换信息的端口;

 

D万一集群中的Leader服务器挂了,须要一个端口来从新进行选举,选出一个新的Leader,而这个端口就是用来执行选举时服务器相互通讯的端口。

 

集群模式下配置一个文件myid这个文件在dataDir目录下,这个文件里面有一个数据就是A的值,Zookeeper启动时读取此文件,拿到里面数据与zoo.cfg里面的配置信息比较从而判断究竟是哪一个server

 

3集群操做

 

1/opt/module/zookeeper-3.4.10/zkData目录下建立一个myid的文件

 

touch myid

 

添加myid文件,注意必定要在linux里面建立notepad++里面极可能乱码

 

2编辑myid文件

 

vi myid

 

文件中添加server的编号:如2

 

3)拷贝配置好的zookeeper到其余机器上

 

scp -r zookeeper-3.4.10/ root@hadoop103.atguigu.com:/opt/app/

 

scp -r zookeeper-3.4.10/ root@hadoop104.atguigu.com:/opt/app/

 

分别修改myid文件中内容为34

 

4分别启动zookeeper

 

[root@hadoop102 zookeeper-3.4.10]# bin/zkServer.sh start

 

[root@hadoop103 zookeeper-3.4.10]# bin/zkServer.sh start

 

[root@hadoop104 zookeeper-3.4.10]# bin/zkServer.sh start

 

5查看状态

 

[root@hadoop102 zookeeper-3.4.10]# bin/zkServer.sh status

 

JMX enabled by default

 

Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg

 

Mode: follower

 

[root@hadoop103 zookeeper-3.4.10]# bin/zkServer.sh status

 

JMX enabled by default

 

Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg

 

Mode: leader

 

[root@hadoop104 zookeeper-3.4.5]# bin/zkServer.sh status

 

JMX enabled by default

 

Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg

 

Mode: follower

 

2.2 Kafka集群部署

 

1解压安装包

 

[atguigu@hadoop102 software]$ tar -zxvf kafka_2.11-0.11.0.0.tgz -C /opt/module/

 

2修改解压后的文件名称

 

[atguigu@hadoop102 module]$ mv kafka_2.11-0.11.0.0/ kafka

 

3)在/opt/module/kafka目录下建立logs文件夹

 

[atguigu@hadoop102 kafka]$ mkdir logs

 

4修改配置文件

 

[atguigu@hadoop102 kafka]$ cd config/

 

[atguigu@hadoop102 config]$ vi server.properties

 

输入如下内容:

 

#broker的全局惟一编号,不能重复

broker.id=0

#删除topic功能使能

delete.topic.enable=true

#处理网络请求的线程数量

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运行日志存放的路径

log.dirs=/opt/module/kafka/logs

#topic在当前broker上的分区个数

num.partitions=1

#用来恢复和清理data下数据的线程数量

num.recovery.threads.per.data.dir=1

#segment文件保留的最长时间,超时将被删除

log.retention.hours=168

#配置链接Zookeeper集群地址

zookeeper.connect=hadoop102:2181,hadoop103:2181,hadoop104:2181

 

5)配置环境变量

 

[root@hadoop102 module]# vi /etc/profile

 

#KAFKA_HOME

export KAFKA_HOME=/opt/module/kafka

export PATH=$PATH:$KAFKA_HOME/bin

 

[root@hadoop102 module]# source /etc/profile

 

6)分发安装包

 

[root@hadoop102 etc]# xsync profile

 

[atguigu@hadoop102 module]$ xsync kafka/

 

7)分别在hadoop103hadoop104上修改配置文件/opt/module/kafka/config/server.propertiesbroker.id=1broker.id=2

 

broker.id不得重复

 

8启动集群

 

依次在hadoop102hadoop103hadoop104节点上启动kafka

 

[atguigu@hadoop102 kafka]$ bin/kafka-server-start.sh config/server.properties &

 

[atguigu@hadoop103 kafka]$ bin/kafka-server-start.sh config/server.properties &

 

[atguigu@hadoop104 kafka]$ bin/kafka-server-start.sh config/server.properties &

 

9)关闭集群

 

[atguigu@hadoop102 kafka]$ bin/kafka-server-stop.sh stop

 

[atguigu@hadoop103 kafka]$ bin/kafka-server-stop.sh stop

 

[atguigu@hadoop104 kafka]$ bin/kafka-server-stop.sh stop

 

2.3 Kafka命令行操做

 

1)查看当前服务器中的全部topic

 

[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --list

 

2)建立topic

 

[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --create --replication-factor 3 --partitions 1 --topic first

 

选项说明:

 

--topic 定义topic

 

--replication-factor  定义副本数

 

--partitions  定义分区数

 

3)删除topic

 

[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --delete --topic first

 

须要server.properties中设置delete.topic.enable=true不然只是标记删除或者直接重启。

 

4)发送消息

 

[atguigu@hadoop102 kafka]$ bin/kafka-console-producer.sh --broker-list hadoop102:9092 --topic first

 

>hello world

 

>atguigu  atguigu

 

5)消费消息

 

[atguigu@hadoop103 kafka]$ bin/kafka-console-consumer.sh --zookeeper hadoop102:2181 --from-beginning --topic first

 

--from-beginning会把first主题中以往全部的数据都读取出来。根据业务场景选择是否增长该配置。

 

6)查看某个Topic的详情

 

[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --describe --topic first

 

2.4 Kafka配置信息

 

2.4.1 Broker配置信息

 

属性

默认值

描述

broker.id

 

必填参数,broker的惟一标识

log.dirs

/tmp/kafka-logs

Kafka数据存放的目录。能够指定多个目录,中间用逗号分隔,当新partition被建立的时会被存放到当前存放partition最少的目录。

port

9092

BrokerServer接受客户端链接的端口号

zookeeper.connect

null

Zookeeper的链接串,格式为:hostname1:port1,hostname2:port2,hostname3:port3。能够填一个或多个,为了提升可靠性,建议都填上。注意,此配置容许咱们指定一个zookeeper路径来存放此kafka集群的全部数据,为了与其余应用集群区分开,建议在此配置中指定本集群存放目录,格式为:hostname1:port1,hostname2:port2,hostname3:port3/chroot/path 。须要注意的是,消费者的参数要和此参数一致。

message.max.bytes

1000000

服务器能够接收到的最大的消息大小。注意此参数要和consumermaximum.message.size大小一致,不然会由于生产者生产的消息太大致使消费者没法消费。

num.io.threads

8

服务器用来执行读写请求的IO线程数,此参数的数量至少要等于服务器上磁盘的数量。

queued.max.requests

500

I/O线程能够处理请求的队列大小,若实际请求数超过此大小,网络线程将中止接收新的请求。

socket.send.buffer.bytes

100 * 1024

The SO_SNDBUFF buffer the server prefers for socket connections.

socket.receive.buffer.bytes

100 * 1024

The SO_RCVBUFF buffer the server prefers for socket connections.

socket.request.max.bytes

100 * 1024 * 1024

服务器容许请求的最大值, 用来防止内存溢出,其值应该小于 Java heap size.

num.partitions

1

默认partition数量,若是topic在建立时没有指定partition数量,默认使用此值,建议改成5

log.segment.bytes

1024 * 1024 * 1024

Segment文件的大小,超过此值将会自动新建一个segment,此值能够被topic级别的参数覆盖。

log.roll.{ms,hours}

24 * 7 hours

新建segment文件的时间,此值能够被topic级别的参数覆盖。

log.retention.{ms,minutes,hours}

7 days

Kafka segment log的保存周期,保存周期超过此时间日志就会被删除。此参数能够被topic级别参数覆盖。数据量大时,建议减少此值。

log.retention.bytes

-1

每一个partition的最大容量,若数据量超过此值,partition数据将会被删除。注意这个参数控制的是每一个partition而不是topic。此参数能够被log级别参数覆盖。

log.retention.check.interval.ms

5 minutes

删除策略的检查周期

auto.create.topics.enable

true

自动建立topic参数,建议此值设置为false,严格控制topic管理,防止生产者错写topic

default.replication.factor

1

默认副本数量,建议改成2

replica.lag.time.max.ms

10000

在此窗口时间内没有收到followerfetch请求,leader会将其从ISR(in-sync replicas)中移除。

replica.lag.max.messages

4000

若是replica节点落后leader节点此值大小的消息数量,leader节点就会将其从ISR中移除。

replica.socket.timeout.ms

30 * 1000

replicaleader发送请求的超时时间。

replica.socket.receive.buffer.bytes

64 * 1024

The socket receive buffer for network requests to the leader for replicating data.

replica.fetch.max.bytes

1024 * 1024

The number of byes of messages to attempt to fetch for each partition in the fetch requests the replicas send to the leader.

replica.fetch.wait.max.ms

500

The maximum amount of time to wait time for data to arrive on the leader in the fetch requests sent by the replicas to the leader.

num.replica.fetchers

1

Number of threads used to replicate messages from leaders. Increasing this value can increase the degree of I/O parallelism in the follower broker.

fetch.purgatory.purge.interval.requests

1000

The purge interval (in number of requests) of the fetch request purgatory.

zookeeper.session.timeout.ms

6000

ZooKeeper session 超时时间。若是在此时间内server没有向zookeeper发送心跳,zookeeper就会认为此节点已挂掉。 此值过低致使节点容易被标记死亡;若过高,.会致使太迟发现节点死亡。

zookeeper.connection.timeout.ms

6000

客户端链接zookeeper的超时时间。

zookeeper.sync.time.ms

2000

H ZK follower落后 ZK leader的时间。

controlled.shutdown.enable

true

容许broker shutdown。若是启用,broker在关闭本身以前会把它上面的全部leaders转移到其它brokers上,建议启用,增长集群稳定性。

auto.leader.rebalance.enable

true

If this is enabled the controller will automatically try to balance leadership for partitions among the brokers by periodically returning leadership to the “preferred” replica for each partition if it is available.

leader.imbalance.per.broker.percentage

10

The percentage of leader imbalance allowed per broker. The controller will rebalance leadership if this ratio goes above the configured value per broker.

leader.imbalance.check.interval.seconds

300

The frequency with which to check for leader imbalance.

offset.metadata.max.bytes

4096

The maximum amount of metadata to allow clients to save with their offsets.

connections.max.idle.ms

600000

Idle connections timeout: the server socket processor threads close the connections that idle more than this.

num.recovery.threads.per.data.dir

1

The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.

unclean.leader.election.enable

true

Indicates whether to enable replicas not in the ISR set to be elected as leader as a last resort, even though doing so may result in data loss.

delete.topic.enable

false

启用deletetopic参数,建议设置为true

offsets.topic.num.partitions

50

The number of partitions for the offset commit topic. Since changing this after deployment is currently unsupported, we recommend using a higher setting for production (e.g., 100-200).

offsets.topic.retention.minutes

1440

Offsets that are older than this age will be marked for deletion. The actual purge will occur when the log cleaner compacts the offsets topic.

offsets.retention.check.interval.ms

600000

The frequency at which the offset manager checks for stale offsets.

offsets.topic.replication.factor

3

The replication factor for the offset commit topic. A higher setting (e.g., three or four) is recommended in order to ensure higher availability. If the offsets topic is created when fewer brokers than the replication factor then the offsets topic will be created with fewer replicas.

offsets.topic.segment.bytes

104857600

Segment size for the offsets topic. Since it uses a compacted topic, this should be kept relatively low in order to facilitate faster log compaction and loads.

offsets.load.buffer.size

5242880

An offset load occurs when a broker becomes the offset manager for a set of consumer groups (i.e., when it becomes a leader for an offsets topic partition). This setting corresponds to the batch size (in bytes) to use when reading from the offsets segments when loading offsets into the offset manager’s cache.

offsets.commit.required.acks

-1

The number of acknowledgements that are required before the offset commit can be accepted. This is similar to the producer’s acknowledgement setting. In general, the default should not be overridden.

offsets.commit.timeout.ms

5000

The offset commit will be delayed until this timeout or the required number of replicas have received the offset commit. This is similar to the producer request timeout.

 

2.4.2 Producer配置信息

 

属性

默认值

描述

metadata.broker.list

 

启动时producer查询brokers的列表,能够是集群中全部brokers的一个子集。注意,这个参数只是用来获取topic的元信息用,producer会从元信息中挑选合适的broker并与之创建socket链接。格式是:host1:port1,host2:port2

request.required.acks

0

参见3.2节介绍

request.timeout.ms

10000

Broker等待ack的超时时间,若等待时间超过此值,会返回客户端错误信息。

producer.type

sync

同步异步模式。async表示异步,sync表示同步。若是设置成异步模式,能够容许生产者以batch的形式push数据,这样会极大的提升broker性能,推荐设置为异步。

serializer.class

kafka.serializer.DefaultEncoder

序列号类,.默认序列化成 byte[]

key.serializer.class

 

Key的序列化类,默认同上。

partitioner.class

kafka.producer.DefaultPartitioner

Partition类,默认对key进行hash

compression.codec

none

指定producer消息的压缩格式,可选参数为: “none”, “gzip” and “snappy”。关于压缩参见4.1

compressed.topics

null

启用压缩的topic名称。若上面参数选择了一个压缩格式,那么压缩仅对本参数指定的topic有效,若本参数为空,则对全部topic有效。

message.send.max.retries

3

Producer发送失败时重试次数。若网络出现问题,可能会致使不断重试。

retry.backoff.ms

100

Before each retry, the producer refreshes the metadata of relevant topics to see if a new leader has been elected. Since leader election takes a bit of time, this property specifies the amount of time that the producer waits before refreshing the metadata.

topic.metadata.refresh.interval.ms

600 * 1000

The producer generally refreshes the topic metadata from brokers when there is a failure (partition missing, leader not available…). It will also poll regularly (default: every 10min so 600000ms). If you set this to a negative value, metadata will only get refreshed on failure. If you set this to zero, the metadata will get refreshed after each message sent (not recommended). Important note: the refresh happen only AFTER the message is sent, so if the producer never sends a message the metadata is never refreshed

queue.buffering.max.ms

5000

启用异步模式时,producer缓存消息的时间。好比咱们设置成1000时,它会缓存1秒的数据再一次发送出去,这样能够极大的增长broker吞吐量,但也会形成时效性的下降。

queue.buffering.max.messages

10000

采用异步模式时producer buffer 队列里最大缓存的消息数量,若是超过这个数值,producer就会阻塞或者丢掉消息。

queue.enqueue.timeout.ms

-1

当达到上面参数值时producer阻塞等待的时间。若是值设置为0buffer队列满时producer不会阻塞,消息直接被丢掉。若值设置为-1producer会被阻塞,不会丢消息。

batch.num.messages

200

采用异步模式时,一个batch缓存的消息数量。达到这个数量值时producer才会发送消息。

send.buffer.bytes

100 * 1024

Socket write buffer size

client.id

“”

The client id is a user-specified string sent in each request to help trace calls. It should logically identify the application making the request.

 

2.4.3 Consumer配置信息

 

属性

默认值

描述

group.id

 

Consumer的组ID,相同goup.idconsumer属于同一个组。

zookeeper.connect

 

Consumerzookeeper链接串,要和broker的配置一致。

consumer.id

null

若是不设置会自动生成。

socket.timeout.ms

30 * 1000

网络请求的socket超时时间。实际超时时间由max.fetch.wait + socket.timeout.ms 肯定。

socket.receive.buffer.bytes

64 * 1024

The socket receive buffer for network requests.

fetch.message.max.bytes

1024 * 1024

查询topic-partition时容许的最大消息大小。consumer会为每一个partition缓存此大小的消息到内存,所以,这个参数能够控制consumer的内存使用量。这个值应该至少比server容许的最大消息大小大,以避免producer发送的消息大于consumer容许的消息。

num.consumer.fetchers

1

The number fetcher threads used to fetch data.

auto.commit.enable

true

若是此值设置为trueconsumer会周期性的把当前消费的offset值保存到zookeeper。当consumer失败重启以后将会使用此值做为新开始消费的值。

auto.commit.interval.ms

60 * 1000

Consumer提交offset值到zookeeper的周期。

queued.max.message.chunks

2

用来被consumer消费的message chunks 数量, 每一个chunk能够缓存fetch.message.max.bytes大小的数据量。

auto.commit.interval.ms

60 * 1000

Consumer提交offset值到zookeeper的周期。

queued.max.message.chunks

2

用来被consumer消费的message chunks 数量, 每一个chunk能够缓存fetch.message.max.bytes大小的数据量。

fetch.min.bytes

1

The minimum amount of data the server should return for a fetch request. If insufficient data is available the request will wait for that much data to accumulate before answering the request.

fetch.wait.max.ms

100

The maximum amount of time the server will block before answering the fetch request if there isn’t sufficient data to immediately satisfy fetch.min.bytes.

rebalance.backoff.ms

2000

Backoff time between retries during rebalance.

refresh.leader.backoff.ms

200

Backoff time to wait before trying to determine the leader of a partition that has just lost its leader.

auto.offset.reset

largest

What to do when there is no initial offset in ZooKeeper or if an offset is out of range ;smallest : automatically reset the offset to the smallest offset; largest : automatically reset the offset to the largest offset;anything else: throw exception to the consumer

consumer.timeout.ms

-1

若在指定时间内没有消息消费,consumer将会抛出异常。

exclude.internal.topics

true

Whether messages from internal topics (such as offsets) should be exposed to the consumer.

zookeeper.session.timeout.ms

6000

ZooKeeper session timeout. If the consumer fails to heartbeat to ZooKeeper for this period of time it is considered dead and a rebalance will occur.

zookeeper.connection.timeout.ms

6000

The max time that the client waits while establishing a connection to zookeeper.

zookeeper.sync.time.ms

2000

How far a ZK follower can be behind a ZK leader

 

本教程由尚硅谷教育大数据研究院出品,如需转载请注明来源,欢迎你们关注尚硅谷公众号(atguigu)了解更多。

相关文章
相关标签/搜索