部署环境:
1.通外网
2.centos7.9
3.运行过程中所有所需的安装包均使用wget下载
#!/bin/bash
#function:Elasticsearch+Logstash+Kibana一键部署
#author:Mr.xie 20230920
##########################################
if
[ "$USER" != "root" ]
then
echo "错误:非root用户,权限不足!"
exit 0
fi
###############防火墙及SElinux############
systemctl stop firewalld && systemctl disable firewalld && echo "防火墙已经关闭"
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config && echo "关闭selinux"
###############测试外网是否通常############
ping -c 3 www.baidu.com
if
[ $? = 0 ]
then
echo "网络正常"
else
echo "网络异常"
exit 1
fi
################### Elasticsearch ###################
El_path="/opt/elasticsearch-7.12.0-x86_64.rpm"
# 检查文件是否存在
if [ -e "$El_path" ]; then
echo "文件 $El_path 存在,正在解压缩..."
rpm -i /opt/elasticsearch-7.12.0-x86_64.rpm
else
echo "文件 $El_path 不存在。正在下载..."
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.12.0-x86_64.rpm -P /opt
if
[ $? = 0 ]
then
echo "文件已下载。正在解压缩..."
rpm -i /opt/elasticsearch-7.12.0-x86_64.rpm
else
echo "Elasticsearch文件下载失败,请手动上传至 /opt"
exit 1
fi
fi
# 设置用户登入shell
chsh -s /bin/bash elasticsearch
# 查看es的安装路径
rpm -ql elasticsearch &> /dev/null
############ 修改ES数据和日志存储路径 ############
# 创建目录,用于存储elasticsearch数据
mkdir -p /data/elasticsearch/datadir
# 修改目录权限
chown -R elasticsearch:elasticsearch /data/elasticsearch/datadir
# 创建日志目录
mkdir -p /data/elasticsearch/logdir
# 修改目录权限
chown -R elasticsearch:elasticsearch /data/elasticsearch/logdir
############ 修改ES配置文件 ############
cp /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.bak
sed -i '17s/.*/cluster.name: my-application/' /etc/elasticsearch/elasticsearch.yml
sed -i '23s/.*/node.name: node-1/' /etc/elasticsearch/elasticsearch.yml
sed -i 's/^path.data:.*/path.data: \/data\/elasticsearch\/datadir\//' /etc/elasticsearch/elasticsearch.yml
sed -i 's/^path.logs:.*/path.logs: \/data\/elasticsearch\/logdir\//' /etc/elasticsearch/elasticsearch.yml
sed -i '43s/.*/bootstrap.memory_lock: false/' /etc/elasticsearch/elasticsearch.yml
sed -i '56s/.*/network.host: 0.0.0.0/' /etc/elasticsearch/elasticsearch.yml
sed -i '61s/.*/http.port: 9200/' /etc/elasticsearch/elasticsearch.yml
sed -i '70s/.*/discovery.seed_hosts: ["127.0.0.1"]/' /etc/elasticsearch/elasticsearch.yml
sed -i '74s/.*/cluster.initial_master_nodes: ["node-1"]/' /etc/elasticsearch/elasticsearch.yml
# 启动服服务
systemctl start elasticsearch
if
[ $? = 0 ]
then
curl http://localhost:9200
echo -e "\n\033[32m-----------------------------------------------\033[0m"
echo -e "\033[32melasticsearch启动成功\033[0m"
else
echo -e "\033[32melasticsearch启动失败\033[0m"
exit 1
fi
################### Logstash ###################
Lo_path="/opt/logstash-7.12.0-x86_64.rpm"
# 检查文件是否存在
if [ -e "$Lo_path" ]; then
echo "文件 $Lo_path 存在,正在解压缩..."
rpm -i /opt/logstash-7.12.0-x86_64.rpm
else
echo "文件 $Lo_path 不存在。正在下载..."
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.12.0-x86_64.rpm -P /opt
if
[ $? = 0 ]
then
echo "文件已下载。正在解压缩..."
rpm -i /opt/logstash-7.12.0-x86_64.rpm
else
echo "Logstash文件下载失败,请手动上传至 /opt"
exit 1
fi
fi
############ 修改logstash数据和日志存储路径 ############
# 创建目录,用于存储logstash数据
mkdir -p /data/logstash/datadir
# 修改目录权限
chown -R logstash:logstash /data/logstash/datadir
# 创建日志目录
mkdir -p /data/logstash/logdir
# 修改目录权限
chown -R logstash:logstash /data/logstash/logdir
############ 修改logstash配置文件 ############
cp /etc/logstash/logstash.yml /etc/logstash/logstash.yml.bak
sed -i '28s#path.data: /var/lib/logstash#path.data: /data/logstash/datadir#' /etc/logstash/logstash.yml
sed -i '73s#.*#path.config: /etc/logstash/conf.d#' /etc/logstash/logstash.yml
sed -i '249s#path.logs: /var/log/logstash#path.logs: /data/logstash/logdir#' /etc/logstash/logstash.yml
# 启动服服务
systemctl start logstash
if
[ $? = 0 ]
then
ln -s /usr/share/logstash/bin/logstash.lib.sh /bin/logstash
echo "Jason" | logstash -e 'input { stdin { } } output { stdout {} }'
echo -e "\n\033[32m-----------------------------------------------\033[0m"
echo -e "\033[32mlogstash启动成功\033[0m"
else
echo -e "\033[32mlogstash启动失败\033[0m"
exit 1
fi
################### Kibana ###################
Ki_path="/opt/kibana-7.12.0-x86_64.rpm"
# 检查文件是否存在
if [ -e "$Ki_path" ]; then
echo "文件 $Ki_path 存在,正在解压缩..."
rpm -i /opt/kibana-7.12.0-x86_64.rpm
else
echo "文件 $Ki_path 不存在。正在下载..."
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.12.0-x86_64.rpm -P /opt
if
[ $? = 0 ]
then
echo "文件已下载。正在解压缩..."
rpm -i /opt/kibana-7.12.0-x86_64.rpm
else
echo "Kibana文件下载失败,请手动上传至 /opt"
exit 1
fi
fi
############ 修改Ki配置文件 ############
sed -i '2s/^#//' /etc/kibana/kibana.yml
sed -i '7s/.*/server.host: "0.0.0.0"/' /etc/kibana/kibana.yml
sed -i '32s/^#//' /etc/kibana/kibana.yml
sed -i '36s/^#//' /etc/kibana/kibana.yml
sed -i '111s/.*/i18n.locale: "zh-CN"/' /etc/kibana/kibana.yml
# 启动服服务
ipadd=$(ip a | grep "inet "|grep "ens33" | awk '{print $2}' | awk -F / '{print $1}')
systemctl start kibana
if
[ $? = 0 ]
then
#curl http://localhost:5601
echo -e "\n\033[32m-----------------------------------------------\033[0m"
echo -e "\033[32mkibana启动成功\033[0m"
echo -e "\033[32m游览器中输入 http://$ipadd:5601/ 进行查看\033[0m"
else
echo -e "\033[32mkibana启动失败\033[0m"
exit 1
fi