ELK概述
ELK是三個開源軟體的縮寫,分别表示:Elasticsearch , Logstash, Kibana , 它們都是開源軟體。新增了一個FileBeat,它是一個輕量級的日志收集處理工具(Agent),Filebeat占用資源少,适合于在各個伺服器上搜集日志後傳輸給Logstash,官方也推薦此工具。
各工具合作流程圖
- Elasticsearch是個開源分布式搜尋引擎,提供搜集、分析、存儲資料三大功能。它的特點有:分布式,零配置,自動發現,索引自動分片,索引副本機制,restful風格接口,多資料源,自動搜尋負載等。
- Logstash 主要是用來日志的搜集、分析、過濾日志的工具,支援大量的資料擷取方式。一般工作方式為c/s架構,client端安裝在需要收集日志的主機上,server端負責将收到的各節點日志進行過濾、修改等操作在一并發往elasticsearch上去。
Logstash工作流程
-
Kibana是一個開源的分析和可視化平台,設計用于和Elasticsearch一起工作。
可以用Kibana來搜尋,檢視,并和存儲在Elasticsearch索引中的資料進行互動(增删改資料都可以)。
可以輕松地執行進階資料分析,并且以各種圖示、表格和地圖的形式可視化資料。
Kibana使得了解大量資料變得很容易。它簡單的、基于浏覽器的界面使你能夠快速建立和共享動态儀表闆,實時顯示Elasticsearch查詢的變化。
-
Filebeat隸屬于Beats。目前Beats包含四種工具:
1.Packetbeat(搜集網絡流量資料)
2.Metricbeat(搜集系統、程序和檔案系統級别的 CPU 和記憶體使用情況等資料)
3.Filebeat(搜集檔案資料)
4.Winlogbeat(搜集 Windows 事件日志資料)
Filebeat具體工作流程
ELK6.5 環境搭建
先在各伺服器上安裝JDK并配置
cd /usr/local/src
wget https://download.oracle.com/otn-pub/java/jdk/8u191-b12/2787e4a523244c269598db4e85c51e0c/jdk-8u191-linux-x64.tar.gz
tar xf jdk-8u191-linux-x64.tar.gz
mv jdk1.8.0_191/ /usr/local/java
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/local/java
export PATH=$PATH:$JAVA_HOME/bin
source /etc/profile
安裝ElasticSearch6.5.2
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.2.tar.gz
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.2.tar.gz.sha512
shasum -a 512 -c elasticsearch-6.5.2.tar.gz.sha512
tar -xzf elasticsearch-6.5.2.tar.gz
cd elasticsearch-6.5.2/
## 由于虛拟機隻配置設定了1G記憶體,是以需要修改JVM的初始化記憶體,這裡修改配置檔案 jvm.options
cd /usr/local/src/elasticsearch-6.5.2/config
vim config/jvm.options
修改 jvm.options
# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space
-Xms384m
-Xmx384m
修改配置檔案 config/elasticsearch.yml
cluster.name: myapplication
node.name: node-105
path.data: /usr/local/src/elasticsearch-6.5.2/data
path.logs: /usr/local/src/elasticsearch-6.5.2/logs
bootstrap.memory_lock: false
network.host: 192.168.56.105
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.168.56.105", "192.168.56.101", "192.168.56.103"]
discovery.zen.minimum_master_nodes: 2
gateway.recover_after_nodes: 3
注意:bootstrap.memory_lock:true 配置的意義:鎖定實體記憶體位址,防止es記憶體被交換出去,也就是避免es使用swap交換分區,頻繁的交換,會導緻IOPS變高。
建立ElasticSearch程序使用者 es
sudo groupadd es
sudo useradd es -g es
## 為es使用者設定一個密碼
sudo passwd es
修改 /etc/security/limits.conf 加入以下内容
es soft memlock unlimited
es hard memlock unlimited
es hard nofile 65536
es soft nofile 65536
es hard nproc 65536
es soft nproc 65536
注意:修改後必須退出重新登入才會生效
将ElasticSearch相關的目錄的所有者改為es賬戶
sudo chown -R es:es /usr/local/src/jdk1.8.0_191
sudo chown -R es:es /usr/local/src/elasticsearch-6.5.2
啟動ElasticSearch
cd /usr/local/src/elasticsearch-6.5.2
su es
./bin/elasticsearch
啟動報錯:
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
vim /etc/sysctl.conf
vm.max_map_count=262144
sudo sysctl -p
或
sysctl -w vm.max_map_count=262144
檢視叢集啟動後能否正常工作
curl -X GET "192.168.56.105:9200/"
{
"name" : "node-105",
"cluster_name" : "myapplication",
"cluster_uuid" : "dBHz7YwUQLa3F943QhsW2g",
"version" : {
"number" : "6.5.2",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "9434bed",
"build_date" : "2018-11-29T23:58:20.891072Z",
"build_snapshot" : false,
"lucene_version" : "7.5.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
檢視es叢集節點狀态
curl http://192.168.56.105:9200/_cluster/health?pretty
{
"cluster_name" : "myapplication",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 3,
"number_of_data_nodes" : 3,
"active_primary_shards" : 0,
"active_shards" : 0,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
Elasticsearch安裝插件
./bin/elasticsearch-plugin install analysis-icu
./bin/elasticsearch-plugin install analysis-smartcn
檢視已安裝的elasticsearch插件
./bin/elasticsearch-plugin list
Elasticsearch内部原理
Elasticsearch 天生就是分布式的,并且在設計時屏蔽了分布式的複雜性.
Elasticsearch 盡可能地屏蔽了分布式系統的複雜性。這裡列舉了一些在背景自動執行的操作:
- 配置設定文檔到不同的容器 或 分片 中,文檔可以儲存在一個或多個節點中
- 按叢集節點來均衡配置設定這些分片,進而對索引和搜尋過程進行負載均衡
- 複制每個分片以支援資料備援,進而防止硬體故障導緻的資料丢失 将叢集中任一節點的請求路由到存有相關資料的節點
- 叢集擴容時無縫整合新節點,重新配置設定分片以便從離群節點恢複
一個運作中的 Elasticsearch 執行個體稱為一個 節點,而叢集是由一個或者多個擁有相同 cluster.name 配置的節點組成, 它們共同承擔資料和負載的壓力。當有節點加入叢集中或者從叢集中移除節點時,叢集将會重新平均分布所有的資料。
當一個節點被選舉成為 主 節點時, 它将負責管理叢集範圍内的所有變更,例如增加、删除索引,或者增加、删除節點等。 而主節點并不需要涉及到文檔級别的變更和搜尋等操作,是以當叢集隻擁有一個主節點的情況下,即使流量的增加它也不會成為瓶頸。 任何節點都可以成為主節點。
作為使用者,我們可以将請求發送到 叢集中的任何節點 ,包括主節點。 每個節點都知道任意文檔所處的位置,并且能夠将我們的請求直接轉發到存儲我們所需文檔的節點。 無論我們将請求發送到哪個節點,它都能負責從各個包含我們所需文檔的節點收集回資料,并将最終結果傳回給用戶端。 Elasticsearch 對這一切的管理都是透明的。
kibana 安裝配置
cd /usr/local/src
wget https://artifacts.elastic.co/downloads/kibana/kibana-6.5.2-linux-x86_64.tar.gz
tar -zxf kibana-6.5.2-linux-x86_64.tar.gz
cd kibana-6.5.2-linux-x86_64
##修改配置檔案
vim config/kibana.yml
# 修改kibana外網位址的host
server.host: "192.168.56.102"
# 修改kibana要連接配接的ES伺服器
elasticsearch.url: "http://192.168.56.105:9200"
./bin/kibana
驗證是否成功 浏覽器中輸入 http://192.168.56.102:5601
Logstash 安裝配置
cd /usr/local/src
wget https://artifacts.elastic.co/downloads/logstash/logstash-6.5.2.tar.gz
tar -zxf logstash-6.5.2.tar.gz
cd logstash-6.5.2
## 建立配置檔案 config/default.conf
# 監聽5044端口作為輸入
input {
beats {
port => "5044"
}
}
# 資料過濾
filter {
grok {
match => { "message" => "MAINNGINXLOG %{COMBINEDAPACHELOG} %{QS:x_forwarded_for}" }
}
date {
match => [ "timestamp", "dd/MMM/yyyy:HH:mm:ss Z"]
target => ["datetime"]
}
geoip {
source => "clientip"
}
}
# 輸出配置為ES伺服器的9200端口
output {
elasticsearch {
hosts => ["192.168.56.105:9200"]
index => "access_log"
}
}
# 啟動服務
./bin/logstash -f config/default.conf
Filebeat 安裝配置
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.5.2-linux-x86_64.tar.gz
tar -zxf filebeat-6.5.2-linux-x86_64.tar.gz
cd filebeat-6.5.2-linux-x86_64
## 修改配置檔案 filebeat.yml
# 修改資料來源,配置為nginx的日志目錄,包括access.log及error.log
filebeat.inputs:
- type:log
enabled: true
paths:
- /var/log/nginx/*.log
# kibana相關設定,配置為kibana伺服器位址
setup.kibana:
host: "192.168.56.102:5601"
# logstash的host位址及端口
output.logstash:
hosts: ["192.168.56.102:5044"]
## 運作filebeat -e 在終端顯示一些處理過程資訊
./filebeat -e -c filebeat.yml &