1. 程式人生 > >ELK+kafka日誌系統搭建-實戰

ELK+kafka日誌系統搭建-實戰

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

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

集中化管理日誌後,日誌的統計和檢索又成為一件比較麻煩的事情,一般我們使用grep、awk和wc等Linux命令能實現檢索和統計,但是對於要求更高的查詢、排序和統計等要求和龐大的機器數量依然使用這樣的方法難免有點力不從心。
  • Elasticsearch是個開源分散式搜尋引擎,它的特點有:分散式,零配置,自動發現,索引自動分片,索引副本機制,restful風格介面,多資料來源,自動搜尋負載等。
  • Logstash是一個完全開源的工具,他可以對你的日誌進行收集、過濾,並將其儲存供以後使用(如,搜尋)。
  • Kibana 也是一個開源和免費的工具,它Kibana可以為 Logstash 和 ElasticSearch 提供的日誌分析友好的 Web 介面,可以幫助您彙總、分析和搜尋重要資料日誌。
ELK+kafka日誌系統原理(介質為日誌) Windows/linux的logstash(客戶端)--->kafka(佇列)--->kakfa上的logstash(也是一個客戶端)--->ES(儲存)--->kibana(介面) 角色: 10.10.13.17  ES java 10.10.13.18  ES Java
10.10.13.15  logstash  kafka java 10.10.13.12  nginx   kibana   10.10.12.7    Linux客戶端 logstash Java 一、安裝ES (10.10.13.17) yun install -y java1.7.0-openjdk* useradd elkuser groupadd elkuser usermod -G elkuser elkuser wget https : //download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.3.tar.gz tar -xf elasticsearch-1.7.3.tar.gz -C 
/home(這有掛在分割槽,所以裝這裡) mv  /home/elasticsearch-1.7.3  /home/elasticsearch chown -R elkuser.elkuser /home/elasticsearch/* 編寫配置檔案: vi /home/elasticsearch/config/elasticsearch.yml node.data: true index.number_of_shards: 5 index.number_of_replicas: 0 bootstrap.mlockall: true network.bind_host: 10.10.13.17 network.host: 10.10.13.17 http.port: 16780 http.cors.enabled: true http.cors.allow-origin: "*" 注意:這裡預設儲存資料路徑在:/elasticsearch/data/elasticsearch/nodes/0/indices/目錄下 啟動ES #/elasticsearch/elasticsearch-1.5.2/bin/service/elasticsearch  start或stop  #手工啟動/停止 重啟指令碼restartES.sh #cat /elasticsearch/elasticsearch-1.5.2/restartES.sh            /elasticsearch/elasticsearch-1.5.2/bin/service/elasticsearch stop            sleep 10s            /elasticsearch/elasticsearch-1.5.2/bin/service/elasticsearch start 驗證elasticsearch部署是否正常,訪問http://10.10.13.17:16780/輸出如下資訊           {             "status" : 200,             "name" : "All-American",             "cluster_name" : "elasticsearch",             "version" : {               "number" : "1.5.2",               "build_hash" : "62ff9868b4c8a0c45860bebb259e21980778ab1c",               "build_timestamp" : "2015-04-27T09:21:06Z",               "build_snapshot" : false,               "lucene_version" : "4.10.4"             },             "tagline" : "You Know, for Search"           } 安裝elasticsearch外掛:               head(叢集幾乎所有資訊,還能進行簡單的搜尋查詢,觀察自動恢復的情況等)               bigdesk(該外掛可以檢視叢集的jvm資訊,磁碟IO,索引建立刪除資訊等)               kopf(提供了一個簡單的方法,一個elasticsearch叢集上執行常見的任務)               #/elasticsearch/elasticsearch-1.5.2/bin/plugin -install mobz/elasticsearch-head               #/elasticsearch/elasticsearch-1.5.2/bin/plugin -install lukas-vlcek/bigdesk               #/elasticsearch/elasticsearch-1.5.2/bin/plugin -install lmenezes/elasticsearch-kopf/1.6 安裝好之後,訪問方式為: http://10.10.13.17:16780/_plugin/head,由於叢集中現在暫時沒有資料,所以顯示為空 定期清除日誌(指令碼cleanES.sh),制定清除crontab計劃           #cat /root/cleanES.sh           #! /bin/bash           DAY=$(date -d  "20 days ago" +%Y.%m.%d)           DAYy=$(date -d  "21 days ago" +%Y.%m.%d)           DAY1=$(date -d "30 days ago" +%Y.%m.%d)           DAY3=$(date -d "300 days ago" +%Y.%m.%d)           DAY2=$(date -d "120 days ago" +%Y.%m)           #haproxy 日誌logstash-YYYY.mm.dd 索引載入目錄儲存15天,40天以前日誌刪除           #echo logstash-$DAY logstash-YYYY.mm.dd           #echo logstash-$DAY2           #echo logstash-$DAY1 logstash-YYYY.mm           mv /elasticsearch/elasticsearch-1.5.2/data/elasticsearch/nodes/0/indices/logstash-$DAY /elasticsearch/elasticsearch-1.5.2/bakup_data/           mv /elasticsearch/elasticsearch-1.5.2/data/elasticsearch/nodes/0/indices/linuxlog-$DAY /elasticsearch/elasticsearch-1.5.2/bakup_data/           #rm -rf /elasticsearch/elasticsearch-1.5.2/data/elasticsearch/nodes/0/indices/logstash-$DAY2           rm -rf /elasticsearch/elasticsearch-1.5.2/bakup_data/logstash-$DAY1           rm -rf /elasticsearch/elasticsearch-1.5.2/bakup_data/linuxlog-$DAY3           #從叢集中刪除移走的索引           curl -XDELETE "http://10.10.13.17:16780/linuxlog-$DAYy"           curl -XDELETE "http://10.10.13.17:16780/logstash-$DAYy"           清除日誌crontab計劃任務           #crontab -e           30 3 * * * sh /root/cleanES.sh 二、安裝ES  10.10.13.18 和第一步安裝ES一樣,配置檔案不一樣而已 編寫配置檔案: vi /home/elasticsearch/config/elasticsearch.yml node.data: true index.number_of_shards: 5 index.number_of_replicas: 0 bootstrap.mlockall: true network.bind_host: 10.10.13.18 network.host: 10.10.13.18 http.port: 16780 http.cors.enabled: true http.cors.allow-origin: "*" 啟動ES #/elasticsearch/elasticsearch-1.5.2/bin/service/elasticsearch  start或stop  #手工啟動/停止 重啟指令碼和清楚日誌指令碼跟第一步一樣 安裝elasticsearch外掛:               head(叢集幾乎所有資訊,還能進行簡單的搜尋查詢,觀察自動恢復的情況等)               bigdesk(該外掛可以檢視叢集的jvm資訊,磁碟IO,索引建立刪除資訊等)               kopf(提供了一個簡單的方法,一個elasticsearch叢集上執行常見的任務)               #/elasticsearch/elasticsearch-1.5.2/bin/plugin -install mobz/elasticsearch-head               #/elasticsearch/elasticsearch-1.5.2/bin/plugin -install lukas-vlcek/bigdesk               #/elasticsearch/elasticsearch-1.5.2/bin/plugin -install lmenezes/elasticsearch-kopf/1.6 安裝好之後,訪問方式為: http://10.10.13.18:16780/_plugin/head,由於叢集中現在暫時沒有資料,所以顯示為空, 此時,es叢集的部署完成。 ==================================================== 三、Linux客戶端logstash安裝及測試輸出日誌到ES  (10.10.12.7) yun install -y java1.7.0-openjdk* wget https://download.elasticsearch.org/logstash/logstash/logstash-2.2.0.tar.gz tar xf logstash-2.2.0.tar.gz -C /usr/local/ cd /usr/local/logstash-2.2.0/ mkdir etc  建立配置檔案資料夾 cd etc/ vi logstash-agent.conf input { file { type => "system-messages" path => "/var/log/messages" } } output { elasticsearch { hosts => ["10.10.13.17:16780","10.10.13.18:16780"] } } 將12.7的系統日誌輸出到ES 啟動logstash: /usr/local/logstash/bin/logstash -f logstash-agent.conf & 稍後在http://10.10.13.17:16780/_plugin/head上檢視有無日誌輸出
這樣可以看到logstash輸出到ES成功 系統日誌我們已經成功的收集,並且已經寫入到es叢集中,那上面的演示是logstash直接將日誌寫入到es叢集中的,這種場合我覺得如果量不是很大的話直接像上面已將將輸出output定義到es叢集即可,如果量大的話需要加上訊息佇列來緩解es叢集的壓力。前面已經提到了我這邊之前使用的是單臺redis作為訊息佇列,但是redis不能作為list型別的叢集,也就是redis單點的問題沒法解決,所以這裡我選用了kafka.但是由於資源問題現在只用一臺kafka。在搭建kafka叢集時,需要提前安裝zookeeper叢集,當然kafka已經自帶zookeeper程式只需要解壓並且安裝配置就行了 四、安裝kafka  (10.10.13.15) yun install -y java1.7.0-openjdk* #wget http://apache.fayea.com/kafka/0.9.0.1/kafka_2.11-0.8.2.2.tgz #tar -zxvf kafka_2.11-0.8.2.2.tgz #mv kafka_2.11-0.8.2.2 /usr/local/kafka vi /usr/local/kafka/config/server.properties broker.id=0 listeners=PLAINTEXT://:9092 host.name=10.10.13.15 advertised.host.name=10.10.13.15 num.network.threads=3 num.io.threads=8 socket.send.buffer.bytes=102400 socket.receive.buffer.bytes=102400 socket.request.max.bytes=104857600 log.dirs=/elasticsearch/kafka-logs num.partitions=4 num.recovery.threads.per.data.dir=1 log.retention.hours=168 log.segment.bytes=1073741824 log.retention.check.interval.ms=300000 zookeeper.connect=localhost:2181 zookeeper.connection.timeout.ms=6000 #vi /usr/local/kafka/config/zookeeper.properties dataDir=/kafka/zookeeper clientPort=2181 maxClientCnxns=0 autopurge.snapRetainCount=100 autopurge.purgeInterval=12 啟動zookeeper/kafka服務,可以用netstat -tnlp看是否正常監聽2181和9092,可以設定為開機自動啟動服務               #/usr/local/kafka/bin/zookeeper-server-start.sh /usr/local/kafka/config/zookeeper.properties  > /dev/null &               #/usr/local/kafka/bin/kafka-server-start.sh /usr/local/kafka/config/server.properties  > /dev/null & kafka常用命令(可以在bin下對不同指令碼執行--hlep查詢幫助): <>建立一個topic                 #bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test                 #注意:factor大小不能超過broker數 <>檢視當前所有topic                 #bin/kafka-topics.sh --list --zookeeper localhost:2181                 __consumer_offsets                 bar                 foo                 haproxy                 linuxlog ((先建立linux客戶端使用topic,客戶端logstash可以輸出到該topic))   這就是我們使用ELK收集Linux日誌                 m-test-topic                 test-1 - marked for deletion                 winlog (先建立windows客戶端使用topic,客戶端logstash可以輸出到該topic) 這就是我們使用ELK收集windows日誌                 winlog2 測試: 傳送訊息,這裡使用的是生產者角色
 /bin/bash /usr/local/kafka/bin/kafka-console-producer.sh --broker-list 10.10.13.15:9092 --topic summer This is a messages welcome to kafka     
接收訊息,這裡使用的是消費者角色 # /usr/local/kafka/bin/kafka-console-consumer.sh --zookeeper  10.10.13.15:2181 --topic summer --from-beginning
This is a messages welcome to kafka
<>開機自動啟動服務 vim /etc/rc.local                 touch /var/lock/subsys/local                 mount /dev/sdc /elasticsearch/  #開機自動掛載儲存資料目錄                 /usr/local/kafka/bin/zookeeper-server-start.sh /usr/local/kafka/config/zookeeper.properties  > /dev/null &                 /usr/local/kafka/bin/kafka-server-start.sh /usr/local/kafka/config/server.properties  > /dev/null & 那如何將資料從kafka中讀取然後給我們的es叢集呢? 五、kafka端安裝logstash輸出接收到的日誌到ES (10.10.13.15) yun install -y java1.7.0-openjdk* wget https://download.elasticsearch.org/logstash/logstash/logstash-2.2.0.tar.gz tar xf logstash-2.2.0.tar.gz -C /usr/local/ cd /usr/local/logstash-2.2.0/ mkdir etc  建立配置檔案資料夾 cd etc/ vi linuxlog-es.conf  (logstash的配置檔案隨意定義,只要啟動的時候指定就行) 將kafka上收取到的topic為Linuxlog的日誌輸出到ES input { kafka { zk_connect => "10.10.13.15:2181" topic_id => "linuxlog" codec => plain reset_beginning => false consumer_threads => 5 decorate_events => true } } output { elasticsearch { hosts => ["10.10.13.17:16780","10.10.13.18:16780"] index => "linuxlog-%{+YYYY-MM}" } } 看到效果前還有一件事,就是把Linux客戶端(10.10.12.7)上的日誌輸出到kafka: 12.7上重新配置logstash配置檔案: vi /usr/local/logstash/etc/logstash-agent.conf input    {     file {     type => "redis"     path => "/var/log/redis/redis.log"  我這裡以12.7上裝的redis日誌為例     }     } output    {     kafka {     topic_id => "linuxlog"     bootstrap_servers => "10.10.13.15:9092"     workers => 2     }     } 然後我們就可以在ES的監控頁面http://10.10.13.17:16780/_plugin/head看到了! nginx/kibana訪問入口以及良好頁面展示效果 六、安裝nginx、kibana  (10.10.13.12) 安裝部署nginx,修改相應的配置檔案 從ngnix官網下載ngnix壓縮包,解壓並進入其目錄,並測試啟動服務                 #wget http://nginx.org/download/nginx-1.6.2.tar.gz                                #tar -zxf nginx-1.6.2.tar.gz -C /usr/local/                 #mv /usr/local/nginx-1.6.2 /usr/local/nginx                 #cd /usr/lcoal/nginx                 #./configure --prefix=/usr/local/ngnix                 #此處有可能報錯,Ngnix依賴於pcre庫,所以要先安裝pcre庫                 yum install pcre pcre-devel                 make && make install                 #啟動服務,訪問http://10.10.13.12能夠出現nginx歡迎頁面                 #./usr/local/ngnix/sbin/ngnix 安裝部署kibana3,並修改相應配置 下載kibana3安裝包,https://github.com/elasticsearch/kibana,並解壓至/usr/local/nginx/html                 #wget https://github.com/elasticsearch/kibana/kibana-3.1.2-linux-x64.tar.gz                              #tar -zxf kibana-3.1.2-linux-x64.tar.gz -C /usr/local/nginx/html/kibana-latest/ #如果kibana-latest目錄不存在可以先建立 修改kibana-latest目錄下的config.js檔案指向elasticsearch的配置項elasticsearch: "http://"+"10.10.13.17"+":16780"                 注意:其中"+"格式應與此處保持一致,否則會出現無法連線到ES的情況                 #vim //usr/local/nginx/html/kibana-latest/config.js /** @scratch /configuration/config.js/1  *  * == Configuration  * config.js is where you will find the core Kibana configuration. This file contains parameter that  * must be set before kibana is run for the first time.  */ define(['settings'], function (Settings) {   /** @scratch /configuration/config.js/2    *    * === Parameters    */   return new Settings({     /** @scratch /configuration/config.js/5      *      * ==== elasticsearch      *      * The URL to your elasticsearch server. You almost certainly don't      * want +http://localhost:9200+ here. Even if Kibana and Elasticsearch are on      * the same host. By default this will attempt to reach ES at the same host you have      * kibana installed on. You probably want to set it to the FQDN of your      * elasticsearch host      *      * Note: this can also be an object if you want to pass options to the http client. For example:      *      *  +elasticsearch: {server: "http://localhost:9200", withCredentials: true}+      *      */     elasticsearch: "http://"+"10.10.13.17"+":16780",     /** @scratch /configuration/config.js/5      *      * ==== default_route      *      * This is the default landing page when you don't specify a dashboard to load. You can specify      * files, scripts or saved dashboards here. For example, if you had saved a dashboard called      * `WebLogs' to elasticsearch you might use:      *      * default_route: '/dashboard/elasticsearch/WebLogs',      */     default_route     : '/dashboard/file/default.json',     /** @scratch /configuration/config.js/5      *      * ==== kibana-int      *      * The default ES index to use for storing Kibana specific object      * such as stored dashboards      */     kibana_index: "kibana-int",     /** @scratch /configuration/config.js/5      *      * ==== panel_name      *      * An array of panel modules available. Panels will only be loaded when they are defined in the      * dashboard, but this list is used in the "add panel" interface.      */     panel_names: [       'histogram',       'map',       'goal',       'table',       'filtering',       'timepicker',       'text',       'hits',       'column',       'trends',       'bettermap',       'query',       'terms',       'stats',       'sparklines'     ]   }); }); 驗證kibana能否正常顯示,並且簡單新增圖形效果。 訪問http://10.10.13.12正常顯示elasticsearch內容,預設kibana的logstash展示介面只有event時間展示柱狀圖和all event表 #展示其他圖形效果需在介面中新增:                 <>點選右下角的 add a rows  進入Dashboard Settings > create new rows > save                 <>在新增加的row 中點選Add panel to empty row > add panel > terms > fild > save                 <>fild長度 、顯示寬度和others 、missing值展示可自行選擇。