ELK 概念
- 是elasticsearch、logstash、kibana三个开源软件的组合
一个公司出的三个单独的软件,但是需要相辅相成才能运行 - 具备数据收集、存储、分析功能
- 实时数据检索和分析场合
功能
- logstash 负责文本数据的收集、处理
- elasticsearch 负责数据存储和索引
- kibana 负责数据的可视化和统计分析
ELK特点
- 处理方式灵活
- 配置简易上手
- 检索性能高效
- 集群线性拓展
- 前端操作绚丽
filebeat介绍
因为logstash的开销太大,所以这里使用filebeat代替logstash来采集日志。
也有使用了filebeat采集,然后输出到logstash来过滤的方案,但是我只想最小化依赖安装(其实是因为懒。。
filebeat属于beat软件系列,和ELK一样,都是一个公司的产品。
组件版本说明
截止2018.8.9时三个组件的最新版
Elasticsearch 6.3.2
filebeat 6.3.2
kibana 6.3.2
架构说明:
在aws内网搭建如下
A:EK 日志系统
安装Elasticsearch + kibana,监听0.0.0.0
用aws的防火墙规则限制仅filtbeat的ip访问
- ES 9200 端口
- kibana 5601 端口
B: 业务机 日志采集
安装filebeat
采集到的日志推送到内网的ES和kibana
安装Elasticsearch
推荐:服务器内存 >=4G
依赖:java运行环境
注意:使用普通用户运行,否则会报错
(虽然可以通过指定参数使之能在root下运行,安全考虑不建议这样做
java环境
openjdk1
2yum search open-jdk
yum install java-1.8.0-openjdk.x86_64
或者oracle-jdk1
curl -s https://raw.githubusercontent.com/qwqmeow/scripts/master/centos_install_jdk8.sh | bash
下载源码
下载页面:https://www.elastic.co/downloads/elasticsearch
v6.3.2的地址:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.3.2.tar.gz
cd /opt/elk/
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.3.2.tar.gz
tar zxvf elasticsearch-6.3.2.tar.gz
cd elasticsearch-6.3.2
程序配置参数
config/elasticsearch.yml
vim config/elasticsearch.yml
- 数据和日志路径
1 | path.data:/opt/elk/elasticsearch/data |
- 网络监听的ip
1 | network.host: 0.0.0.0 |
0.0.0.0 监听外网
127.0.0.1 监听本机localhost
- 文件最下面添加两行(可选)
如果你需要安装一个Elasticsearch-head的插件方便管理的话,你需要加上这两行,用于运行跨域。
1 | http.cors.enabled: true |
系统参数修改
主要是修改打开的最大文件数,打开的最大线程数,虚拟内存
- 限制用户对系统资源的使用
vim /etc/security/limits.conf
文件末尾加上1
2
3
4* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
需要reboot后才会生效
vim /etc/security/limits.d/20-nproc.conf
1
2* soft nproc 4096
root soft nproc unlimited
如果打开就是4096,那就不用修改
- 句柄数和MMap
vim /etc/sysctl.conf
加上1
2vm.max_map_count=655360
fs.file-max=655360
运行sysctl -p
使文件生效
服务器内存不足4G的解决方案
通过环境变量ES_JAVA_OPTS
来限制内存使用量
1 | ES_JAVA_OPTS="-Xms512m -Xmx512m" ./bin/elasticsearch -d |
参数说明:
- -Xms:限制启动最小内存
- -Xmx:限制启动最大内存
运行elasticsearch
在elasticsearch的目录下1
./bin/elasticsearch -d
参数说明:1
-d daemon模式
查看运行:1
ps -ef|grep elastic
是在运行
curl http://localhost:92001
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17{
"name" : "iYMmhl6",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "Fpg3kTYiROu6lgHGGq80bg",
"version" : {
"number" : "6.3.2",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "053779d",
"build_date" : "2018-07-20T05:20:23.451332Z",
"build_snapshot" : false,
"lucene_version" : "7.3.1",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
elasticsearch head 插件(可选)
依赖:nodejs环境
nodejs环境
参考nvm那节(忘记填坑了
1 | curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash |
下载 head 源码
1 | wget https://github.com/mobz/elasticsearch-head/archive/master.zip |
如果国内环境,npm install
比较慢的话,建议使用淘宝镜像cnpm
代替npm
:1
npm install -g cnpm --registry=https://registry.npm.taobao.org
报错:因为需要解压phantomjs,没有安装bzip2
1
2tar (child): bzip2: Cannot exec: No such file or directory
tar (child): Error is not recoverable: exiting now
安装bzip21
2yum install -y bzip2
npm install
npm install 完成1
Done. Phantomjs binary available at /opt/elk/elasticsearch-head-master/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs
运行 head插件1
nohup npm run start &> run.log &
访问head插件
http://ip:9200
因为这个没有认证功能,生产环节我就没有装了。
安装kibana
日志可视化平台
下载源码
下载页面:https://www.elastic.co/downloads/kibana
v6.3.2的地址:https://artifacts.elastic.co/downloads/kibana/kibana-6.3.2-linux-x86_64.tar.gz
运行kibana
1 | nohup ./bin/kibana -H 0.0.0.0 &> run.log & |
如果不加-H
默认会监听localhost
kibana默认监听端口是5601
浏览器打开http://ip:5601 ,即可访问到
添加认证功能
因为kibana从,,取消了认证功能,如果在公网使用是极其不安全的,所以需要用apache或者nginx添加的认证功能。
使用nginx的basic auth来添加。1
2yum -y install nginx httpd-tools
mkdir -p /opt/elk/nginx/passwd
将以下命令中的user
和password
换为你自己想要的:1
htpasswd -c -b /opt/elk/nginx/passwd/kibana.passwd user password
nginx配置:/etc/nginx/nginx.conf1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
auth_basic "Kibana Auth";
auth_basic_user_file /opt/elk/nginx/passwd/kibana.passwd;
proxy_pass http://localhost:5601;
proxy_set_header Host $host:5601;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Via "nginx";
}
运行kibana1
nohup ./bin/kibana -H 127.0.0.1 &> run.log &
然后我们访问公网ip的http地址,会弹出basic auth对话框,输入正确后,即可跳转到kiana的平台。
filebeat
数据采集器,安装在业务机器上。
下载源码
下载页面:https://www.elastic.co/downloads/beats/filebeat
下载地址:
v6.3.2:64-bit
配置文件
需要修改的配置文件是 filebeat.yml
重要部分的配置如下
1 | filebeat.inputs: |
使用如下命令,将目前的日志推送到kibana和es:1
./filebeat -e -c filebeat.yml -d "Publish"
ctrl+c终止掉,再执行如下,使之保持运行1
nohup ./filebeat -e -c filebeat.yml >/dev/null 2>&1 &
访问kibana的平台地址
左侧 - Management - Index Patterns
创建index匹配规则
规则名:filebeat-*
时间: @timestamp
这个时间是指导入日志的时间,而不是日志文件中的时间。
然后你就能在kibana中看到日志了。