ELFK各功能
Elasticsearch: 数据库,存储数据 java语言
logstash: 日志收集,过滤数据 java语言
kibana: 分析,过滤,展示 java语言
filebeat: 收集日志,传输到ES或logstash go语言
Elasticsearch入门:
简单介绍:
开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful 风格接口,多数据源,自动搜索负载等。
官方网站:
https://www.elastic.co
中文社区:
https://elasticsearch.cn
下载地址:
https://mirrors.tuna.tsinghua.edu.cn/elasticstack/6.x/yum/6.6.0/
安装及配置elasticsearch
1.安装elasticsearch:
前提: jdk环境
复制elasticsearch安装包到虚拟机
2.elasticsearch目录和文件:
/etc/elasticsearch/elasticsearch.yml #配置文件
/etc/elasticsearch/jvm.options #java虚拟机
/etc/init.d/elasticsearch #服务启动脚本
/etc/sysconfig/elasticsearch #elasticsearch服务变量
/usr/lib/sysctl.d/elasticsearch.conf #设置elasticsearch用户使用的内存大小
/usr/lib/systemd/system/elasticsearch.service #添加系统服务文件
/var/log/elasticsearch/elasticsearch.log #日志文件路径
3.修改配置文件
vim /etc/elasticsearch/elasticsearch.yml
node.name: node-1 #群集中本机节点名
path.data: /data/elasticsearch #数据目录
path.logs: /var/log/elasticsearch #日志目录
bootstrap.memory_lock: true #锁定内存,需要和/etc/elasticsearch/jvm.options关联
network.host: 192.168.1.102,127.0.0.1 #监听的ip地址
http.port: 9200 #端口号
4.分配锁定内存
vim /etc/elasticsearch/jvm.options
-Xms1g #分配最小内存
-Xmx1g #分配最大内存,官方推荐为物理内存的一半,但最大为32G
5.修改锁定内存后,无法重启,解决方法如下:
systemctl edit elasticsearch
添加:
[Service]
LimitMEMLOCK=infinity
F2保存回车退出
6.创建数据目录,并修改权限
mkdir -p /data/elasticsearch
chown -R elasticsearch.elasticsearch /data/elasticsearch/
7.启动elasticsearch服务
systemctl daemon-reload
systemctl restart elasticsearch
此时可在浏览器查看主机及集群信息
查看单主机
http://IP地址:9200/
查看群集健康状态
http://IP地址:9200/_cluster/health?pretty
查看整个群集状态信息
http://IP地址:9200/_cluster/state?pretty
8.可使用es-head插件查看elasticsearch的数据
下载地址
https://github.com/mobz/elasticsearch-head
下载后,解压,复制crx目录下es-head.crx到桌面改名es-head.crx为es-head.crx.zip,解压es-head.crx.zip到es-head.crx目录,把目录es-head.crx,上传到谷歌浏览器开发工具–扩展程序里.
elasticsearch群集状态:
状态颜色:
灰色:未连接
绿色:数据完整态
黄色:副本不完整
红色:数据分片不完整
紫色:数据分片复制过程
群集主机角色:
主节点master:负责管理调度
工作节点: 负责处理数据
默认情况,所有节点都是工作节点,即主节点也处理数据
往群集中添加第二台主机:192.168.1.103
安装es,步骤参考第一台,注意配置文件需要修改
vim /etc/elasticsearch/elasticsearch.yml
node.name: node-2 ---节点名需要修改
path.data: /data/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true --内存锁按需求开启或关闭 关闭则不需要锁定内存等操作
network.host: 192.168.1.103,127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["主节点ip", "本节点ip"]
discovery.zen.minimum_master_nodes: 2 #添加的值=节点数/2 + 1
其余步骤参考4-7步。
可以通过json语句创建索引,修改副本数但是不可以修改分片数。举例如下
(1)创建索引index1时,修改分片为3和副本数为2
curl -X PUT 192.168.1.143:9200/index1 -H 'Content-Type: application/json' -d '{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
}'
(2)针对已有索引,可修改副本数,不可改分片数。下面语句把index1的副本数由2改为1
curl -X PUT '192.168.1.102:9200/index1/_settings?pretty' -H 'Content-Type: application/json' -d '{
"settings": {
"number_of_replicas": "1"
}
}'
filebeat
filebeat官方文档:
https://www.elastic.co/guide/en/beats/filebeat/current/index.html
efk收集nginx日志项目
1.安装elasticsearch(参考上面)
2.安装kibana
(1)安装kibana
cd /data/soft
rpm -ivh kibana-6.6.0-x86_64.rpm
(2)修改配置文件
vim /etc/kibana/kibana.yml
修改:
server.port: 5601
server.host: "192.168.1.104" #主机ip
server.name: "db01" #自己所在主机的主机名
elasticsearch.hosts: ["http://192.168.1.104:9200"] #es服务器的ip,便于接收日志数据
保存退出
(3)启动kibana
systemctl start kibana
3.在es主机上安装filebeat
(1).安装filebeat
cd /data/soft
rpm -ivh filebeat-6.6.0-x86_64.rpm
(2).修改配置文件(yml格式注意对齐)
vim /etc/filebeat/filebeat.yml
修改:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
output.elasticsearch:
hosts: ["192.168.1.104:9200"]
保存退出
(3).启动filebeat
systemctl start filebeat
4.在es主机安装nginx,httpd-tools
1.配置yum源,安装nginx httpd-tools
yum -y install epel-release
yum -y install nginx httpd-tools
2.启动nginx
systemctl start nginx
3.使用ab压力测试工具测试访问
ab -n 100 -c 20 http://192.168.1.104/
4.在es浏览器查看filebeat索引和数据
5.在kibana添加索引
management-->create index
discover-->右上角-->选择today
6.修改nginx的日志格式为json
vim /etc/nginx/nginx.conf
添加在http {}内:
log_format log_json '{ "@timestamp": "$time_local", '
'"remote_addr": "$remote_addr", '
'"referer": "$http_referer", '
'"request": "$request", '
'"status": $status, '
'"bytes": $body_bytes_sent, '
'"agent": "$http_user_agent", '
'"x_forwarded": "$http_x_forwarded_for", '
'"up_addr": "$upstream_addr",'
'"up_host": "$upstream_http_host",'
'"up_resp_time": "$upstream_response_time",'
'"request_time": "$request_time"'
' }';
access_log /var/log/nginx/access.log log_json;
保存退出,重启服务
systemctl restart nginx
清空日志:vim /var/log/nginx/access.log
ab测试访问,生成json格式日志
5.修改filebeat配置文件
vim /etc/filebeat/filebeat.yml
修改为:
filebeat.inputs:
- type: log
enabled: true --开启收集功能
paths:
- /var/log/nginx/access.log --日志路径
json.keys_under_root: true
json.overwrite_keys: true
output.elasticsearch:
hosts: ["192.168.1.104:9200"] --elasticsearch主机地址
index: "nginx-%{[beat.version]}-%{+yyyy.MM}"
setup.template.name: "nginx"
setup.template.patten: "nginx-*"
setup.template.enabled: false
setup.template.overwrite: true
保存退出
重启服务:systemctl restart filebeat
9.配置access.log和error.log分开
vim /etc/filebeat/filebeat.yml
修改为:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
json.keys_under_root: true
json.overwrite_keys: true
tags: ["access"]
- type: log
enabled: true
paths:
- /var/log/nginx/error.log
tags: ["error"]
output.elasticsearch:
hosts: ["192.168.1.104:9200"]
#index: "nginx-%{[beat.version]}-%{+yyyy.MM}"
indices:
- index: "nginx-access-%{[beat.version]}-%{+yyyy.MM}"
when.contains:
tags: "access"
- index: "nginx-error-%{[beat.version]}-%{+yyyy.MM}"
when.contains:
tags: "error"
setup.template.name: "nginx"
setup.template.patten: "nginx-*"
setup.template.enabled: false
setup.template.overwrite: true
保存退出
重启服务:
systemctl restart filebeat
每次修改完filebeat,删掉日志内容,ab测试生成日志,再去kibana添加,在discover选项查看结果。