ELK是elastic 公司旗下三款产品ElasticSearch 、Logstash 、Kibana的首字母组合,主要用于日志收集、分析与报表展示。
ELK Stack包含:ElasticSearch、Logstash、Kibana。(ELK Stack 5.0版本以后-->Elastic Stack == ELK Stack+Beats)
ElasticSearch是一个搜索引擎,用来搜索、分析、存储日志。它是分布式的,也就是说可以横向扩容,可以自动发现,索引自动分片,总之很强大。
Logstash用来采集日志,把日志解析为Json格式交给ElasticSearch。
Kibana是一个数据可视化组件,把处理后的结果通过WEB界面展示。
Beats是一个轻量级日志采集器,其实Beats家族有5个成员
1.filebeat:进行文件和目录采集,可用于收集日志数据。
2.heartbeat:系统间连通性检测,可收集icmp, tcp, http等系统的连通性情况。
3.Winlogbeat:专门针对windows的事务日志的数据采集。
4.packetbeat:通过网络抓包、协议分析,收集网络相关数据。
5.metricbeat:进行指标采集,主要用于监控系统和软件的性能。(系统、中间件等)
我们首先下载elasticsearch,logstash和kibana,国内推荐下载地址:https://elasticsearch.cn/download/
首先关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config setenforce 0
关闭防火墙并停用开机启动
systemctl stop firewalld.service systemctl disable firewalld.service
更换阿里云yum源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo_bak wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
更新yum缓存
yum makecache
环境优化
修改内核/etc/sysctl.conf
vi /etc/sysctl.conf vm.max_map_count=262144
保存退出,使修改的内核生效
sysctl -p
修改文件/etc/security/limits.conf
vi /etc/security/limits.conf * hard nofile 65536 * soft nofile 65536 * soft nproc 65536 * hard nproc 65536
保存退出
安装jdk
yum install -y java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64
创建ELK存放数据和日志的目录
mkdir -p /data/elk
安装ELK各个组件
yum localinstall elasticsearch-7.4.0-x86_64.rpm -y yum localinstall logstash-7.4.0.rpm -y yum localinstall kibana-7.4.0-x86_64.rpm -y
创建ELK各个组件日志和数据存放目录
mkdir -p /data/elk/{elasticsearch,logstash,kibana}
赋权
chown -R elasticsearch:elasticsearch /data/elk/elasticsearch chown -R logstash:logstash /data/elk/logstash chown -R kibana:kibana /data/elk/kibana
然后需要修改elasticseach配置文件
vi /etc/elasticsearch/elasticsearch.yml cluster.name: elk node.name: elk-1 path.data: /data/elk/elasticsearch/data path.logs: /data/elk/elasticsearch/logs network.host: 0.0.0.0 http.port: 9200 discovery.seed_hosts: ["172.26.61.61"] cluster.initial_master_nodes: ["elk-1"]
保存退出
然后修改配置logstash文件
vi /etc/logstash/logstash.yml path.data: /data/elk/logstash/data path.logs: /data/elk/logstash/logs
保存退出
ln -s /usr/share/logstash/bin/logstash /usr/local/bin/
新建一个配置文件用于收集系统日志
vi /etc/logstash/conf.d/logstash.conf input { beats { port => 5044 codec => plain { charset => "UTF-8" } } } output { elasticsearch { hosts => "172.26.61.61:9200" manage_template => false index => "%{[@metadata][beat]}-%{+YYYY.MM.dd}" #document_type => "%{[@metadata][type]}" } }
修改kibana配置文件
vi /etc/kibana/kibana.yml server.port: 5601 server.host: "0.0.0.0" elasticsearch.hosts: ["http://172.26.61.61:9200"] kibana.index: ".kibana" logging.dest: /data/elk/kibana/kibana.log i18n.locale: "zh-CN"
启动ELK各个组件服务,并添加到开机启动
systemctl daemon-reload systemctl start elasticsearch systemctl enable elasticsearch systemctl start logstash systemctl enable logstash systemctl start kibana systemctl enable kibana
上述启动好了以后我们测试elasticsearch状态
curl 'http://172.26.61.61:9200/_cluster/state/nodes?pretty' { "cluster_name" : "elk", "cluster_uuid" : "oVtHzuOrRCGzvItoCmDQVA", "nodes" : { "mf4AQKAZSMy4nB94nIUNvQ" : { "name" : "elk-1", "ephemeral_id" : "J2NxanUkQUGsN76RTps7Bw", "transport_address" : "172.26.61.61:9300", "attributes" : { "ml.machine_memory" : "3954323456", "xpack.installed" : "true", "ml.max_open_jobs" : "20" } } } }
查看elasticsearch的master
curl 'http://172.26.61.61:9200/_cluster/state/master_node?pretty' { "cluster_name" : "elk", "cluster_uuid" : "oVtHzuOrRCGzvItoCmDQVA", "master_node" : "mf4AQKAZSMy4nB94nIUNvQ" } curl 'http://172.26.61.61:9200/_cat/master?v' id host ip node mf4AQKAZSMy4nB94nIUNvQ 172.26.61.61 172.26.61.61 elk-1
查看健康状态
curl 'http://172.26.61.61:9200/_cat/health?v' epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 1572078718 08:31:58 elk green 1 1 3 3 0 0 0 0 - 100.0% curl 'http://172.26.61.61:9200/_cluster/health?pretty' { "cluster_name" : "elk", "status" : "green", "timed_out" : false, "number_of_nodes" : 1, "number_of_data_nodes" : 1, "active_primary_shards" : 3, "active_shards" : 3, "relocating_shards" : 0, "initializing_shards" : 0, "unassigned_shards" : 0, "delayed_unassigned_shards" : 0, "number_of_pending_tasks" : 0, "number_of_in_flight_fetch" : 0, "task_max_waiting_in_queue_millis" : 0, "active_shards_percent_as_number" : 100.0 }
上述ELK安装基本没有问题了,浏览器访问http://ip:5601,即可访问kibana页面