需求背景
• 业务发展越来越庞大,服务器越来越多
• 各种访问日志、应用日志、错误日志量越来越多
• 开发人员排查问题,需要到服务器上查日志,不方便
• 运营人员需要一些数据,需要我们运维到服务器上分析日志
ELK介绍
• 中文指南https://www.gitbook.com/book/chenryn/elk-stack-guide-cn/details
• ELK Stack (5.0版本之后)à Elastic Stack == (ELK Stack + Beats)
• ELK Stack包含:ElasticSearch、Logstash、Kibana
• ElasticSearch是一个搜索引擎,用来搜索、分析、存储日志。它是分布式的,也就是说可以横向扩容,可以自动发现,索引自动分片,总之很强大。文档https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
• Logstash用来采集日志,把日志解析为json格式交给ElasticSearch。
• Kibana是一个数据可视化组件,把处理后的结果通过web界面展示
• Beats在这里是一个轻量级日志采集器,其实Beats家族有5个成员
• 早期的ELK架构中使用Logstash收集、解析日志,但是Logstash对内存、cpu、io等资源消耗比较高。相比 Logstash,Beats所占系统的CPU和内存几乎可以忽略不计
• x-pack对Elastic Stack提供了安全、警报、监控、报表、图表于一身的扩展包,是收费的
ELK架构
ELK安装 – 准备工作
• 准备3台机器130,132,133
• 角色划分:
• 3台全部安装elasticsearch(后续简称es) ,1主节点130,2数据节点132,133
• es主130上安装kibana
• 1台es数据节点132上安装logstash 3台机器全部安装jdk8(openjdk即可)
• yum install -y java-1.8.0-openjdk
ELK安装 – 安装es
•官方文档 https://www.elastic.co/guide/en/elastic-stack/current/installing-elastic-stack.html
• 以下操作3台机器上都要执行
• rpm –import https://artifacts.elastic.co/GPG-KEY-elasticsearch
• vim /etc/yum.repos.d/elastic.repo //加入如下内容
[elasticsearch-6.x]
name=Elasticsearch repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
• yum install -y elasticsearch //也可以直接下载rpm文件,然后安装
• wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.0.rpm
• rpm -ivh elasticsearch-6.0.0.rpm
ELK安装 – 配置es
• elasticsearch配置文件/etc/elasticsearch和/etc/sysconfig/elasticsearch
• 参考https://www.elastic.co/guide/en/elasticsearch/reference/6.0/rpm.html
• 在130上编辑配置文件vi /etc/elasticsearch/elasticsearch.yml//增加或更改
• cluster.name: aminglinux
• node.master: true//意思是该节点为主节点
• node.data: false
• network.host: 0.0.0.0
• discovery.zen.ping.unicast.hosts: [“192.168.133.130”, “192.168.133.132”, “192.168.133.133”]
• 在132和133上同样编辑配置文件vi /etc/elasticsearch/elasticsearch.yml//增加或更改
• cluster.name: aminglinux
• node.master: false
• node.data: true
• network.host: 0.0.0.0
• discovery.zen.ping.unicast.hosts: [“192.168.133.130”, “192.168.133.132”, “192.168.133.133”]
ELK安装 – 安装x-pack(可省略)
• 3台机器上都要执行
• cd /usr/share/elasticsearch/bin/ (可省略)
• ./elasticsearch-plugin install x-pack //如果速度慢,就下载x-pack压缩包(可省略)
• cd /tmp/; wget https://artifacts.elastic.co/downloads/packs/x-pack/x-pack-6.0.0.zip (可省略)
• ./elasticsearch-plugin install file:///tmp/x-pack-6.0.0.zip (可省略)
• 启动elasticsearch服务
• systemctl enable elasticsearch.service
• systemctl start elasticsearch.service
• 以下操作只需要在130上执行
• 安装x-pack后就可以为内置用户设置密码了,如下
• /usr/share/elasticsearch/bin/x-pack/setup-passwords interactive (可省略)
• curl localhost:9200 -u elastic //输入密码,可以查看到输出信息(可省略)
ELK安装 – curl查看es
• 130上执行
• curl ‘localhost:9200/_cluster/health?pretty’ 健康检查
• curl ‘localhost:9200/_cluster/state?pretty’ 集群详细信息
• 参考 http://zhaoyanblog.com/archives/732.html
ELK安装 – 安装kibana
• 以下在130上执行
• 前面已经配置过yum源,这里就不用再配置了
• yum install -y kibana
• 若速度太慢,可以直接下载rpm包
• wget https://artifacts.elastic.co/downloads/kibana/kibana-6.0.0-x86_64.rpm
• rpm -ivh kibana-6.0.0-x86_64.rpm
• kibana同样也需要安装x-pack(可省略)
• 安装方法同elasticsearch的x-pack
• cd /usr/share/kibana/bin (可省略)
• ./kibana-plugin install x-pack //如果这样安装比较慢,也可以下载zip文件(可省略)
• wget https://artifacts.elastic.co/downloads/packs/x-pack/x-pack-6.0.0.zip//这个文件和前面下载的那个其实是一个(可省略)
• ./kibana-plugin install file:///tmp/x-pack-6.0.0.zip (可省略)
•
• 以下在130上执行
• vim /etc/kibana/kibana.conf //增加
server.host: 0.0.0.0
elasticsearch.url: “http://192.168.133.130:9200”
logging.dest: /var/log/kibana.log
• touch /var/log/kibana.log; chmod 777 /var/log/kibana.log
• systemctl restart kibana
• 浏览器里访问http://192.168.133.130:5601/
• 用户名elastic,密码为之前你设置过的密码(如果未安装x-pack,不需要用户名密码)
• 若无法输入用户名密码,查日志/var/log/kibana.log
• 出现错误 Status changed from uninitialized to red - Elasticsearch is still initializing the kibana index.
• 解决办法:curl -XDELETE http://192.168.133.130:9200/.kibana -uelastic
ELK安装 – 安装logstash
• 以下在132上执行
• logstash目前不支持java9
• 直接yum安装(配置源同前面es的源)
• yum install -y logstash //如果慢,就下载rpm包
• wget https://artifacts.elastic.co/downloads/logstash/logstash-6.0.0.rpm
• cd /usr/share/logstash/bin/(可省略)
• ./logstash-plugin install file:///tmp/x-pack-6.0.0.zip (可省略)
• systemctl enable logstash
• systemctl start logstash
• logstash需要先安装java8,目前不支持java9
• 直接yum安装
• yum install -y logstash //如果慢,就下载rpm包
• wget https://artifacts.elastic.co/downloads/logstash/logstash-6.0.0.rpm
• rpm -ivh logstash-6.0.0.rpm
• logstash也需要安装x-pack(可省略)
• cd /usr/share/logstash/bin/ (可省略)
• ./logstash-plugin install file:///tmp/x-pack-6.0.0.zip (可省略)
logstash收集syslog日志
• 以下在132上操作
• 编辑配置文件 vi /etc/logstash/conf.d/syslog.conf//加入如下内容
input {
syslog {
type => "system-syslog"
port => 10514
}
}
output {
stdout {
codec => rubydebug
}
}
• 检测配置文件是否有错
• cd /usr/share/logstash/bin
• ./logstash –path.settings /etc/logstash/ -f /etc/logstash/conf.d/syslog.conf –config.test_and_exit
• 以下在132上操作
• 前台形式启动logstash
• ./logstash –path.settings /etc/logstash/ -f /etc/logstash/conf.d/syslog.conf//这样可以在屏幕上查看到日志输出,不能敲命令
• 再开一个终端
• 检测是否开启10514端口:netstat -lnp |grep 10514
• vi /etc/rsyslog.conf//在#### RULES下面增加一行
• . @@127.0.0.1:10514
• systemctl restart rsyslog
• 从130ssh到132上,可以在logstash前台的终端上看到ssh登录的相关日志
• 结束logstash,在前台的那个终端上按ctrl c
• 以下在132上操作
• 后台形式启动logstash
• 编辑配置文件 vi /etc/logstash/conf.d/syslog.conf//配置文件内容改为如下
input {
syslog {
type => "system-syslog"
port => 10514
}
}
output {
elasticsearch {
hosts => ["192.168.130.132:9200"]
index => "system-syslog-%{+YYYY.MM}"
}
}
• systemctl start logstash //启动需要一些时间,启动完成后,可以看到9600端口和10514端口已被监听
• 130上执行curl ‘localhost:9200/_cat/indices?v’ 可以获取索引信息
• curl -XGET ‘localhost:9200/indexname?pretty’ 可以获指定索引详细信息
• curl -XDELETE ‘localhost:9200/logstash-xxx-*’ 可以删除指定索引
• 浏览器访问192.168.132.130:5601,到kibana配置索引
• 左侧点击“Managerment”-> “Index Patterns”-> “Create Index Pattern”
• Index pattern这里需要根据前面curl查询到的索引名字来写,否则下面的按钮是无法点击的
logstash收集nginx日志
• 132上 编辑配置文件 vi /etc/logstash/conf.d/nginx.conf//加入如下内容
input {
file {
path => "/tmp/elk_access.log"
start_position => "beginning"
type => "nginx"
}
}
filter {
grok {
match => { "message" => "%{IPORHOST:http_host} %{IPORHOST:clientip} - %{USERNAME:remote_user} \[%{HTTPDATE:timestamp}\] \"(?:%{WORD:http_verb} %{NOTSPACE:http_request}(?: HTTP/%{NUMBER:http_version})?|%{DATA:raw_http_request})\" %{NUMBER:response} (?:%{NUMBER:bytes_read}|-) %{QS:referrer} %{QS:agent} %{QS:xforwardedfor} %{NUMBER:request_time:float}"}
}
geoip {
source => "clientip"
}
}
output {
stdout { codec => rubydebug }
elasticsearch {
hosts => ["192.168.133.132:9200"]
index => “nginx-test-%{+YYYY.MM.dd}”
}
}
• 以下在132上操作
• 检测配置文件是否有错
• cd /usr/share/logstash/bin
• ./logstash –path.settings /etc/logstash/ -f /etc/logstash/conf.d/nginx.conf –config.test_and_exit
• yum install -y nginx
• vi /etc/nginx/conf.d/elk.conf//写入如下内容
server {
listen 80;
server_name elk.aming.com;
location / {
proxy_pass http://192.168.133.130:5601;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
access_log /tmp/elk_access.log main2;
}
• 以下在132上操作
• vim /etc/nginx/nginx.conf//增加如下内容
log_format main2 ‘ httphost h t t p h o s t remote_addr - remoteuser[ r e m o t e u s e r [ time_local] “$request” ‘
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$upstream_addr" $request_time';
• nginx -t
• systemctl start nginx
• 绑定hosts 192.168.133.132 elk.aming.com
• 浏览器访问,检查是否有日志产生
• systemctl restart logstash
• 130上curl ‘localhost:9200/_cat/indices?v’
• 检查是否有nginx-test开头的索引生成
• 如果有,才能到kibana里去配置该索引
• 左侧点击“Managerment”-> “Index Patterns”-> “Create Index Pattern”
• Index pattern这里写nginx-test-*
• 之后点击左侧的Discover
使用Beats采集日志
• https://www.elastic.co/cn/products/beats
• filebeat metricbeat packetbeat winlogbeat auditbeat heartbeat
• 可扩展,支持自定义构建
• 在133上执行
• wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.0.0-x86_64.rpm
• rpm -ivh filebeat-6.0.0-x86_64.rpm
• 首先编辑配置文件
• vim /etc/filebeat/filebeat.yml //增加或者更改
filebeat.prospectors:
type: log
paths:
- /var/log/messages
output.console:
enable: true
• /usr/share/filebeat/bin/filebeat -c /etc/filebeat/filebeat.yml //可以在屏幕上看到对应的日志信息
• 再编辑配置文件
• vim /etc/filebeat/filebeat.yml //增加或者更改
filebeat.prospectors:
input_type: log
paths:
- /var/log/messages
output.elasticsearch:
hosts: [“192.168.133.130:9200”]
• systemctl start filebeat
扩展部分
• x-pack 收费,免费 http://www.jianshu.com/p/a49d93212eca
https://www.elastic.co/subscriptions
• Elastic stack演进 http://70data.net/1505.html
• 基于kafka和elasticsearch,linkedin构建实时日志分析系统 http://t.cn/RYffDoE
• 使用redis http://blog.lishiming.net/?p=463
• ELK+Filebeat+Kafka+ZooKeeper 构建海量日志分析平台 https://www.cnblogs.com/delgyd/p/elk.html