1、elasticsearch回顧
1.什么叫搜索
2.為什么mysql不適合全文搜素
3.什么是全文搜索lucene
4.es的應(yīng)用場景
5.es的特點
6.es的安裝部署
- java環(huán)境
- 內(nèi)存不能小于2G
- 磁盤空間給足
- 配置文件開啟內(nèi)存鎖定
- jvm虛擬機最大最小內(nèi)存都一樣
- 綁定了內(nèi)網(wǎng)I和本地IP
7.解決內(nèi)存鎖定
- 配置文件增加允許內(nèi)存鎖定參數(shù)
- 重啟es
8.測試
- ps -ef|grep elasticsearch
- ss -lntup|grep 9200
9.開放防火墻端口
- 9200
- 9300 通訊端口
10.安裝es-head插件
- 安裝包npm
- chrome瀏覽器插件
11.概念
- index 庫
- type 表
- doc 行數(shù)據(jù)
- fields 字段
- shards 分片 分庫 分表
12.操作命令CRUD
- put
- get
- post
- delete
13.集群配置
cluster.name: linux58 #集群模式函卒,必須打開,同一個集群要全部一樣
node.name: node-1 #節(jié)點名稱,每個節(jié)點都不一樣
path.data: /data/elasticsearch #如果你更換了目錄,要授權(quán)給es用戶和組
path.logs: /var/log/elasticsearch #如果是集群模式豺总,日志名為{集群名.log}
bootstrap.memory_lock: true #內(nèi)存鎖定困曙,一定要打開鳄哭,然后修改system配置
network.host: 10.0.0.51,127.0.0.1 #綁定內(nèi)網(wǎng)IP,本地IP可以選擇不做
http.port: 9200 #默認(rèn)9200,還有一個隱藏的通訊端口,9300
discovery.zen.ping.unicast.hosts: ["10.0.0.51", "10.0.0.52"] #集群發(fā)現(xiàn)地址,只要包含自己和集群內(nèi)任意一個節(jié)點就可以。
discovery.zen.minimum_master_nodes: 1 #選舉相關(guān)參數(shù),多數(shù)節(jié)點數(shù)量node/2 + 1
http.cors.enabled: true #為了讓es-head可以訪問es,同下
http.cors.allow-origin: "*" #為了讓es-head可以訪問es,同上
14.介紹集群的相關(guān)重要信息
- 分片數(shù)和副本數(shù)
- 默認(rèn)創(chuàng)建為5分片嫂沉,1副本
- 分片一旦創(chuàng)建好就不能改變了
- 副本數(shù)可以隨時動態(tài)調(diào)整
- 集群健康狀態(tài)
-- red #有數(shù)據(jù)不完整
--yellow #數(shù)據(jù)都完整蚓土,但是有索引的副本或分片狀態(tài)不正常
--green #數(shù)據(jù)都完整蜀漆,副本也符合條件
-監(jiān)控節(jié)點數(shù)量
15.模擬故障現(xiàn)象
- 停掉服務(wù)器谅河,觀察集群狀態(tài)
- 要注意,不能短時間內(nèi)連續(xù)壞多臺
- 要注意,不能短時間關(guān)閉重啟多個回合
- 如果是0副本旧蛾,數(shù)據(jù)所在的哪臺機器莽龟,不能壞掉,不能數(shù)據(jù)丟失
16.Kibana管理es集群
- 打開監(jiān)控功能
- DevTools可以方便執(zhí)行命令
17.中文分詞器
- 所有es節(jié)點都安裝
- 所有es節(jié)點安裝完成后要重啟才能生效
- 先創(chuàng)建索引
- 在創(chuàng)建映射關(guān)系(類似于mysql建表語句)
- 然后指定使用中文分詞器
- 搜素測試
ELK功能圖
image.png
ELK架構(gòu)圖
image.png
1锨天、ELK日志收集
E: Elasticsearch
F: Filebeat
L: Logstash
K: kibana
2毯盈、常規(guī)分析日志的操作
2.1、分析需求:
1.找出訪問網(wǎng)站頻次最高的IP排名前十
2.找出訪問網(wǎng)站排名前十的URL
3.找出中午10點到2點之間www網(wǎng)站訪問頻次最高的IP
4. 對比昨天這個時間段和今天這個時間段訪問頻次有什么變化
5.對比上周這個時間和今天這個時間的區(qū)別
6.找出特定的頁面被訪問了多少次
7.找出有問題的IP地址病袄,并告訴我這個IP地址都訪問了什么頁面搂赋,在對比前幾天他來過嗎?他從什么時間段開始訪問的益缠,什么時間段走了
8.找出來訪問最慢的前十個頁面并統(tǒng)計平均響應(yīng)時間脑奠,對比昨天這也頁面訪問也這么慢嗎?
9.找出搜索引擎今天各抓取了多少次幅慌?抓取了哪些頁面宋欺?響應(yīng)時間如何?
10.找出偽造成搜索引擎的IP地址
11.5分鐘之內(nèi)告訴我結(jié)果
2.2胰伍、需要收集哪些日志
系統(tǒng)層面: message secure
代理層:nginx haproxy lvs
web層:nginx tomcat php apache
數(shù)據(jù)庫層:mysql redis mongo elasticsearch
3齿诞、收集nginx日志
3.1、還原環(huán)境骂租,方便查詢?nèi)罩?/h1>
systemctl stop elasticsearch
systemctl stop kibana
rm -rf /var/lib/kibana/*
rm -rf /data/elasticsearch/*
systemctl start elasticsearch
systemctl start kibana
3.1祷杈、收集nginx日志
1.安裝啟動nginx
yum install ntpdate -y
ntpdate time1.aliyun.com
yum install nginx -y
systemctl start nginx
2.安裝filebeat
1)上傳壓縮包
2)安裝
yum install filebeat-6.6.0-x86_64.rpm
3.修改配置文件
vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: false
paths:
- /var/log/nginx/access.log
output.elasticsearch:
hosts: ["10.0.0.51:9200"]
4.啟動filebeat
systemctl start filebeat.service
systemctl enable filebeat.service
image.png
image.png
3.2、filebeat原理:
1.類似于tail -f
2.30秒檢測一下日志有沒有發(fā)生變化
3.3渗饮、停掉filebeat在啟動中間數(shù)據(jù)如何處理
1. 直接從最后開始讀取的話數(shù)據(jù)就會丟失
2. 從頭開始讀取數(shù)據(jù)就會重復(fù)收集
3. 從斷開的地方開始讀取
3.4但汞、不完善的地方
不能單獨統(tǒng)計展示訪問網(wǎng)站的某項內(nèi)容
3.5、nginx日志格式
我們期望的格式
$remote_addr:10.0.0.1
$remote_user:-
[$time_local]:[10/Jul/2019:17:59:52 +0800]
$request:GET/db01.html HTTP/1.1"
$status :404
$body_bytes_sent:3650
$http_referer:-
$http_user_agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36
$http_x_forwarded_for:-
操作步驟:注意互站!所有nginx服務(wù)器都需要操作私蕾!
1.修改nginx配置文件
log_format json '{ "time_local": "$time_local", '
'"remote_addr": "$remote_addr", '
'"referer": "$http_referer", '
'"request": "$request", '
'"status": $status, '
'"bytes": $body_bytes_sent, '
'"agent": "$http_user_agent", '
'"x_forwarded": "$http_x_forwarded_for", '
'"up_addr": "$upstream_addr",'
'"up_host": "$upstream_http_host",'
'"upstream_time": "$upstream_response_time",'
'"request_time": "$request_time"'
' }';
access_log /var/log/nginx/access.log json;
2.重啟nginx
nginx -t
systemctl reload nginx
3.重新訪問nginx產(chǎn)生新數(shù)據(jù)
curl 10.0.0.51
4.修改后的日志格式
{
"time_local": "10/Jul/2019:18:55:59 +0800",
"remote_addr": "10.0.0.1",
"referer": "-",
"request": "GET /db01.html HTTP/1.1",
"status": 404,
"bytes": 3650,
"agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
"x_forwarded": "-",
"up_addr": "-",
"up_host": "-",
"upstream_time": "-",
"request_time": "0.000"
}
5.清空nginx日志
> /var/log/nginx/access.log
6.修改filebeat配置文件
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
json.keys_under_root: true
json.overwrite_keys: true
output.elasticsearch:
hosts: ["10.0.0.51:9200"]
7.es刪除舊的索引
8.重啟filebeat
systemctl restart filebeat
image.png
systemctl stop elasticsearch
systemctl stop kibana
rm -rf /var/lib/kibana/*
rm -rf /data/elasticsearch/*
systemctl start elasticsearch
systemctl start kibana
1.安裝啟動nginx
yum install ntpdate -y
ntpdate time1.aliyun.com
yum install nginx -y
systemctl start nginx
2.安裝filebeat
1)上傳壓縮包
2)安裝
yum install filebeat-6.6.0-x86_64.rpm
3.修改配置文件
vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: false
paths:
- /var/log/nginx/access.log
output.elasticsearch:
hosts: ["10.0.0.51:9200"]
4.啟動filebeat
systemctl start filebeat.service
systemctl enable filebeat.service
image.png
image.png
1.類似于tail -f
2.30秒檢測一下日志有沒有發(fā)生變化
1. 直接從最后開始讀取的話數(shù)據(jù)就會丟失
2. 從頭開始讀取數(shù)據(jù)就會重復(fù)收集
3. 從斷開的地方開始讀取
不能單獨統(tǒng)計展示訪問網(wǎng)站的某項內(nèi)容
我們期望的格式
$remote_addr:10.0.0.1
$remote_user:-
[$time_local]:[10/Jul/2019:17:59:52 +0800]
$request:GET/db01.html HTTP/1.1"
$status :404
$body_bytes_sent:3650
$http_referer:-
$http_user_agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36
$http_x_forwarded_for:-
操作步驟:注意互站!所有nginx服務(wù)器都需要操作私蕾!
1.修改nginx配置文件
log_format json '{ "time_local": "$time_local", '
'"remote_addr": "$remote_addr", '
'"referer": "$http_referer", '
'"request": "$request", '
'"status": $status, '
'"bytes": $body_bytes_sent, '
'"agent": "$http_user_agent", '
'"x_forwarded": "$http_x_forwarded_for", '
'"up_addr": "$upstream_addr",'
'"up_host": "$upstream_http_host",'
'"upstream_time": "$upstream_response_time",'
'"request_time": "$request_time"'
' }';
access_log /var/log/nginx/access.log json;
2.重啟nginx
nginx -t
systemctl reload nginx
3.重新訪問nginx產(chǎn)生新數(shù)據(jù)
curl 10.0.0.51
4.修改后的日志格式
{
"time_local": "10/Jul/2019:18:55:59 +0800",
"remote_addr": "10.0.0.1",
"referer": "-",
"request": "GET /db01.html HTTP/1.1",
"status": 404,
"bytes": 3650,
"agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
"x_forwarded": "-",
"up_addr": "-",
"up_host": "-",
"upstream_time": "-",
"request_time": "0.000"
}
5.清空nginx日志
> /var/log/nginx/access.log
6.修改filebeat配置文件
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
json.keys_under_root: true
json.overwrite_keys: true
output.elasticsearch:
hosts: ["10.0.0.51:9200"]
7.es刪除舊的索引
8.重啟filebeat
systemctl restart filebeat
image.png