Shell案例之一键部署kafka
一、案例问题
(1)Kafka是用Java编写的,需要配置jdk环境变量
(2)Kafka配置文件数目多
(3)命令安装繁琐
二、案例分析:
(1)检查Java环境
(2)下载并解压kafka
(3)设置环境变量
(4)启动Zookeeper和kafka
(5)验证启动是否成功
三、流程图:
四、案例实现:
(1)首先配置Java环境:
下载地址:Apache Kafka
选择需要的版本下载到本地,上传到虚拟机(wget拉取速度慢)
通过wget下载到本地,解压到/usr/local/jdk/文件下
(2)配置环境变量
打开/etc/profile在文件最后填写如下内容
|-------------------------------------------------------------------------------|
| export JAVA_HOME=/usr/local/jdk/jdk1.8.0_271 export PATH=JAVA_HOME/bin:PATH |
当看到输入命令# which java,得到如下输出证明jdk环境配置完成
(3)编写一键部署脚本:
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [root@localhost shell]# cat 1.sh #!/bin/bash installDir="/opt/module/kafka" kafka_version="1.1.1" scala_version="2.11" #检查Java环境 if [ -z "JAVA_HOME" \] \|\| \[ ! -f "JAVA_HOME/bin/java" ]; then echo "JAVA_HOME未设置或无效,请安装JDK 1.8并设置Java环境变量再来执行此脚本" exit 1 fi #检查并创建安装目录 if [ ! -d "{installDir}" \]; then sudo mkdir -p "{installDir}" if [ ? -eq 0 \]; then echo "安装目录{installDir}已创建" else echo "请确保您有足够的权限来创建目录,请增加权限后再次执行" exit 1 fi fi #下载kafka压缩包 if [ ! -f /tmp/kafka_scala_version-kafka_version.tgz ]; then wget https://downloads.apache.org/kafka/$kafka_version/kafka_$scala_version-$kafka_version.tgz -P /tmp/ if [ ? -eq 0 \]; then echo "kafka_scala_version-kafka_version.tgz下载成功" else echo "kafka_scala_version-kafka_version.tgz下载失败,请重试或手动下载到/tmp目录下再次执行" echo "下载地址:https://archive.apache.org/dist/kafka/kafka_version/kafka_scala_version-kafka_version.tgz" exit 1 fi fi #解压kafka压缩包 tar -zxvf /tmp/kafka_scala_version-kafka_version.tgz -C $installDir if [ ? -eq 0 \]; then echo "/tmp/kafka_scala_version-kafka_version.tgz解压成功" else echo "/tmp/kafka_scala_version-kafka_version.tgz解压失败,请查看异常信息后重试" exit 1 fi #设置kafka环境变量 if \[ -z "KAFKA_HOME" ]; then echo >> ~/.bashrc echo '#KAFKA_HOME' >> ~/.bashrc echo "export KAFKA_HOME=installDir/kafka_scala_version-kafka_version" \>\> \~/.bashrc echo 'export PATH=PATH:KAFKA_HOME/bin' \>\> \~/.bashrc else echo "KAFKA_HOME已有设置:KAFKA_HOME" Fi # 备份原始配置文件 cp "installDir/kafka_2.11-1.1.1/config/server.properties" "installDir/kafka_2.11-1.1.1/config/server.properties.bak" #修改kafka配置文件 ip_addr=$(ip addr | grep 'inet ' | awk '{print 2}'\| tail -n 1 \| grep -oP '\\d+\\.\\d+\\.\\d+\\.\\d+') sed -i "s\|#listeners=PLAINTEXT://:9092\|listeners=PLAINTEXT://ip_addr:9092|" "installDir/kafka_scala_version-$kafka_version/config/server.properties" if [ $? -eq 0 ]; then echo "kafka配置文件修改成功" else echo "kafka配置文件修改失败,请查看异常信息后重试" exit 1 fi echo "kafka下载、安装、配置成功" exit 0 |
五、实现解析:
确定完jdk环境后,运行一键部署脚本
1.检查并创建安装目录:
◎检查指定的安装目录是否存在,若不存在使用mkdir -p 创建
◎若创建成功,输出提示信息,否则,提示用户增加权限后重新执行
2.下载kafka压缩包:
◎检查/tmp目录下是否已经存在kafka压缩包,如果不存在使用wget下载
◎如果下载成功,输出提示信息,否则提示用户重试或手动下载
3.解压kafka压缩包:
◎使用tar命令将kafka压缩包解压到指定的安装目录
◎如果解压成功,输出提示信息,否则,提示用户查看异常信息后重试
4.设置kafka环境变量
◎检查KAFKA_HOME环境变量是否已经设置
◎若未设置,则将kafka的安装路径添加到~/.bashrc文件中,并更新PATH变量
◎若已经设置,输出当前的KAFKA_HOME值
5.修改kafka配置文件
◎获取主机的IP地址(最后一个匹配的IP地址)
◎使用sed命令修改server.properties文件中的listeners配置,将其设置为PLAINTEXT://9092
◎如果修改成功,输出提示信息,否则,提示用户查看异常信息后重试
下面是运行过程:
6.进入安装目录:
7.启动Zookeeper和kafka
六、测试结果:
(1)通过jps命令查看Java进程
(2)查看端口已被监听,启动成功
(3)测试创建一个topic
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| #移动工作目录 [root@localhost kafka]# cd /opt/kafka/bin #创建topic [root@localhost bin]# ./kafka-topics.sh --create --zookeeper 192.168.110.153:2181 --replication-factor 1 --partitions 1 --topic topic1 Created topic "topic1". #查看topic信息 [root@localhost bin]# ./kafka-topics.sh --describe --zookeeper 192.168.110.153:2181 --topic topic1 Topic:topic1 PartitionCount:1 ReplicationFactor:1 Configs: Topic: topic1 Partition: 0 Leader: 0 Replicas: 0 Isr: 0 #启动生产者控制台 [root@localhost bin]# ./kafka-console-producer.sh --broker-list 192.168.110.153:9092 --topic1 >test >123456 #启动消费者控制台(新开一个窗口) [root@localhost bin]# ./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic yahei --from-beginning test 123456 |