ELK搭建(filebeat、elasticsearch、logstash、kibana)


ELK部署(文章有點兒長,搭建時請到官網將tar包下載好,按步驟可以完成搭建使用)

ELK指的是ElasticSearchLogStashKibana三個開源工具

LogStash是負責數據的收集和過濾處理

ElasticSearch 是一個開源分布式搜索引擎,負責數據的存儲、檢索和分析

Kibana 是提供可視化的頁面,對數據進行可視化預覽

 

首先前提安裝好java環境,java1.8

yum -y list java

yum install java-1.8.0-openjdk.x86_64

 

然后在官方下載ElasticSearch Kibana LogStash包

https://www.elastic.co/cn/downloads

這邊用的版本elasticsearch-6.3.2 Kibana-6.3.2 LogStash-6.3.2

 

 

一、對系統參數做調整

vim /etc/security/limits.conf (修改如下內容)

* soft nofile 65536

* hard nofile 65536

* soft nproc 65536

* hard nproc 65536

 

vim /etc/security/limits.d/20-nproc.conf (修改如下內容)

* soft nproc  4096

root soft nproc  unlimited

 

vim /etc/sysctl.conf  (添加如下內容)

vm.max_map_count = 655360

fs.file-max=655360

 

sysctl -p 使內容生效

確保所有生效 必須重啟服務器一遍 reboot

 

二、安裝Elasticsearch

elasticsearch需要新建一個用戶用來啟動

useradd -d /home/els -m els

su els

mkdir -p /home/els/tools/

tar包放置 /home/els/tools

cd /home/els/tools

tar -xzf elasticsearch-6.3.2.tar.gz

mv elasticsearch-6.3.2 /home/els/

cd /home/els/elasticsearch-6.3.2/

vim config/elasticsearch.yml(修改如下內容)

 

path.data: /home/els/elasticsearch-6.3.2/data    #自定義 數據存放地址

path.logs: /home/els/elasticsearch-6.3.2/logs   #自定義 日志存放地址

http.cors.enabled: true #開啟跨域訪問支持 默認為false

http.cors.allow-origin: "*" #跨域訪問允許的域名地址,支持正則

network.host: 0.0.0.0  #任意IP可以訪問

 

chown -R els.els /home/els/elasticsearch-6.3.2

nohup ./bin/elasticsearch &   

瀏覽器訪問 ip:9200 得到如下內容 便搭建成功

 

三、搭建elasticsearch_head

mkdir -p /root/ELK

cd /root/ELK

yum install git npm

git clone https://github.com/mobz/elasticsearch-head.git

cd elasticsearch-head

npm install

nohup npm run start &

然后瀏覽器訪問ip:9100 連接你的Elasticsearch  ip:9200

 

四、安裝Logstash

Logstash的安裝同樣如此,可以直接用root用戶 將6.3.2的tar包解壓,進入目錄

Vim /你定義的目錄/logstash-6.3.2/config/test.conf

input {

file{

path => [“/var/log/messages”] #這可以是個列表 多個Log日志也是可以的

    start_position => "beginning"

}

 

}

output{

ctdout{

codec => rubydebug #直接輸出到屏幕

}

elasticsearch {

        hosts => ["http://13.1.18.135:9200"]  #同時輸出到elastisearch

        index => "syslog-%{+YYYY.MM.dd}"

        action => "index"

        document_type => "test"

}

 

cd /你定義的目錄/logstash-6.3.2/

./bin/logstash  -f config/test.conf

屏幕有輸出,同時瀏覽器訪問ip:9100,也能看到根據索引的輸出信息

后期會詳細說Logstash檢索功能以及通過緩存中間件來傳遞數據接收數據

 

五、安裝kibana

同樣時將tar包解壓到你的目錄下

tar zxvf kibana-6.2.2-darwin-x86_64.tar.gz

cd /你的目錄/kibana-6.2.2-darwin-x86_64

vim config/kibana.yml (修改如下內容)

server.host: "0.0.0.0"

elasticsearch.url: "http://localhost:9200"   #我這里使用同一台服務器 不是的話請用IP

 

nohup ./bin/kibana -H 0.0.0.0 &  #后台啟用

瀏覽器訪問 ip:5601

然后到左邊菜單最下方Management 設置索引值

create Index Patern

輸入 syslog* *匹配所有,與之前Logstash中輸出outputindex

 

最后創建

然后到左邊菜單中Discover中查看信息

左邊設置當前的索引 ,右上角設置檢索日期,和設置自動更新

搭建完成

接着以一個數據傳遞方式添加一個filebeat工具 是個輕量級的數據采集 可以直接輸出到Logstash或者elasticsearch

配置同樣簡單

下載filebeat-6.3.2-linux-x86_64.tar.gz

解壓到你的目錄下 (修改下輸入參數和輸出參數)

vim filebeat-6.3.2-linux-x86_64/filebeat.yml

修改參數請按yml格式

可以輸入多個地址

輸入配置:

參數type , enabled, paths, fields(這個參數傳入Logstash字段,用於判斷)

 

輸出配置:

output.logstash:

hosts: ["13.1.18.135:5022"] #表示傳輸到主機的5022端口負責接送數據

 

開啟filebeat  注意-e -c順序

./filebeat -e -c filebeat.yml

 

#可自行使用

#output.redis:  

# hosts: ["13.1.18.135:6379"]

#  password: '123456'  #redis.confrequirepass 123456 密碼必須配置

#  key: "log_file" #傳輸的鍵值

#  db: 0   #0

#  timeout: 5  #超時時間

 

輸出配置可以有Elasticsearch,中間件緩存 redis,rabbitmq,kafka

 

此時需要修改之前的Logstash中配置文件的test.confinput

 

input {

 

       beats {

 

       port => 5022  #開啟5022端口接收數據

 

      #Filebeat傳來數據

 

}

 

}   

 

 

 

#同上的redis配置

 

#input {

 

#        redis{

 

#        port => '6379'

 

#        host => '13.1.18.135'

 

#        data_type => "list"

 

#        type => "log"

 

#        key => "log_file"

 

#        db => '0'

 

#        password => '123456'

 

#}

 

 

 

重啟啟動logstash

 

./bin/logstash  -f config/test.conf

 

當前的傳輸過程如下圖

 

 

后期需要可以為多個filebeat用於多台服務器的日志同時采集,elastcsearch也可以設置為多個節點

下篇文章關於logstash的檢索配置

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM