kafka cluster的搭建

文章目录

本文使用工具或者服务

Zookeeper 簡介

  • ZooKeeper是一个分布式协调服务,它的主要作用是为分布式系统提供一致性服务,提供的功能包括:配置维护、命名服务、分布式同步、组服务等。Kafka的运行依赖ZooKeeper。
    1. Broker注册
    2. Topic注册
    3. 生产者负载均衡
    4. 消费者负载均衡
    5. 消费者注册

Kafka 簡介

  • Kafka是由Apache软件基金会开发的一个开源流处理平台,由Scala和Java编写。Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者在网站中的所有动作流数据。
    1. Kafka 是一种高吞吐量的分布式发布订阅消息系统,有如下特性:
    2. 通过O(1)的磁盘数据结构提供消息的持久化,这种结构对于即使数以TB的消息存储也能够保持长时间的稳定性能。
    3. 高吞吐量 即使是非常普通的硬件Kafka也可以支持每秒数百万的消息。
    4. 支持通过Kafka服务器和消费机集群来分区消息。

zookeeper 與kafka之間的關係

  • ZooKeeper用于分布式系统的协调,Kafka使用ZooKeeper也是基于相同的原因。ZooKeeper主要用来协调Kafka的各个broker,不仅可以实现broker的负载均衡,而且当增加了broker或者某个broker故障了,ZooKeeper将会通知生产者和消费者,这样可以保证整个系统正常运转。

安裝zooker和kafka 之前首先要做系統的優化和配置

  1. 文件句柄數的調整
  2. oracle Jre8 安裝
  3. 磁盤的分區和挂載點必須滿足kafka大數量的存儲xfs分區
  4. 關閉selinux
  5. 防火墻暫時關閉(後面看可以統一規劃端口)
  6. 創建非root的kafka管理用戶並添加sudoer權限

注意事項

  1. kafka是一個分佈式的消息系統,我們會臨時存儲消息,如果存儲消息的時間長,那麽需要大空間,所以分區的時候已經要注意,就是安裝kafka的時候存儲一定要挂載到大的硬盤空間上

Zookeeper 配置

 1# The number of milliseconds of each tick
 2tickTime=2000
 3# The number of ticks that the initial
 4# synchronization phase can take
 5initLimit=10
 6# The number of ticks that can pass between
 7# sending a request and getting an acknowledgement
 8syncLimit=5
 9# the directory where the snapshot is stored.
10# do not use /tmp for storage, /tmp here is just
11# example sakes.
12dataDir=/opt/conf
13# /opt/conf/myid #myid文件,里面的内容为数字,用于标识主机,如果这个文件没有的话,zookeeper无法启动
14# the port at which the clients will connect
15clientPort=2181
16# the maximum number of client connections.
17# increase this if you need to handle more clients
18#maxClientCnxns=60
19#
20# Be sure to read the maintenance section of the
21# administrator guide before turning on autopurge.
22#
23# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
24#
25# The number of snapshots to retain in dataDir
26#autopurge.snapRetainCount=3
27# Purge task interval in hours
28# Set to "0" to disable auto purge feature
29#autopurge.purgeInterval=1
30
31## Metrics Providers
32#
33# https://prometheus.io Metrics Exporter
34#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
35#metricsProvider.httpPort=7000
36#metricsProvider.exportJvmInfo=true
37server.1=192.168.25.231:12888:13888
38server.2=192.168.25.232:12888:13888
39server.3=192.168.25.233:12888:13888
40server.4=192.168.25.234:12888:13888
41server.5=192.168.25.235:12888:13888
42
43#配置参数说明:
44tickTime:客户端与服务器或者服务器与服务器之间每个tickTime时间就会发送一次心跳。通过心跳不仅能够用来监听机器的工作状态,还可以通过心跳来控制Flower跟Leader的通信时间,默认2
45initLimit:集群中的follower服务器(F)leader服务器(L)之间初始连接时能容忍的最多心跳数(tickTime的数量)。
46syncLimit:集群中flower服务器F)跟leaderL)服务器之间的请求和答应最多能容忍的心跳数。   
47dataDir:该属性对应的目录是用来存放myid信息跟一些版本,日志,跟服务器唯一的ID信息等
48clientPort:客户端连接的接口,客户端连接zookeeper服务器的端口zookeeper会监听这个端口,接收客户端的请求访问!这个端口默认是2181
49service.N=YYY:A:B
50N:代表服务器编号(也就是myid里面的值
51YYY:服务器地址
52A:表示 Flower  Leader的通信端口,简称服务端内部通信的端口(默认2888),我们使用12888
53B:表示 是选举端口(默认是3888),我们使用

Kafka的配置

  1
  2# Licensed to the Apache Software Foundation (ASF) under one or more
  3# contributor license agreements.  See the NOTICE file distributed with
  4# this work for additional information regarding copyright ownership.
  5# The ASF licenses this file to You under the Apache License, Version 2.0
  6# (the "License"); you may not use this file except in compliance with
  7# the License.  You may obtain a copy of the License at
  8#
  9#    http://www.apache.org/licenses/LICENSE-2.0
 10#
 11# Unless required by applicable law or agreed to in writing, software
 12# distributed under the License is distributed on an "AS IS" BASIS,
 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14# See the License for the specific language governing permissions and
 15# limitations under the License.
 16
 17# see kafka.server.KafkaConfig for additional details and defaults
 18
 19############################# Server Basics #############################
 20
 21# The id of the broker. This must be set to a unique integer for each broker.
 22broker.id=1
 23
 24############################# Socket Server Settings #############################
 25
 26# The address the socket server listens on. It will get the value returned from
 27# java.net.InetAddress.getCanonicalHostName() if not configured.
 28#   FORMAT:
 29#     listeners = listener_name://host_name:port
 30#   EXAMPLE:
 31#     listeners = PLAINTEXT://your.host.name:9092
 32listeners=PLAINTEXT://192.168.25.231:9092
 33
 34# Hostname and port the broker will advertise to producers and consumers. If not set,
 35# it uses the value for "listeners" if configured.  Otherwise, it will use the value
 36# returned from java.net.InetAddress.getCanonicalHostName().
 37#advertised.listeners=PLAINTEXT://your.host.name:9092
 38
 39# Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details
 40#listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL
 41
 42# The number of threads that the server uses for receiving requests from the network and sending responses to the network
 43num.network.threads=3
 44
 45# The number of threads that the server uses for processing requests, which may include disk I/O
 46num.io.threads=8
 47
 48# The send buffer (SO_SNDBUF) used by the socket server
 49socket.send.buffer.bytes=102400
 50
 51# The receive buffer (SO_RCVBUF) used by the socket server
 52socket.receive.buffer.bytes=102400
 53
 54# The maximum size of a request that the socket server will accept (protection against OOM)
 55socket.request.max.bytes=104857600
 56
 57
 58############################# Log Basics #############################
 59
 60# A comma separated list of directories under which to store log files
 61log.dirs=/kafka-data/kafka-logs
 62
 63# The default number of log partitions per topic. More partitions allow greater
 64# parallelism for consumption, but this will also result in more files across
 65# the brokers.
 66num.partitions=1
 67
 68# The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
 69# This value is recommended to be increased for installations with data dirs located in RAID array.
 70num.recovery.threads.per.data.dir=1
 71
 72############################# Internal Topic Settings  #############################
 73# The replication factor for the group metadata internal topics "__consumer_offsets" and "__transaction_state"
 74# For anything other than development testing, a value greater than 1 is recommended to ensure availability such as 3.
 75offsets.topic.replication.factor=1
 76transaction.state.log.replication.factor=1
 77transaction.state.log.min.isr=1
 78
 79############################# Log Flush Policy #############################
 80
 81# Messages are immediately written to the filesystem but by default we only fsync() to sync
 82# the OS cache lazily. The following configurations control the flush of data to disk.
 83# There are a few important trade-offs here:
 84#    1. Durability: Unflushed data may be lost if you are not using replication.
 85#    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.
 86#    3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to excessive seeks.
 87# The settings below allow one to configure the flush policy to flush data after a period of time or
 88# every N messages (or both). This can be done globally and overridden on a per-topic basis.
 89
 90# The number of messages to accept before forcing a flush of data to disk
 91#log.flush.interval.messages=10000
 92
 93# The maximum amount of time a message can sit in a log before we force a flush
 94#log.flush.interval.ms=1000
 95
 96############################# Log Retention Policy #############################
 97
 98# The following configurations control the disposal of log segments. The policy can
 99# be set to delete segments after a period of time, or after a given size has accumulated.
100# A segment will be deleted whenever *either* of these criteria are met. Deletion always happens
101# from the end of the log.
102
103# The minimum age of a log file to be eligible for deletion due to age
104log.retention.hours=168
105
106# A size-based retention policy for logs. Segments are pruned from the log unless the remaining
107# segments drop below log.retention.bytes. Functions independently of log.retention.hours.
108#log.retention.bytes=1073741824
109
110# The maximum size of a log segment file. When this size is reached a new log segment will be created.
111log.segment.bytes=1073741824
112
113# The interval at which log segments are checked to see if they can be deleted according
114# to the retention policies
115log.retention.check.interval.ms=300000
116
117############################# Zookeeper #############################
118
119# Zookeeper connection string (see zookeeper docs for details).
120# This is a comma separated host:port pairs, each corresponding to a zk
121# server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
122# You can also append an optional chroot string to the urls to specify the
123# root directory for all kafka znodes.
124zookeeper.connect=192.168.25.231:2181,192.168.25.232:2181,192.168.25.233:2181,192.168.25.234:2181,192.168.25.235:2181
125
126# Timeout in ms for connecting to zookeeper
127zookeeper.connection.timeout.ms=18000
128
129
130
131############################# Group Coordinator Settings #############################
132
133# The following configuration specifies the time, in milliseconds, that the GroupCoordinator will delay the initial consumer rebalance.
134# 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.
135# The default value for this is 3 seconds.
136# We override this to 0 here as it makes for a better out-of-the-box experience for development and testing.
137# 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.
138group.initial.rebalance.delay.ms=0

安装kafka的ui 监控

1docker run -p 8080:8080 \
2	-e KAFKA_CLUSTERS_0_NAME=local \
3	-e KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS=192.168.25.231:9092,192.168.25.232:9092,192.168.25.231:9092,192.168.25.233:9092,192.168.25.234:9092,192.168.25.235:9092 \
4	-d provectuslabs/kafka-ui:latest 
  1. 访问http://ip:8080/ 就可以看到kafka管理页面