ELK 项目
项目需求:
环境准备
三台机器:
OS : Centos 7.4
主机名:elk1.wpic.com elk2.wpic.com elk3.wpic.com
ELK 介绍:
Elasticsearch
Elasticsearch介绍
ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是第二流行的企业搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
安装 Elasticsearch
软件版本:Elasticsearch 5.6
导入 PGP Key
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
添加yum 仓库
vim /etc/yum.repos.d/elasticsearch.repo [elasticsearch-5.x] name=Elasticsearch repository for 5.x packages baseurl=https://artifacts.elastic.co/packages/5.x/yum gpgcheck=1 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch enabled=1 autorefresh=1 type=rpm-md
安装
yun install java -y yum install elasticsearch -y
配置
主机:elk1.wpic.com
[root@elk1 elasticsearch]# grep -v "^#" /etc/elasticsearch/elasticsearch.yml | grep -v "^$" cluster.name: wpic node.name: ELK-1 network.host: 192.168.8.210 http.port: 9200 discovery.zen.ping.unicast.hosts: ["192.168.8.203", "192.168.8.210","192.168.8.214"] http.cors.enabled: true http.cors.allow-origin: "*"
主机:elk2.wpic.com
[root@elk2 elasticsearch]# grep -v "^#" /etc/elasticsearch/elasticsearch.yml | grep -v "^$" cluster.name: wpic node.name: ELK-2 network.host: 192.168.8.203 http.port: 9200 discovery.zen.ping.unicast.hosts: ["192.168.8.203", "192.168.8.210","192.168.8.214"] http.cors.enabled: true http.cors.allow-origin: "*"
主机:elk3.wpic.com
[root@saltmaster elasticsearch]# grep -v "^#" /etc/elasticsearch/elasticsearch.yml | grep -v "^$" cluster.name: wpic node.name: ELK-3 network.host: 192.168.8.214 http.port: 9200 discovery.zen.ping.unicast.hosts: ["192.168.8.203", "192.168.8.210","192.168.8.214"] http.cors.enabled: true http.cors.allow-origin: "*"
所有主机执行如下命令启动和设置开机启动 elasticsearch
systemctl start elasticsearch systemctl enable elasticsearch
安装elasticsearch-head 插件
Head 插件是展示elasticsearch cluster 的前端页面,可以查看集群状态,执行查询操作,原理是调用的elasticsearch 的API.
github 地址:https://github.com/mobz/elasticsearch-head
安装 (因为此插件是node.js 编写,所以首先要安装node.js ,详情可看官网,此处省略)
git clone git://github.com/mobz/elasticsearch-head.git cd elasticsearch-head npm install npm run start open http://localhost:9100/ #localhost 替换成本机IP.
连接到 elasticsearch
配置 CROS (跨域资源共享)
vim /etc/elasticsearch/elasticsearch.yml # 追加以下配置 http.cors.enabled: true http.cors.allow-origin: "*"
安装kibana
Kibana是一个开源的分析和可视化平台,使用Kibana可以搜索,查看,存储Elasticsearch数据,可以轻松地执行先进的数据分析和制作图表,表格和地图使数据可视化. 它是nodejs 程序编写的. 可以配置在一台主机,也可以三台主机都配置,实现负载均衡.
安装
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch vim /etc/yum.repos.d/kibana.repo [kibana-5.x] name=Kibana repository for 5.x packages baseurl=https://artifacts.elastic.co/packages/5.x/yum gpgcheck=1 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch enabled=1 autorefresh=1 type=rpm-md yum install kibana
配置kibana
vim /etc/kibana/kibana.yml server.port: 5601 server.host: "192.168.8.210" # 安装 kibana 的服务器 elasticsearch.url: "http://192.168.8.210:9200" # 安装 elasticsearch 的服务器,任何一台都可以。
启动kibana 并测试访问
systemctl start kibana systemctl enable kibana http://server ip:5601 # 输入kibana 服务器ip 测试
使用 ELK 监控分析nginx 日志
使用 filebeat 监控 niginx 并将数据传给 elasticsearch
Filebeat监视日志目录或特定的日志文件,并将文件传送给 elasticsearch or logstash or redis.
安装filebeat (在web服务器端)
curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.6.6-x86_64.rpm #下载rpm包 yum install filebeate -y
filebeat 模块之 nginx 模块详解
安装:
所有 elasticsearch 节点上安装如下插件,然后重启所有 elasticsearch 节点服务
/usr/share/elasticsearch/bin/elasticsearch-plugin install ingest-user-agent /usr/share/elasticsearch/bin/elasticsearch-plugin install ingest-geoip
配置 filebeat 使用 nginx 模块,然后重启 filebeat.
mv /etc/filebeat/filebeat.yml /etc/filebeat/filebeat.yml.bak
cp /etc/filebeat/filebeat.full.yml /etc/filebeat/filebeat.yml
vim /etc/filebeat/filebeat.yml
- module: nginx # 打开nginx 模块
# Access logs
access:
enabled: true
var.paths: ["/var/log/nginx/access.log"]
error:
enabled: true
var.paths: ["/var/log/nginx/error.log"]
output.elasticsearch:
hosts: ["192.168.8.210:9200"]
index: "filebeat-%{+yyyy.MM}"
在 web 服务器端 导入 索引,导入dashboard.
/usr/share/filebeat/scripts/import_dashboards -only-index /usr/share/filebeat/scripts/import_dashboards -only-dashboards
测试,访问kibana


浙公网安备 33010602011771号