ELK
Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
Logstash是一个完全开源的工具,他可以对你的日志进行收集、过滤,并将其存储供以后使用(如,搜索)。
Kibana 也是一个开源和免费的工具,它Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助您汇总、分析和搜索重要数据日志。
官方包下载 Elastic 产品:搜索、分析、日志和安全 | Elastic
1.Elasticsearch
elasticsearch 不允许以 root 权限来运行!所以需要创建一个非root用户,以非root用户来起es
#创建用户组es
groupadd es
#创建新用户es,设置用户组为es,密码es
useradd es -g es -p es
#授权,更改elasticsearch-7.1.1文件夹所属用户及用户组为es:es
chown -R es:es elasticsearch-7.1.1
#切换用户es
su - es
安装解压
tar -xzvf elasticsearch-7.*.tar.gz
修改配置
1)调整jvm内存大小(机器内存够也可不调整)
vim elasticsearch-7.1.1/config/jvm.options
#修改如下配置
-Xms512m
-Xmx512m
2)elasticsearch.yml
vi elasticsearch-7.1.1/config/elasticsearch.yml
network.host: 10.0.33.47
#此处ip修改为您的本机ip
http.port: 9200
cluster.name: es_cluster
path.data: /tmp/elasticsearch/data
path.logs: /tmp/elasticsearch/logs
注:#对应目录下用es用户创建data和logs文件夹
node.name: node-1
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
cluster.initial_master_nodes: ["node-1"]
3)修改虚拟机配置
#修改虚拟机配置1
修改指令:vim /etc/security/limits.conf
添加以下内容:
* soft nofile 65536
* hard nofile 65536
#修改虚拟机配置2
修改指令:vim /etc/sysctl.conf
修改内容:
#添加以下配置信息
vm.max_map_count=262144
并且执行命令使前两个配置生效:
sysctl -p
#elasticsearch常用命令
启动
#启动命令
.elasticsearch-7.1.1/bin/elasticsearch
#后台启动命令
.elasticsearch-7.1.1/bin/elasticsearch -d
#设置开机自启动
systemctl enable elasticsearch.service
安装Elasticsearch-Head
elasticsearch-head是一个基于node.js的前端工程
1)nodejs安装
# wget https://nodejs.org/dist/v10.9.0/node-v10.9.0-linux-x64.tar.xz // 下载
# tar xf node-v10.9.0-linux-x64.tar.xz // 解压
# cd node-v10.9.0-linux-x64/ // 进入解压目录
# ./bin/node -v // 执行node命令 查看版本
v10.9.0
解压文件的 bin 目录底下包含了 node、npm 等命令,我们可以使用 ln 命令来设置软连接:
ln -s /usr/software/nodejs/bin/npm /usr/local/bin/
ln -s /usr/software/nodejs/bin/node /usr/local/bin/
2)phantomjs安装配置
Wget https://github.com/Medium/phantomjs/releases/download/v2.1.1/phantomjs-2.1.1-linux-x86_64.tar.bz2
#缺少bzip2包的同学记得安装下bzip2
yum -y install bzip2
tar -xvf phantomjs-2.1.1-linux-x86_64.tar.bz2
vim /etc/profile
export PATH=$PATH:/usr/local/phantomjs-2.1.1-linux-x86_64/bin
#注意环境变量$Path移动在最前面
source /etc/profile
3)elasticsearch-head安装
• git clone git://github.com/mobz/elasticsearch-head.git
• cd elasticsearch-head
• npm install -g cnpm --registry=https://registry.npm.taobao.org
修改Elasticsearch配置,允许跨域访问,修改后重新启动Elasticsearch
vi elasticsearch-7.1.1/config/elasticsearch.yml
#添加如下配置,支持跨域访问
http.cors.enabled: true
http.cors.allow-origin: "*"
杀掉elsaticsearch进程,重启
# elasticsearch-head目录下执行
npm run start
注意:启动时可能缺少grunt
全局安装grunt cli工具:
npm install -g grunt-cli
如提示有权限问题则使用:
sudo npm install -g grunt-cli
确认安装成功并查看grunt版本:
grunt --version
如其它项目使用grunt命令时提示找不到grunt命令,如下提示 grunt -v
grunt-cli: The grunt command line interface (v1.3.2)
Fatal error: Unable to find local grunt.
If you're seeing this message, grunt hasn't been installed locally to
your project. For more information about installing and configuring grunt,
please see the Getting Started guide:
https://gruntjs.com/getting-started
则可以使用
npm install grunt --save-dev
或者直接
npm install
安装完成后 ,访问地址
http://localhost:9100/ es head
http://localhost:9200/ es
2.Logstash
安装
tar -zxvf logstash-*.tar.gz
配置logstash输出到elasticsearch
修改logstash的安装目录的config目录下的logstash-sample.conf文件
cd logstash-7.3.0/config/
vim log4j_to_es.conf
修改完配置如下:
input {
stdin { }
beats {
port => 5000
}
tcp {
port => 4569
codec => "json"
}
}
output {
elasticsearch {
action => "index"
hosts => "10.0.33.47:9200"
index => "logstash-%{+YYYY-MM}"
}
stdout { codec=> rubydebug }
}
启动
./bin/logstash -f config/log4j_to_es.conf &
3.Kibana
安装
tar -zxvf kibana-7.*.tar.gz
修改Kibana的配置文件
cd kibana-*/config/
vim kibana.yml
修改配置文件如下:
# 端口
server.port: 5601
# 指定本机ip让外部能访问
server.host: "0.0.0.0"
# 请求数据指向的elasticsearch服务器
elasticsearch.hosts: ["http://ip:9200"]
启动
sh kibana &
访问地址:http://ip:5601
查看通过logstash传输的日志列表
配置索引模式
查看日志
代码集成