ELK5.X搭建並收集Nginx日誌
ELK
本文轉載https://www.i4t.com
ELK Stack 是 Elasticsearch、Logstash、Kibana 三個開源軟件的組合。在實時數據檢索和分析場合,三者通常是配合共用,而且又都先後歸於 Elastic.co
公司名下,故有此簡稱。
ELK Stack 在最近兩年迅速崛起,成爲機器數據分析,或者說實時日誌處理領域,開源界的第一選擇。和傳統的日誌處理方案相比,ELK Stack 具有如下幾個優點:
• 處理方式靈活。Elasticsearch 是實時全文索引,不需要像 storm 那樣預先編程才能使用;
• 配置簡易上手。Elasticsearch 全部採用 JSON 接口,Logstash 是 Ruby DSL 設計,都是目前業界最通用的配置語法設計;
• 檢索性能高效。雖然每次查詢都是實時計算,但是優秀的設計和實現基本可以達到全天數據查詢的秒級響應;
• 集羣線性擴展。不管是 Elasticsearch 集羣還是 Logstash 集羣都是可以線性擴展的;
• 前端操作炫麗。Kibana 界面上,只需要點擊鼠標,就可以完成搜索、聚合功能,生成炫麗的儀表板。
ELK地址:https://www.elastic.co/
Logstash 最佳實踐:http://udn.yyuap.com/doc/logstash-best-practice-cn/index.html
Elasticsearch 權威指南:http://www.learnes.net/index.html
ELKStack中文社區:https://kibana.logstash.es/content/
更多ELK文章:https://www.i4t.com/category/elkstack/
對於日誌來說,最常見的需求就是收集、存儲、查詢、展示,開源社區正好有相對應的開源項目:logstash(收集)、elasticsearch(存儲+搜索)、kibana(展示),我們將這三個組合起來的技術稱之爲ELKStack,所以說ELKStack指的是Elasticsearch(java)、Logstash(jruby)、Kibana技術棧的結合,一個通 用的架構如下圖所示:
圖片解釋:elk 前面主要靠logstash來進行收集日誌,logstash將日誌上傳到broker上,後面還有一個logstash用來讀取broker中的日誌,在將日誌存儲到es裏面,最後用kibana練到es上進行展示
提示:我們可以將Elasticsearch簡稱爲es
1.Elasticsearch介紹ElasticSearch
是一個基於Lucene
的搜索服務器。它提供了一個分佈式多用戶能力的全文搜索引擎,基於RESTful web接口。Elasticsearch是用Java開發的,並作爲Apache許可條款下的開放源碼發佈,是第二流行的企業搜索引擎。設計用於雲計算中,能夠達到實時搜索,穩定,可靠,快速,安裝使用方便。
2.Logstash介紹
Logstash
是一個完全開源的工具,他可以對你的日誌進行收集、分析,並將其存儲供以後使用(如,搜索),您可以使用它。說到搜索,logstash帶有一個web界面,搜索和展示所有日誌。
kibana也是一個開源和免費的工具,他可以幫助您彙總、分析和搜索重要數據日誌並提供友好的web界面。他可以爲 Logstash 和 ElasticSearch 提供的日誌分析的 Web 界面。
提示:logstash與es沒有任何關係
ELK幾個概念
INPUT 進
FILTER (支持過濾的功能)
OUTPUT 出
首先LogStash是收集日誌,它收集完日誌就需要把日誌存儲下來,所以我們可以用運輸者的身份來表示LogStash(INPUT or OUTPUT)LogStash可以在日誌發送之前做一個過濾(OUTPUT之前)
3.Kibana簡介Kibana
是爲 Elasticsearch 設計的開源分析和可視化平臺。你可以使用 Kibana 來搜索,查看存儲在 Elasticsearch 索引中的數據並與之交互。你可以很容易實現高級的數據分析和可視化,以圖表的形式展現出來。
Kibana版本使用php、ruby、js、jruby、node.js等不同語言編寫
Kibana 4使用nodejs進行編寫(nodejs號稱全棧,什麼都可以幹)
本段文章截取https://www.i4t.com/category/elkstack/
溫馨提示:本次搭建屬於單點,不算是集羣。集羣搭建只需要改相關的配置文件重啓即可
環境 CentOS Linux release 7.4.1708 (Core)
版本
elasticsearch-5.5.0.tar.gz
kibana-5.5.0-linux-x86_64.tar.gz
logstash-5.5.0.tar.gz
主機
10.4.11.11 Elasticsearch Kibana
10.4.11.12 logstash nginx
如果Nginx和elk都在一臺,可以都安裝在一臺服務器上!
環境配置
#時間同步
#關閉selinux 及防火牆
#內核優化
一、基礎環境配置及軟件包下載
1.設置yum源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum makecache
yum install wget vim lsof net-tools lrzsz -y
2.時間同步
yum -y install ntp
systemctl enable ntpd
systemctl start ntpd
ntpdate -u cn.pool.ntp.org
hwclock --systohc
timedatectl set-timezone Asia/Shanghai
3.關閉Selinux && 防火牆
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
4.內核優化
echo '
* hard nofile 65536
* soft nofile 65536
* soft nproc 65536
* hard nproc 65536
'>>/etc/security/limit.conf
echo '
vm.max_map_count = 262144
net.core.somaxconn=65535
net.ipv4.ip_forward = 1
'>>/etc/sysctl.conf
sysctl -p
下載軟件包
mkdir /root/elk
Elasticsearch:
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.0.tar.gz
Kibana:
wget https://artifacts.elastic.co/downloads/kibana/kibana-5.5.0-linux-x86_64.tar.gz
Logstash:
wget https://artifacts.elastic.co/downloads/logstash/logstash-5.5.0.tar.gz
安裝Jdk環境
1.源碼安裝
下載安裝http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
配置Java環境
tar zxf jdk-8u91-linux-x64.tar.gz -C /usr/local/
ln –s /usr/local/jdk1.8.0_91 /usr/local/jdk
vim /etc/profile
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH
source /etc/profile
看到如下信息,java環境配置成功
# java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
2.yum安裝
yum install -y java
[[email protected] ~]# java -version
openjdk version "1.8.0_65"
OpenJDK Runtime Environment (build 1.8.0_65-b17)
OpenJDK 64-Bit Server VM (build 25.65-b01, mixed mode)
二、安裝Elasticsearch
我們elk都使用普通用戶進行啓動管理
新建持久化目錄及logo目錄
mkdir /data1/{es-data,es-logs} -p
tar xf elasticsearch-5.5.0.tar.gz -C /usr/local/
ln -s /usr/local/elasticsearch-5.5.0 /usr/local/elasticsearch
修改配置文件,修改後如下
grep -Ev "^$|#" /usr/local/elasticsearch-5.5.0/config/elasticsearch.yml
node.name: my-es
path.data: /data1/es-data
path.logs: /data1/es-logs
config.reload.automatic: true
config.reload.interval: 10
http.host: "10.4.82.217"
http.cors.enabled: true
http.cors.allow-origin: "*"
配置說明
node.name: my-es #設置節點名稱,一般寫主機名
path.data: /data1/es-data #創建logstash 和插件使用的持久化目錄
path.logs: /data1/es-logs #創建es日誌路徑
config.reload.automatic: true #開啓配置文件自動加載
config.reload.interval: 10 #定義配置文件重載時間週期
http.host: "10.4.82.217" #定義訪問主機名,一般爲域名或IP
http.cors.enabled: true #使用head插件需要開啓
http.cors.allow-origin: "*"
更多配置說明:https://blog.csdn.net/ma_shou_feng/article/details/83783213
在運行es之前,我們需要創建用戶,設置屬主屬組
useradd elk
chown -R elk /data1/es-*
[[email protected] ~]# ll /data1/
總用量 32
drwxr-xr-x 2 elk root 4096 1月 25 04:48 es-data
drwxr-xr-x 2 elk root 4096 1月 25 04:48 es-logs
drwxr-xr-x 3 root root 4096 8月 24 20:17 etcd
drwxr-xr-x 2 root root 4096 8月 16 11:41 kubelet
drwx------. 2 root root 16384 7月 13 2018 lost+found
設置es服務
chown -R elk /usr/local/elasticsearch-5.5.0/
[[email protected] local]# ll /usr/local/elasticsearch-5.5.0/
總用量 236
drwxr-xr-x 2 elk root 4096 1月 25 04:49 bin
drwxr-xr-x 2 elk root 4096 1月 25 04:54 config
drwxr-xr-x 2 elk root 4096 7月 1 2017 lib
-rw-r--r-- 1 elk root 11358 7月 1 2017 LICENSE.txt
drwxr-xr-x 13 elk root 4096 7月 1 2017 modules
-rw-r--r-- 1 elk root 194187 7月 1 2017 NOTICE.txt
drwxr-xr-x 2 elk root 4096 7月 1 2017 plugins
-rw-r--r-- 1 elk root 9548 7月 1 2017 README.textile
啓動es服務
啓動之前需要su到普通用戶上
[[email protected] ~]# su - elk
[[email protected] ~]$ /usr/local/elasticsearch/bin/elasticsearch -d
# -d 參數爲後臺啓動,第一次安裝好可以不加-d先啓動測試,查看是否有error或者報錯
前臺查看啓動成功,我們就可以加上-d參數讓它後臺運行
檢查是否啓動成功
[elk@i4t ~]$ /usr/local/elasticsearch/bin/elasticsearch -d
[elk@i4t~]$ lsof -i:9200
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 7157 elk 312u IPv6 391237534 0t0 TCP node1:wap-wsp (LISTEN)
[elk@i4t ~]$ curl 10.4.82.217:9200
{
"name" : "my-es",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "tXBJMwRhQUyPNrwKETCWgg",
"version" : {
"number" : "5.5.0",
"build_hash" : "260387d",
"build_date" : "2017-06-30T23:16:05.735Z",
"build_snapshot" : false,
"lucene_version" : "6.6.0"
},
"tagline" : "You Know, for Search"
}
Elasticsearch常用命令
curl -XDELETE 'http://host.IP.address:9200/logstash-*' 刪除索引(後面爲索引名稱)
curl -XGET 'host.IP.address:9200/_cat/health?v&pretty' 查看集羣狀態
curl -XGET 'host.IP.address:9200/_cat/indices?v&pretty' 查看索引
三、安裝logstash
logstash安裝的Nginx服務器上,並非es服務器
1.解壓安裝logstash
tar xf logstash-5.5.0.tar.gz -C /usr/local/
ln -s /usr/local/logstash-5.5.0 /usr/local/logstash
2.創建配置文件
mkdir /usr/local/logstash/conf/
vim /usr/local/logstash/conf/nginx.conf
input {
file {
path => "/usr/local/nginx/logs/access.log"
start_position => "beginning"
}
}
filter {
}
output {
#stdout { codec=> rubydebug }
elasticsearch {
hosts => ["10.4.82.118:9200"]
index => "static_nginx_access-%{+YYYY.MM.dd}"
}
}
#這裏面實際只是定義了一個nginx log路徑
#stdout { codec=> rubydebug } 將內容打印出來,方便測試
#host 填寫es主機
index 索引名稱
四、安裝Kibana
wget https://artifacts.elastic.co/downloads/kibana/kibana-5.5.0-linux-x86_64.tar.gz
tar xf kibana-5.5.0-linux-x86_64.tar.gz -C /usr/local/
chown -R elk.elk kibana-5.5.0-linux-x86_64
kibana配置文件修改
[root@YZSJHL82-217 ~]# vim /usr/local/kibana-5.5.0-linux-x86_64/config/kibana.yml
server.port: 5601
server.host: "10.4.82.217"
elasticsearch.url: "http://127.0.0.1:9200"
[root@YZSJHL82-217 ~]# grep -Ev "^$|#" /usr/local/kibana-5.5.0-linux-x86_64/config/kibana.yml
server.port: 5601 #kibana端口
server.host: "10.4.82.217" #kibana主機
elasticsearch.url: "http://127.0.0.1:9200" #ES服務器端口
啓動
[root@YZSJHL82-217 ~]# su - elk
前臺啓動
[elk@i4t kibana-5.5.0-linux-x86_64]$ /usr/local/kibana-5.5.0-linux-x86_64/bin/kibana
後臺啓動
[elk@i4t kibana-5.5.0-linux-x86_64]$ /usr/local/kibana-5.5.0-linux-x86_64/bin/kibana &
溫馨提示:可以先前臺啓動查看日誌,正常之後在後臺啓動
查看端口
[root@i4t ~]# lsof -i:5601
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 30297 elk 12u IPv4 420317733 0t0 TCP node1:esmagent (LISTEN)
[root@i4t ~]# netstat -lntup|grep 5601
tcp 0 0 10.4.82.217:5601 0.0.0.0:* LISTEN 30297/node
訪問kibana
http://ip:5601
因爲現在沒有數據,我們現在配置Nginx進行Nginx的日誌收集
五、安裝並配置Nginx收集日誌
安裝Nginx
wget http://nginx.org/download/nginx-1.10.3.tar.gz
yum install -y gcc glibc gcc-c++ prce-devel openssl-devel pcre-devel
useradd -s /sbin/nologin www -M
tar xf nginx-1.10.3.tar.gz && cd nginx-1.10.3
./configure --prefix=/usr/local/nginx-1.10.3 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
make && make install
ln -s /usr/local/nginx-1.10.3 /usr/local/nginx
手動啓動
/usr/local/nginx/sbin/nginx
設置開機啓動
echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local
查看服務器是否啓動
netstat -lntp|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7058/nginx: master
需要配置在我們要收集的服務器上
vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
log_format json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"remote_user":"$remote_user",'
'"request":"$request",'
'"http_user_agent":"$http_user_agent",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"requesturi":"$request_uri",'
'"url":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"status":"$status"}';
access_log logs/access.log json;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
}
###########################
實際上就是添加了json格式的日誌
log_format json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"remote_user":"$remote_user",'
'"request":"$request",'
'"http_user_agent":"$http_user_agent",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"requesturi":"$request_uri",'
'"url":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"status":"$status"}';
access_log logs/access.log json;
日誌保存在/usr/local/nginx/logs/下
配置完成後,訪問nginx。查看日誌是否修改爲json
[root@i4tnginx]# tail -f logs/access.log
{"@timestamp""host":"10.4.82.203","clientip":"10.2.52.15","remote_user":"-","request":"GET / HTTP/1.1","http_user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:60.0) Gecko/20100101 Firefox/60.0","size":0,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"10.4.82.203","requesturi":"/","url":"/index.html","domain":"10.4.82.203","xff":"-","referer":"-","status":"304"}
{"@timestamp""host":"10.4.82.203","clientip":"10.2.52.15","remote_user":"-","request":"GET / HTTP/1.1","http_user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:60.0) Gecko/20100101 Firefox/60.0","size":0,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"10.4.82.203","requesturi":"/","url":"/index.html","domain":"10.4.82.203","xff":"-","referer":"-","status":"304"}
測試logstash配置文件是否正常
我們儘量都是用elk用戶,所以需要提前將logstash設置爲elk的屬主屬組
chown -R elk.elk /usr/local/logstash
/usr/local/logstash/bin/logstash -f /usr/local/logstash/conf/nginx.conf -t
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
Sending Logstash's logs to /usr/local/logstash/logs which is now configured via log4j2.properties
Configuration OK
[2019-01-28T11:54:38,481][INFO ][logstash.runner ] Using config.test_and_exit mode. Config Validation Result: OK. Exiting Logstash
在Nginx服務器上啓動logstash
[root@abcdocker logstash]# su - elk
[elk@abcdocker ~]$ /usr/local/logstash/bin/logstash -f /usr/local/logstash/conf/nginx.conf
溫馨提示,一定要確保logs目錄elk有權限寫入,建議我們在啓動elk之前在執行一次chown -R elk.elk /usr/local/logstash
請確保logstash中的file文件有讀取權限,否則無法在ES中創建索引!
我們可以查一下索引
[root@YZSJHL82-203 local]# curl -XGET '10.4.82.217:9200/_cat/indices?v&pretty'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open .kibana 9l1XmifhTd2187a9Zpkqsw 1 1 1 0 3.2kb 3.2kb
yellow open pro_nginx_access-2019.02.15 Guze8x5hTymSzqzQKu5PTQ 5 1 1315 0 1.3mb 1.3mb
Kibana 配置
目前logstash已經將收集的日誌存儲在es裏面,我們需要用kibana進行展示
查看索引命令
[root@YZSJHL82-203 local]# curl -XGET '10.4.82.217:9200/_cat/indices?v&pretty'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open .kibana 9l1XmifhTd2187a9Zpkqsw 1 1 1 0 3.2kb 3.2kb
yellow open pro_nginx_access-2019.02.15 Guze8x5hTymSzqzQKu5PTQ 5 1 1315 0 1.3mb 1.3mb
Kibana創建索引
創建完畢後查看索引
六、Elasticsearch Head插件安裝
elasticsearch-head是一個界面化的集羣操作和管理工具,可以對集羣進行傻瓜式操作。你可以通過插件把它集成到es(首選方式),也可以安裝成一個獨立webapp。
- 顯示集羣的拓撲,並且能夠執行索引和節點級別操作
- 搜索接口能夠查詢集羣中原始json或表格格式的檢索數據
- 能夠快速訪問並顯示集羣的狀態
- 有一個輸入窗口,允許任意調用RESTful API。這個接口包含幾個選項,可以組合在一起以產生有趣的結果;
- 請求方法(get、put、post、delete),查詢json數據,節點和路徑
- 支持JSON驗證器
- 支持重複請求計時器
- 支持使用javascript表達式變換結果
- 收集結果的能力隨着時間的推移(使用定時器),或比較的結果
- 能力圖表轉換後的結果在一個簡單的條形圖(包括時間序列)
關於Head插件安裝有2中,如果使用的yum安裝es可以直接用es中的plugin install mobz/elasticsearch-head
另一種就是需要安裝Node環境
ELKStack 實戰之 Elasticsearch [一]
安裝插件時使用到了npm,所以我們需要安裝npm
二進制安裝npm https://www.i4t.com/2840.html
[root@i4t ~]# yum install git npm #安裝git和npm
[root@i4t ~]# cd /usr/local/
[root@i4t elasticsearch-head]# git clone https://github.com/mobz/elasticsearch-head.git #克隆代碼
[root@i4t local]# cd elasticsearch-head/
#需要注意的是執行以下命令需要在elasticsearch-head目錄下執行
npm install
npm run start ##這是在前臺運行,如果想讓它在後臺運行npm run start &
需要提前在elasticsearch.yml配置如下,如果安裝我的文檔安裝都是已經配置好了
vim /usr/local/elasticsearch/elasticsearch.yml
http.cors.enabled: true # elasticsearch中啓用CORS
http.cors.allow-origin: "*" #允許訪問的IP地址段,* 爲所有IP都可以訪問
默認端口是9100,默認監控的主機也是localhost
請目錄結構和我保持一致
修改默認端口以及默認連接ES地址vim elasticsearch-head/Gruntfile.js
修改默認的端口
因爲head插件默認打開是連接localhost的,我們需要將localhost修改爲我們的ipvim elasticsearch-head/_site/app.js
將localhost換成es的ip即可
修改完成後直接要npm run start &
即可,不需要build
因爲是單點,所以是Yellow狀態