Elasticsearch+Logstash+Kibana搭建日誌平臺

1 ELK簡介

ELK是Elasticsearch+Logstash+Kibana的簡稱java

  • ElasticSearch是一個基於Lucene的分佈式全文搜索引擎,提供 RESTful API進行數據讀寫node

  • Logstash是一個收集,處理和轉發事件和日誌消息的工具python

  • Kibana是Elasticsearch的開源數據可視化插件,爲查看存儲在ElasticSearch提供了友好的Web界面,並提供了條形圖,線條和散點圖,餅圖和地圖等分析工具

總的來講,ElasticSearch負責存儲數據,Logstash負責收集日誌,並將日誌格式化後寫入ElasticSearch,Kibana提供可視化訪問ElasticSearch數據的功能。linux

1.1  環境準備

10.0.0.7           elasticsearch 
10.0.0.8           logstash 
10.0.0.9           kibana 
環境準備:
[root@es-node1 ~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core)
[root@logstash ~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[root@kibana ~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
#官網下載地址
https://www.elastic.co/downloads
  • 每臺機器須要優化操做系統不然報錯
[root@es-node1 ~]# cat /etc/security/limits.conf|tail -5
* soft nofile 65536
* hard nofile 65536
* soft nproc  65536
* hard nproc  65536
[root@es-node1 ~]# cat /etc/security/limits.d/20-nproc.conf|tail -2
*          soft    nproc     65536
root       soft    nproc     unlimited
[root@es-node1 ~]# cat /etc/sysctl.conf |tail -2
vm.max_map_count=655360
fs.file-max=655360
[root@es-node1 ~]# sysctl -p
vm.max_map_count = 655360
fs.file-max = 655360

1.2  搭建單機6.0版本elasticsearch

[root@es-node1 ~]# ll
total 197448
-rw-r--r-- 1 root root  28017602 Sep 12 23:02 elasticsearch-6.0.0.tar.gz
-rw-r--r-- 1 root root 174163338 Sep 12 23:02 jdk-8u151-linux-x64.rpm
[root@es-node1 ~]# useradd elasticsearch
[root@es-node1 ~]# mv elasticsearch-6.0.0.tar.gz /usr/local/src/
[root@es-node1 ~]# rpm -ivh jdk-8u151-linux-x64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:jdk1.8-2000:1.8.0_151-fcs        ################################# [100%]
Unpacking JAR files...
	tools.jar...
	plugin.jar...
	javaws.jar...
	deploy.jar...
	rt.jar...
	jsse.jar...
	charsets.jar...
	localedata.jar...
[root@es-node1 ~]# cd /usr/local/src/
[root@es-node1 src]# ll
total 27364
-rw-r--r-- 1 root root 28017602 Sep 12 23:02 elasticsearch-6.0.0.tar.gz
[root@es-node1 src]# tar xf elasticsearch-6.0.0.tar.gz -C ../
[root@es-node1 src]# cd ..
[root@es-node1 local]# mv elasticsearch-6.0.0/ elasticsearch
[root@es-node1 local]# cd elasticsearch/
[root@es-node1 elasticsearch]# chown -R elasticsearch.elasticsearch .
[root@es-node1 elasticsearch]# mkdir -p /data/es/logs /data/es/data
[root@es-node1 elasticsearch]# chown -R elasticsearch.elasticsearch /data/es/logs /data/es/data
[root@es-node1 elasticsearch]# su - elasticsearch
[elasticsearch@es-node1 ~]$ cd /usr/local/elasticsearch/
[elasticsearch@es-node1 elasticsearch]$ cd config/
[elasticsearch@es-node1 config]$ cp elasticsearch.yml{,.bak}
[elasticsearch@es-node1 config]$ cat elasticsearch.yml
cluster.name: es 
node.name: es1
path.data: /data/es/data
path.logs: /data/es/logs
network.host: 10.0.0.7
http.port: 9200
transport.tcp.port: 9300
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["10.0.0.7:9300"]
discovery.zen.minimum_master_nodes: 1
http.cors.enabled: true
http.cors.allow-origin: "*"
#增長端口
[root@es-node1 ~]# firewall-cmd --add-port=9200/tcp --permanent
[root@es-node1 ~]# firewall-cmd --add-port=9300/tcp --permanent
#從新加載防火牆規則
[root@es-node1 ~]# firewall-cmd --reload
#後臺啓動
[elasticsearch@es-node1 ~]$ /usr/local/elasticsearch/bin/elasticsearch -d
[root@es-node1 ~]# curl http://10.0.0.7:9200/_cluster/health  #顯示green爲正常
  • green  最健康得狀態,說明全部的分片包括備份均可用
  • yellow 基本的分片可用,可是備份不可用(或者是沒有備份)
  • red      部分的分片可用,代表分片有一部分損壞。此時執行查詢部分數據仍然能夠查到,遇到這種狀況,仍是趕快解決比較好

1.3  安裝elasticsearch-head

#安裝node步驟略
[elasticsearch@es-node1 ~]$  cd /usr/local/src/
[elasticsearch@es-node1 ~]$  git clone https://github.com/mobz/elasticsearch-head.git
[elasticsearch@es-node1 ~]$  cd /usr/local/src/elasticsearch-head/
[elasticsearch@es-node1 ~]$  npm install 
[elasticsearch@es-node1 ~]$  npm run start &

  

1.4 安裝logstash

[root@logstash ~]# ll
total 279772
-rw-r--r-- 1 root root 174163338 Sep 13 03:08 jdk-8u151-linux-x64.rpm
-rw-r--r-- 1 root root 112316625 Sep 13 03:08 logstash-6.0.0.tar.gz
 [root@logstash ~]# rpm -ivh jdk-8u151-linux-x64.rpm 
 [root@logstash ~]# tar xf logstash-6.0.0.tar.gz -C /usr/local/
 [root@logstash ~]# cd /usr/local/
 [root@logstash ~]# mv logstash-6.0.0/ logstash
  • 修改配置文件
[root@logstash config]# mkdir -p /data/logstash/{data,logs}
[root@logstash config]# cat logstash.yml 
path.data: /data/logstash/data
path.logs: /data/logstash/logs
  •  編寫收集日誌的conf文件
[root@logstash logstash]# mkdir -p /usr/local/logstash/conf.d
[root@logstash logstash]# cd  /usr/local/logstash/conf.d
[root@logstash logstash]# cat conf.d/system-log.conf 
input {
  file {
    path => "/var/log/messages"
    type => "systemlog"
    start_position => "beginning"
    stat_interval => "3"
  }
  file {
    path => "/var/log/secure"
    type => "securelog"
    start_position => "beginning"
    stat_interval => "3"
  }
}
 
output {
  if [type] == "systemlog" {
    elasticsearch {
      hosts => ["10.0.0.7:9200"]
      index => "system-log-%{+YYYY.MM.dd}"
       }
    }
  if [type] == "securelog" {
    elasticsearch {
      hosts => ["10.0.0.7:9200"]
      index => "secury-log-%{+YYYY.MM.dd}"
      }
   }
}   
#檢查文件ogstash
[root@logstash logstash]# ./bin/logstash -f ./conf.d/system-log.conf -t
#啓動logstash
[root@logstash logstash]# ./bin/logstash -f ./conf.d/system-log.conf 

  此時elasticsearch的狀態git

1.5 安裝kibana

[root@kibana src]# ll /usr/local/src/
total 61216
-rw-r--r-- 1 root root 62681537 Sep 11 10:27 kibana-6.0.0-linux-x86_64.tar.gz
[root@kibana src]# tar xf kibana-6.0.0-linux-x86_64.tar.gz
[root@kibana src]# mv kibana-6.0.0-linux-x86_64 ../kibana
#更改配置文件
[root@kibana config]# cat kibana.yml 
server.port: 5601
server.host: "10.0.0.9"
elasticsearch.url: "http://10.0.0.7:9200"
#啓動kibana
[root@kibana kibana]# ./bin/kibana
  • 添加索引

 

  • 這樣咱們最基本的收集日誌就完成了,固然了這只是開始還差不少優化以及dashboard圖形等展現
相關文章
相關標籤/搜索