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

news/2024/9/25 23:19:03/

文章目录

  • 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

http://www.ppmy.cn/news/1463512.html

相关文章

操作教程|通过DataEase开源BI工具对接金山多维表格

前言 金山多维表格是企业数据处理分析经常会用到的一款数据表格工具,它能够将企业数据以统一的列格式整齐地汇总至其中。DataEase开源数据可视化分析工具可以与金山多维表格对接,方便企业更加快捷地以金山多维表格为数据源,制作出可以实时更…

酷开科技大屏营销,多元需求唤醒“客厅经济”

随着科技的发展和消费者习惯的变化,OTT大屏营销正逐渐成为客厅经济的新风向。OTT不仅改变了人们获取信息和娱乐的方式,也为品牌营销提供了新的机遇和挑战,OTT大屏营销已经成为客厅经济的重要组成部分。酷开科技通过其自主研发的智能电视操作系…

用队列实现栈 用栈实现队列 设计循环队列

用队列实现栈 思路 栈的特点:后进先出 队列的特点:先进先出 使用两个队列实现栈: 我们可以使用两个队列,一个队列为:空队列,一个队列为:非空队列 当我们要出队列时: 将 size - …

【Python百日进阶-Web开发-Peewee】Day294 - 查询示例(三)修改数据

文章目录 14.5 修改数据 Modifying Data14.5.1 向表中插入一些数据 Insert some data into a table14.5.2 向表中插入多行数据 Insert multiple rows of data into a table14.5.3 将计算数据插入表中 Insert calculated data into a table14.5.4 更新一些现有数据 Update some …

30.哀家要长脑子了!---栈与队列

1.388. 文件的最长绝对路径 - 力扣(LeetCode) 其实看懂了就还好 用一个栈来保存所遍历过最大的文件的绝对路径的长度,栈顶元素是文件的长度,栈中元素的个数是该文件目录的深度,非栈顶元素就是当时目录的长度 检查此…

实用篇| huggingface网络不通

之前文章《Transformer原理》中介绍过,Transformers 是由 Hugging Face 开发的一个包,支持加载目前绝大部分的预训练模型。随着 BERT、GPT 等大规模语言模型的兴起,越来越多的公司和研究者采用 Transformers 库来构建应用。 Hugging Face是一家美国公司…

three.js官方案例webgl_loader_fbx.html学习

目录 1.1 添加库引入 1.2 添加必要的组件scene,camera,webrenderer等 1.3 模型加载 1.4 半球光 1.5 动画 1.6 换个自己的fbx模型 1.7 fbx模型和fbx动画关联 1.7 html脚本全部如下 1.8 fbx.js全部脚本如下 1.1 添加库引入 import * as THREE from three; import Stats …

软考结束。有什么要说的

1. 竟然是机试,出乎我意料。是 考试机构觉得笔试成本高了么。这次的考试是机试,相比以往有所不一样。感言是不是以后都会在固定地点考试也说不准。 2. 遇到年轻人。 这次旁边的一个女同学第一次参加,还像我询问了一些关于软考的事。我是有…