第1章 ELKStack
對於日志來說,最常見的需求就是收集、存儲、查詢、展示,開源社區正好有相對應的開源項目:logstash(收集)、elasticsearch(存儲+搜索)、kibana(展示),我們將這三個組合起來的技術稱之為ELKStack,所以說ELKStack指的是Elasticsearch、Logstash、Kibana技術棧的結合。
1.1 Elasticsearch介紹
ElasticSearch是一個基於Lucene的搜索服務器。它提供了一個分布式多用戶能力的全文搜索引擎,基於RESTful web接口。Elasticsearch是用Java開發的,並作為Apache許可條款下的開放源碼發布,是第二流行的企業搜索引擎。設計用於雲計算中,能夠達到實時搜索,穩定,可靠,快速,安裝使用方便。
1.1.1 Elasticsearch幾個重要術語
NRT
elasticsearch是一個近似實時的搜索平台,從索引文檔到可搜索有些延遲,通常為1秒。
集群
集群就是一個或多個節點存儲數據,其中一個節點為主節點,這個主節點是可以通過選舉產生的,並提供跨節點的聯合索引和搜索的功能。集群有一個唯一性標示的名字,默認是elasticsearch,集群名字很重要,每個節點是基於集群名字加入到其集群中的。因此,確保在不同環境中使用不同的集群名字。一個集群可以只有一個節點。強烈建議在配置elasticsearch時,配置成集群模式。
節點
節點就是一台單一的服務器,是集群的一部分,存儲數據並參與集群的索引和搜索功能。像集群一樣,節點也是通過名字來標識,默認是在節點啟動時隨機分配的字符名。當然啦,你可以自己定義。該名字也蠻重要的,在集群中用於識別服務器對應的節點。
節點可以通過指定集群名字來加入到集群中。默認情況下,每個節點被設置成加入到elasticsearch集群。如果啟動了多個節點,假設能自動發現對方,他們將會自動組建一個名為elasticsearch的集群。
索引
索引是有幾分相似屬性的一系列文檔的集合。如nginx日志索引、syslog索引等等。索引是由名字標識,名字必須全部小寫。這個名字用來進行索引、搜索、更新和刪除文檔的操作。
索引相對於關系型數據庫的庫。
類型
在一個索引中,可以定義一個或多個類型。類型是一個邏輯類別還是分區完全取決於你。通常情況下,一個類型被定於成具有一組共同字段的文檔。如ttlsa運維生成時間所有的數據存入在一個單一的名為logstash-ttlsa的索引中,同時,定義了用戶數據類型,帖子數據類型和評論類型。
類型相對於關系型數據庫的表。
文檔
文檔是信息的基本單元,可以被索引的。文檔是以JSON格式表現的。
在類型中,可以根據需求存儲多個文檔。
雖然一個文檔在物理上位於一個索引,實際上一個文檔必須在一個索引內被索引和分配一個類型。
文檔相對於關系型數據庫的列。
分片和副本
在實際情況下,索引存儲的數據可能超過單個節點的硬件限制。如一個十億文檔需1TB空間可能不適合存儲在單個節點的磁盤上,或者從單個節點搜索請求太慢了。為了解決這個問題,elasticsearch提供將索引分成多個分片的功能。當在創建索引時,可以定義想要分片的數量。每一個分片就是一個全功能的獨立的索引,可以位於集群中任何節點上。
分片的兩個最主要原因:
a、水平分割擴展,增大存儲量
b、分布式並行跨分片操作,提高性能和吞吐量
分布式分片的機制和搜索請求的文檔如何匯總完全是有elasticsearch控制的,這些對用戶而言是透明的。
網絡問題等等其它問題可以在任何時候不期而至,為了健壯性,強烈建議要有一個故障切換機制,無論何種故障以防止分片或者節點不可用。為此,elasticsearch讓我們將索引分片復制一份或多份,稱之為分片副本或副本。
副本也有兩個最主要原因:
- 高可用性,以應對分片或者節點故障。出於這個原因,分片副本要在不同的節點上。
- 提供性能,增大吞吐量,搜索可以並行在所有副本上執行。
總之,每一個索引可以被分成多個分片。索引也可以有0個或多個副本。復制后,每個索引都有主分片(母分片)和復制分片(復制於母分片)。分片和副本數量可以在每個索引被創建時定義。索引創建后,可以在任何時候動態的更改副本數量,但是,不能改變分片數。
默認情況下,elasticsearch為每個索引分片5個主分片和1個副本,這就意味着集群至少需要2個節點。索引將會有5個主分片和5個副本(1個完整副本),每個索引總共有10個分片。
每個elasticsearch分片是一個Lucene索引。一個單個Lucene索引有最大的文檔數LUCENE-5843, 文檔數限制為2147483519(MAX_VALUE – 128)。 可通過_cat/shards來監控分片大小。
1.2 logstash
1.2.1 LogStash 介紹
LogStash由JRuby語言編寫,基於消息(message-based)的簡單架構,並運行在Java虛擬機(JVM)上。不同於分離的代理端(agent)或主機端(server),LogStash可配置單一的代理端(agent)與其它開源軟件結合,以實現不同的功能。
1.2.2 LogStash的四大組件
- Shipper:發送事件(events)至LogStash;通常,遠程代理端(agent)只需要運行這個組件即可;
- Broker and Indexer:接收並索引化事件;
- Search and Storage:允許對事件進行搜索和存儲;
- Web Interface:基於Web的展示界面
正是由於以上組件在LogStash架構中可獨立部署,才提供了更好的集群擴展性。
1.2.3 LogStash主機分類
- 代理主機(agent host):作為事件的傳遞者(shipper),將各種日志數據發送至中心主機;只需運行Logstash 代理(agent)程序;
- 中心主機(central host):可運行包括中間轉發器(Broker)、索引器(Indexer)、搜索和存儲器(Search and Storage)、Web界面端(Web Interface)在內的各個組件,以實現對日志數據的接收、處理和存儲。
1.3 Kibana
Logstash是一個完全開源的工具,他可以對你的日志進行收集、分析,並將其存儲供以后使用(如,搜索),您可以使用它。說到搜索,logstash帶有一個web界面,搜索和展示所有日志。
1.4 工作原理圖
如圖:Logstash收集AppServer產生的Log,並存放到ElasticSearch集群中,而Kibana則從ES集群中查詢數據生成圖表,再返回給Browser。
1.5 使用ELK必要性(解決運維痛點)
- 開發人員不能登錄線上服務器查看詳細日志
- 各個系統都有日志,日志數據分散難以查找
- 日志數據量大,查詢速度慢,或者數據不夠實時
第2章 ELK部署
2.1 環境准備
[root@server elasticsearch]# cat /etc/redhat-release
CentOS release 6.6 (Final)
[root@server elasticsearch]# uname -r
2.6.32-504.el6.x86_64
配置表:
備注:另外可以增添服務器組,集群。
服務器配置表如下:
2.2 Elasticsearch
Elasticsearch需要Java環境,可以直接使用yum安裝。也可以從Oracle官網下載JDK進行安裝。開始之前要確保JDK正常安裝並且環境變量也配置正確。
2.2.1 安裝JDK
[root@m01 ~]# yum install -y java [root@m01 ~]# java -version openjdk version "1.8.0_121" OpenJDK Runtime Environment (build 1.8.0_121-b13) OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)
2.2.2 添加elasticsearch、logstash、kibana的yum倉庫
1、下載並安裝GPG key
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
2、添加elasticsearch.repo源 (或者直接從官網下載rpm包上傳到服務器上面安裝)
cat >/etc/yum.repos.d/elasticsearch.repo<<EOF [elasticsearch-2.x] name=Elasticsearch repository for 2.x packages baseurl=http://packages.elastic.co/elasticsearch/2.x/centos gpgcheck=1 gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch enabled=1 EOF
3、安裝elasticsearch (存儲+搜索)
[root@server elasticsearch]# yum install -y elasticsearch
4、 修改 limits.conf
#在結尾添加如下兩行代碼 [root@server elasticsearch]#vi /etc/security/limits.conf elasticsearch soft memlock unlimited elasticsearch hard memlock unlimited #檢查 [root@server elasticsearch]# tail -2 /etc/security/limits.conf elasticsearch soft memlock unlimited elasticsearch hard memlock unlimited
5、創建目錄並授權
[root@m01 ~]# mkdir -p /data/es-data [root@m01 ~]# chown -R elasticsearch.elasticsearch /data/es-data/
6、配置Elasticsearch
cat >/etc/elasticsearch/elasticsearch.yml<<EOF
cluster.name: elk-cluter
node.name: linux-node1
path.data: /data/es-data
path.logs: /var/log/elasticsearch/
bootstrap.mlockall: true
network.host: 192.168.30.129
http.port: 9200
EOF
#檢查配置文件
[root@m01 ~]# grep '^[a-z]' /etc/elasticsearch/elasticsearch.yml cluster.name: elk-cluter # 集群的名稱,判別節點是否是統計集群 node.name: linux-node1 # 節點的名稱 path.data: /data/es-data # 數據存放路徑 path.logs: /var/log/elasticsearch/ # 日志路徑 bootstrap.mlockall: true # 鎖住內存,使內存不會在swap中使用 network.host:192.168.30.129 # 允許訪問的IP或域名 http.port: 9200 # 端口
7、啟動Elasticsearch
[root@server elasticsearch]# /etc/init.d/elasticsearch start 正在啟動 elasticsearch:OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N [確定] #檢查啟動 [root@server elasticsearch]# ps -ef|grep elasticsearch 498 1422 1 96 21:50 ? 00:00:15 /usr/bin/java -Xms256m -Xmx1g -Djava.awt.headless=true -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -XX:+DisableExplicitGC -Dfile.encoding=UTF-8 -Djna.nosys=true -Des.path.home=/usr/share/elasticsearch -cp /usr/share/elasticsearch/lib/elasticsearch-2.4.4.jar:/usr/share/elasticsearch/lib/* org.elasticsearch.bootstrap.Elasticsearch start -p /var/run/elasticsearch/elasticsearch.pid -d -Des.default.path.home=/usr/share/elasticsearch -Des.default.path.logs=/var/log/elasticsearch -Des.default.path.data=/var/lib/elasticsearch -Des.default.path.conf=/etc/elasticsearch root 1440 1046 0 21:51 pts/0 00:00:00 grep elasticsearch
8、測試網站看是否能訪問
備注:如果是用域名,請先做域名解析,否則無法訪問成功,並要加端口。(不要用內網IP,無法訪問成功,除非你用VPN)
#windows系統,在hosts文件里,配置域名解析 192.168.30.129:9200 server.nulige.com
192.168.30.128:9200 slave.nulige.com
在linux中訪問測試:
#可以用ip訪問
[root@server ~]# curl 192.168.30.129:9200
{
"name" : "linux-node1",
"cluster_name" : "elk-cluter",
"cluster_uuid" : "lBOcpanYR9WQnM3zZQ7org",
"version" : {
"number" : "2.4.4",
"build_hash" : "fcbb46dfd45562a9cf00c604b30849a6dec6b017",
"build_timestamp" : "2017-01-03T11:33:16Z",
"build_snapshot" : false,
"lucene_version" : "5.5.2"
},
"tagline" : "You Know, for Search"
}
在windows系統中訪問
2.2.3 Elasticsearch插件
1、安裝Elasticsearch集群管理插件
[root@server elasticsearch]# /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head
-> Installing mobz/elasticsearch-head...
Trying https://github.com/mobz/elasticsearch-head/archive/master.zip ...
Downloading ...................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................DONE
Verifying https://github.com/mobz/elasticsearch-head/archive/master.zip checksums if available ...
NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)
Installed head into /usr/share/elasticsearch/plugins/head #安裝路徑
在線安裝(若無法下載,請使用離線安裝方法):
離線安裝:
[root@server elasticsearch]# wget https://github.com/mobz/elasticsearch-head/archive/master.zip [root@server elasticsearch]# /usr/share/elasticsearch/bin/plugin install file:/root/master.zip -> Installing from file:/root/master.zip... Trying file:/root/master.zip ... Downloading .........DONE Verifying file:/root/master.zip checksums if available ... NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify) Installed head into /usr/share/elasticsearch/plugins/head
插件安裝幫助信息:
[root@server elasticsearch]# /usr/share/elasticsearch/bin/plugin install -h NAME install - Install a plugin SYNOPSIS plugin install <name or url> DESCRIPTION This command installs an elasticsearch plugin. It can be used as follows: Officially supported or commercial plugins require just the plugin name: plugin install analysis-icu plugin install shield Plugins from GitHub require 'username/repository' or 'username/repository/version': plugin install lmenezes/elasticsearch-kopf plugin install lmenezes/elasticsearch-kopf/1.5.7 Plugins from Maven Central or Sonatype require 'groupId/artifactId/version': plugin install org.elasticsearch/elasticsearch-mapper-attachments/2.6.0 Plugins can be installed from a custom URL or file location as follows: plugin install http://some.domain.name//my-plugin-1.0.0.zip plugin install file:/path/to/my-plugin-1.0.0.zip OFFICIAL PLUGINS The following plugins are officially supported and can be installed by just referring to their name - analysis-icu - analysis-kuromoji - analysis-phonetic - analysis-smartcn - analysis-stempel - cloud-aws - cloud-azure - cloud-gce - delete-by-query - discovery-multicast - lang-javascript - lang-python - mapper-attachments - mapper-murmur3 - mapper-size OPTIONS -t,--timeout Timeout until the plugin download is abort -v,--verbose Verbose output -h,--help Shows this message -b,--batch Enable batch mode explicitly, automatic confirmation of security permissions
2.2.4 訪問head集群插件
http://192.168.30.129:9200/_plugin/head/
示例1:用復合查詢
通過post提交,設置查詢條件,得到結果
通過get的方式,用id的值,查詢出結果:
示例2:基本查詢
2.2.5 server.nulige.com(主) 安裝監控插件(根據需求選裝)
[root@server head]# /usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf -> Installing lmenezes/elasticsearch-kopf... Trying https://github.com/lmenezes/elasticsearch-kopf/archive/master.zip ... Downloading ..................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................DONE Verifying https://github.com/lmenezes/elasticsearch-kopf/archive/master.zip checksums if available ... NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify) Installed kopf into /usr/share/elasticsearch/plugins/kopf
訪問網站地址:
http://server.nulige.com:9200/_plugin/kopf/#!/cluster
2.2.6 elasticsearch集群
如果要配置集群,另外配置集群名相同的節點,通過組播進行通信,會通過cluster進行查找,如果無法通過組播查詢,修改成單播即可。
配置文件參考如下:
grep "^[a-Z]" /etc/elasticsearch/elasticsearch.yml
內容:
cluster.name: elk-cluter
node.name: linux-node2
path.data: /data/es-data
path.logs: /var/log/elasticsearch/
bootstrap.mlockall: true
network.host: 192.168.30.128
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.168.30.129","192.168.30.128"] #單播(配置一台即可,生產可以使用組播方式)
修改方法:
#停止服務 [root@slave ~]# /etc/init.d/elasticsearch stop #啟動服務 [root@slave ~]# /etc/init.d/elasticsearch start
備注:先停止服務,再修改配置文件,再啟動服務,可在集群管理插件頁面查到相關信息。
在瀏覽器中查看分片信息,一個索引默認被分成了5個分片,每份數據被分成了五個分片(可以調節分片數量),下圖中外圍帶綠色框的為主分片,不帶框的為副本分片,主分片丟失,副本分片會復制一份成為主分片,起到了高可用的作用,主副分片也可以使用負載均衡加快查詢速度,但是如果主副本分片都丟失,則索引就是徹底丟失。
2.3 LogStash部署與配置
先安裝JDK,也可以下載安裝Oracle的JDK或者使用 yum安裝openjdk。
yum install -y java
2.3.1 安裝logstash (作用:收集日志)
1、下載並安裝GPG key
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
2、添加yum倉庫
cat >/etc/yum.repos.d/logstash.repo<<EOF [logstash-2.3] name=Logstash repository for 2.3.x packages baseurl=https://packages.elastic.co/logstash/2.3/centos gpgcheck=1 gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch enabled=1 EOF
3、安裝logstash
yum install -y logstash
2.3.2 logstash入門
啟動一個logstash,-e:在命令行執行;input輸入,stdin標准輸入,是一個插件;output輸出,stdout:標准輸出
[root@node1 tools]# /opt/logstash/bin/logstash -e 'input { stdin {} } output { stdout{codec => rubydebug} }' OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N hello #<===會停在這里,等待讓你輸入內容。 #等待一會,才會返回下面結果: Settings: Default pipeline workers: 1 Pipeline main started { "message" => "hello", "@version" => "1", "@timestamp" => "2017-04-08T04:44:14.153Z", "host" => "node1.nulige.com" }
#他會顯示出內容,版本號,時間,主機名
使用rubydebug顯示詳細輸出,codec為一種編碼器
使用logstash講信息寫入到Elasticsearch:
[root@node1 tools]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["server.nulige.com:9200"] } }' OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N hello #輸入一條內容 Settings: Default pipeline workers: 1 Pipeline main started
每一條輸出的內容稱為一個事件,多個相同的輸出的內容合並到一起稱為:一個事件
2.3.3 收集nginx訪問日志
#修改nginx日志格式為json log_format json '{ "@timestamp": "$time_local", ' '"@fields": { ' '"remote_addr": "$remote_addr", ' '"remote_user": "$remote_user", ' '"body_bytes_sent": "$body_bytes_sent", ' '"request_time": "$request_time", ' '"status": "$status", ' '"request": "$request", ' '"request_method": "$request_method", ' '"http_referrer": "$http_referer", ' '"body_bytes_sent":"$body_bytes_sent", ' '"http_x_forwarded_for": "$http_x_forwarded_for", ' '"http_user_agent": "$http_user_agent" } }';
2.4 安裝Kibana
1、添加kibana.repo源
cat >/etc/yum.repos.d/kibana.repo<<EOF [kibana-4.5] name=Kibana repository for 4.5.x packages baseurl=http://packages.elastic.co/kibana/4.5/centos gpgcheck=1 gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch enabled=1 EOF
2、安裝kibana
#展示效果 [root@server ~]# yum install -y kibana
3、修改kibana的配置文件,開啟下面選項
[root@server ~]# vi /opt/kibana/config/kibana.yml server.port: 5601 server.host: "0.0.0.0" elasticsearch.url: "http://server.nulige.com:9200" elasticsearch.preserveHost: true kibana.index: ".kibana"
#檢查
[root@server ~]# egrep -v "^$|#" /opt/kibana/config/kibana.yml server.port: 5601 server.host: "0.0.0.0" elasticsearch.url: "http://server.nulige.com:9200" elasticsearch.preserveHost: true kibana.index: ".kibana"
4、啟動服務並檢查
[root@server ~]# /etc/init.d/kibana restart kibana started
[root@server ~]# ps -ef|grep kibana kibana 3559 1 55 14:16 pts/0 00:00:07 /opt/kibana/bin/../node/bin/node /opt/kibana/bin/../src/cli root 3571 2931 0 14:17 pts/0 00:00:00 grep kibana
5、訪問網站:
http://server.nulige.com:5601
6、創建默認索引
7、展示今天的數據