是npm install 而不是cnpm install
是npm run start而不是 npm start
此處是network.host
kibana處是server.host
ELK安裝
1 安裝elasticsearch
1.1 下載安裝包并解壓
cd /usr/local
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.2.tar.gz
tar -zxvf elasticsearch-6.6.2.tar.gz
cd elasticsearch-6.6.2
1.2 修改配置文件
vi config/Elasticsearch.yml
# 設置外網(wǎng)訪問,默認外網(wǎng)無法訪問
network.host: 0.0.0.0
1.3 修改jvm分配大小
vi config/jvm.options
# xms要和xmx大小一致暖哨,內(nèi)存大小根據(jù)自己需要調(diào)整
-Xms512m
-Xmx512m
1.4 修改系統(tǒng)環(huán)境變量
# 查看當前系統(tǒng)環(huán)境變量
sysctl -a|grep vm.max_map_count
# 結果
65530
vi /etc/sysctl.conf
# 添加如下內(nèi)容
vm.max_map_count=262144
# 此修改需重啟才能生效赌朋,若不方便重啟,執(zhí)行如下命令即可篇裁,保證當前環(huán)境做了修改
sysctl -w vm.max_map_count=262144
1.5 修改最大文件描述符數(shù)量和用戶最大線程數(shù)
vim /etc/security/limits.conf
# 添加如下內(nèi)容沛慢,星號代表所用用戶
* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096
1.6 啟動
# 添加用戶
useradd zhoutian
echo "123456" | passwd zhoutian --stdin
# 把elasticsearch設置為用戶所屬
chown -R zhoutian:zhoutian ./
# 在root下修改權限文件,授予用戶超級管理員的權限
vi /etc/sudoers
# 添加如下內(nèi)容
zhoutian ALL=(ALL) ALL
# 前臺啟動
bin/elasticsearch
# 后臺啟動
bin/elasticsearch -d
1.7 分詞器
將相關分詞器下載后解壓到elasticsearch的plugins目錄下即可使用
ik分詞器下載地址
https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v6.6.2
拼音分詞器下載地址
https://github.com/medcl/elasticsearch-analysis-pinyin/releases/tag/v6.6.2
2 安裝head插件
2.1 head插件主要用途
elasticsearch-head是一個用來瀏覽达布、與Elastic Search簇進行交互的web前端展示插件团甲。
elasticsearch-head是一個用來監(jiān)控Elastic Search狀態(tài)的客戶端插件。
elasticsearch主要有以下操作——
1)簇瀏覽黍聂,顯示簇的拓撲并允許你執(zhí)行索引(index)和節(jié)點層?的操作躺苦。
2)查詢接口,允許你查詢簇并以原始json格式或表格的形式顯示檢索結果产还。
3)顯示簇狀態(tài)匹厘,有許多快速訪問的tabs用來顯示簇的狀態(tài)。
4)支持持Restful API接口脐区,包含了許多選項產(chǎn)生感興趣的結果集乔,包括:
第一,請求方式:get坡椒、put扰路、post、delete倔叼;json請求數(shù)據(jù)汗唱、節(jié)點node、路徑path丈攒。
第二哩罪,JSON驗證器授霸。
第三,定時請求的能力际插。
第四碘耳,用javascript表達式傳輸結果的能力。
第五框弛,統(tǒng)計?段時間的結果或該段時間結果?對的能力辛辨。
第六,以簡單圖標的形式繪制傳輸結果
2.2 安裝node.js
2.2.1 下載安裝包并解壓
cd /usr/local/
wget https://nodejs.org/dist/v10.16.3/node-v10.16.3-linux-x64.tar.xz
tar -xJf node-v10.16.3-linux-x64.tar.xz
# 重命名
mv node-v10.16.3-linux-x64 nodeJS
2.2.2 修改配置文件
vim /etc/profile
# 在文件最后面追加node.js環(huán)境變量
export NODE_HOME=/usr/local/node-v10.16.3-linux-x64
export PATH=$NODE_HOME/bin:$PATH
# 刷新配置文件
source /etc/profile
2.2.3 驗證是否安裝成功
node -v
npm -v
2.3 安裝head插件
2.3.1 下載安裝包并解壓
cd /usr/local/
wget https://github.com/mobz/elasticsearch-head/archive/master.zip
unzip master.zip
2.3.2 執(zhí)行安裝命令
cd elasticsearch-head-master/
npm install
2.3.3 運行
npm run start
2.3.4 連接配置
此時未連接瑟枫,需要配置才能連接:
2.3.4.1 修改 Gruntfile.js文件:
connect: {
server: {
options: {
port: 9100,
base: '.',
keepalive: true,
hostname: '*' # 此行為新添加
}
}
}
2.3.4.2 修改app.js
vi _site/app.js
this._super();
this.prefs = services.Preferences.instance();
-- 改為自己的elasticsearch的地址和端口
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://192.168.81.130:9200";
2.3.4.3 啟動CORS
當head插件訪問es時斗搞,您必須在elasticsearch中啟動CORS,否則您的瀏覽器將拒絕跨域慷妙,因為默認情況下不允許跨域僻焚。
vi elasticsearch-6.6.2/config/elasticearch.yml
-- 在配置文件末尾添加如下內(nèi)容,
http.cors.enabled: true
http.cors.allow-origin: "*"
由于這樣配置的任何地方都可以訪問膝擂,所以有安全風險虑啤。
2.3.5 重啟ES和head插件可訪問
3 安裝logstash
3.1 LogStash插件介紹
Logstash是一個具有實時管道的開源數(shù)據(jù)收集引擎〖懿觯可以動態(tài)地統(tǒng)?不同來源的數(shù)據(jù)狞山,并將數(shù)據(jù)歸到不同目的地。也是一個管理事件和日志工具绩蜻。你可以用它來收集日志铣墨,分析它們,并將它們儲存起來以供以后使用办绝。
Logstash 通常都是和 Kibana 以及 Elasticsearch 一起使用伊约。
3.2 logStash安裝
注意版本和elasticsearch、kibana 必須保持?致孕蝉,es屡律、kibana都是6.6.2版本
3.2.1 下載安裝包并解壓
cd /usr/local/
wget https://artifacts.elastic.co/downloads/logstash/logstash-6.6.2.tar.gz
tar -zxvf logstash-6.6.2.tar.gz
3.2.2 啟動
cd logstash-6.6.2/bin/
./logstash -e 'input{ stdin{} } output{ stdout{} }'
./logstash -e 'input{ stdin{} } output{ stdout{ codec => json } }'
日志內(nèi)容寫入elasticsearch。注意elasticsearch插件的語法格式:hosts 對應數(shù)組
./logstash -e 'input{ stdin{} } output{ elasticsearch{hosts =>["192.168.81.130:9200"]} }'
日志內(nèi)容寫入elasticsearch降淮,同時輸出到控制臺
./logstash -e 'input{ stdin{} } output{ elasticsearch{hosts =>["192.168.81.130:9200"]} stdout{} }'
3.3 logStash插件
3.3.1 input插件
輸入較常用的幾個插件:stdin超埋、file、http佳鳖、tcp
3.3.2 output插件
輸入較常用的幾個插件:stdout霍殴、file、http系吩、tcp来庭、elasticsearch
3.3.3 codec插件
codec(Code Decode)插件作用于input和output插件,負責將數(shù)據(jù)在原始與Logstash之間轉(zhuǎn)換穿挨,常用的codec有:
- plain 讀取原始內(nèi)容
- dots 將內(nèi)容簡化為點進行輸出
- rubydebug 將內(nèi)容按照ruby格式輸出月弛,方便調(diào)試
- line 處理帶有換行符的內(nèi)容
- json 處理json格式的內(nèi)容
- multiline 處理多?數(shù)據(jù)的內(nèi)容
3.4 logStash配置
創(chuàng)建配置
cd ../config
touch logstash.conf
詳細配置如下
input { stdin { } }
output {
elasticsearch { hosts => ["192.168.81.130:9200"] }
stdout { codec => rubydebug }
}
啟動logstash
cd ../bin/
./logstash -f config/logstash.conf
3.5 file日志收集
創(chuàng)建新的配置文件
mv logstash.conf file.conf
詳細配置如下
input {
file{
path => "/var/log/messages" # 收集messages文件日志
type => "system"
start_position => "beginning" # 記錄上次收集的位置
}
}
output {
elasticsearch {
hosts => ["192.168.81.130:9200"] # 寫入elasticsearch的地址
index => "system-%{+YYYY.MM.dd}" # 定義索引的名稱
}
stdout { codec => rubydebug }
}
啟動logstash
cd ../bin/
./logstash -f config/file.conf
3.6 Java日志收集
#在原來file文件的基礎上進行編輯
input {
file{
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
#加一個file插件收集elasticsearch日志
#es就是java語言開發(fā)的肴盏。
file{
path => "/usr/local/elasticsearch-6.6.2/logs/elasticsearch.log"
type => "es-info"
start_position => "beginning"
}
}
output {
#判斷,導入到不同的索引庫帽衙,否則會放入同一個索引庫中
if [type] == "system"{
elasticsearch {
hosts => ["192.168.81.130:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
#判斷菜皂,導入到不同的索引庫,否則會放入同一個索引庫中
if [type] == "es-info"{
elasticsearch {
hosts => ["192.168.81.130:9200"]
index => "es-info-%{+YYYY.MM.dd}"
}
}
stdout { codec => rubydebug }
}
目前導入日志都是按照行導入的厉萝,但是有些日志多行是一句話恍飘,如果分開的話就不太容查看日志的完整的意思了〖叫海可以使用codec插件來進行解決常侣,codec把多行日志合并為一行蜡饵。
file{
path => "/usr/local/elasticsearch-6.6.2/logs/elasticsearch.log"
type => "es-info"
start_position => "beginning"
# 使用正則表達式弹渔,合并多行日志
codec => multiline {
pattern => "^\[" # 發(fā)現(xiàn)中括號就合并日志
negate => true
what => "previous"
}
}
4 安裝Kibana
4.1 kibana 插件介紹
kibana 插件提供了Marvel監(jiān)控的UI界面。kibana是一個與elasticsearch一起工作的開源的分析和可視化的平臺溯祸。使用kibana可以查詢肢专、查看并與存儲在elasticsearch索引的數(shù)據(jù)進行交互操作。使用kibana能執(zhí)行高級的數(shù)據(jù)分析焦辅,并能以圖表博杖、表格和地圖的形式查看數(shù)據(jù)。 kibana使得理解大容量的數(shù)據(jù)變得非常容易筷登。它非常簡單剃根,基于瀏覽器的接口使我們能夠快速的創(chuàng)建和分享顯示elasticsearch查詢結果實時變化的儀表盤。
4.2 kibana下載
wget https://artifacts.elastic.co/downloads/kibana/kibana-6.6.2-linux-x86_64.tar.gz
tar -zxvf kibana-6.6.2-linux-x86_64.tar.gz
4.3 環(huán)境配置
cd /kibana-6.6.2-linux-x86_64/config/
vi kibana.yml
# 將默認配置改成如下
server.port: 5601
server.host: "192.168.81.130"
elasticsearch.hosts: ["http://192.168.81.130:9200"]
kibana.index: ".kibana"
4.4 啟動前方、訪問與關閉
# 前臺啟動
./bin/kibana
# 后臺啟動
nohup ./bin/kibana &
訪問地址:192.168.81.130:5601
查看啟動日志 : tail -f nohup
kibana使用ps -ef|grep kibana是查不到進程的狈醉,主要原因大概是因為kibana是node寫的。所以kibana 運行的時候是運行在node 里面惠险。
所以可以使用ps -ef|grep node查看到進程苗傅。但是帶有node不一定就是kibana的進程。怎么辦呢 班巩? 我們知道5601是kibana對外的tcp端口渣慕。所以使用netstat -tunlp|grep 5601就可以查到進程id了 。
5 nginx日志
5.1 JSON格式日志
5.1.1 文本格式(默認格式)
開啟日志抱慌,把日志寫入到日志文件
日志內(nèi)容如下所示
發(fā)現(xiàn)日志都是一行普通文本格式的日志逊桦。這樣不利于logstash收集整理,是否能把這些日志直接變成json格式呢抑进。當然是可以的强经。
5.1.2 JSON格式
日志格式轉(zhuǎn)換配置如下所示:
輸出日志引用此格式輸出日志:
日志輸出效果如下所示:
發(fā)現(xiàn)日志已經(jīng)完成變成json格式了。
5.2 logstash收集
5.2.1 配置文件
# 把日志文件日志以json格式輸入
input {
file{
path => "/usr/local/src/logs/host.access.log"
codec => json
}
}
# 在logstash 控制臺看一下輸出日志的格式
output {
stdout {
codec => rubydebug
}
}
5.2.2 導入到es
# 輸入插件
input {
file{
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
file{
path => "/home/es/elasticsearch-6.2.4/logs/elasticsearch.log"
type => "es-info"
start_position => "beginning"
}
# 收集nginx日志
file{
path => "/usr/local/src/logs/host.access.log"
type => "nginx-log"
start_position => "beginning"
codec => json
}
}
# 輸出插件
output {
if [type] == "system"{
elasticsearch {
hosts => ["192.168.66.66:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
if [type] == "es-info"{
elasticsearch {
hosts => ["192.168.66.66:9200"]
index => "es-info-%{+YYYY.MM.dd}"
}
}
# 添加nginx日志
if [type] == "nginx-log"{
elasticsearch {
hosts => ["192.168.66.66:9200"]
index => "nginx-log-%{+YYYY.MM.dd}"
}
}
# 同時在控制臺輸出
stdout { codec => rubydebug }
}