https://www.elastic.co/guide/index.html官网文档
ES集群:选举出一个节点,所有节点数据对等,主出问题再选举一个主不影响服务
https://www.elastic.co/guide/en/elasticsearch/guide/current/administration.html:ES使用指南
安装ES
yum install -y java:安装java
wget es.tar.gz
tar xf es.tar.gz
mv es /usr/local/es
yum安装es(选择的2.最大版本)自动添加elasticsearch用户
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
vim /etc/yum.repos.d/elasticsearch.repo #行首不能有空格
[elasticsearch-2.x]
name=Elasticsearch repository for 2.x packages
baseurl=http://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
yum install elasticsearch -y
vim es/elasticsearch.yml
cluster.name:name #设置集群名
node.name:name #设置节点名
path.data:/data/es-data #设置data目录
path.logs:/var/log/elasticsearch #设置日志目录
bootstrap.mlockall:true #内存不会交换到swap分区
network.host:ip #监听IP
http.port:9200 #http端口
chown -R elasticsearch:elasticsearch /data/es-data
service elasticsearch start
启动报错:unable to install syscall filter: seccomp unavailable: requires kernel 3.5+ with CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER compiled in
升级java-jdk到1.8.0
启动报错:bootstrap checks failed
ES5.2以后的版本默认bootstrap.system_call_filter为true进行检测,centos7以下不支持,配置文件bootstrap.memory_lock: true下添加bootstrap.system_call_filter: false
日志警告处理
vim /etc/security/limits.conf #允许内存锁
# allow user 'elasticsearch' mlockall
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited
vim /etc/sysctl.conf #设置open file最大数
fs.file-max = 65535
sysctl -p
curl -i 'http://192.168.1.180:9200/
-i:返回状态码
通过curl来获取es状态
安装es-Marvel插件(收费先不用)
/usr/share/elasticsearch/bin/plugin install marvel-agent
/usr/share/elasticsearch/bin/plugin remove marvel-agent:不删除可能集群不成功
ES-5.x以上没有plugin命令,只能手动安装插件
手动安装head
yum install -y nodejs
yum install npm
npm install -g grunt
wget https://github.com/mobz/elasticsearch-head/archive/master.zip
unzip elasticsearch-head-master.zip
cd /usr/local/elasticsearch-head-master/
npm install
vim /etc/elasticsearch/elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"
nohup grunt server &
访问时使用外网IP并用外网IP连接
安装head插件(github)
/usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head
http://192.168.1.180:9200/_plugin/head/:访问head插件
web添加查询删除,查看es状态
有数据时概览里可以看到把索引为0-4共5个分片,绿色粗线为主分片,有集群时绿色细线为副分片,灰色为没有,主分片出问题,可用副分片提升为主,查也可以用副分片;
集群:es启动时广播集群名,收到一样的设置为一个集群
vim es/elasticsearch.yml
设置相同的集群名、不同的节点名
discovery.zen.ping.multicast.enabled: false
#默认为广播,可能无法发现集群,上面设置为关闭
discovery.zen.ping.unicast.hosts: ["ip","ip"]
#设置单播的各节点IP,设置一个非本机就行
可以设置节点只为主并不记录数据或只为从
web上集群上星号代表主节点
安装lukas-vlcek/bigdesk插件(github)可能新版本不支持
/usr/share/elasticsearch/bin/plugin install lukas-vlcek/bigdesk
安装kopf插件
/usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf
http://192.168.1.180:9200/_plugin/kopf #web访问
yum安装logstash
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
vim /etc/yum.repos.d/logstash.repo
[logstash-5.x]
name=Elastic repository for 5.x packages
baseurl=https://artifacts.elastic.co/packages/5.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
yum install logstash
bin/logstash -e 'input { stdin{} } output { stdout{} }'
前台执行,-e执行,输入和输出stdin、stdout为插件,通过改变输入输出插件来改变输入输出显示
/usr/share/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.1.180:9200"] } stdout{ codec => rubydebug } }' #从显示器输入,然后输出一份到elastiearch,再输出一份到屏幕
以配置文件运行
vim /etc/logstash/conf.d/logstash.conf
input { stdin { } } #从显示器输入
output {
elasticsearch { hosts => ["ip:9200"] } #输出到ES
stdout { codec => rubydebug } #另外输出到显示器
}
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/logstash.conf #运行logstash文件
vim file.conf
input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
file {
path => "/var/log/elasticsearch/myqhj-es.log"
type => "es-error"
start_position => "beginning"
codec => multiline {
pattern => "^\["
negate => "true"
what => "previous"
}
}
}
output {
if [type] == "system" {
elasticsearch {
hosts => ["192.168.1.180:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
if [type] == "es-error" {
elasticsearch {
hosts => ["192.168.1.180:9200"]
index => "es-error-%{+YYYY.MM.dd}"
}
}
}
从2个文件输入
if判断可能判断type,且type为最开始收集所设置的type
path:设置文件路径
type:设置类型,ES类型中记录此name和里面数据
start_position => "beginning":设置从文件开头读取,默认从文件最后一行开始读取
输出通过if判断分别对应输出到用index来指定命令的索引
codec => multiline:设置匹配哪些行为一行一个事件输出;
pattern =>设置匹配字符,支持正则;
negate => "true|false"和what => "previous|next"符合使用,true、previous为匹配到字符的上一行为一大行,false、next为匹配到字符的本行为上一大行
注意:当logstash读取一个文件后,下次只读取新添加的行
收集nginx日志
vim nginx.conf,设置nginx使用json格式写日志
log_format json '{"@timestamp":"$time_iso8601",'
'"@version":"1",'
'"client":"$remote_addr",'
'"url":"$uri",'
'"status":"$status",'
'"domain":"$host",'
'"host":"$server_addr",'
'"size":"$body_bytes_sent",'
'"responsetime":"$request_time",'
'"referer":"$http_referer",'
'"ua":"$http_user_agent"'
'}';
logstash配置文件设置输入方式
file {
path => "/data/logs/host.access.log"
codec => "json"
type => "nginx-log"
start_position => "beginning"
}
使用syslog收集日志:/var/log/messages
input {
syslog {
type => "system-syslog"
host => "192.168.1.180" #监听IP
port => "514" #监听TCP和UDP514
}
}
修改syslog发送到收集端口
vim /etc/rsyslog.conf
*.* @@192.168.1.180:514
service rsyslog restart
input-tcp:单独追加收集
input {
tcp {
type => "add"
host => "192.168.1.180" #监听IP
port => "6666" #监听TCP和UDP514
}
}
发送文件到端口
nc ip 6666 > /file
echo "1" | nc ip 6666
echo "1" > /dev/tcp/ip/6666
filter-grok插件:输入后通过filter过滤,再到输出
input {
stdin {}
}
filter {
grok {
match => { "message" => "%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration}" }
}
}
输入55.3.244.1 GET /index.html 15824 0.043,会被filter中正则匹配处理,再输出:
client: 55.3.244.1
method: GET
request: /index.html
bytes: 15824
duration: 0.043
/usr/share/logstash/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-4.1.2/patterns/:logstash定义的正则
mysql-slow-log的filter匹配输出(下面为一个测试)
match => [ "message", "(?m)^# User@Host: %{USER:user}\[[^\]]+\] @ (?:(?<clienthost>\S*) )?\[(?:%{IP:clientip})?\]\s+Id: %{NUMBER:row_id:int}\s*# Query_time: %{NUMBER:query_time:float}\s+Lock_time: %{NUMBER:lock_time:float}\s+Rows_sent: %{NUMBER:rows_sent:int}\s+Rows_examined: %{NUMBER:rows_examined:int}\s*(?:use %{DATA:database};\s*)?SET timestamp=%{NUMBER:timestamp};\s*(?<query>(?<action>\w+)\s+.*)\n#\s*" ]
redis插件:输入、输出格式一样
output|input {
redis {
host => "ip"
port => "port"
db => "5" #存储到哪个库
data_type => "list" #设置存储类型为列表
key => "name" #存储到哪个key
}
}
安装kibana:
wegt 下载kibana.tar.gz #最新6.1必须ES、logstash都为6.1
tar xf kibana.tar.gz
mv kibana-4.6.6 /usr/local/
ln -s /usr/local/kibana-4.6.6 /usr/local/kibana
vim /usr/local/kibana/config/kibana.yml
server.port: 5601 #设置kibana监听端口
server.host: "" #设置kibana监听IP
elasticsearch.url: "ip:port" #设置ES
kibana.index: ".kibana" #kibana数据存到ES
nohup /usr/local/kibana/bin/kibana & #后台运行
http://192.168.1.180:5601
没有默认的索引,先添加一个ES已有索引,时间可以用*来匹配
Settings可以添加索引
通过右上角选择时间查询数据
通过左上角>添加message来只显示信息
logstash写入时间为UTC时区,kibana转换为东八区
kibana生产使用:每个ES上安装,nginx反向代理并提供验证
架构:数据-logstash-消息队列|redis-logstash|python脚本-ES-kibana
ES开源安全插件:https://github.com/floragunncom/search-guard
使用nginx反向代理访问kibana,并设置访问用户密码验证
生产上线ELK
日志分类
系统日志:syslog插件
访问日志:nginx的codec json插件
错误日志:file插件,mulitline插件
运行日志:file插件,codec json插件
设备日志:syslog插件
debug日志:file插件,codec json插件或mulitline插件
日志标准化
路径固定、格式json
系统日志-->错误日志-->运行日志-->访问日志
ES集群:选举出一个节点,所有节点数据对等,主出问题再选举一个主不影响服务
https://www.elastic.co/guide/en/elasticsearch/guide/current/administration.html:ES使用指南
安装ES
yum install -y java:安装java
wget es.tar.gz
tar xf es.tar.gz
mv es /usr/local/es
yum安装es(选择的2.最大版本)自动添加elasticsearch用户
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
vim /etc/yum.repos.d/elasticsearch.repo #行首不能有空格
[elasticsearch-2.x]
name=Elasticsearch repository for 2.x packages
baseurl=http://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
yum install elasticsearch -y
vim es/elasticsearch.yml
cluster.name:name #设置集群名
node.name:name #设置节点名
path.data:/data/es-data #设置data目录
path.logs:/var/log/elasticsearch #设置日志目录
bootstrap.mlockall:true #内存不会交换到swap分区
network.host:ip #监听IP
http.port:9200 #http端口
chown -R elasticsearch:elasticsearch /data/es-data
service elasticsearch start
启动报错:unable to install syscall filter: seccomp unavailable: requires kernel 3.5+ with CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER compiled in
升级java-jdk到1.8.0
启动报错:bootstrap checks failed
ES5.2以后的版本默认bootstrap.system_call_filter为true进行检测,centos7以下不支持,配置文件bootstrap.memory_lock: true下添加bootstrap.system_call_filter: false
日志警告处理
vim /etc/security/limits.conf #允许内存锁
# allow user 'elasticsearch' mlockall
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited
vim /etc/sysctl.conf #设置open file最大数
fs.file-max = 65535
sysctl -p
curl -i 'http://192.168.1.180:9200/
-i:返回状态码
通过curl来获取es状态
安装es-Marvel插件(收费先不用)
/usr/share/elasticsearch/bin/plugin install marvel-agent
/usr/share/elasticsearch/bin/plugin remove marvel-agent:不删除可能集群不成功
ES-5.x以上没有plugin命令,只能手动安装插件
手动安装head
yum install -y nodejs
yum install npm
npm install -g grunt
wget https://github.com/mobz/elasticsearch-head/archive/master.zip
unzip elasticsearch-head-master.zip
cd /usr/local/elasticsearch-head-master/
npm install
vim /etc/elasticsearch/elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"
nohup grunt server &
访问时使用外网IP并用外网IP连接
安装head插件(github)
/usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head
http://192.168.1.180:9200/_plugin/head/:访问head插件
web添加查询删除,查看es状态
有数据时概览里可以看到把索引为0-4共5个分片,绿色粗线为主分片,有集群时绿色细线为副分片,灰色为没有,主分片出问题,可用副分片提升为主,查也可以用副分片;
集群:es启动时广播集群名,收到一样的设置为一个集群
vim es/elasticsearch.yml
设置相同的集群名、不同的节点名
discovery.zen.ping.multicast.enabled: false
#默认为广播,可能无法发现集群,上面设置为关闭
discovery.zen.ping.unicast.hosts: ["ip","ip"]
#设置单播的各节点IP,设置一个非本机就行
可以设置节点只为主并不记录数据或只为从
web上集群上星号代表主节点
安装lukas-vlcek/bigdesk插件(github)可能新版本不支持
/usr/share/elasticsearch/bin/plugin install lukas-vlcek/bigdesk
安装kopf插件
/usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf
http://192.168.1.180:9200/_plugin/kopf #web访问
yum安装logstash
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
vim /etc/yum.repos.d/logstash.repo
[logstash-5.x]
name=Elastic repository for 5.x packages
baseurl=https://artifacts.elastic.co/packages/5.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
yum install logstash
bin/logstash -e 'input { stdin{} } output { stdout{} }'
前台执行,-e执行,输入和输出stdin、stdout为插件,通过改变输入输出插件来改变输入输出显示
/usr/share/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.1.180:9200"] } stdout{ codec => rubydebug } }' #从显示器输入,然后输出一份到elastiearch,再输出一份到屏幕
以配置文件运行
vim /etc/logstash/conf.d/logstash.conf
input { stdin { } } #从显示器输入
output {
elasticsearch { hosts => ["ip:9200"] } #输出到ES
stdout { codec => rubydebug } #另外输出到显示器
}
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/logstash.conf #运行logstash文件
vim file.conf
input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
file {
path => "/var/log/elasticsearch/myqhj-es.log"
type => "es-error"
start_position => "beginning"
codec => multiline {
pattern => "^\["
negate => "true"
what => "previous"
}
}
}
output {
if [type] == "system" {
elasticsearch {
hosts => ["192.168.1.180:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
if [type] == "es-error" {
elasticsearch {
hosts => ["192.168.1.180:9200"]
index => "es-error-%{+YYYY.MM.dd}"
}
}
}
从2个文件输入
if判断可能判断type,且type为最开始收集所设置的type
path:设置文件路径
type:设置类型,ES类型中记录此name和里面数据
start_position => "beginning":设置从文件开头读取,默认从文件最后一行开始读取
输出通过if判断分别对应输出到用index来指定命令的索引
codec => multiline:设置匹配哪些行为一行一个事件输出;
pattern =>设置匹配字符,支持正则;
negate => "true|false"和what => "previous|next"符合使用,true、previous为匹配到字符的上一行为一大行,false、next为匹配到字符的本行为上一大行
注意:当logstash读取一个文件后,下次只读取新添加的行
收集nginx日志
vim nginx.conf,设置nginx使用json格式写日志
log_format json '{"@timestamp":"$time_iso8601",'
'"@version":"1",'
'"client":"$remote_addr",'
'"url":"$uri",'
'"status":"$status",'
'"domain":"$host",'
'"host":"$server_addr",'
'"size":"$body_bytes_sent",'
'"responsetime":"$request_time",'
'"referer":"$http_referer",'
'"ua":"$http_user_agent"'
'}';
logstash配置文件设置输入方式
file {
path => "/data/logs/host.access.log"
codec => "json"
type => "nginx-log"
start_position => "beginning"
}
使用syslog收集日志:/var/log/messages
input {
syslog {
type => "system-syslog"
host => "192.168.1.180" #监听IP
port => "514" #监听TCP和UDP514
}
}
修改syslog发送到收集端口
vim /etc/rsyslog.conf
*.* @@192.168.1.180:514
service rsyslog restart
input-tcp:单独追加收集
input {
tcp {
type => "add"
host => "192.168.1.180" #监听IP
port => "6666" #监听TCP和UDP514
}
}
发送文件到端口
nc ip 6666 > /file
echo "1" | nc ip 6666
echo "1" > /dev/tcp/ip/6666
filter-grok插件:输入后通过filter过滤,再到输出
input {
stdin {}
}
filter {
grok {
match => { "message" => "%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration}" }
}
}
输入55.3.244.1 GET /index.html 15824 0.043,会被filter中正则匹配处理,再输出:
client: 55.3.244.1
method: GET
request: /index.html
bytes: 15824
duration: 0.043
/usr/share/logstash/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-4.1.2/patterns/:logstash定义的正则
mysql-slow-log的filter匹配输出(下面为一个测试)
match => [ "message", "(?m)^# User@Host: %{USER:user}\[[^\]]+\] @ (?:(?<clienthost>\S*) )?\[(?:%{IP:clientip})?\]\s+Id: %{NUMBER:row_id:int}\s*# Query_time: %{NUMBER:query_time:float}\s+Lock_time: %{NUMBER:lock_time:float}\s+Rows_sent: %{NUMBER:rows_sent:int}\s+Rows_examined: %{NUMBER:rows_examined:int}\s*(?:use %{DATA:database};\s*)?SET timestamp=%{NUMBER:timestamp};\s*(?<query>(?<action>\w+)\s+.*)\n#\s*" ]
redis插件:输入、输出格式一样
output|input {
redis {
host => "ip"
port => "port"
db => "5" #存储到哪个库
data_type => "list" #设置存储类型为列表
key => "name" #存储到哪个key
}
}
安装kibana:
wegt 下载kibana.tar.gz #最新6.1必须ES、logstash都为6.1
tar xf kibana.tar.gz
mv kibana-4.6.6 /usr/local/
ln -s /usr/local/kibana-4.6.6 /usr/local/kibana
vim /usr/local/kibana/config/kibana.yml
server.port: 5601 #设置kibana监听端口
server.host: "" #设置kibana监听IP
elasticsearch.url: "ip:port" #设置ES
kibana.index: ".kibana" #kibana数据存到ES
nohup /usr/local/kibana/bin/kibana & #后台运行
http://192.168.1.180:5601
没有默认的索引,先添加一个ES已有索引,时间可以用*来匹配
Settings可以添加索引
通过右上角选择时间查询数据
通过左上角>添加message来只显示信息
logstash写入时间为UTC时区,kibana转换为东八区
kibana生产使用:每个ES上安装,nginx反向代理并提供验证
架构:数据-logstash-消息队列|redis-logstash|python脚本-ES-kibana
ES开源安全插件:https://github.com/floragunncom/search-guard
使用nginx反向代理访问kibana,并设置访问用户密码验证
生产上线ELK
日志分类
系统日志:syslog插件
访问日志:nginx的codec json插件
错误日志:file插件,mulitline插件
运行日志:file插件,codec json插件
设备日志:syslog插件
debug日志:file插件,codec json插件或mulitline插件
日志标准化
路径固定、格式json
系统日志-->错误日志-->运行日志-->访问日志