本文将以三台服务器为例,介绍在 linux 系统下kafka的部署方式。
1. zookeeper下载
下载地址:Apache Kafka
选择需要的介质下载,这里以 kafka_2.11-1.1.1.tgz 为例
2.环境准备
部署kafka需要先部署JDK 以及zookeeper ,JDK部署可以参考Linux下JDK 安装-CSDN博客
zookeeper 部署可以参考 Linux 下 zookeeper 集群部署-CSDN博客。
3.kafka部署
注:以下操作三台机器均需要修改
3.1 修改系统配置文件
(1)编辑 hosts 文件
vi /etc/hosts
添加如下内容
ip(第一台机器) kafka1
ip(第二台机器) kafka2
ip(第三台机器) kafka3
(2)编辑ulimit
vi /etc/security/limits.d/20-nproc.conf
添加如下内容
* soft nofile 655350
* hard nofile 655350
(3)编辑系统参数
vi /etc/sysctl.conf
添加如下内容
vm.max_map_count=655350
保存后执行命令生效
sysctl -p
3.2 开放端口
kafka 默认需要开通节点 9092 端口
(1)查看防火墙状态
systemctl status firewalld
(2)开放端口
firewall-cmd --zone=public --add-port=9092/tcp --permanent
(3)防火墙重新加载配置
firewall-cmd --reload
(4) 查看防火墙所有开放的端口
firewall-cmd --zone=public --list-ports
3.3 安装 kafka
(1) 解压
上传kafka介质( kafka_2.11-1.1.1.tgz)到 /opt 目录
解压到当前目录下
tar zxfv kafka_2.11-1.1.1.tgz
(2) 配置 jvm.option
touch /opt/kafka_2.11-1.1.1/bin/kafka-run-class.sh
vi /opt/kafka_2.11-1.1.1/bin/kafka-run-class.sh
添加如下内容
export KAFKA_HEAP_OPTS="-Xmx4g -Xms4g -XX:MetaspaceSize=96m -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:G1HeapRegionSize=16M -XX:MinMetaspaceFreeRatio=50 -XX:MaxMetaspaceFreeRatio=80"
export JMX_PORT=9988
(3) 修改 server.properties 配置文件
vi /opt/kafka_2.11-1.1.1/config/server.properties
注:broker.id及listeners 修改为对应节点ID和地址,zookeeper.connect改为zk 地址
#################### Server Basics ####################
# The id of the broker. This must be set to a unique integer for each broker.
# 修改为节点ID
broker.id=1
#################### 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://kafka1: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
# 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=3
transaction.state.log.replication.factor=3
transaction.state.log.min.isr=2
#################### 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 地址
zookeeper.connect=zk1:2181,zk2:2181,zk3: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)创建数据目录
mkdir -p /data/kafka
(5)启动
/opt/kafka_2.11-1.1.1/bin/kafka-server-start.sh -daemon /opt/kafka_2.11-1.1.1/config/server.properties
3.4 验证
(1) 创建topic
/opt/kafka_2.11-1.1.1/bin/kafka-topics.sh --create --zookeeper zk1 --replication-factor 2 --partitions 1 --topic hello
(2) 连接producer
/opt/kafka_2.11-1.1.1/bin/kafka-console-producer.sh --broker-list kafka1:9092 --topic hello
(3) 连接consumer
/opt/kafka_2.11-1.1.1/bin/kafka-console-consumer.sh --bootstrap-server kafka2:9092 --topic hello --from-beginning
/opt/kafka_2.11-1.1.1/bin/kafka-console-consumer.sh --bootstrap-server kafka3:9092 --topic hello --from-beginning
(3) 测试
在producer的shell中输入字符如:test
consumer中会显示test
4. 设置服务开机自启动
注:以下操作三台机器均需要修改
(1)关闭kafka
/opt/kafka_2.11-1.1.1/bin/kafka-server-stop.sh
(2)创建启动服务文件
touch /etc/systemd/system/kafka.service
vi /etc/systemd/system/kafka.service
3)编写启动脚本
[Unit]
Description=kafka.service
After=network.target remote-fs.target
[Service]
User=root
Type=forking
ExecStart=/usr/bin/bash /opt/kafka_2.11-1.1.1/bin/kafka-server-start.sh -daemon /opt/kafka_2.11-1.1.1/config/server.properties
ExecStop=/usr/bin/bash /opt/kafka_2.11-1.1.1/bin/kafka-server-stop.sh
ExecReload=$ExecStop;$ExecStart
LimitCORE=infinity
LimitNOFILE=204800
LimitNPROC=204800
[Install]
WantedBy=multi-user.target
(4)关闭和启动服务
启动
systemctl start kafka.service
停止
systemctl stop kafka.service
重启
systemctl restart kafka.service
(5)设置服务是否开机启动
添加系统服务
systemctl enable kafka.service
删除系统服务
systemctl disable kafka.service
(6)重启机器
reboot
查看kafka是否开机自启动。
标签:log,--,server,####################,集群,Linux,kafka,1.1 From: https://blog.csdn.net/qq_59998784/article/details/140094323