ELK 搭建
日志分析工具ELK 搭建
一、简介:
ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana
1、Elasticsearch是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。
它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
2、Logstash 主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式。
一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。
3、Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助汇总、分析和搜索重要数据日志。
4、FileBeat,它是一个轻量级的日志收集处理工具(Agent),Filebeat占用资源少,适合于在各个服务器上搜集日志后传输给Logstash,官方也推荐此工具。
二、环境准备
1、本地DNS解析
cat /etc/hosts
192.168.100.100 node1.stu.com
192.168.100.10 node2.stu.com
2、修改文件描述符
/etc/systemd/system.conf
/etc/systemd/user.conf
DefaultLimitNOFILE=65535
DefaultLimitNPROC=65535
3、server和agent服务器时间同步
yum install ntpdate -y
*/5 * * * * /usr/sbin/ntpdate ntp.aliyun.com
三、ELASTICSEARCH
1、安装:
yum install -y java-1.8.0-openjdk
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.0.rpm
rpm -ivh elasticsearch-6.4.0.rpm
2、配置
vim /etc/elasticsearch/elasticsearch.yml
cluster.name: elk-cluster
node.name: node1
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
LimitMEMLOCK=infinity
network.host: 192.168.100.100
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.168.100.100", "192.168.100.10"]
bootstrap.memory_lock: true 服务启动的时候锁定足够的内存,防止数据写入swap
LimitMEMLOCK=infinity(无限)
与集群中其他节点的传输端口为9300,接受HTTP请求的端口为9200
vim /etc/elasticsearch/jvm.options
-Xms1g
-Xmx1g
systemctl start elasticsearch
systemctl enable elasticsearch
3、测试
http://192.168.100.100:9200/
四、logstash
1、安装:
yum install java-1.8.0-openjdk.x86_64 -y
wget https://artifacts.elastic.co/downloads/logstash/logstash-6.4.0.rpm
rpm -ivh logstash-6.4.0.rpm
2、配置
搜集系统内核日志:chmod 644 /var/log/messages
vim /etc/logstash/conf.d/syslog.conf
input {
file {
path => "/var/log/messages"
type => "systemlog"
start_position => "beginning"
stat_interval => "2"
}
}
output {
elasticsearch {
hosts => ["192.168.100.100:9200"]
index => "logstash-systemlog-%{+YYYY.MM.dd}"
}
}
检查配置文件语法是否错误:/usr/share/logstash/bin/logstash -tf /etc/logstash/conf.d/syslog.conf
(可以先不执行这个命令)
systemctl start logstash
systemctl enable logstash
3、验证:curl -XGET 'localhost:9600/?pretty'
9600端口:API来检索有关Logstash的运行时指标
五、kibana
1、安装:
rpm -ivh kibana-6.4.0-x86_64.rpm
2、配置
vim /etc/kibana/kibana.yml
server.port: 5601
server.host: "192.168.100.100"
elasticsearch.url: "http://192.168.100.100:9200"
systemctl start kibana
systemctl enable kibana
3、验证
http://192.168.100.100:5601/status
4、nginx+kibana
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name kibana.stu.com;
auth_basic "kibana auth password";
auth_basic_user_file /etc/nginx/kibana.auth;
#root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://kibana;
}
}
upstream kibana {
server 192.168.100.1:5601 weight=1 max_fails=2 fail_timeout=2;
}
}