ELK搭建與使用詳解

1.環(huán)境部署

安裝軟件 主機(jī)名 IP地址 內(nèi)存需求
Elasticsearch mes 192.168.13.142 至少3G
Logstash log 192.168.13.143 至少2G
head,Kibana head-kib 192.168.13.139 至少2G

地址與版本

Elasticsearch: 6.5.4  #https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.4.tar.gz
Logstash: 6.5.4  #https://artifacts.elastic.co/downloads/logstash/logstash-6.5.4.tar.gz
Kibana: 6.5.4  #https://artifacts.elastic.co/downloads/kibana/kibana-6.5.4-linux-x86_64.tar.gz
Kafka: 2.11-2.1  #https://archive.apache.org/dist/kafka/2.1.0/kafka_2.11-2.1.0.tgz
Filebeat: 6.5.4
相應(yīng)的版本最好下載對應(yīng)的插件

相關(guān)地址:
官網(wǎng)地址

2.Elasticsearch部署

系統(tǒng)類型:Centos7.5
節(jié)點IP:172.16.13.142
軟件版本:jdk-8u211-linux-x64.tar.gz东羹、elasticsearch-6.5.4.tar.gz
1.安裝配置jdk

JDK下載地址

[root@mes ~]# tar xzf jdk-8u211-linux-x64.tar.gz -C /usr/local/ #解壓
[root@mes ~]# cd /usr/local/
[root@mes local]# mv jdk1.8.0_211/ java 
[root@mes local]# echo '
JAVA_HOME=/usr/local/java
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH
' >>/etc/profile
[root@mes local]# source /etc/profile
[root@mes local]# java -version   #出現(xiàn)版本號就代表jdk配置成功
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
2.安裝配置ES
[root@mes ~]# useradd elsearch #創(chuàng)建運(yùn)行用戶
[root@mes ~]# echo "123456" | passwd --stdin "elsearch"  #設(shè)置密碼
[root@mes ~]# tar xzf elasticsearch-6.5.4.tar.gz -C /usr/local/
[root@mes ~]# cd /usr/local/elasticsearch-6.5.4/config/
[root@mes config]# cp elasticsearch.yml elasticsearch.yml.bak #備份
[root@mes config]# vim elasticsearch.yml    
# 在最后一行添加如下內(nèi)容
cluster.name: elk
node.name: elkyjssjm
node.master: true
node.data: true
path.data: /data/elasticsearch/data
path.logs: /data/elasticsearch/logs
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
network.host: 0.0.0.0
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"
#discovery.zen.ping.unicast.hosts: ["192.168.246.234", "192.168.246.231","192.168.246.235"]
#discovery.zen.minimum_master_nodes: 2
#discovery.zen.ping_timeout: 150s
#discovery.zen.fd.ping_retries: 10
#client.transport.ping_timeout: 60s

參數(shù)詳解

cluster.name        集群名稱剂桥,各節(jié)點配成相同的集群名稱。
node.name       節(jié)點名稱百姓,各節(jié)點配置不同渊额。
node.master     指示某個節(jié)點是否符合成為主節(jié)點的條件。
node.data       指示節(jié)點是否為數(shù)據(jù)節(jié)點。數(shù)據(jù)節(jié)點包含并管理索引的一部分旬迹。
path.data       數(shù)據(jù)存儲目錄火惊。
path.logs       日志存儲目錄。
bootstrap.memory_lock       內(nèi)存鎖定奔垦,是否禁用交換屹耐。
bootstrap.system_call_filter    系統(tǒng)調(diào)用過濾器。
network.host    綁定節(jié)點IP椿猎。
http.port       端口惶岭。
discovery.zen.ping.unicast.hosts    提供其他 Elasticsearch 服務(wù)節(jié)點的單點廣播發(fā)現(xiàn)功能。
discovery.zen.minimum_master_nodes  集群中可工作的具有Master節(jié)點資格的最小數(shù)量犯眠,官方的推薦值是(N/2)+1按灶,其中N是具有master資格的節(jié)點的數(shù)量。
discovery.zen.ping_timeout      節(jié)點在發(fā)現(xiàn)過程中的等待時間筐咧。
discovery.zen.fd.ping_retries        節(jié)點發(fā)現(xiàn)重試次數(shù)鸯旁。
http.cors.enabled               是否允許跨源 REST 請求,表示支持所有域名量蕊,用于允許head插件訪問ES铺罢。
http.cors.allow-origin              允許的源地址。
設(shè)置JVM堆大小残炮,一般設(shè)置為內(nèi)存的一半韭赘,但最少2G
[root@mes ~]# sed -i 's/-Xms1g/-Xms2g/' /usr/local/elasticsearch-6.5.4/config/jvm.options
[root@mes ~]# sed -i 's/-Xmx1g/-Xmx2g/' /usr/local/elasticsearch-6.5.4/config/jvm.options
創(chuàng)建ES數(shù)據(jù)及日志存儲目錄并修改屬主和屬組毁枯,與上面配置文件中的路徑一一對應(yīng)
[root@mes ~]# mkdir -p /data/elasticsearch/data       
[root@mes ~]# mkdir -p /data/elasticsearch/logs 
[root@mes ~]# chown -R elsearch:elsearch /data/elasticsearch   #給剛剛創(chuàng)建的目錄修改屬主和屬組
[root@mes ~]# chown -R elsearch:elsearch /usr/local/elasticsearch-6.5.4    
系統(tǒng)優(yōu)化:
1.增加最大進(jìn)程數(shù)
[root@mes ~]# vim /etc/security/limits.conf    
#在文件最后面添加如下內(nèi)容
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
================================================================
解釋:
soft  xxx  : 代表警告的設(shè)定圾另,可以超過這個設(shè)定值捶闸,但是超過后會有警告榴芳。
hard  xxx  : 代表嚴(yán)格的設(shè)定,不允許超過這個設(shè)定的值羡宙。
nofile : 是每個進(jìn)程可以打開的文件數(shù)的限制
nproc  : 是操作系統(tǒng)級別對每個用戶創(chuàng)建的進(jìn)程數(shù)的限制
================================================================
2.增加最大內(nèi)存映射數(shù)
[root@mes ~]# vim /etc/sysctl.conf   
#添加如下
vm.max_map_count=262144  #elasticsearch用戶擁有的內(nèi)存權(quán)限太小烁登,至少需要262144贫导;
vm.swappiness=0         #表示最大限度使用物理內(nèi)存抱完,在內(nèi)存不足的情況下,然后才是swap空間
[root@mes ~]# su - elsearch
Last login: Sat Aug  3 19:48:59 CST 2019 on pts/0
[root@mes ~]$ cd /usr/local/elasticsearch-6.5.4/
[root@mes elasticsearch-6.5.4]$ ./bin/elasticsearch  #先啟動看看報錯不刃泡,需要多等一會
終止之后(等cpu降下來再終止)
[root@mes elasticsearch-6.5.4]$ nohup ./bin/elasticsearch &  #放后臺啟動
[1] 11462
nohup: ignoring input and appending output to ‘nohup.out’
[root@mes elasticsearch-6.5.4]$ tail -f nohup.out   #看一下是否啟動
或者:
su - elsearch -c "cd /usr/local/elasticsearch-6.5.4 && nohup bin/elasticsearch &"

測試:訪問http://192.168.13.142:9200/ 記得加端口9200


在這里插入圖片描述

3.head巧娱,Kibana安裝配置

系統(tǒng)類型:Centos7.5
節(jié)點IP:172.16.13.139
軟件版本:node-v4.4.7-linux-x64.tar.gz、elasticsearch-head-master.zip烘贴、phantomjs-2.1.1-linux-x86_64.tar.bz2禁添、kibana-6.5.4-linux-x86_64.tar.gz
1.head及其插件的安裝配置
1.安裝node
[root@head-kib ~]# wget https://npm.taobao.org/mirrors/node/latest-v4.x/node-v4.4.7-linux-x64.tar.gz
[root@head-kib ~]# tar -zxf node-v4.4.7-linux-x64.tar.gz –C /usr/local
[root@head-kib ~]# vim /etc/profile   
#在最后添加如下變量
NODE_HOME=/usr/local/node-v4.4.7-linux-x64
PATH=$NODE_HOME/bin:$PATH
export NODE_HOME PATH
[root@head-kib ~]# source /etc/profile
[root@head-kib ~]# node --version  #檢查node版本號
v4.4.7
2.下載head插件grunt
[root@head-kib ~]# wget https://github.com/mobz/elasticsearch-head/archive/master.zip
[root@head-kib ~]# cp elasticsearch-head-master.zip /usr/local/
[root@head-kib ~]# yum -y install unzip
[root@head-kib local]# cd /usr/local
[root@head-kib local]# unzip  elasticsearch-head-master.zip
[root@head-kib local]# cd elasticsearch-head-master/
[root@head-kib elasticsearch-head-master]# npm config set registry https://registry.npm.taobao.org #改源地址,要不然下面的安裝時間會特別長
[root@head-kib elasticsearch-head-master]# npm install -g grunt-cli  #安裝grunt
[root@head-kib elasticsearch-head-master]# grunt --version  #檢查grunt版本號
grunt-cli v1.3.2
[root@head-kib elasticsearch-head-master]# vim /usr/local/elasticsearch-head-master/Gruntfile.js   
#在95行左右
在這里插入圖片描述
[root@head-kib elasticsearch-head-master]# vim /usr/local/elasticsearch-head-master/_site/app.js     
#在4374行左右
在這里插入圖片描述
3.下載head必要的文件
[root@head-kib ~]# wget https://github.com/Medium/phantomjs/releases/download/v2.1.1/phantomjs-2.1.1-linux-x86_64.tar.bz2
[root@head-kib ~]# yum -y install bzip2
[root@head-kib ~]# tar -jxf phantomjs-2.1.1-linux-x86_64.tar.bz2 -C /tmp/  #解壓
4.運(yùn)行head
[root@head-kib ~]# cd /usr/local/elasticsearch-head-master/
[root@head-kib elasticsearch-head-master]# npm config set registry https://registry.npm.taobao.org
[root@head-kib elasticsearch-head-master]# npm install
...
grunt-contrib-jasmine@1.0.3 node_modules/grunt-contrib-jasmine
├── sprintf-js@1.0.3
├── lodash@2.4.2
├── es5-shim@4.5.13
├── chalk@1.1.3 (escape-string-regexp@1.0.5, supports-color@2.0.0, ansi-styles@2.2.1, strip-ansi@3.0.1, has-ansi@2.0.0)
├── jasmine-core@2.99.1
├── rimraf@2.6.3 (glob@7.1.4)
└── grunt-lib-phantomjs@1.1.0 (eventemitter2@0.4.14, semver@5.7.0, temporary@0.0.8, phan
[root@head-kib elasticsearch-head-master]# nohup grunt server &
[root@head-kib elasticsearch-head-master]# tail -f nohup.out 
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100

測試:訪問http://192.168.13.139:9100/


在這里插入圖片描述
2.Kibana部署
1.安裝和配置
[root@head-kib ~]# tar zvxf kibana-6.5.4-linux-x86_64.tar.gz -C /usr/local/
[root@head-kib ~]# cd /usr/local/kibana-6.5.4-linux-x86_64/config/
[root@head-kib config]# vim kibana.yml
#在末尾添加以下內(nèi)容
server.port: 5601 
server.host: "192.168.13.139"  #本機(jī)
elasticsearch.url: "http://192.168.13.142:9200" #ES節(jié)點
kibana.index: ".kibana"
====================================================================================
server.port kibana 服務(wù)端口桨踪,默認(rèn)5601
server.host kibana 主機(jī)IP地址老翘,默認(rèn)localhost
elasticsearch.url  用來做查詢的ES節(jié)點的URL,默認(rèn)http://localhost:9200
kibana.index       kibana在Elasticsearch中使用索引來存儲保存的searches, visualizations和dashboards,默認(rèn).kibana
=====================================================================================
2.啟動
[root@head-kib config]# cd ..
[root@head-kib kibana-6.5.4-linux-x86_64]# nohup ./bin/kibana & 
[1] 12054
[root@head-kib kibana-6.5.4-linux-x86_64]# nohup: ignoring input and appending output to ‘nohup.out’

我們可以用nginx進(jìn)行反向代理铺峭,不至于顯示出我們真正的IP和端口號
我是在這一臺上面部署了nginx墓怀,在任意一臺部署都可以

[root@head-kib ~]# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm   #下載源
[root@head-kib ~]# yum -y install nginx #安裝
[root@head-kib ~]# cd /etc/nginx/conf.d/
[root@head-kib conf.d]# cp default.conf default.conf.bak #備份
[root@head-kib conf.d]# vim default.conf
#清空并添加以下內(nèi)容
server {
        listen       80;
        server_name  192.168.13.139;  #你的nginx所在的機(jī)器
        #charset koi8-r;
        # access_log  /var/log/nginx/host.access.log  main;
        # access_log off;

         location / {  
             proxy_pass http://192.168.13.139:5601; #kibana所在的機(jī)器
             proxy_set_header Host $host:5601;  
             proxy_set_header X-Real-IP $remote_addr;  
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
             proxy_set_header Via "nginx";
                     }
         location /status { 
             stub_status on; #開啟網(wǎng)站監(jiān)控狀態(tài) 
             access_log /var/log/nginx/kibana_status.log; #監(jiān)控日志 
             auth_basic "NginxStatus"; }

         location /head/{
             proxy_pass http://192.168.246.13.139:9100; #head所在的機(jī)器
             proxy_set_header Host $host:9100;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header Via "nginx";
                         }  
}
root@head-kib ~]# systemctl start nginx  #啟動nginx

測試:訪問 http://192.168.13.139/
剛開始沒數(shù)據(jù),會提示你創(chuàng)建新的數(shù)據(jù)


在這里插入圖片描述

在這里插入圖片描述

4.Logstash部署

系統(tǒng)類型:Centos7.5
節(jié)點IP:192.168.13.143  
軟件版本:jdk-8u121-linux-x64.tar.gz卫键、logstash-6.5.4.tar.gz

Logstash運(yùn)行同樣依賴jdk傀履,本次為節(jié)省資源,故將Logstash安裝在了kafka244.231節(jié)點莉炉。
你想收集哪臺機(jī)器上的信息就將Logstash安裝到哪臺上钓账。

1.安裝配置jdk
[root@log ~]# tar xzf jdk-8u211-linux-x64.tar.gz -C /usr/local/ #解壓
[root@log ~]# cd /usr/local/
[root@log local]# mv jdk1.8.0_211/ java 
[root@log local]# echo '
JAVA_HOME=/usr/local/java
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH
' >>/etc/profile
[root@log local]# source /etc/profile
[root@log local]# java -version   #出現(xiàn)版本號就代表jdk配置成功
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
2.安裝配置Logstash
[root@log ~]# tar xvzf logstash-6.5.4.tar.gz -C /usr/local/ #安裝
創(chuàng)建目錄,將輸入輸出都放在該目錄里面
[root@log ~]# mkdir -p /usr/local/logstash-6.5.4/etc/conf.d
[root@log ~]# cd /usr/local/logstash-6.5.4/etc/conf.d/       
[root@log conf.d]# vim input.conf       
#在下面添加
input{                        #讓logstash可以讀取特定的事件源絮宁。
    file{                                       #從文件讀取
        path => ["/var/log/nginx/access_json.log"]        #要輸入的文件路徑
    #   code => "json"               #定義編碼梆暮,用什么格式輸入和輸出,由于日志就是json格式绍昂,這里不用再寫
        type => "nginx"                       #定義一個類型啦粹,通用選項. 用于激活過濾器
    }
}
output{           #輸出插件,將事件發(fā)送到特定目標(biāo)
    elasticsearch {            #輸出到es
        hosts => ["192.168.13.142:9200"]       #指定es服務(wù)的ip加端口
        index => ["%{type}-%{+YYYY.MM.dd}"]     #引用input中的type名稱治专,定義輸出的格式
    }
}
我們這里以nginx的日志為例
[root@log ~]# cd
[root@log ~]# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@log ~]# yum install -y nginx
將原來的日志格式注釋掉定義成json格式:
[root@log conf.d]# vim /etc/nginx/nginx.conf
# 在http模塊里面添加
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"'
               '}';
        access_log  /var/log/nginx/access_json.log  json; #引用定義的json格式的日志:
在這里插入圖片描述
啟動nginx
[root@log ~]# systemctl start nginx 
[root@log ~]# systemctl enable nginx
啟動logstash
[root@log ~]# cd /usr/local/logstash-6.5.4/
[root@log logstash-6.5.4]# nohup bin/logstash -f etc/conf.d/  --config.reload.automatic &

訪問http://192.168.13.143/ 多刷新幾次效果更明顯


在這里插入圖片描述

然后去head插件頁面查看是否有nginx索引出現(xiàn) http://192.168.13.139:9100/
發(fā)現(xiàn)之后卖陵,去配置kibanna添加索引


在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

然后你就能看見nginx日志的可視化界面了。

你們的評論和點贊是我寫文章的最大動力张峰,蟹蟹泪蔫。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市喘批,隨后出現(xiàn)的幾起案子撩荣,更是在濱河造成了極大的恐慌,老刑警劉巖饶深,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件餐曹,死亡現(xiàn)場離奇詭異,居然都是意外死亡敌厘,警方通過查閱死者的電腦和手機(jī)台猴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來俱两,“玉大人饱狂,你說我怎么就攤上這事∠懿剩” “怎么了休讳?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長尿孔。 經(jīng)常有香客問我俊柔,道長筹麸,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任雏婶,我火速辦了婚禮物赶,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘尚骄。我一直安慰自己块差,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布倔丈。 她就那樣靜靜地躺著憨闰,像睡著了一般。 火紅的嫁衣襯著肌膚如雪需五。 梳的紋絲不亂的頭發(fā)上鹉动,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天,我揣著相機(jī)與錄音宏邮,去河邊找鬼泽示。 笑死,一個胖子當(dāng)著我的面吹牛蜜氨,可吹牛的內(nèi)容都是我干的械筛。 我是一名探鬼主播,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼飒炎,長吁一口氣:“原來是場噩夢啊……” “哼埋哟!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起郎汪,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤赤赊,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后煞赢,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體抛计,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年照筑,在試婚紗的時候發(fā)現(xiàn)自己被綠了吹截。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡凝危,死狀恐怖饭弓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情媒抠,我是刑警寧澤,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布咏花,位于F島的核電站趴生,受9級特大地震影響阀趴,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜苍匆,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一刘急、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧浸踩,春花似錦叔汁、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至折剃,卻和暖如春另假,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背怕犁。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工边篮, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人奏甫。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓戈轿,卻偏偏與公主長得像,于是被迫代替她去往敵國和親阵子。 傳聞我的和親對象是個殘疾皇子思杯,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內(nèi)容