ELK安裝配置,監控nginx日誌

ELK安裝配置,監控nginx日誌,小白學習中,只爲作個記錄。原理什麼的還不清楚。流程通了!方便往後回顧吧!java

一、準備工做linux

a)關閉防火牆nginx

關閉防火牆:service iptables stop vim

永久關閉防火牆:chkconfig iptables offcentos

查看防火牆狀態:service iptables statusruby

b)關閉SELinux服務器

永久有效:修改/etc/sysconfig/selinuxcurl

將文本中的SELINUX=enforcing,改成SELINUX=disabled。而後重啓elasticsearch

即時有效:setenforce  0      學習

查看狀態:getenforce

 

兩臺機器

機器1,IP=192.168.10.128  安裝部署elk

機器2,IP=192.168.10.129  安裝部署ngix以及filebeat

 

安裝部署機器1,(ELK)

獲取安裝軟件包
mkdir /elk;cd /elk
wget https://artifacts.elastic.co/downloads/logstash/logstash-6.2.3.tar.gz
wget https://artifacts.elastic.co/downloads/kibana/kibana-6.2.3-linux-x86_64.tar.gz
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.3.tar.gz

並解壓複製到/usr/local/目錄下
cd /elk
tar xf elasticsearch-6.2.3.tar.gz
tar -xf logstash-6.2.3.tar.gz
tar -xf kibana-6.2.3-linux-x86_64.tar.gz

cp -a elasticsearch-6.2.3 /usr/local/
cp -a logstash-6.2.3 /usr/local/
cp -a kibana-6.2.3-linux-x86_64 /usr/local/

 

yum -y install java-1.8*    #最新版本elasticsearch基於jdk1.8,須要先安裝jdk【本地配置的yum源中就有能夠直接使用】

【elasticsearch配置】
useradd elasticsearch      #建立elasticsearch用戶
chown -R elasticsearch.elasticsearch /usr/local/elasticsearch-6.2.3/
su - elasticsearch        #須要切換到 elasticsearch用戶啓動服務
cd /usr/local/elasticsearch-6.2.3/
./bin/elasticsearch -d       #啓動服務

查看進程是否成功(須要等待一下)
netstat -antp
9200 端口

 若出現錯誤能夠查看日誌

cat /usr/local/elasticsearch-6.2.3/logs/elasticsearch.log

測試是否能夠正常訪問
curl localhost:9200

 

 

【logstash配置】

注意切回root用戶
vim /usr/local/logstash-6.2.3/vendor/bundle/jruby/2.3.0/gems/logstash-patterns-core-4.1.2/patterns/grok-patterns   #添加nginx的過濾配置,使用的grok過濾插件解析日誌(這裏寫的是一個正則,直接照抄了,具體沒有研究)

#Nginx log

WZ([^]*)
NGINXACCESS %{IP:remote_jp} \- \- \[%{HTTPDATE:timestamp}\] "%{WORD:method}% {WZ:request} HTTP/%{NUMBER:httpversion}" %{NUMBER:status} %{NUMBER:bytes} %{QS:referer} %{QS:agent} %{QS:xforward}

 

建立logstash配置文件 
[root@server local]# vim /usr/local/logstash-6.2.3/default.conf    #這裏須要關注下路徑,後面啓動服務須要調用這個路徑

input {
beats {
port => "5044"
}
}
# 數據過濾
filter {
grok {
match => { "message" => "%{NGINXACCESS}" }
}
geoip {
# nginx客戶端ip
source => "192.168.10.129"
}
}
# 輸出配置爲本機的9200端口,這是ElasticSearch服務的監聽端口
output {
elasticsearch {
hosts => ["127.0.0.1:9200"]
}
}

啓動logstash服務
cd /usr/local/logstash-6.2.3/
nohup ./bin/logstash -f default.conf & #會調用 default.conf配置文件,未設置路徑時,默認在執行命令所在路徑,不然會報下面的錯誤

[2019-06-14T15:51:21,543][INFO ][logstash.config.source.local.configpathloader] No config files found in path {:path=>"/usr/local/logstash-6.2.3/bin/default.conf"}

檢查服務是否啓動

netstat -natp|grep 5044

 

 

【kibana配置】

修改配置文件

vim /usr/local/kibana-6.2.3-linux-x86_64/config/kibana.yml

將 

server.host: "localhost"

修改成

server.host: "192.168.10.128"

 

啓動Kibana服務
cd /usr/local/kibana-6.2.3-linux-x86_64/
nohup bin/kibana &

netstat -natp|grep 5601

kibana的頁面

 

 

 

機器2,IP=192.168.10.129  安裝部署ngix以及filebeat

【nginx客戶端配置】
yum -y install nginx

nginx #自帶yum源,沒法下載


可使用以下配置
cd /etc/yum.repos.d/
vim nginx.repo
netstat -natp|grep 5601

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

yum install nginx -y

啓動nginx,並設置開機自啓動

service nginx start
chkconfig nginx on
chkconfig --list nginx

nginx的頁面

 

 

【filebeat】下載filebeat並解壓到/usr/local路徑下

wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.2.3-linux-x86_64.tar.gz
tar -xf ./filebeat-6.2.3-linux-x86_64.tar.gz -C /usr/local/
配置filebeat

vim /usr/local/filebeat-6.2.3-linux-x86_64/filebeat.yml
24 enabled: false #修改成true
27 paths:
28 - /var/log/*.log #修改成/var/log/nginx/*.log
143 #output.elasticsearch:
144 # Array of hosts to connect to. #註釋掉,關閉elasticsearch收集
145 # hosts: ["localhost:9200"]

153 #output.logstash:
154 # The Logstash hosts
155 #hosts: ["127.0.0.1:5044"] #取消註釋,將logstash開啓收集,並將IP修改成ELK服務器的地址

啓動filebeat
cd /usr/local/filebeat-6.2.3-linux-x86_64
nohup ./filebeat -e -c filebeat.yml & #注意路徑,這裏選用相對路徑,而且注意調用的配置文件路徑(filebeat.yml)

 

以上步驟完成後,多刷新幾回nginx頁面,稍後在kibana頁面上就能夠看到對應的日誌信息

 

 目前,僅限安裝了,其餘深刻的東東,尚未了解。先記錄下,以便之後使用吧!

相關文章
相關標籤/搜索