Linux基础
1 前言
一个问题,一篇文章,一出故事。
公司让更新生产环境的Kafka,于是整理此文,之前部署的环境详细如下,
2 最佳实践
2.1 创建测试数据
In hd19,
kafka-topics.sh --create --partitions 1 --replication-factor 3 --topic quickstart-events --bootstrap-server hd19.cmdschool.org:9092
创建成功会收到如下提示,
Created topic quickstart-events.
以上创建主题,然后你可以使用如下命令查询主题,
kafka-topics.sh --describe --topic quickstart-events --bootstrap-server hd20.cmdschool.org:9092
可见如下显示,
Topic: quickstart-events TopicId: 7m1AW3MkQ4us6togB60exw PartitionCount: 1 ReplicationFactor: 3 Configs: min.insync.replicas=2,max.message.bytes=16777216 Topic: quickstart-events Partition: 0 Leader: 19 Replicas: 19,21,20 Isr: 19,21,20
把事件写入主题,
kafka-console-producer.sh --topic quickstart-events --bootstrap-server hd21.cmdschool.org:9092 >This is my first event >This is my second event
以上按下【Ctrl+c】即可结束,然后你可以使用如下命令查询事件,
kafka-console-consumer.sh --topic quickstart-events --from-beginning --bootstrap-server hd19.cmdschool.org:9092
可见如下显示,
This is my first event This is my second event
以上按下【Ctrl+c】即可结束
2.2 滚动升级集群(一台一台操作)
2.2.1 下载软件包
cd ~ wget https://dlcdn.apache.org/kafka/3.1.0/kafka_2.13-3.1.0.tgz --no-check-certificate
另外,如果需要其他版本请从如下链接下载,
https://dlcdn.apache.org/kafka/
2.2.2 部署新软件包
tar -xf kafka_2.13-3.1.0.tgz mv kafka_2.13-3.1.0 /usr/ rm -f /etc/kafka/conf rm -f /var/log/kafka ln -s /usr/kafka_2.13-3.1.0/config/ /etc/kafka/conf ln -s /usr/kafka_2.13-3.1.0/logs/ /var/log/kafka chown kafka:kafka -R /usr/kafka_2.13-3.1.0/ chmod 755 -R /usr/kafka_2.13-3.1.0/ echo 'export KAFKA_HOME=/usr/kafka_2.13-3.1.0' > /etc/profile.d/kafka.sh echo 'export PATH=${KAFKA_HOME}/bin:$PATH' >> /etc/profile.d/kafka.sh cp /usr/kafka_2.13-3.1.0/config/server.properties /usr/kafka_2.13-3.1.0/config/server.properties.default cp -a /usr/kafka_2.13-3.0.0/config/server.properties /usr/kafka_2.13-3.1.0/config/server.properties
重要的是,我们将使用新的软件包,但启动前需要声明当前的协议版本,
echo 'inter.broker.protocol.version=3.0.0' >> /etc/kafka/conf/server.properties
2.2.3 增加JMX监控需求(可选)
vim /usr/kafka_2.13-3.1.0/bin/kafka-server-start.sh
你需要在非注解第一行加入如下声明,
export JMX_PORT=9988
2.2.4 停止服务
systemctl stop kafka.service systemctl status kafka.service
2.2.5 修改服务启动文件并启动服务
vim /usr/lib/systemd/system/kafka.service
配置修改如下,
[Unit] Description=Apache Kafka manager Wants=network.target zoo.service After=network.target zoo.service Documentation=https://kafka.apache.org/documentation/#gettingStarted [Service] Type=simple User=kafka WorkingDirectory=/var/lib/kafka ExecStart=/bin/sh -c '. /etc/profile;/usr/kafka_2.13-3.1.0/bin/kafka-server-start.sh /etc/kafka/conf/server.properties' ExecStop=/bin/sh -c '. /etc/profile;/usr/kafka_2.13-3.1.0/bin/kafka-server-stop.sh /etc/kafka/conf/server.properties' [Install] WantedBy=multi-user.target
然后,你需要执行以下命令重载服务使配置生效,
systemctl daemon-reload
启动服务应用新的配置,
systemctl start kafka.service systemctl status kafka.service
2.3 验证升级后的集群状态
kafka-console-consumer.sh --topic quickstart-events --from-beginning --bootstrap-server hd19.cmdschool.org:9092
可见如下显示,
This is my first event This is my second event
2.4 滚动升级集群broker协议版本(一台一台操作)
vim /etc/kafka/conf/server.properties
修改如下配置,
inter.broker.protocol.version=3.1.0
配置修改后,你需要重启服务使配置生效,
systemctl restart kafka.service systemctl status kafka.service
2.5 验证升级后的集群状态
kafka-console-consumer.sh --topic quickstart-events --from-beginning --bootstrap-server hd19.cmdschool.org:9092
可见如下显示,
This is my first event This is my second event
参阅文档
==============
https://kafka.apache.org/documentation/#upgrade
没有评论