海量日志系統-集群模式


什么要做日志分析平台?

隨着業務量的增長,每天業務服務器將會產生上億條的日志,單個日志文件達幾個GB,這時我們發現用Linux自帶工具,cat grep awk 分析越來越力不從心了,而且除了服務器日志,還有程序報錯日志,分布在不同的服務器,查閱繁瑣。 

待解決的痛點:

1、大量不同種類的日志成為了運維人員的負擔,不方便管理;

2、單個日志文件巨大,無法使用常用的文本工具分析,檢索困難;

3、日志分布在多台不同的服務器上,業務一旦出現故障,需要一台台查看日志。

為了解決以上困擾:

接下來我們要一步步構建這個日志分析平台,架構圖如下:

架構解讀 : (整個架構從左到右,總共分為5層)

第一層、數據采集層

最左邊的是業務服務器集群,上面安裝了filebeat做日志采集,同時把采集的日志分別發送給兩個logstash服務。

第二層、數據處理層,數據緩存層

logstash服務把接受到的日志經過格式處理,轉存到本地的kafka broker+zookeeper 集群中。

第三層、數據轉發層

這個單獨的Logstash節點會實時去kafka broker集群拉數據,轉發至ES DataNode。

第四層、數據持久化存儲

ES DataNode 會把收到的數據,寫磁盤,建索引庫。

第五層、數據檢索,數據展示

ES Master + Kibana 主要 協調 ES集群,處理數據檢索請求,數據展示。

筆者為了節約寶貴的服務器資源,把一些可拆分的服務合並在同一台主機。大家可以根據自己的實際業務環境自由拆分,延伸架構。

開 工 !

操作系統環境 :  CentOS release 6.5 

各服務器角色分配 :

IP 角色 所屬集群
10.10.1.2 業務服務器+filebeat 業務服務器集群
10.10.1.30 Logstash+Kafka+ZooKeeper

Kafka Broker 集群

10.10.1.31 Logstash+Kafka+ZooKeeper
10.10.1.32 Kafka+ZooKeeper
10.10.1.50 Logstash 數據轉發
10.10.1.60 ES DataNode

Elasticsearch 集群

10.10.1.90 ES DataNode
10.10.1.244 ES Master+Kibana

軟件包版本:

jdk-8u101-linux-x64.rpm

logstash-2.3.2.tar.gz 

filebeat-1.2.3-x86_64.rpm

kafka_2.11-0.10.0.1.tgz

zookeeper-3.4.9.tar.gz

elasticsearch-2.3.4.rpm 

kibana-4.5.3-linux-x64.tar.gz

一、安裝部署Elasticsearch集群

布置ES Master節點 10.10.1.244

1、安裝jdk1.8,elasticsearch-2.3.4

oracle官網 jdk 下載地址:  http://www.oracle.com/technetwork/java/javase/downloads/index.html

elasticsearch 官網:  https://www.elastic.co/

# 安裝命令
yum install jdk-8u101-linux-x64.rpm elasticsearch-2.3.4.rpm -y # ES 會被默認安裝在 /usr/share/elasticsearch/

2、系統調優,JVM調優

# 配置系統最大打開文件描述符數
vim /etc/sysctl.conf fs.file-max=65535 # 配置進程最大打開文件描述符 vim /etc/security/limits.conf # End of file * soft nofile 65535 * hard nofile 65535 # 配置 JVM內存 vim /etc/sysconfig/elasticsearch ES_HEAP_SIZE=4g # 這台機器的可用內存為8G

3、編寫ES Master節點配置文件

# /etc/elasticsearch/elasticsearch.yml

# ---------------------------------- Cluster -----------------------------------
# Use a descriptive name for your cluster: cluster.name: bigdata # ------------------------------------ Node ------------------------------------ node.name: server1 node.master: true node.data: false # ----------------------------------- Index ------------------------------------ index.number_of_shards: 5 index.number_of_replicas: 0 index.refresh_interval: 120s # ----------------------------------- Paths ------------------------------------ path.data: /home/elk/data path.logs: /var/log/elasticsearch/elasticsearch.log # ----------------------------------- Memory ----------------------------------- bootstrap.mlockall: true indices.fielddata.cache.size: 50mb #------------------------------------ Network And HTTP -------------------------- network.host: 0.0.0.0 http.port: 9200 # ------------------------------------ Translog ---------------------------------- index.translog.flush_threshold_ops: 50000 # --------------------------------- Discovery ------------------------------------ discovery.zen.minimum_master_nodes: 1 discovery.zen.ping.timeout: 200s discovery.zen.fd.ping_timeout: 200s discovery.zen.fd.ping.interval: 30s discovery.zen.fd.ping.retries: 6 discovery.zen.ping.unicast.hosts: ["10.10.1.60:9300","10.10.1.90:9300","10.10.1.244:9300",] discovery.zen.ping.multicast.enabled: false # --------------------------------- merge ------------------------------------------ indices.store.throttle.max_bytes_per_sec: 100mb

注: path.data、path.logs 這兩個參數指定的路徑,如果沒有需要自己創建,還要賦予權限給elasticsearch用戶。(后面的ES DataNode也同樣)

4、安裝head、kopf、bigdesk 開源插件

安裝方法有兩種 :

1、使用ES自帶的命令plugin

# head
/usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head # kopf /usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf # bigdesk /usr/share/elasticsearch/bin/plugin install hlstudio/bigdesk

2、自行下載插件的源碼包安裝

我們通過plugin命令安裝的插件,其實是安裝到了這個路徑:/usr/share/elasticsearch/plugins

而plugin install 命令后面跟的這一串 mobz/elasticsearch-head 其實是github上的一個地址。

前面加上github的官網地址就是  https://github.com/mobz/elasticsearch-head  可以復制到瀏覽器中打開,找到該插件的源碼倉庫。

現在知道了,想要找插件自己可以去github上搜一下出來一大堆。隨便選一個然后取后面那串路徑,用ES自帶的命令安裝。

如果安裝失敗了,那么就手動下載該插件的源碼包。 解壓后直接整個目錄mv到 ES 的插件安裝路徑下。 

也就是這里: /usr/share/elasticsearch/plugins/

那如何訪問安裝好的插件呢?

http://ES_server_ip:port/_plugin/plugin_name

Example:

http://127.0.0.1:9200/_plugin/head/

http://127.0.0.1:9200/_plugin/kopf/

這時,ES Master已經配置好了。

布置ES DataNode節點  10.10.1.90

安裝和系統調優方法同上,插件不用安裝,只是配置文件不同。

編寫配置文件

# ---------------------------------- Cluster -----------------------------------
# Use a descriptive name for your cluster: cluster.name: bigdata # ------------------------------------ Node ------------------------------------ node.name: server2 node.master: false node.data: true # ----------------------------------- Index ------------------------------------ index.number_of_shards: 5 index.number_of_replicas: 0 index.refresh_interval: 120s # ----------------------------------- Paths ------------------------------------ path.data: /home/elk/data,/disk2/elk/data2 path.logs: /var/log/elasticsearch/elasticsearch.log # ----------------------------------- Memory ----------------------------------- bootstrap.mlockall: true indices.fielddata.cache.size: 50mb #------------------------------------ Network And HTTP -------------------------- network.host: 0.0.0.0 http.port: 9200 # ------------------------------------ Translog ---------------------------------- index.translog.flush_threshold_ops: 50000 # --------------------------------- Discovery ------------------------------------ discovery.zen.minimum_master_nodes: 1 discovery.zen.ping.timeout: 200s discovery.zen.fd.ping_timeout: 200s discovery.zen.fd.ping.interval: 30s discovery.zen.fd.ping.retries: 6 discovery.zen.ping.unicast.hosts: ["10.10.1.244:9300",] discovery.zen.ping.multicast.enabled: false # --------------------------------- merge ------------------------------------------ indices.store.throttle.max_bytes_per_sec: 100mb

10.10.1.60 也准備好了。

布置另一台ES DataNode節點 10.10.1.90

編寫配置文件

# ---------------------------------- Cluster -----------------------------------
# Use a descriptive name for your cluster: cluster.name: bigdata # ------------------------------------ Node ------------------------------------ node.name: server3 node.master: false node.data: true # ----------------------------------- Index ------------------------------------ index.number_of_shards: 5 index.number_of_replicas: 0 index.refresh_interval: 120s # ----------------------------------- Paths ------------------------------------ path.data: /home/elk/single path.logs: /var/log/elasticsearch/elasticsearch.log # ----------------------------------- Memory ----------------------------------- bootstrap.mlockall: true indices.fielddata.cache.size: 50mb #------------------------------------ Network And HTTP -------------------------- network.host: 0.0.0.0 http.port: 9200 # ------------------------------------ Translog ---------------------------------- index.translog.flush_threshold_ops: 50000 # --------------------------------- Discovery ------------------------------------ discovery.zen.minimum_master_nodes: 1 discovery.zen.ping.timeout: 200s discovery.zen.fd.ping_timeout: 200s discovery.zen.fd.ping.interval: 30s discovery.zen.fd.ping.retries: 6 discovery.zen.ping.unicast.hosts: ["10.10.1.244:9300",] discovery.zen.ping.multicast.enabled: false # --------------------------------- merge ------------------------------------------ indices.store.throttle.max_bytes_per_sec: 100mb

5、現在三台ES節點已經准備就緒,分別啟動服務

# 10.10.1.244
/etc/init.d/elasticsearch start
# 10.10.1.60 /etc/init.d/elasticsearch start # 10.10.1.90 /etc/init.d/elasticsearch start

6、訪問head插件,查看集群狀態

此時 Elasticsearch 集群已經准備完成

二、配置位於架構圖中第二層的ZooKeeper集群

配置 10.10.1.30 節點

1、安裝,配置 zookeeper

zookeeper官網:  http://zookeeper.apache.org/

# zookeeper 依賴 java,如果之前沒安裝過JDK,則需要安裝. rpm -ivh jdk-8u101-linux-x64.rpm # 解壓程序 tar xf zookeeper-3.4.9.tar.gz

編寫配置文件

# conf/zoo.cfg

# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/u01/zookeeper/zookeeper-3.4.9/data # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 server.11=10.10.1.30:2888:3888 server.12=10.10.1.31:2888:3888 server.13=10.10.1.32:2888:3888 # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir # autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature # autopurge.purgeInterval=1

同步配置文件到其他兩台節點

注: zookeeper 集群,每個節點的配置文件都是一樣的。所以直接同步過去,不需要做任何修改。

不熟悉zookeeper的朋友,可以參考這里:  http://tchuairen.blog.51cto.com/3848118/1859494

scp zoo.cfg 10.10.1.31:/usr/local/zookeeper-3.4.9/conf/ scp zoo.cfg 10.10.1.32:/usr/local/zookeeper-3.4.9/conf/

2、創建myid文件

# 10.10.1.30
echo 11 >/usr/local/zookeeper-3.4.9/data/myid # 10.10.1.31 echo 12 >/usr/local/zookeeper-3.4.9/data/myid # 10.10.1.32 echo 13 >/usr/local/zookeeper-3.4.9/data/myid

3、啟動服務 &  查看節點狀態

# 10.10.1.30 bin/zkServer.sh start bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg Mode: leader # 10.10.1.31 bin/zkServer.sh start bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg Mode: follower # 10.10.1.32 bin/zkServer.sh start bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg Mode: follower

此時zookeeper集群配置完成

三、配置位於架構圖中第二層的Kafka Broker集群

Kafka官網:  http://kafka.apache.org/

不熟悉Kafka的朋友可以參考: http://tchuairen.blog.51cto.com/3848118/1855090

配置 10.10.1.30 節點

1、安裝,配置 kafka

# 解壓程序
tar xf kafka_2.11-0.10.0.1.tgz

編寫配置文件

############################# Server Basics ############################# broker.id=1 ############################# Socket Server Settings ############################# num.network.threads=3 # The number of threads doing disk I/O num.io.threads=8 # The send buffer (SO_SNDBUF) used by the socket server socket.send.buffer.bytes=102400 # The receive buffer (SO_RCVBUF) used by the socket server socket.receive.buffer.bytes=102400 # The maximum size of a request that the socket server will accept (protection against OOM) socket.request.max.bytes=104857600 ############################# Log Basics ############################# log.dirs=/usr/local/kafka/kafka_2.11-0.10.0.1/data num.partitions=6 num.recovery.threads.per.data.dir=1 ############################# Log Flush Policy ############################# # The number of messages to accept before forcing a flush of data to disk #log.flush.interval.messages=10000 # The maximum amount of time a message can sit in a log before we force a flush #log.flush.interval.ms=1000 ############################# Log Retention Policy ############################# log.retention.hours=60 log.segment.bytes=1073741824 log.retention.check.interval.ms=300000 ############################# Zookeeper ############################# zookeeper.connect=10.10.1.30:2181,10.10.1.31:2181,10.10.1.32:2181 zookeeper.connection.timeout.ms=6000

注: 其他兩個節點的配置文件也基本相同,只有一個參數需要修改 broker.id 。 它用於唯一標識節點,所以絕對不能相同,不然會節點沖突。

同步配置文件到其他兩台節點

scp server.properties 10.10.1.31:/usr/local/kafka/kafka_2.11-0.10.0.1/config/ scp server.properties 10.10.1.32:/usr/local/kafka/kafka_2.11-0.10.0.1/config/ # 修改 broker.id # 10.10.1.31 broker.id=2 # 10.10.1.32 broker.id=3

2、配置主機名對應IP的解析

vim /etc/hosts

10.10.1.30 server1 10.10.1.31 server2 10.10.1.32 server3 # 記得同步到其他兩台節點

3、啟動服務 

bin/kafka-server-start.sh config/server.properties
# 其他兩台節點啟動方式相同

Kafka+ZooKeeper集群配置完成

四、配置位於架構圖中第二層的Logstash服務

配置 10.10.1.30 節點

1、安裝,配置 logstash

# 解壓程序
tar xf logstash-2.3.2.tar.gz

配置 GeoLiteCity , 用於地圖顯示IP訪問的城市

官網地址:  http://dev.maxmind.com/geoip/legacy/geolite/

下載地址: http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

解壓

gunzip GeoLiteCity.dat.gz

編寫配置文件

input {
    beats {
    port => 5044
    codec => "json" } } filter { if [type] == "nginxacclog" { geoip { source => "clientip" # 與日志中訪問地址的key要對應 target => "geoip" database => "/usr/local/logstash/GeoLiteCity.dat" add_field => [ "[geoip][coordinates]","%{[geoip][longitude]}" ] add_field => [ "[geoip][coordinates]","%{[geoip][latitude]}" ] } mutate { convert => [ "[geoip][coordinates]","float" ] } } } output { kafka { workers => 2 bootstrap_servers => "10.10.1.30:9092,10.10.1.31:9092,10.10.1.32:9092" topic_id => "peiyinlog" } }

2、啟動服務

/usr/local/logstash/bin/logstash agent -f logstash_in_kafka.conf &

10.10.1.31 節點的這塊配置,與上述完全相同。(略)

位於第二層、數據處理層的 Logstash 配置完成

五、配置數據采集層,業務服務器+Filebeat

1、定制Nginx日志格式

log_format json '{"@timestamp":"$time_iso8601",' '"slbip":"$remote_addr",' '"clientip":"$http_x_forwarded_for",' '"serverip":"$server_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"domain":"$host",' '"method":"$request_method",' '"requesturi":"$request_uri",' '"url":"$uri",' '"appversion":"$HTTP_APP_VERSION",' '"referer":"$http_referer",' '"agent":"$http_user_agent",' '"status":"$status",' '"devicecode":"$HTTP_HA"}'; # 在虛擬主機配置中調用 access_log /alidata/log/nginx/access/access.log json;

2、安裝 Filebeat

Filebeat 也是 Elasticsearch 公司的產品,在官網可以下載。

# rpm 包安裝
yum install filebeat-1.2.3-x86_64.rpm -y

3、編寫 Filebeat 配置文件

################### Filebeat Configuration Example ######################### ############################# Filebeat ###################################### filebeat: prospectors: - paths: - /var/log/messages input_type: log document_type: messages - paths: - /alidata/log/nginx/access/access.log input_type: log document_type: nginxacclog - paths: - /alidata/www/logs/laravel.log input_type: log document_type: larlog - paths: - /alidata/www/logs/500_error.log input_type: log document_type: peiyinlar_500error - paths: - /alidata/www/logs/deposit.log input_type: log document_type: lar_deposit - paths: - /alidata/www/logs/call_error.log input_type: log document_type: call_error - paths: - /alidata/log/php/php-fpm.log.slow input_type: log document_type: phpslowlog multiline: pattern: '^[[:space:]]' negate: true match: after registry_file: /var/lib/filebeat/registry ############################# Output ########################################## output: logstash: hosts: ["10.26.95.215:5044"] ############################# Shipper ######################################### shipper: name: "host_6" ############################# Logging ######################################### logging: files: rotateeverybytes: 10485760 # = 10MB

4、啟動服務

/etc/init.d/filebeat start

數據采集層,Filebeat配置完成。

現在業務服務器上的日志數據已經在源源不斷的寫入緩存了。

六、配置位於架構圖中的第三層,數據轉發層

Logstash安裝上面已經講過(略)

編寫Logstash配置文件

# kafka_to_es.conf

input{
    kafka {
        zk_connect => "10.10.1.30:2181,10.10.1.31:2181,10.10.1.32:2181"
        group_id => "logstash" topic_id => "peiyinlog" reset_beginning => false consumer_threads => 50 decorate_events => true } } # 刪除一些不需要的字段 filter { if [type] == "nginxacclog" { mutate { remove_field => ["slbip","kafka","domain","serverip","url","@version","offset","input_type","count","source","fields","beat.hostname","host","tags"] } } } output { if [type] == "nginxacclog" { # stdout {codec => rubydebug } elasticsearch { hosts => ["10.10.1.90:9200","10.10.1.60:9200"] index => "logstash-nginxacclog-%{+YYYY.MM.dd}" manage_template => true flush_size => 50000 idle_flush_time => 10 workers => 2 } } if [type] == "messages" { elasticsearch { hosts => ["10.10.1.90:9200","10.10.1.60:9200"] index => "logstash-messages-%{+YYYY.MM.dd}" manage_template => true flush_size => 50000 idle_flush_time => 30 workers => 1 } } if [type] == "larlog" { elasticsearch { hosts => ["10.10.1.90:9200","10.10.1.60:9200"] index => "logstash-larlog-%{+YYYY.MM.dd}" manage_template => true flush_size => 2000 idle_flush_time => 10 } } if [type] == "deposit" { elasticsearch { hosts => ["10.10.1.90:9200","10.10.1.60:9200"] index => "logstash-deposit-%{+YYYY.MM.dd}" manage_template => true flush_size => 2000 idle_flush_time => 10 } } if [type] == "phpslowlog" { elasticsearch { hosts => ["10.10.1.90:9200","10.10.1.60:9200"] index => "logstash-phpslowlog-%{+YYYY.MM.dd}" manage_template => true flush_size => 2000 idle_flush_time => 10 } } }

啟動服務

/usr/local/logstash/bin/logstash agent -f kafka_to_es.conf &

數據轉發層已經配置完成

這時數據已經陸陸續續的從kafka取出,轉存到ES DataNode。

我們登陸到任意一台kafka主機,查看數據的緩存和消費情況

七、修改ES的索引模版配置

為什么要做這一步呢? 因為logstash寫入數據到ES時,會自動選用一個索引模版。 我們可以看一下 

這個模版其實也挺好,不過有一個參數,我標記出來了。 "refresh_interval":"5s"  這個參數用於控制,索引的刷新頻率。 索引的刷新頻率越快,你搜索到的數據就實時。  這里是5秒。 一般我們日志場景不需要這么高的實時性。 可以適當降低該參數,提高ES 索引庫的寫入速度。  

上傳自定義模版

curl -XPUT http://10.10.1.244:9200/_template/logstash2 -d ' { "order":1, "template":"logstash-*", "settings":{ "index":{ "refresh_interval":"120s" } }, "mappings":{ "_default_":{ "_all":{ "enabled":false } } } }'

由於這個自定義模版,我把優先級 order 定義的比logstash模版高,而模版的匹配規則又一樣,所以這個自定義模版的配置會覆蓋原logstash模版。

我這里只是簡單描述。 如果要詳細理解其中道理,請查看我的 ES 調優篇。

八、配置 Kibana 數據展示層

10.10.1.244 節點

Kibana是ELK套件中的一員,也屬於elasticsearch 公司,在官網提供下載。

安裝

tar xf kibana-4.5.3-linux-x64.tar.gz # 很簡單,只要解壓就可以用。

修改配置文件

# vim kibana-4.5.3-linux-x64/config/kibana.yml # Kibana is served by a back end server. This controls which port to use. server.port: 5601 # The host to bind the server to. server.host: "0.0.0.0" # The Elasticsearch instance to use for all your queries. elasticsearch.url: " # 修改這三個參數就好了

啟動服務

打開瀏覽器訪問: http://10.10.1.244:5601/

定制 Elasticsearch 索引的 Index pattern 

默認情況下,Kibana認為你要訪問的是通過Logstash導入Elasticsearch的數據,這時候你可以用默認的 logstash-* 作為你的 index pattern。 通配符(*)匹配索引名中任意字符任意個數。 

選擇一個包含了時間戳的索引字段(字段類型為 date 的字段),可以用來做基於時間的處理。Kibana 會讀取索引的 

映射,然后列出所有包含了時間戳的字段。如果你的索引沒有基於時間的數據.

關閉 Index contains time-based events 參數。

如果一個新索引是定期生成,而且索引名中帶有時間戳,選擇 Use event times to  create index names 選項,

然后再選擇 Index pattern interval 。這可以提高搜索性能,Kibana 會至搜索你指定的時間范圍內的索引。在你用 Logstash 輸出數據給Elasticsearch 的情況下尤其有效。

由於我們的索引是用日期命名,按照每天分割的。 index pattern 如下

數據展示

完 工 !


免責聲明!

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



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