ELK yum 安裝部署


ELK 官網:

https://www.elastic.co/cn/what-is/elk-stack

ElasticSearch

ElasticSearch 是一個高可用開源全文檢索和分析組件。提供存儲服務,搜索服務,大數據准實時分析等。一般用於提供一些提供復雜搜索的應用

基本概念:

Index

定義:類似於mysql中的database。索引只是一個邏輯上的空間,物理上是分為多個文件來管理的。 
命名:必須全小寫 
描述:在實踐過程中每個index都會有一個相應的副 本。主要用來在硬件出現問題時,用來回滾數據的。這也某種程序上,加劇了ES對於內存高要求

Type

定義:類似於mysql中的table,根據用戶需求每個index中可以新建任意數量的type。

Document

定義:對應mysql中的row。有點類似於MongoDB中的文檔結構,每個Document是一個json格式的文本

Mapping

更像是一個用來定義每個字段類型的語義規范。在mysql中類似sql語句,在ES中經過包裝后,都被封裝為友好的Restful風格的接口進行操作。

這一點也是為什么開發人員更願意使用ES的原因。

Shards & Replicas

定義:能夠為每個索引提供水平的擴展以及備份操作。保證了數據的完整性和安全性
描述: 
Shards:在單個節點中,index的存儲始終是有限制,並且隨着存儲的增大會帶來性能的問題。為了解決這個問題,ElasticSearch提供一個能夠分割單個index到集群各個節點的功能。你可以在新建這個索引時,手動的定義每個索引分片的數量。 
Replicas:在每個node出現宕機或者下線的情況,Replicas能夠在該節點下線的同時將副本同時自動分配到其他仍然可用的節點。而且在提供搜索的同時,允許進行擴展節點的數量,在這個期間並不會出現服務終止的情況。 
默認情況下,每個索引會分配5個分片,並且對應5個分片副本,同時會出現一個完整的副本【包括5個分配的副本數據】。

 

前提:關閉防火牆,關閉selinux

systemctl stop firewalld

setenforce 0

 

系統優化:

[root@node1 ~]# cat  /etc/security/limits.conf |tail -n 15
*		soft	nproc		65535
*		soft	nofile		65535
*		hard	nproc		65535
*		hard	nofile		65535

vi /etc/sysctl.conf 加一行
vm.max_map_coun=655360

 vm.max_map_coun=655360

三個組件都安裝到一台服務器上

129:ES+kibana

131:logstash

1 安裝jdk

rpm -ivh jdk-8u131-linux-x64_.rpm

2 配置elasticsearch 的yum源

[root@node1 ~]# cat /etc/yum.repos.d/elasticsearch.repo 
[elasticsearch-6.x]
name=Elasticsearch repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md

 yum install elasticsearch

配置開機自啟動

[root@node1 ~]# systemctl enable elasticsearch.service

開啟服務

[root@node1 ~]# systemctl start elasticsearch.service

驗證服務是否啟動

[root@localhost elasticsearch]# netstat  -lptnu|grep java
tcp6       0      0 127.0.0.1:9200          :::*                    LISTEN      51790/java          
tcp6       0      0 ::1:9200                :::*                    LISTEN      51790/java          
tcp6       0      0 127.0.0.1:9300          :::*                    LISTEN      51790/java          
tcp6       0      0 ::1:9300                :::*                    LISTEN      51790/java 

監聽端口:

9200作為Http協議,主要用於外部通訊

9300作為Tcp協議,ES集群之間是通過9300進行通訊

 

配置文件路徑:

[root@localhost elasticsearch]# ll /etc/elasticsearch/
total 36
-rw-rw----. 1 root elasticsearch   199 Nov  7 01:56 elasticsearch.keystore
-rw-rw----. 1 root elasticsearch  2869 Oct 15 23:31 elasticsearch.yml
-rw-rw----. 1 root elasticsearch  3685 Oct 15 23:31 jvm.options
-rw-rw----. 1 root elasticsearch 13085 Oct 15 23:31 log4j2.properties
-rw-rw----. 1 root elasticsearch   473 Oct 15 23:31 role_mapping.yml
-rw-rw----. 1 root elasticsearch   197 Oct 15 23:31 roles.yml
-rw-rw----. 1 root elasticsearch     0 Oct 15 23:31 users
-rw-rw----. 1 root elasticsearch     0 Oct 15 23:31 users_roles

 

[root@huazai007 ~]# cat  /etc/elasticsearch/elasticsearch.yml |grep -v '^#'
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 172.18.47.110
http.port: 9200

 

日志文件路徑:

[root@localhost elasticsearch]# pwd
/var/log/elasticsearch
[root@localhost elasticsearch]# ll
total 48
-rw-r--r--. 1 elasticsearch elasticsearch     0 Nov  7 01:58 elasticsearch_access.log
-rw-r--r--. 1 elasticsearch elasticsearch     0 Nov  7 01:58 elasticsearch_audit.log
-rw-r--r--. 1 elasticsearch elasticsearch     0 Nov  7 01:58 elasticsearch_deprecation.log
-rw-r--r--. 1 elasticsearch elasticsearch     0 Nov  7 01:58 elasticsearch_index_indexing_slowlog.log
-rw-r--r--. 1 elasticsearch elasticsearch     0 Nov  7 01:58 elasticsearch_index_search_slowlog.log
-rw-r--r--. 1 elasticsearch elasticsearch  8502 Nov  7 01:58 elasticsearch.log
-rw-r--r--. 1 elasticsearch elasticsearch 35454 Nov  7 02:00 gc.log.0.current

elasticsearch 配置詳解:

cluster部分:==========
cluster.name: elasticsearch
配置es的集群名稱,默認是elasticsearch,es會自動發現在同一網段下的es,
如果在同一網段下有多個集群,就可以用這個屬性來區分不同的集群。

node部分:===========
node.name: node-1
定義節點名稱
node.attr.rack: r1
向節點添加自定義屬性

Paths部分:==========
path.data: /var/lib/elasticsearch
設置索引數據的存儲路徑
path.logs: /var/log/elasticsearch
設置日志文件的存儲路徑

Memory部分:=======
bootstrap.memory_lock: true
啟動時鎖定內存,為了保證es正常運行

Network部分:========
network.host: 192.168.0.1
設置綁定的ip地址,可以是ipv4或ipv6的
http.port: 9200
設置對外服務的http端口,默認為9200。

Discovery部分:=======

discovery.zen.ping.unicast.hosts: ["host1", "host2"]
設置集群中master節點的初始列表,可以通過這些節點來自動發現新加入集群的節點
discovery.zen.minimum_master_nodes:
設置這個參數來保證集群中的節點可以知道其它N個有master資格的節點。
默認為1,對於大的集群來說,可以設置大一點的值(2-4)

Gateway部分:========
gateway.recover_after_nodes: 3
設置集群中N個節點啟動時進行數據恢復,默認為1

Various部分: [ˈveriəs]=======各種各樣====
action.destructive_requires_name: true
刪除索引時需要顯式名稱

 

常用命令:

驗證服務

curl -X GET http://127.0.0.1:9200

elasticsearch 查看集群統計信息

curl -XGET 'http://localhost:9200/_cluster/stats?pretty'

查看集群狀態

curl -X GET 'localhost:9200/_cat/health?v'

創建索引:test_index

curl -X PUT HTTP://localhost:9200/test_index?pretty

elasticsearch 查看所有索引

curl -X GET HTTP://localhost:9200/_cat/indices?v

curl -s http://192.168.1.9:9200/_cat/indices|grep "msg"|awk '{print $3}'|sort

刪除索引:test_index

curl -XDELETE 'localhost:9200/test_index?pretty'

 logstash工作原理:

Logstash事件處理有三個階段:inputs → filters → outputs。是一個接收,處理,轉發日志的工具。

支持系統日志,webserver日志,錯誤日志,應用日志,總之包括所有可以拋出來的日志類型。

Input模塊:輸入數據到logstash

一些常用的輸入為:

file:從文件系統的文件中讀取,類似於tail-f命令

redis:從redis service中讀取

beats:從filebeat中讀取

kafka:從kafka隊列中讀取

 

Filters:數據中間處理,對數據進行操作。

一些常用的過濾器為:

grok:解析任意文本數據,Grok 是 Logstash 最重要的插件。

它的主要作用就是將文本格式的字符串,轉換成為具體的結構化的數據,配合正則表達式使用。內置120多個解析語法。

官方提供的grok表達式:https://github.com/logstash-plugins/logstash-patterns-core/tree/master/patterns
grok在線調試:https://grokdebug.herokuapp.com/

mutate [ˈmjuːteɪt]:對字段進行轉換。

例如對字段進行刪除、替換、修改、重命名等。

drop:丟棄一部分events不進行處理。

clone:拷貝 event,這個過程中也可以添加或移除字段。

geoip:添加地理信息(為前台kibana圖形化展示使用)

 

Outputs模塊outputslogstash處理管道的最末端組件。

一個event可以在處理過程中經過多重輸出,但是一旦所有的outputs都執行結束,這個event也就完成生命周期。

一些常見的outputs為:

elasticsearch:可以高效的保存數據,並且能夠方便和簡單的進行查詢。

file:將event數據保存到文件中。

graphite [ˈɡræfaɪt]:將event數據發送到圖形化組件中,一個很流行的開源存儲圖形化展示的組件。

Codecs模塊codecs 是基於數據流的過濾器,它可以作為inputoutput的一部分配置。Codecs可以幫助你輕松的分割發送過來已經被序列化的數據。

一些常見的codecs:

json:使用json格式對數據進行編碼/解碼。

multiline:將匯多個事件中數據匯總為一個單一的行。比如:java異常信息和堆棧信息。

3 logstash 安裝

配置logstash yum 源

[logstash-6.x]
name=Elastic repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md

 yum install logstash

 

配置文件路徑:

[root@localhost logstash]# pwd
/etc/logstash
[root@localhost logstash]# ll
total 36
drwxrwxr-x. 2 root root    6 Oct 16 00:57 conf.d
-rw-r--r--. 1 root root 1915 Oct 16 00:57 jvm.options
-rw-r--r--. 1 root root 4568 Oct 16 00:57 log4j2.properties
-rw-r--r--. 1 root root  342 Oct 16 00:57 logstash-sample.conf
-rw-r--r--. 1 root root 8236 Nov  7 02:33 logstash.yml
-rw-r--r--. 1 root root  285 Oct 16 00:57 pipelines.yml
-rw-------. 1 root root 1696 Oct 16 00:57 startup.options

 

日志文件路徑:

[root@localhost logstash]# pwd
/var/log/logstash
[root@localhost logstash]# ll
total 4
-rw-r--r--. 1 logstash logstash 1644 Nov  7 02:36 logstash-plain.log
-rw-r--r--. 1 logstash logstash    0 Nov  7 02:35 logstash-slowlog-plain.log

 

logstash是用來收集日志,並對日志做過濾處理的,我們下面要分析的是系統日志,所以要編寫一個收集日志的配置文件

vim /etc/logstash/conf.d/system.conf

input日志輸入模塊:日志的獲取方式和路徑

input { file { path => "/var/log/messages" type => "system-log" start_position => "beginning" } } output日志的輸出模塊:導出你的數據 output { elasticsearch { hosts => "192.168.10.129:9200" index => "system_log-%{+YYYY.MM.dd}" } }

這里為了測試,將/var/log/messages日志的權限修改為了644

chmod 777 /var/log/messages

 

如果conf.d 下有多個配置文件需要修改pipelines.yml(管道)文件

- pipeline.id: main
  path.config: "/etc/logstash/conf.d/system.conf"
- pipeline.id: sec
  path.config: "/etc/logstash/conf.d/sec.conf"

 

配置開機自啟動

 [root@localhost ~]# systemctl enable logstash.service
啟動logstash服務
[root@localhost ~]# systemctl start logstash.service

監聽端口:

[root@localhost logstash]# netstat  -lptnu|grep java
tcp6       0      0 127.0.0.1:9600          :::*                    LISTEN      6722/java 

 

logstash 無法啟動報錯:

 

 解決:

 

 

 4 安裝kibana

配置yum 源

[kibana-6.x]
name=Kibana repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md

 systemctl enable kibana.service

systemctl start kibana

 配置kibana

這里只需要配置監聽地址和elasticsearch的ip就可以了

vim /etc/kibana/kibana.yml

[root@localhost yum.repos.d]# cat /etc/kibana/kibana.yml |grep -v "^#"
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://192.168.10.129:9200"]

啟動服務:

Systemctl start kibana

監聽端口:5601

 在kibana上創建索引

 

 

 

 給系統日志添加可視化圖形

 

 選擇創建圖形類型為線形圖

 

 選擇繪畫哪個索引的圖形

 

 選擇x軸為繪畫日期的柱狀圖,然后點擊開始獲取數據

 


免責聲明!

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



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