ELK快速入門(一)基本部署

ELK快速入門一-基本部署

ELK簡介

什麼是ELK?通俗來說,ELK是由ElasticsearchLogstashKibana 三個開源軟件組成的一個組合體,這三個軟件當中,每一個軟件用於完成不一樣的功能,ELK又稱ELKstack,官網 https://www.elastic.co/ , ELK主要優勢有以下幾個:
一、處理方式靈活:elasticsearch是實時全文索引,具備強大的搜索功能
二、配置相對簡單:elasticsearch所有使用JSON接口,logstash使用模塊配置,kibana的配置文件部分更簡單
三、檢索性能高:基於優秀的設計,雖然每次查詢都是實時,可是也能夠達到百億級數據的查詢秒級響應
四、集羣線性擴展:elasticsearchlogstash均可以靈活線性擴展
五、前端操做絢麗:kibana的前端設計比較絢麗,並且操做簡單html

Elasticsearch

elasticsearch是一個高度可擴展全文搜索和分析引擎,基於Apache Lucene 構建,能對大容量的數據進行接近實時的存儲、搜索和分析操做,能夠處理大規模日誌數據,好比NginxTomcat、系統日誌等功能。前端

Logstash

數據收集引擎。它支持動態的從各類數據源蒐集數據,並對數據進行過濾、分析、豐富、統一格式等操做,而後存儲到用戶指定的位置;支持普通log、自定義json格式的日誌解析。java

Kibana

數據分析和可視化平臺。一般與 Elasticsearch 配合使用,對其中數據進行搜索、分析和以統計圖表的方式展現。node

ELK部署環境準備

這裏實驗所使用系統CentOS 7.4 x86_64,服務器信息以下。並關閉防火牆和selinux,及host綁定等。本文所使用全部的軟件包 下載 提取碼:ow1blinux

IPAddr HostName Mem
192.168.1.31 linux-elk1.exmaple.com 3G
192.168.1.32 linux-elk2.exmaple.com 3G
epel源配置
# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

Elasticsearch部署

由於elasticsearch服務運行須要java環境,所以兩臺elasticsearch服務器須要安裝java環境。git

安裝JDK

centos7默認是安裝了jdk,若是須要安裝高版本可使用一下步驟,這裏使用下面的yum安裝jdk 1.8.0_211 。注意:兩個節點都要安裝。github

方法一:yum安裝下載好的JDK包,將下載好的軟件包上傳到服務器進行安裝,首先卸載自帶的jdk;再進行安裝。
下載地址:https://pan.baidu.com/s/1VK1iCnvouppZ06jsVBOaRw  提取碼:lofc

[root@linux-elk1 ~]# rpm -qa |grep jdk |xargs yum -y remove {}\;
[root@linux-elk1 ~]# yum -y localinstall jdk-8u211-linux-x64.rpm
[root@linux-elk1 ~]# java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)

方法二:源碼安裝JDK,將下載的軟件包上傳到服務器進行安裝。
下載地址:https://pan.baidu.com/s/1AAPyPzhdclNNCb0m6ooVYQ  提取碼:x18u

[root@linux-elk1 ~]# tar xf jdk-8u211-linux-x64.tar.gz -C /usr/local/
[root@linux-elk1 ~]# ln -s /usr/local/jdk1.8.0_211 /usr/local/java
[root@linux-elk1 ~]# sed -i.ori '$a export JAVA_HOME=/usr/local/java \nexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH \nexport CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar' /etc/profile
[root@linux-elk1 ~]# source /etc/profile
[root@linux-elk1 ~]# java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)

安裝Elasticsearch

兩臺節點都須要安裝elasticsearch,使用yum安裝會很慢,因此先下載下來傳到服務器進行安裝,官網下載地址:https://www.elastic.co/cn/downloads/past-releases#elasticsearch
本文所使用的包下載:https://pan.baidu.com/s/1djYOs3PQjtq16VkPMETAWg 提取碼:b15vshell

將下載的elasticsearch包上傳到服務器進行安裝。
[root@linux-elk1 ~]# yum -y localinstall elasticsearch-6.8.1.rpm
[root@linux-elk2 ~]# yum -y localinstall elasticsearch-6.8.1.rpm


配置elasticsearch,linux-elk2配置一個相同的節點,經過組播進行通訊,若是沒法經過組播查詢,修改爲單播便可。
[root@linux-elk1 ~]# vim /etc/elasticsearch/elasticsearch.yml
cluster.name: ELK-Cluster    #ELK的集羣名稱,名稱相同即屬因而同一個集羣
node.name: elk-node1    #本機在集羣內的節點名稱
path.data: /elk/data    #數據存放目錄
path.logs: /elk/logs    #日誌保存目錄
bootstrap.memory_lock: true    #服務啓動的時候鎖定足夠的內存,防止數據寫入swap
network.host: 192.168.1.31    #監聽的IP地址
http.port: 9200    #服務監聽的端口
discovery.zen.ping.unicast.hosts: ["192.168.1.31", "192.168.1.32"]    #單播配置一臺便可


修改內存限制,內存鎖定須要進行配置須要2g以上內存,不然會致使沒法啓動elasticsearch。
[root@linux-elk1 ~]# vim /usr/lib/systemd/system/elasticsearch.service
# 在[Service]下加入下面這行內容
LimitMEMLOCK=infinity
[root@linux-elk1 ~]# systemctl daemon-reload
[root@linux-elk1 ~]# vim /etc/elasticsearch/jvm.options
-Xms2g
-Xmx2g     #最小和最大內存限制,爲何最小和最大設置同樣大?參考:https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html


建立數據目錄和日誌目錄及權限修改
[root@linux-elk1 ~]# mkdir -p /elk/{data,logs}
[root@linux-elk1 ~]# chown elasticsearch.elasticsearch /elk/ -R


啓動elasticsearch及檢查端口是否處於監聽狀態
[root@linux-elk1 ~]# systemctl start elasticsearch
[root@linux-elk1 ~]# netstat -nltup |grep java
tcp6       0      0 192.168.1.31:9200       :::*                    LISTEN      12887/java          
tcp6       0      0 192.168.1.31:9300       :::*                    LISTEN      12887/java



將配置文件copy到linux-elk2上面並進行修改,配置啓動等。
[root@linux-elk1 ~]# scp /etc/elasticsearch/elasticsearch.yml 192.168.1.32:/etc/elasticsearch/elasticsearch.yml
[root@linux-elk2 ~]# grep ^[a-Z] /etc/elasticsearch/elasticsearch.yml 
cluster.name: ELK-Cluster
node.name: elk-node2
path.data: /elk/data
path.logs: /elk/logs
bootstrap.memory_lock: true
network.host: 192.168.1.32
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.168.1.31", "192.168.1.32"]
[root@linux-elk2 ~]# vim /usr/lib/systemd/system/elasticsearch.service
# 在[Service]下加入下面這行內容
LimitMEMLOCK=infinity
[root@linux-elk2 ~]# systemctl daemon-reload
[root@linux-elk2 ~]# vim /etc/elasticsearch/jvm.options
-Xms2g
-Xmx2g
[root@linux-elk2 ~]# mkdir -p /elk/{data,logs}
[root@linux-elk2 ~]# chown elasticsearch.elasticsearch /elk/ -R
[root@linux-elk2 ~]# systemctl start elasticsearch
[root@linux-elk2 ~]#  netstat -nltup |grep java
tcp6       0      0 192.168.1.32:9200       :::*                    LISTEN      18667/java          
tcp6       0      0 192.168.1.32:9300       :::*                    LISTEN      18667/java

經過瀏覽器訪問elasticsearch端口npm

監控elasticsearch集羣狀態json

經過shell命令獲取集羣狀態,這裏獲取到的是一個json格式的返回值,例如對status進行分析,若是等於green(綠色)就是運行在正常,等於yellow(黃色)表示副本分片丟失,red(紅色)表示主分片丟失。

[root@linux-elk1 ~]# curl http://192.168.1.31:9200/_cluster/health?pretty=true
{
  "cluster_name" : "ELK-Cluster",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 2,
  "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
}

[root@linux-elk1 ~]# curl http://192.168.1.32:9200/_cluster/health?pretty=true
{
  "cluster_name" : "ELK-Cluster",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 2,
  "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插件head

咱們不可能常常經過命令來查看集羣的信息,因此就使用到了插件 –head。件是爲了完成不一樣的功能,官方提供了一些插件但大部分是收費的,另外也有一些開發愛好者提供的插件,能夠實現對elasticsearch集羣的狀態監控與管理配置等功能。
head:主要用來作集羣管理的插件
下載地址:https://github.com/mobz/elasticsearch-head

1)安裝

# 安裝npm和git
[root@linux-elk1 ~]# yum -y install npm git

# 安裝elasticsearch-head插件
[root@linux-elk1 ~]# cd /usr/local/src/
[root@linux-elk1 src]# git clone git://github.com/mobz/elasticsearch-head.git
[root@linux-elk1 src]# cd elasticsearch-head/
[root@linux-elk1 elasticsearch-head]# npm install grunt -save --registry=https://registry.npm.taobao.org
[root@linux-elk1 elasticsearch-head]# ll node_modules/grunt    #肯定該目錄有生成文件
總用量 24
drwxr-xr-x. 2 root root   19 4月   6 2016 bin
-rw-r--r--. 1 root root 7111 4月   6 2016 CHANGELOG
drwxr-xr-x. 4 root root   47 7月   4 09:21 lib
-rw-r--r--. 1 root root 1592 3月  23 2016 LICENSE
drwxr-xr-x. 5 root root   50 7月   4 09:21 node_modules
-rw-r--r--. 1 root root 4108 7月   4 09:21 package.json
-rw-r--r--. 1 root root  878 2月  12 2016 README.md
[root@linux-elk1 elasticsearch-head]# npm install --registry=https://registry.npm.taobao.org    #執行安裝
[root@linux-elk1 elasticsearch-head]# npm run start &    #後臺啓動服務
[root@linux-elk1 ~]# ss -nlt |grep 9100
LISTEN     0      128          *:9100                     *:* 

#------------------------補充說明------------------------
因爲上面npm安裝時候超級慢,使用taobao源一樣慢,這裏將已安裝的打成了包,能夠直接下載使用便可
下載地址:https://pan.baidu.com/s/16zDlecKVfmkEeInPcRx9NQ   提取碼:h890
[root@linux-elk1 ~]# yum -y install npm
[root@linux-elk1 ~]# cd /usr/local/src/
[root@linux-elk1 src]# ls
elasticsearch-head.tar.gz
[root@linux-elk1 src]# tar xvzf elasticsearch-head.tar.gz
[root@linux-elk1 src]# cd elasticsearch-head/
[root@linux-elk1 elasticsearch-head]# npm run start &
#--------------------------------------------------------

# 修改elasticsearch服務配置文件,開啓跨域訪問支持,而後重啓elasticsearch服務
[root@linux-elk1 ~]# vim /etc/elasticsearch/elasticsearch.yml
http.cors.enabled: true     #最下方添加
http.cors.allow-origin: "*"

爲了方便管理elasticsearch-head插件,編寫一個啓動腳本

[root@linux-elk1 ~]# vim /usr/bin/elasticsearch-head
#!/bin/bash
#desc: elasticsearch-head service manager
#date: 2019

data="cd /usr/local/src/elasticsearch-head/; nohup npm run start > /dev/null 2>&1 & "

function START (){
    eval $data && echo -e "elasticsearch-head start\033[32m     ok\033[0m"
}

function STOP (){
    ps -ef |grep grunt |grep -v "grep" |awk '{print $2}' |xargs kill -s 9 > /dev/null && echo -e "elasticsearch-head stop\033[32m      ok\033[0m"
}

case "$1" in
    start)
        START
        ;;
    stop)
        STOP
        ;;
    restart)
        STOP
        sleep 3
        START
        ;;
    *)
        echo "Usage: elasticsearch-head (start|stop|restart)"
        ;;
esac

[root@linux-elk1 ~]# chmod +x /usr/bin/elasticsearch-head

2)瀏覽器訪問9100端口,將鏈接地址修改成elasticsearch地址。

3)測試提交數據

 

4)驗證索引是否存在

5)查看數據

6)Master和Slave的區別:

Master的職責:
統計各node節點狀態信息、集羣狀態信息統計、索引的建立和刪除、索引分配的管理、關閉node節點等
Savle的職責:
同步數據、等待機會稱爲Master

Logstash部署

Logstash 是一個開源的數據收集引擎,能夠水平伸縮,並且logstash是整個ELK當中擁有最多插件的一個組件,其能夠接收來自不一樣來源的數據並贊成輸出到指定的且能夠是多個不一樣目的地。官網下載地址:https://www.elastic.co/cn/downloads/past-releases#logstash

安裝logstash

[root@linux-elk1 ~]# wget https://artifacts.elastic.co/downloads/logstash/logstash-6.8.1.rpm
[root@linux-elk1 ~]# yum -y localinstall logstash-6.8.1.rpm

測試logstash是否正常

1)測試標準輸入輸出

[root@linux-elk1 ~]# /usr/share/logstash/bin/logstash -e 'input { stdin {} } output { stdout { codec => rubydebug} }'  
hello world    #輸入

{
      "@version" => "1",    #事件版本號,一個事件就是一個ruby對象
    "@timestamp" => 2019-07-04T04:30:35.106Z,    #當前事件發生的事件
          "host" => "linux-elk1.exmaple.com",    #標記事件發生在哪裏
       "message" => "hello world"    #消息的具體內容
}

2)測試輸出到文件

[root@linux-elk1 ~]# /usr/share/logstash/bin/logstash   -e 'input { stdin{} } output { file { path => "/tmp/log-%{+YYYY.MM.dd}messages.gz"}}'
hello world   #輸入
[INFO ] 2019-07-04 17:33:06.065 [[main]>worker0] file - Opening file {:path=>"/tmp/log-2019.07.04messages.gz"}

[root@linux-elk1 ~]# tail /tmp/log-2019.07.04messages.gz 
{"message":"hello world","@version":"1","host":"linux-elk1.exmaple.com","@timestamp":"2019-07-04T09:33:05.698Z"}

3)測試輸出到elasticsearch

[root@linux-elk1 ~]# /usr/share/logstash/bin/logstash   -e 'input {  stdin{} } output { elasticsearch {hosts => ["192.168.1.31:9200"] index => "mytest-%{+YYYY.MM.dd}" }}'

4)elasticsearch服務器驗證收到數據

[root@linux-elk1 ~]# ll /elk/data/nodes/0/indices/
總用量 0
drwxr-xr-x. 8 elasticsearch elasticsearch 65 7月   4 17:23 4jaihRq6Qu6NQWVxbuRQZg
drwxr-xr-x. 8 elasticsearch elasticsearch 65 7月   4 17:22 kkd_RCldSeaCX3y1XKzdgA

kibana部署

Kibana是一個經過調用elasticsearch服務器進行圖形化展現搜索結果的開源項目。官網下載地址:https://www.elastic.co/cn/downloads/past-releases#kibana

安裝kibana

[root@linux-elk1 ~]# wget https://artifacts.elastic.co/downloads/kibana/kibana-6.8.1-x86_64.rpm
[root@linux-elk1 ~]# yum -y localinstall kibana-6.8.1-x86_64.rpm
[root@linux-elk1 ~]# vim /etc/kibana/kibana.yml 
[root@linux-elk1 ~]# grep ^[a-Z] /etc/kibana/kibana.yml 
server.port: 5601    #監聽端口
server.host: "192.168.1.31"    #監聽地址
elasticsearch.hosts: ["http://192.168.1.31:9200"]    #elasticsearch服務器地址
i18n.locale: "zh-CN"    #修改成中文

啓動kibana並驗證

[root@linux-elk1 ~]# systemctl start kibana
[root@linux-elk1 ~]# systemctl enable kibana
[root@linux-elk1 ~]# ss -nlt  |grep 5601
LISTEN     0      128    192.168.1.31:5601                     *:*

查看狀態

經過logstash收集系統message日誌

說明:經過logstash收集別的日誌文件,前提須要logstash用戶對被收集的日誌文件有讀的權限並對寫入的文件有寫的權限

1)配置logstash配置文件

[root@linux-elk1 ~]# vim /etc/logstash/conf.d/system-log.conf
input {
    file {
        path => "/var/log/messages"    #日誌路徑
        type => "systemlog"            #類型,自定義,在進行多個日誌收集存儲時能夠經過該項進行判斷輸出
        start_position => "beginning"        #logstash 從什麼位置開始讀取文件數據,默認是結束位置,也就是說 logstash 進程會以相似 tail -F 的形式運行。若是你是要導入原有數據,把這個設定改爲 "beginning",logstash 進程就從頭開始讀取,相似 less +F 的形式運行。
        stat_interval => "2"    #logstash 每隔多久檢查一次被監聽文件狀態(是否有更新),默認是 1 秒
    }
}

output {
    elasticsearch {
        hosts => ["192.168.1.31:9200"]        #elasticsearch服務器地址
        index => "logstash-%{type}-%{+YYYY.MM.dd}"    #索引名稱
    }
}

2)檢測配置文件語法是否有錯誤

[root@linux-elk1 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/system-log.conf -t    #檢測配置文件是否有語法錯誤
WARNING: Could not find logstash.yml which is typically located in $LS_HOME/config or /etc/logstash. You can specify the path using --path.settings. Continuing using the defaults
Could not find log4j2 configuration at path /usr/share/logstash/config/log4j2.properties. Using default config which logs errors to the console
[WARN ] 2019-07-05 10:09:59.423 [LogStash::Runner] multilocal - Ignoring the 'pipelines.yml' file because modules or command line options are specified
Configuration OK
[INFO ] 2019-07-05 10:10:27.993 [LogStash::Runner] runner - Using config.test_and_exit mode. Config Validation Result: OK. Exiting Logstash

3)修改日誌文件的權限並重啓logstash

[root@linux-elk1 ~]# ll /var/log/messages     
-rw-------. 1 root root 786219 7月   5 10:10 /var/log/messages
#這裏能夠看到該日誌文件是600權限,而elasticsearch是運行在elasticsearch用戶下,這樣elasticsearch是沒法收集日誌的。因此這裏須要更改日誌的權限,不然會報權限拒絕的錯誤。在日誌中查看/var/log/logstash/logstash-plain.log 是否有錯誤。
[root@linux-elk1 ~]# chmod 644 /var/log/messages
[root@linux-elk1 ~]# systemctl  restart logstash

4)elasticsearch界面查看並查詢

5)kibana界面建立索引並查看

相關文章
相關標籤/搜索