当前位置: 首页 > news >正文

Kafka 集群部署(CentOS 单机模拟版)

文章目录

  • 0.前置说明
  • 1.安装Java11
  • 2.集群部署
    • 2.1 安装ZooKeeper
    • 2.2 安装Kafka
  • 2.3 封装启动脚本

0.前置说明

由于我们手里只有一台Linux机器,所以我们实现的是简单的单机模拟的集群部署,通过修改配置文件,启动 3 个 kafka 时用到 3 个不同的端口(9091,9092,9093)。

1.安装Java11

  1. 切换到你的工作目录下执行:
yum install java-11-openjdk -y
  1. 添加环境变量;
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.23.0.9-3.tl3.x86_64
export PATH=$PATH:$JAVA_HOME/bin
  1. 让你的环境变量生效;
source /etc/profile
  1. 测试是否安装成功;
java -version
openjdk version "11.0.23" 2024-04-16 LTS
OpenJDK Runtime Environment (Red_Hat-11.0.23.0.9-2) (build 11.0.23+9-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-11.0.23.0.9-2) (build 11.0.23+9-LTS, mixed mode, sharing)

2.集群部署

  1. 在你的工作目录下新建目录 cluster(集群);
mkdir cluster
  1. 进入cluster目录下,下载 kafka 安装包 kafka-3.6.1-src.tgz并解压。
rz -E # 上传本地安装包
tar -zxf kafka_2.12-3.6.1.tgz
  1. 改名为 kafka
mv kafka_2.12-3.6.1 kafka

2.1 安装ZooKeeper

  1. 修改文件夹名为zookeeper

因为 kafka 内置了 ZooKeeper 软件,所以此处将解压缩的文件作为 ZooKeeper 软件使用。

mv kafka/ zookeeper
  1. 修改config/zookeeper.properties文件;
cd zookeeper/
vim config/zookeeper.properties 
  • 修改dataDir,zookeeper 数据的存储文件。
# 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.
# the directory where the snapshot is stored.
dataDir=/root/cluster/zookeeper-data/zookeeper
# the port at which the clients will connect
clientPort=2181
# disable the per-ip limit on the number of connections since this is a non-production config
maxClientCnxns=0
# Disable the adminserver by default to avoid port conflicts.
# Set the port to something non-conflicting if choosing to enable this
admin.enableServer=false
# admin.serverPort=8080

2.2 安装Kafka

  1. 将上面解压缩的文件复制一份,改名为broke-1
mv kafka_2.12-3.6.1/ broker-1
ll
total 8
drwxr-xr-x 7 root root 4096 Nov 24 17:43 broker-1
drwxr-xr-x 7 root root 4096 Nov 24 17:43 zookeeper
  1. 修改config/server.properties配置文件;
vim broker-1/config/server.properties
############################# Server Basics ############################## The id of the broker. This must be set to a unique integer for each broker.
broker.id=1 # kafka 节点数字标识,集群内具有唯一性
#......############################# Socket Server Settings ############################## The address the socket server listens on. If not configured, the host name will be equal to the value of
# java.net.InetAddress.getCanonicalHostName(), with PLAINTEXT listener name, and port 9092.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
listeners=PLAINTEXT://:9091
#.......
############################# Log Basics ############################## A comma separated list of directories under which to store log files
log.dirs=/root/cluster/broker-data/broker-1 # 监听器 9091 为本地端口,如果冲突,请重新指定
# .......
############################# 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=localhost:2181 # 数据文件路径,如果不存在,会自动创建# Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=18000 # ZooKeeper 软件连接地址,2181 为默认的ZK 端口号 /kafka 为ZK 的管理节点
  1. 同样的步骤,复制一份broker-2broker-3,并修改配置文件。
# broker-2
############################# Server Basics ############################## The id of the broker. This must be set to a unique integer for each broker.
broker.id=2 # kafka 节点数字标识,集群内具有唯一性
#......############################# Socket Server Settings ############################## The address the socket server listens on. If not configured, the host name will be equal to the value of
# java.net.InetAddress.getCanonicalHostName(), with PLAINTEXT listener name, and port 9092.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
listeners=PLAINTEXT://:9092
#.......
############################# Log Basics ############################## A comma separated list of directories under which to store log files
log.dirs=/root/cluster/broker-data/broker-2 # 监听器 9091 为本地端口,如果冲突,请重新指定
# .......
############################# 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=localhost:2181 # 数据文件路径,如果不存在,会自动创建# Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=18000 # ZooKeeper 软件连接地址,2181 为默认的ZK 端口号 /kafka 为ZK 的管理节点
# broker-3
############################# Server Basics ############################## The id of the broker. This must be set to a unique integer for each broker.
broker.id=3 # kafka 节点数字标识,集群内具有唯一性
#......############################# Socket Server Settings ############################## The address the socket server listens on. If not configured, the host name will be equal to the value of
# java.net.InetAddress.getCanonicalHostName(), with PLAINTEXT listener name, and port 9092.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
listeners=PLAINTEXT://:9093
#.......
############################# Log Basics ############################## A comma separated list of directories under which to store log files
log.dirs=/root/cluster/broker-data/broker-3 # 监听器 9091 为本地端口,如果冲突,请重新指定
# .......
############################# 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=localhost:2181 # 数据文件路径,如果不存在,会自动创建# Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=18000 # ZooKeeper 软件连接地址,2181 为默认的ZK 端口号 /kafka 为ZK 的管理节点

2.3 封装启动脚本

因为 Kafka 启动前,必须先启动 ZooKeeper,并且Kafka集群中有多个节点需要启动,所以启动过程比较繁琐,这里我们将启动的指令进行封装。

  1. zookeeper文件夹下创建zk.sh批处理文件;
cd zookeeper
vim zk.sh## zk.sh
./bin/zookeeper-server-start.sh config/zookeeper.propertieschmod +x zk.sh
  1. broker-1broker-2broker-3文件夹下分别创建kfk.sh批处理文件;
cd broker-1
vim kfk.sh## kfk.sh
./bin/kafka-server-start.sh config/server.propertieschmod +x kfk.sh
  1. cluster文件夹下创建cluster.sh批处理文件,用于启动 kafka 集群。
vim cluster.sh
# cluster.sh
cd zookeeper
./zk.sh
cd ../broker-1
./kfk.sh
cd ../broker-2
./kfk.sh
cd ../broker-3
./kfk.sh
chmod +x cluster.sh
  1. cluster文件夹下创建cluster-clear.sh批处理文件,用于清理和重置 kafka 数据。
vim cluster-clear.sh# cluster-clear.sh 
rm -rf zookeeper-data
rm -rf broker-datachmod +x cluster-clear.sh 
  1. cluster目录下,运行./cluster.sh文件即可启动集群。
# 启动集群
./cluster# 查看是否启动成功,如果新建了data文件,说明启动成功了
ll zookeeper-data/
total 4
drwxr-xr-x 3 root root 4096 May 27 10:20 zookeeperll broker-data/
total 12
drwxr-xr-x 2 root root 4096 May 27 10:21 broker-1
drwxr-xr-x 2 root root 4096 May 27 10:21 broker-2
drwxr-xr-x 2 root root 4096 May 27 10:21 broker-3
  1. 当我们想要关闭集群时,不仅要清理 zookeeper 和 kafka 的数据文件,还有kill -9 结束 zookeeper 进程与 kakfa 进程,这需要我们手动逐一kill
ps axj | grep zookeeper
kill -9 #zookeeper的PIDps axj | grep kafka
kill -9 #3个kafka节点的PID

相关文章:

  • MySQL 索引的使用
  • 03-树2 List Leaves(浙大数据结构PTA习题)
  • C语言分支和循环(2)
  • vue路由跳转之【编程式导航与传参】
  • 万界星空科技MES系统功能介绍
  • 接口基础知识 工具使用
  • 使用 Vue 3 和 qrcode.js 开发二维码显示组件
  • Java 抽象类和接口
  • C++学习第十一天——vector的模拟实现
  • CSS-in-JS学习
  • 实验报告2-多线程并发
  • latex中对目录的处理
  • C++的算法:贪心算法
  • 单片机通信协议(1):SPI简介
  • 前端从零到一开发vscode插件并发布到插件市场
  • Druid 在有赞的实践
  • Iterator 和 for...of 循环
  • JavaScript类型识别
  • 仿天猫超市收藏抛物线动画工具库
  • 服务器从安装到部署全过程(二)
  • 关于List、List?、ListObject的区别
  • 规范化安全开发 KOA 手脚架
  • 技术攻略】php设计模式(一):简介及创建型模式
  • 利用阿里云 OSS 搭建私有 Docker 仓库
  • 聊聊sentinel的DegradeSlot
  • 前端知识点整理(待续)
  • 三分钟教你同步 Visual Studio Code 设置
  • 怎么把视频里的音乐提取出来
  • media数据库操作,可以进行增删改查,实现回收站,隐私照片功能 SharedPreferences存储地址:
  • #LLM入门|Prompt#2.3_对查询任务进行分类|意图分析_Classification
  • #微信小程序(布局、渲染层基础知识)
  • #我与Java虚拟机的故事#连载19:等我技术变强了,我会去看你的 ​
  • (7)摄像机和云台
  • (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示
  • (Python第六天)文件处理
  • (独孤九剑)--文件系统
  • (二) Windows 下 Sublime Text 3 安装离线插件 Anaconda
  • (三)SvelteKit教程:layout 文件
  • (十二)devops持续集成开发——jenkins的全局工具配置之sonar qube环境安装及配置
  • (一)u-boot-nand.bin的下载
  • (转)Linux NTP配置详解 (Network Time Protocol)
  • *setTimeout实现text输入在用户停顿时才调用事件!*
  • .bashrc在哪里,alias妙用
  • .chm格式文件如何阅读
  • .MSSQLSERVER 导入导出 命令集--堪称经典,值得借鉴!
  • .NET Core中Emit的使用
  • .NET:自动将请求参数绑定到ASPX、ASHX和MVC(菜鸟必看)
  • .net6解除文件上传限制。Multipart body length limit 16384 exceeded
  • .Net下使用 Geb.Video.FFMPEG 操作视频文件
  • @LoadBalanced 和 @RefreshScope 同时使用,负载均衡失效分析
  • [10] CUDA程序性能的提升 与 流
  • [Algorithm][动态规划][子序列问题][最长递增子序列][摆动序列]详细讲解
  • [android] 练习PopupWindow实现对话框
  • [Android]使用Retrofit进行网络请求
  • [C#]C#学习笔记-CIL和动态程序集