ELK 套件搭建

http://blog.csdn.net/a464057216/article/category/6135397 (ELK安裝配置系列教程)

http://www.cnblogs.com/onetwo/p/6059231.html (ELK日誌分析平臺搭建全過程) 

http://blog.chinaunix.net/uid-23916356-id-5764256.html (ELK5.4.0部署參考文檔 )

http://zhengmingjing.blog.51cto.com/1587142/d-6 (ELK+Filebeat+Nginx集中式日誌解決方案)

http://blog.chinaunix.net/uid-26168435-id-5759443.html (CentOS7部署ELK5.2  (yum安裝))

http://www.cnblogs.com/NextNight/p/6826621.html (ELK5.3.1從單機到分佈式的安裝與使用)

https://kibana.logstash.es/content/ (ELK stack 中文指南)

http://blog.csdn.net/q15150676766/article/category/7042899 (ELK學習筆記)

 

 http://blog.csdn.net/napoay/article/category/6507166 (Elasticsearch 教程)

http://www.cnblogs.com/ljhdo/category/754111.html  (Elasticsearch 教程入門8篇)

http://www.cnblogs.com/zlslch/category/972313.html (ELK集羣搭建)

http://www.cnblogs.com/zlslch/category/950999.html (ELK概念學習系列)

 

http://www.54tianzhisheng.cn/2017/10/15/ElasticSearch-cluster-health-metrics/ (ElasticSearch 集羣監控 )

https://elasticsearch.cn/topic/Elastic%E6%97%A5%E6%8A%A5 (Elastic中午社區日報)

ELK是三個開源工具組成,簡單解釋如下:

Elasticsearch是個開源分佈式搜索引擎,它的特點有:分佈式,零配置,自動發現,索引自動分片,索引副本機制,restful風格接口,多數據源,自動搜索負載等。

Logstash是一個完全開源的工具,它可以對你的日誌進行收集、過濾,並將其存儲供以後使用(如,搜索)。

Kibana 也是一個開源和免費的工具,它可以爲 Logstash 和 ElasticSearch 提供的日誌分析友好的 Web 界面,可以幫助您彙總、分析和搜索重要數據日誌。

場景分析:

日誌主要包括系統日誌、應用程序日誌和安全日誌等等。運維人員和開發人員可以通過日誌瞭解服務器軟硬件信息、檢查配置過程中的錯誤及錯誤發生的原因。經常分析日誌可以瞭解服務器的負荷,性能安全性,從而及時採取措施糾正錯誤。

通常,日誌被分散的儲存不同的設備上。如果你管理數十上百臺服務器,你還在使用依次登錄每臺機器的傳統方法查閱日誌。這樣是不是感覺很繁瑣和效率低下。當務之急我們使用集中化的日誌管理,例如:開源的syslog,將所有服務器上的日誌收集彙總。

集中化管理日誌後,日誌的統計和檢索又成爲一件比較麻煩的事情,一般我們使用grep、awk和wc等Linux命令能實現檢索和統計,但是對於要求更高的查詢、排序和統計等要求和龐大的機器數量依然使用這樣的方法難免有點力不從心。

這裏採用開源實時日誌分析ELK平臺能夠完美的解決我們上述的問題,當然也還有別的平臺或者工具可以使用,這裏只討論ELK,官方網站:https://www.elastic.co

截止目前elk官網提供最新穩定版本爲5.5.0

 

需要實現效果:

1、系統messages日誌通過本地beat方式(數據不做任何處理)導入到elasticsearch中,最後通過kibana可以查詢。

2、Apache訪問日誌通過遠程beat方式(數據經過處理)導入到elasticsearch中,通過kibana可以搜索日誌中任何一個字段展示,也可以組合模糊查詢索。 也就是說將apache日誌按json格式存儲到elasticsearch中。

3、不同客戶端的Nginx訪問日誌、Apache訪問日誌、系統日誌、tomcat日誌均通過不同的匹配條件正則處理導入到elasticsearch中。Nginx和系統日誌需要編寫簡單對應的正則表達式。

主要注意事項:

1、Elk版本號保持一致。
2、所有節點操作系統版本最好保持一致,儘可能使用目前centos7.3穩定版本。Elk三個節點的配置需要比其它節點都要高一點,爲2C4G,其它均爲2C2G。內存太低這都是我踩過的坑。要求所有節點均能通外網,需要安裝軟件包。

 

3、關閉防火牆和selinux。

4、elk爲了統一都使用tar包安裝所有軟件。用yum安裝特別是logstash會遇到很多坑。

5、搭建過程不難,難的是各個項目互相調試,難的是elk的高級使用方法。

安裝步驟:

1、Elk三個節點安裝jdk,jdk可以去oracle官方網站下載,版本號可以和我的不一樣

ELK 5.0 必須用jdk1.8 以上版本。jdk安裝看

http://yjph83.iteye.com/blog/2097789

2、安裝elasticsearch節點

   wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.0.tar.gz

   tar zxvf elasticsearch-5.5.0.tar.gz

   mv elasticsearch-5.5.0 /usr/local/elasticsearch

   cd /usr/local/elasticsearch/config

   備份elasticsearch默認配置文件,以防修改出錯

   cp elasticsearch.yml elasticsearch.yml.default

官網上的很多種安裝方法說明:官網安裝說明

elasticsearch.yml 的配置說明:ElasticSearch集羣配置詳解

編輯後如下:

 cluster.name: mych-es-cluster

 node.name: node-1

 path.data: /usr/local/elasticsearch/data                     #此目錄必須大空間

 path.logs: /usr/local/elasticsearch/log

 network.host: 10.129.4.46

 http.port: 9200

#--下邊2行一般可以不需要,是用於2臺es以上的集羣才啓用

 discovery.zen.ping.unicast.hosts: ["10.129.4.46"]              #用,分開的“” 所有節點IP

 discovery.zen.minimum_master_nodes: 1                         #集羣可做master的最小節點數

## 以下兩項設置es5.x版本的head插件可以訪問es

http.cors.enabled: true

http.cors.allow-origin: "*"                                    

添加elasticsearch用戶,tar包啓動必須使用普通用戶運

[root@localhost config]# useradd es        #(elasticsearch 的簡寫)

[root@localhost config]# chown -R es:es /usr/local/elasticsearch

 用戶名可以隨便取:我也可以設置爲es

修改vim /etc/sysctl.conf,此處是修改elasticsearch 的虛擬內存,添加如下內容:

  vm.max_map_count = 655360

 並執行  sysctl -p /etc/sysctl.conf

修改打開vim /etc/security/limits.conf文件,修改打開文件句柄數

*   soft    nproc    65536

*   hard   nproc    65536

*   soft    nofile    65536

*   hard   nofile    65536

此時,我們就基本配置完成,則需要切換到es用戶下,啓動elasticsearch;

 su - es        

 cd /usr/local/elasticsearch

 bin/elasticsearch

 注意:第一次啓動需要一些時間,因爲需要做一些初始化動作,如果沒啓動成功請查系elasticsearch的相關日誌解決。注意上面只是前臺啓動調試,在後臺需要加&,需要重新啓動。

或者後臺啓動用如下命令:

[es@localhost ~]$ /usr/local/elasticsearch/bin/elasticsearch -d

-d 就表示後臺運行;也可以直接 將-d 換成 & ;

停止用  jps 查看elasticsearch 進程號,用kill -9 進程號    停止

 

我遇到幾個問題:

ERROR: [2] bootstrap checks failed

[1]: max number of threads [1024] for user [es] is too low, increase to at least [2048]

[2]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk

 

解決方案:

針對【1】 只需要如下執行:

vim /etc/security/limits.d/90-nproc.conf

 *        soft    nproc     2048             # 此行1024修改爲2048

 針對【2】 則

在elasticsearch.yml中在Memory下面配置:

bootstrap.memory_lock: false
bootstrap.system_call_filter: false

 

常見問題:ElasticSearch 5.x 安裝部署常見錯誤或問題 

 

查看端口是否開啓

 [root@localhost config]# netstat -an | more

tcp        0      0 ::ffff:10.129.4.46:9200     :::*                        LISTEN      

tcp        0      0 ::ffff:10.129.4.46:9300     :::*                        LISTEN    

可以查看到9200 和 9300 2個端口已經啓用。即表示elasticsearch安裝成功。

 通過訪問本機測試:

[root@localhost config]# curl  http://10.129.4.46:9200

{

  "name" : "node-1",

  "cluster_name" : "mych-es-cluster",

  "cluster_uuid" : "W4TPOu5hT8q4WXHpiE6ddQ",

  "version" : {

    "number" : "5.5.0",

    "build_hash" : "260387d",

    "build_date" : "2017-06-30T23:16:05.735Z",

    "build_snapshot" : false,

    "lucene_version" : "6.6.0"

  },

  "tagline" : "You Know, for Search"

}

[root@localhost config]# 

 

         elasticsearch基本的API格式 curl  -X請求方法(大寫) IP地址 :port/ES中定義的Index/ES中定義的type/document的ID。注意:elasticsearch的增加是用的PUT方法,更新是POST方法,這和一般的REST規範有所不同。

 

3、安裝logstash節點

 直接將logstash-5.5.0.tar.gz 解壓並移動到/usr/local/目錄下:

[root@localhost home]# tar -zxvf logstash-5.5.0.tar.gz

[root@localhost home]# mv logstash-5.5.0 /usr/local/logstash/

Logstash流水線有兩個必須要素:input和output,filter要素是可選的。input插件讀入數據,filter插件按照配置調整數據格式,output插件將數據寫到目的地。

 

下面通過最簡單的Logstash管道測試一下logstash是否安裝成功:

[root@localhost ~]# cd /usr/local/logstash/

[root@localhost logstash]# ./bin/logstash -e "input{stdin{}} output{stdout{}}"

 ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

Sending Logstash's logs to /usr/local/logstash/logs which is now configured via log4j2.properties

[2017-07-18T09:39:36,551][INFO ][logstash.setting.writabledirectory] Creating directory {:setting=>"path.queue", :path=>"/usr/local/logstash/data/queue"}

[2017-07-18T09:39:36,556][INFO ][logstash.setting.writabledirectory] Creating directory {:setting=>"path.dead_letter_queue", :path=>"/usr/local/logstash/data/dead_letter_queue"}

[2017-07-18T09:39:36,578][INFO ][logstash.agent           ] No persistent UUID file found. Generating new UUID {:uuid=>"29cf823c-a6ae-4af3-a241-9b806b381010", :path=>"/usr/local/logstash/data/uuid"}

[2017-07-18T09:39:36,715][INFO ][logstash.pipeline        ] Starting pipeline {"id"=>"main", "pipeline.workers"=>8, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>5, "pipeline.max_inflight"=>1000}

[2017-07-18T09:39:36,753][INFO ][logstash.pipeline        ] Pipeline main started

The stdin plugin is now waiting for input:

[2017-07-18T09:39:36,839][INFO ][logstash.agent           ] Successfully started Logstash API endpoint {:port=>9600}

      此時表示安裝成功,但是log4j2 的日誌配置文件沒有找到,我們直接輸入:

 hello word!

2017-07-18T01:39:49.222Z localhost.localdomain hello word!

      輸入Ctrl+D則會退出Logstash。-e參數是的Logstash可以直接在命令行模式配置Logstash的管道配置,使用-f參數可以指定Logstash配置文件路徑,使用-h參數可以查看更多的命令行幫助信息。

 

實際使用中Logstash一般配合Elasticsearch和Filebeat使用

       Logstash使用beats input plugin接收來自beat的數據(適配所有遵循beats框架的beat),然後使用Elasticsearch output plugin向Elasticsearch發送數據,比如在/usr/local/logstash/config目錄下添加配置文件beats-input.conf:

 input {

beats { port => 5044 type => "logs" } }

 再添加如下配置文件output.conf:

output { elasticsearch { hosts => "10.129.4.46:9200" index => "%{[@metadata][beat]}-%{+YYYY.MM.dd}" document_type => "%{[@metadata][type]}" } }

 可以將beats-input.conf和output.conf 的內容拷貝到一個文件  logstash_test.conf中,則我們可以用如下命令啓動:

[root@localhost logstash]# bin/logstash -f config/logstash_test.conf &

[2017-07-18T14:27:39,024][INFO ][logstash.pipeline        ] Starting pipeline {"id"=>"main", "pipeline.workers"=>8, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>5, "pipeline.max_inflight"=>1000}

[2017-07-18T14:27:40,657][INFO ][logstash.inputs.beats    ] Beats inputs: Starting input listener {:address=>"0.0.0.0:5044"}

[2017-07-18T14:27:40,738][INFO ][logstash.pipeline        ] Pipeline main started

[2017-07-18T14:27:41,038][INFO ][logstash.agent           ] Successfully started Logstash API endpoint {:port=>9600}

即表示啓動成功!後臺運行程序則:nohup  bin/logstash -f config/logstash_test.conf &

       當然,需要在Beat的配置文件中指定將收集到的數據發送給Logstash,下面以使用Filebeat收集日誌爲例講解Logstash與Filebeat配合的方法。

 

 4、安裝Filebeat

[root@localhost home]# tar -zxvf filebeat-5.5.0-linux-x86_64.tar.gz

[root@localhost home]# mv filebeat-5.5.0-linux-x86_64 /usr/local/filebeat

[root@localhost logstash]# cd /usr/local/filebeat

[root@localhost filebeat]# cp filebeat.yml filebeat.yml.default

 

      filebeat安裝目錄下一般都有 filebeat.xxx.template.json這個文件可以導入到es中作爲es的模板;

curl -XPUT http://10.129.11.86:9200/_template/filebeat?pretty -d@/usr/local/filebeat/filebeat.template.json

注意: 因爲es一旦生產索引後,只能動態增加字段類型,而不能修改原索引的類型;故要修改新索引的類型等屬性,必須修改 該模板並重新覆蓋。

 

編輯 filebeat.yml

- input_type: log

 

  # Paths that should be crawled and fetched. Glob based paths.

  paths:

    - /var/log/club.changhong.com.log      #這裏用club.changhong.com.log作爲我們的日誌輸入項

 

然後配置Filebeat向Logstash輸出,需要在output字段將向Elasticsearch輸出的配置註釋掉,然後將向Logstash的輸出的配置去掉註釋。比如:

output.logstash:

  # The Logstash hosts

  hosts: ["localhost:5044"]

 

我們啓動filebeat:

[root@localhost filebeat]# ./filebeat &

 

        爲了不讓一些執行信息輸出到前臺(控制檯),我們還會加上剛纔提到的>/dev/null 2>&1命令來丟棄所有的輸出:

      nohup ./filebeat -e -c filebeat.yml >/dev/null 2>&1 &

-c:配置文件位置 

-e:關閉日誌輸出

     /dev/null 2>&1    不清楚請看這裏

查看logs目錄下的日誌:



 再查看進程:

ps -ef|grep filebeat

root     12511 16196 14 15:08 pts/4    00:00:01 ./filebeat

es       12643   900  0 15:08 pts/5    00:00:00 grep filebeat

表示filebeat啓動成功!

 

5、安裝kibana

同上邊一樣,解壓kibana的安裝包;

移動到指定目錄下:

備份kibana.yml

[root@localhost config]# tar -zxvf kibana-5.5.0-linux-x86_64.tar.gz

[root@localhost config]# mv kibana-5.5.0-linux-x86_64 /opt/kibana

[root@localhost config]# cd /opt/kibana

[root@localhost config]# cp kibana.yml ./kibana.yml.default

 

vim config/kibana.yml 編輯填寫如下內容

server.port: 5601

server.host: "10.129.4.46"

elasticsearch.url: "http://10.129.4.46:9200"

保存退出, 再用如下命令後臺啓動:

[root@localhost kibana]# nohup ./bin/kibana &

[2] 704

[root@localhost kibana]#   log   [09:35:48.269] [info][status][plugin:[email protected]] Status changed from uninitialized to green - Ready

  log   [09:35:48.380] [info][status][plugin:[email protected]] Status changed from uninitialized to yellow - Waiting for Elasticsearch

  log   [09:35:48.433] [info][status][plugin:[email protected]] Status changed from uninitialized to green - Ready

  log   [09:35:48.508] [info][status][plugin:[email protected]] Status changed from uninitialized to green - Ready

  log   [09:35:48.789] [info][status][plugin:[email protected]] Status changed from uninitialized to green - Ready

  log   [09:35:48.795] [info][listening] Server running at http://10.129.4.46:5601

  log   [09:35:48.798] [info][status][ui settings] Status changed from uninitialized to yellow - Elasticsearch plugin is yellow

  log   [09:35:48.839] [info][status][plugin:[email protected]] Status changed from yellow to green - Kibana index ready

  log   [09:35:48.841] [info][status][ui settings] Status changed from yellow to green - Ready

地址欄輸入:ip:端口  進行驗證。OK

 

可以通過如下命令查查:

[root@localhost ~]# lsof -i :5601

COMMAND  PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME

node    5036 root   12u  IPv4 35586466      0t0  TCP 10.129.4.46:esmagent (LISTEN)

如果沒安裝lsof 命令,則 yum install lsof 即可;

 

停止則  kill -9 PID     # PID 就是lsof 命令下的PID

 

       以上就表示基本跑通了,但是我們實際應用還有許多的調優等;

1、將logstash_test.conf 文件彙總添加

input {

  beats {

    port => 5044

    type => "logs"

  }

}

 

filter {

  grok {

     match => {"message" => "%{COMBINEDAPACHELOG} %{QS:x_forwarded_for}" }

  }

  date {

     match => [ "timestamp" , "dd/MMM/YYYY:HH:mm:ss Z" ]

  }

  geoip {

     source => "clientip"

  }

}

 

output {

  elasticsearch {

    hosts => "10.129.4.46:9200"

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

    document_type => "%{type}"

    flush_size => 20000

    idle_flush_time => 10

    sniffing => true

    template_overwrite => true

  }

}

 

但是,我啓動kibana 時報錯:

 

 最後在 elasticsearch.yml 配置文件中添加以下2行配置重啓搞定;

http.cors.enabled: true

http.cors.allow-origin: "*"

 

es集羣配置一般有如下屬性選擇:

network.host: <hostname> , _local_
cluster.name: <clustername>
node.name: <nodename>
node.master: true
node.data: true
discovery.zen.ping.multicast.enabled: false
discovery.zen.ping.unicast.hosts: [ <host1>, <host2>, <host3>]
discovery.zen.minimum_master_nodes: 2
http.cors.enabled: true
http.cors.allow-origin: "*"
index.number_of_shards: 3
index.number_of_replicas: 1
index.translog.durability: async
index.translog.flush_threshold_size: 1g
index.refresh_interval: 30s
bootstrap.mlockall: true
threadpool.bulk.type: fixed
threadpool.bulk.queue_size:  5000

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章