檔案開篇,我還是要說一遍,雖然我在檔案內容中也會說好多遍,但是希望大家不要嫌我墨跡:
請多看官方檔案,請多看命令列報錯資訊,請多看日志資訊,很多時候它們比百度、比必應、比谷歌有用;
請不要嫌麻煩,打開你的谷歌翻譯,去看看英文的檔案,雖然它沒有中文,雖然你直接看不懂,但它可能是最后幫你真正解決問題的那位;
(翻譯軟體很多,要記得使用,整體頁面翻譯我一般是谷歌瀏覽器或者WIN10升級的新Edge,最近發現頁面整體翻譯有時候Edge比谷歌好,谷歌頁面翻譯居然把代碼都翻譯了,看的不明不白的)
(文字顏色說明:灰色是注釋和一些說明的內容,基本上帶 # 的都是; 綠色是一些命令列列印資訊和檔案內容; 黃色底色的是一些注意事項,可能在你遇到問題時幫你起到一個分析引導的作用)
嘗試安裝了ELK的6.6.1、7.6.1和7.11.2這三個版本,方法步驟全都一樣,這里以我當前使用的7.11.2做示例;
除了要求的jdk版本以外,全都一樣的,當然僅限安裝步驟,組態檔的細節之處還是有不同之處的;
組態檔我會分為兩部分,一部分是安裝通用的,另一部分是個人僅在7.11.2上進行除錯的日志獲取及索引的一些配置,其他版本不確定,我會標記出來,避免誤導;
本人當前使用的是Elasticsearch + Kibana + filebeat直接索引分類 + ElastAlert報警到釘釘;
好處是結構簡單,部署方便,記憶體占用相對低;
若你也想用這個結構,依次查看 1(filebeat資料集成部分跳過) --> 3 --> 4 --> 5 部分即可;
內容索引: ---> 1、安裝部署及檔案配置
---> 2、配置logstash為多個日志來源分別設定索引
---> 3、不使用logstash,而是設定filebeat來為多個日志分別設定索引
---> 4、ElastAlert安裝
---> 5、ElastAlert規則組態檔撰寫及除錯說明
---> 6、ElastAlert安裝及使用中遇到的報錯問題
---> 7、Kibana提示許可過期,不能對索引等功能進行操作
---> 8、Elasticsearch與Kibana之間開啟TLS以滿足“告警和操作”的設定環境(商用功能)
安裝部署及檔案配置
官方下載地址:https://www.elastic.co/cn/downloads/past-releases#elasticsearch
請選擇相應的版本進行安裝,軟體盡量都下載相同版本進行安裝部署:
[root@localhost ~]# ls -ls *.rpm
315352 -rw-r--r-- 1 root root 322917736 Mar 31 04:53 elasticsearch-7.11.2-x86_64.rpm
33512 -rw-r--r-- 1 root root 34316071 Mar 31 05:44 filebeat-7.11.2-x86_64.rpm
249492 -rw-r--r-- 1 root root 255479673 Mar 31 04:52 kibana-7.11.2-x86_64.rpm
358580 -rw-r--r-- 1 root root 367185271 Mar 31 04:53 logstash-7.11.2-x86_64.rpm
相應的我們需要解決環境服務等依賴關系,這里說一下關于jdk的問題:
Unrecognized VM option 'UseParNewGC' Error: Could not create the Java Virtua ...
這個報錯網上找的解釋是新的 java-11-openjdk 廢棄了一個命令導致 ELK6.6.1 版本不能使用該環境,只支持到9版本,相應的可以安裝 java-1.6.0-openjdk ;
我目前安裝的 7.11.2 版本可以直接安裝 java-11-openjdk 或 java-1.8.0-openjdk ;
##安裝jdk [root@localhost ~]# yum install -y java-11-openjdk ##看下java版本,沒問題 [root@localhost ~]# java -version openjdk version "11.0.10" 2021-01-19 LTS OpenJDK Runtime Environment 18.9 (build 11.0.10+9-LTS) OpenJDK 64-Bit Server VM 18.9 (build 11.0.10+9-LTS, mixed mode, sharing) ##記得同步時間 [root@localhost ~]# yum install -y ntpdate ##服務器沒有時間同步可以加到 /etc/rc.local 中 [root@localhost ~]# /usr/sbin/ntpdate ntp.aliyun.com
之后我們就可以使用rpm包直接進行安裝了,非常簡單方便:
##匯入GPG密鑰,如果不能在線匯入,用這個http地址在瀏覽器把密鑰下載下來,然后本地匯入,URL換成路徑檔案即可 [root@localhost ~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch ##安裝rpm軟體包 [root@localhost ~]# rpm -ivh elasticsearch-7.11.2-x86_64.rpm logstash-7.11.2-x86_64.rpm kibana-7.11.2-x86_64.rpm filebeat-7.11.2-x86_64.rpm warning: elasticsearch-7.11.2-x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID d88e42b4: NOKEY Preparing... ################################# [100%] Updating / installing... 1:filebeat-7.11.2-1 ################################# [ 25%] 2:kibana-7.11.2-1 ################################# [ 50%] Creating kibana group... OK Creating kibana user... OK 3:logstash-1:7.11.2-1 ################################# [ 75%] Using bundled JDK: /usr/share/logstash/jdk Using provided startup.options file: /etc/logstash/startup.options OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release. /usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/pleaserun-0.0.31/lib/pleaserun/platform/base.rb:112: warning: constant ::Fixnum is deprecated Successfully created system startup script for Logstash Creating elasticsearch group... OK Creating elasticsearch user... OK 4:elasticsearch-0:7.11.2-1 ################################# [100%] ### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd sudo systemctl daemon-reload sudo systemctl enable elasticsearch.service ### You can start elasticsearch service by executing sudo systemctl start elasticsearch.service Created Kibana keystore in /etc/kibana/kibana.keystore Created elasticsearch keystore in /etc/elasticsearch/elasticsearch.keystore
這里注意一下在安裝Logstash時下面彈出的提示:OpenJDK 64-Bit Server VM warning: If the number of processors .. ,該提示說明你當前的虛擬機處理器的處理器核心數量不足,我們可以將VMware虛擬機的物理配置修改一下:

接下來我們進行配置:
注意:行號是我組態檔的位置,默認位置是這樣,若你的組態檔有改動可能會不一樣,這里做一個標識,方便修改;
Elasticsearch:
[root@localhost ~]# vim /etc/elasticsearch/elasticsearch.yml ##打開注釋,集群名稱和節點名稱都是可以自己進行設定的 17 cluster.name: my-application 23 node.name: node-1 ##資料檔案及日志檔案的路徑,可以使用默認的 /var 下路徑,若自己進行了其他位置的配置需要創建檔案夾并正確授權,配置完組態檔,后面我有寫步驟 33 path.data: /opt/elasticsearch/date 37 path.logs: /opt/elasticsearch/log ##配置系結的網路地址,127是系結本地,相應的,kibana和logstash是同一主機才可進行訪問,若不同主機,要配置為elasticsearch自己主機的IP地址,使其他主機上的服務可以進行訪問,埠默認9200 55 network.host: 127.0.0.1 59 http.port: 9200 ##開啟x-pack的安全功能 89 xpack.security.enabled: true 90 xpack.security.transport.ssl.enabled: true 91 cluster.initial_master_nodes: ["node-1"]
若我們自己配置了資料檔案及日志目錄,需要創建目錄并相應授權:
##根據實際需要,我們創建目錄,這里我就暫時放 opt 下了 [root@localhost ~]# mkdir -pv /opt/elasticsearch/date mkdir: created directory ‘/opt/elasticsearch’ mkdir: created directory ‘/opt/elasticsearch/date’ [root@localhost ~]# mkdir -pv /opt/elasticsearch/log mkdir: created directory ‘/opt/elasticsearch/log’ ##目錄需要有相應的權限才能正常使用,需要注意的是,目錄需要有gid權限 [root@localhost ~]# cd /opt/elasticsearch/ [root@localhost elasticsearch]# chmod 750 * [root@localhost elasticsearch]# chmod g+s * [root@localhost elasticsearch]# chown elasticsearch:elasticsearch * [root@localhost elasticsearch]# ll total 0 drwxr-s--- 2 elasticsearch elasticsearch 6 Apr 3 14:12 date drwxr-s--- 2 elasticsearch elasticsearch 6 Apr 3 14:12 log
之后,配置是開啟x-pack插件的,所以我們需要對一些默認的內置用戶做一些調整:
##必須啟動elasticsearch才能使用 [root@localhost ~]# systemctl start elasticsearch ##進行內置用戶的密碼設定,此處為了省事,我把密碼設定與用戶相同,實際情況根據需要來自行設定 [root@localhost ~]# /usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive Initiating the setup of passwords for reserved users elastic,apm_system,kibana,kibana_system,logstash_system,beats_system,remote_monitoring_user. You will be prompted to enter passwords as the process progresses. Please confirm that you would like to continue [y/N]y Enter password for [elastic]: Reenter password for [elastic]: Enter password for [apm_system]: Reenter password for [apm_system]: Enter password for [kibana_system]: Reenter password for [kibana_system]: Enter password for [logstash_system]: Reenter password for [logstash_system]: Enter password for [beats_system]: Reenter password for [beats_system]: Enter password for [remote_monitoring_user]: Reenter password for [remote_monitoring_user]: Changed password for user [apm_system] Changed password for user [kibana_system] Changed password for user [kibana] Changed password for user [logstash_system] Changed password for user [beats_system] Changed password for user [remote_monitoring_user] Changed password for user [elastic]
Kibana:
[root@localhost ~]# vim /etc/kibana/kibana.yml ##埠默認5601 2 server.port: 5601 ##系結地址,這里暫時用0.0.0.0,是允許所有地址進行訪問的,但是目前配置我們是加了x-pack的認證登錄的,若還需要nginx的代理認證,需要改為127.0.0.1,僅允許本機訪問 7 server.host: "0.0.0.0" ##連接的elasticsearch,本地可以localhost,對應的是elasticsearch設定了127.0.0.1,若是設定的IP地址,則這里也需要改為對應的IP地址 32 elasticsearch.hosts: ["http://localhost:9200"] ##kibana索引設定 36 kibana.index: ".kibana" ##之前我們設定號的kibana用戶和密碼 45 elasticsearch.username: "kibana_system" 46 elasticsearch.password: "kibana_system" ##界面語言改成中文,默認的en是英文 111 i18n.locale: "zh-CN"
logstash:
[root@localhost ~]# vim /etc/logstash/logstash.yml ##默認127.0.0.1,僅允許本機訪問,我們之后會有不同的主機采集日志,所以設定為0.0.0.0,允許訪問 231 http.host: "0.0.0.0" ##埠默認即可,但是需要打開注釋 236 http.port: 9600-9700
然后我們需要設定logstash對日志資訊輸入輸出的組態檔,默認沒有,需要復制模板進行配置:
##復制模板 [root@localhost ~]# cp /etc/logstash/logstash-sample.conf /etc/logstash/conf.d/logstash.conf ##修改組態檔 [root@localhost ~]# vim /etc/logstash/conf.d/logstash.conf # Sample Logstash configuration for creating a simple # Beats -> Logstash -> Elasticsearch pipeline. input { beats { port => 5044 } } output { elasticsearch { hosts => ["http://localhost:9200"] index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}" ##下面的user和password需要打開注釋,我們有x-pack認證,需要用戶及密碼,否則無法連接 user => "elastic" password => "elastic" } }
filebeat:
[root@localhost ~]# vim /etc/filebeat/filebeat.yml ##添加注釋,禁用,我們不直接輸出到elasticsearch 176 #output.elasticsearch: 178 #hosts: ["localhost:9200"] ##這里我們要設定輸出到logstash,本地可以是localhost,異地則設定為IP地址 189 output.logstash: 191 hosts: ["192.168.85.10:5044"]
到這我們可以從瀏覽器去訪問Kibana的可視化界面了:
之后我們點擊偏右上方的添加資料,進行對filebeat模塊設定并采集到日志資料;

可采集的資料型別很豐富,基本常用的都可以找到;

這里提醒一下,我使用的結構這一步是跳過的,上面安裝和組態檔結束之后,可以直接跳到 不使用logstash,而是設定filebeat來為多個日志分別設定索引 繼續進行設定;
官方的提示是直接將filebeat連接到elasticsearch,而目前我們操作流程是filebeat連接到logstash,但是我們可以關閉logstash,去直接連接elasticsearch,組態檔的設定沒有什么區別,開啟
不同的模塊配置即可;
##enable為開啟,disable為關閉,多個模塊空格分隔,例如:system nginx apache ... [root@localhost ~]# filebeat modules enable system Enabled system ##啟用設定,使之生效 [root@localhost ~]# filebeat setup Exiting: Index management requested but the Elasticsearch output is not configured/enabled ##啟動服務,當然,systemctl restart 也可以 [root@localhost ~]# service filebeat start Starting filebeat (via systemctl): [ OK ]


相對的,這里發現一個問題,雖然這個方式很方便、簡單,但是我這邊有一個需求,就是我要獲取到本機的nginx下的3個虛擬主機的不同的日志,獲取的時候
會發現,所有的日志沒有辦法分類,統一都是nginx.access和nginx.error,資料均在filebeat-7.11.2-日期 這個索引里,不能直接按照不同虛擬主機的日志機型分
類,所以,我僅用這個方法測驗了一下資料的獲取情況,就停掉了,之后我采用了之后的兩種方式,就是手動對各個日志進行索引的分類,
這里再提一下,使用這個方法,可以在Overview --> 日志下以流式傳輸的方式查看,并由相應的event.dataset來幫你分辨日志來源,
下面兩種方法不行,但是可以自己去設定每個日志去建立哪個索引,分類更精準,每個日志檔案都可以生成一個屬于自己的索引,
--回傳目錄--
配置logstash為多個日志來源分別設定索引
這個方法是我在被迫直接使用 filebeat 之后(這個方法在下一部分),回家找了一些網上的帖子得到的方法,原理基本是同直接設定 filebeat 的方式一樣,只不過它是通過 logstash 對加了標識的日志資訊做索引分類;
首先我們修改 filebeat 的組態檔:
(這里我收集了nginx的access和error以及系統的日志syslog)
##下列沒有涉及到的部分組態檔不做修改,下面是要進行修改的配置 [root@postgreSQL ~]# vim /etc/filebeat/filebeat.yml
filebeat.inputs: ##型別,默認的log即可 - type: log ##默認是false,不啟用設定,這里設定好的一定要改成true,否則不生效 enabled: true ##日志路徑,絕對路徑,可以使用*來做通配,但是我并沒有這么做,日志索引的資訊我是定期洗掉的,這個也要考慮一下你的日志滾動設定 paths: - /var/log/messages ##這個是為我們采集的每條日志資訊增加一個標簽一樣的東西,來進行標識,為啥是這個,我不知道,反正大家都這么寫的,哈哈 fields: index: syslog-91 - type: log enabled: true paths: - /var/log/nginx/access.log fields: index: nginx-access - type: log enabled: true paths: - /var/log/nginx/error.log fields: index: nginx-error # ---------------------------- Elasticsearch Output ---------------------------- ##這個是默認開啟的,我們要把采集的日志送到logstash中,所以要關閉這個,當前配置只允許開啟一個output設定,不能雙開 #output.elasticsearch: ##這個也注釋上 #hosts: ["localhost:9200"] # ------------------------------ Logstash Output ------------------------------- ##默認關閉的,我們使用logstash進行配置,所以去掉注釋打開 output.logstash: ##地址要填寫logstash得到主機IP地址,埠號默認是5044,但其實它是在logstash主機下的 /etc/logstash/conf.d/logstash.conf 中設定的 hosts: ["192.168.85.10:5044"]
之后我們對logstash進行配置:
主組態檔 /etc/logstash/logstash.yml 我們在安裝的時候已經進行配置了,這里不需要修改;
/etc/logstash/conf.d/logstash.conf 的組態檔我們安裝的時候 cp 復制了一份,這里需要修改:
# Sample Logstash configuration for creating a simple # Beats -> Logstash -> Elasticsearch pipeline. input { beats { port => 5044 } } output { elasticsearch { hosts => ["http://localhost:9200"] ##格式這里,[fields] 和 [index] 分別對應 filebeat 中對日志檔案設定的“標簽” index => "%{[fields][index]}-%{[@metadata][beat]}-%{+YYYY.MM.dd}" user => "elastic" password => "elastic" } }
之后我們分別在相應的主機上重啟 logstash 和 filebeat:
##logstash端重啟服務
[root@localhost kibana]# systemctl restart logstash
##filebeat端,記得開啟nginx,并做幾次成功和失敗的訪問,然后去看看日志重繪情況
[root@postgreSQL ~]# systemctl status filebeat
然后去kibana的索引管理以及 Analytics --> Discover 中查看日志資料:
(查看日志資料需要先創建一個匹配的索引模式,我直接創建一個根據 *-* 匹配的模式,因為命名中由 - 來連接字符)

可以添加指定的欄位,有選擇的顯示內容并進行搜索:

--回傳目錄--
不使用logstash,而是設定filebeat來為多個日志分別設定索引
這個方法其實最初是因為我配置不明白logstash來分索引,而用的一種替代方案,但是后來又發現我安裝ELK的服務器只有8G的記憶體,配置低;所以理所應當的也就把logstash停止了(也就開個nginx,之后加上 Elasticsearch 和 kibana 還有 filebeat 這3個,記憶體已經是平穩使用75%-80%了);
該方案只是替代方案,而且沒有Logstash來為你做日志資訊的格式化輸出,除了結構簡單,順便能稍微省點資源;且由于日常的日志量不大,也不怕丟失,我暫時使用的就是 Elasticsearch + Kibana + filebeat 這個方案;
首先我們調整 filebeat 的組態檔:
(使用上一種方法的組態檔進行修改,順便可以比對一下具體是哪里發生了變化)
[root@postgreSQL ~]# vim /etc/filebeat/filebeat.yml filebeat.inputs: - type: log enabled: true paths: - /var/log/messages ##將fields 和 index 去掉了,使用一個 tags 的標簽來標識日志檔案來源 tags: ["syslog91"] - type: log enabled: true paths: - /var/log/nginx/access.log tags: ["nginx-access"] - type: log enabled: true paths: - /var/log/nginx/error.log tags: ["nginx-error"] # ---------------------------- Elasticsearch Output ---------------------------- output.elasticsearch: # Array of hosts to connect to. ##這里我們要設定連接的elasticsearch的IP地址,當然,相應的我們也需要修改elasticsearch中的配置,下面會有 hosts: ["192.168.85.10:9200"] indices: ##索引名稱 - index: "syslog2--%{+YYYY.MM.dd}" ##匹配包含欄位 when.contains: ##去匹配包含syslog2內容的資訊 tags: "syslog2" - index: "nginx-access--%{+YYYY.MM.dd}" when.contains: tags: "nginx-access" - index: "nginx-error--%{+YYYY.MM.dd}" when.contains: tags: "nginx-error" ##由于x-pack設定,我們需要添加連接到elasticsearch的認證用戶及密碼 username: "elastic" password: "elastic" ##填加kibana的認證用戶及密碼 # =================================== Kibana =================================== # Starting with Beats version 6.0.0, the dashboards are loaded via the Kibana API. # This requires a Kibana endpoint configuration. setup.kibana: username: "kibana_system" password: "kibana_system" ##關閉輸出到logstash # ------------------------------ Logstash Output ------------------------------- #output.logstash: # The Logstash hosts # hosts: ["192.168.85.10:5044"]
之后我們修改elasticsearch和kibana的組態檔:
[root@localhost ~]# vim /etc/elasticsearch/elasticsearch.yml ##將本地系結的127.0.0.1改為主機的IP地址,使其他主機上的filebeat服務訪問到elasticsearch 55 network.host: 192.168.85.10 [root@localhost ~]# vim /etc/kibana/kibana.yml ##kibana組態檔也需要修改,否則連接不到elasticsearch 32 elasticsearch.hosts: ["http://192.168.85.10:9200"]
然后我們重啟服務,去訪問看看是否生效:
##重啟elasticsearch服務,順便把logstash停了 [root@localhost ~]# systemctl stop logstash [root@localhost ~]# systemctl restart elasticsearch ##采集日志的主機重啟filebeat服務 [root@postgreSQL ~]# systemctl restart filebeat


--回傳目錄--
ElastAlert安裝
官方檔案:https://elastalert.readthedocs.io/en/latest/running_elastalert.html#downloading-and-configuring
https://elastalert.readthedocs.io/en/latest/recipes/writing_filters.html#writingfilters
(要了解學習更多,就要常看官方檔案,非常詳細;當然,你可能需要翻譯,它是英文的)
這里特殊說明一下,當前檔案寫作是2021年4月,當前版本的elastalert只可以用python3.6,官方的github的頁面也已經做了說明;
為什么說用python3.6呢,因為我當時沒看檔案,用自帶的python(CentOS7自帶2.7.5)和自己安裝的python3.7都不行,同一步驟
各種報錯,不是缺少插件就是語法錯誤,起初我還認為是真的語法錯誤,但是這軟體這么多人用也沒說有問題,那么問題一定是我
的問題,哈哈;
報錯如果說你沒有某個模塊,你下載安裝相應的模塊即可,但是要記住你安裝是用pip3(我們會yum安裝python36),而不是pip,默認的pip是pip2;
報錯如果說你語法錯誤,那并不是語法錯誤,而是因為python版本的問題,執行時有python的,替換為python3即可;
若使用pip報錯,那么使用pip3看看,使用pip3報錯那么試試pip2,常用 pip list 和 pip3 list 看看自己已經是否安裝了相應的插件,自己一定不要亂套;
解決環境依賴問題:
##解決python3和其他一些依賴關系,當然,下面還需要git,若沒有可以添加git一起安裝 [root@localhost ~]# yum install -y epel-release [root@localhost ~]# yum install -y python36 python-dev libsasl2-devel libffi-dev libffi-devel libssl-dev python36-devel libevent-devel gcc gcc-c++
下載ElastAlert及dingtalk插件:
##git下載需要帶有git命令,yum可以進行安裝,或者在windows主機下載壓縮包拿過來,unzip解壓即可 [root@localhost ~]# git clone https://github.com/Yelp/elastalert.git [root@localhost ~]# wget https://github.com/xuyaoqiang/elastalert-dingtalk-plugin/archive/master.zip [root@localhost ~]# unzip elastalert-dingtalk-plugin-master ##名字太長了,我們改一下吧 [root@localhost ~]# mv elastalert-dingtalk-plugin-master dingtalk ##得到兩個目錄,位置可以自己定,我目前測驗是直接給他放root下了 [root@localhost ~]# ls -ld elastalert elastalert-dingtalk-plugin-master drwxr-xr-x 10 root root 4096 Apr 6 21:54 elastalert drwxr-xr-x 4 root root 123 Apr 8 07:10 dingtalk
安裝ElastAlert和dingtalk插件:
[root@localhost ~]# cd /elastalert/
##安裝模塊
[root@localhost elastalert]# pip3 install setuptools==44.0.0
[root@localhost elastalert]# python setup.py install
##我們還需要一個elasticsearch-py,我們是7.11.2,但是后面創建索引的時候會提示你需要7.0.0,要不不能創建,所以這里我們安裝7.0.0
[root@localhost elastalert]# pip3 install elasticsearch==7.0.0
##切換到dingtalk目錄下
[root@localhost elastalert]# cd ../dingtalk
##這個安裝會大量下包,不要終止它
[root@localhost ~]# pip install -r requirements.txt
##然后我們需要把dingtalk的模塊檔案復制到elastalert目錄下,否則之后我們配置的時候它會報錯找不到模塊
[root@localhost elastalert]# cp -r ../dingtalk/elastalert_modules .
檢查安裝情況:
##安裝成功后會出現4個命令,下面的目錄及其子檔案可以忽略 [root@localhost elastalert]# ls -l /usr/bin/elastalert* -rwxr-xr-x 1 root root 316 Apr 6 21:55 /usr/bin/elastalert -rwxr-xr-x 1 root root 342 Apr 6 21:55 /usr/bin/elastalert-create-index -rwxr-xr-x 1 root root 350 Apr 6 21:55 /usr/bin/elastalert-rule-from-kibana -rwxr-xr-x 1 root root 336 Apr 6 21:55 /usr/bin/elastalert-test-rule /usr/bin/elastalert.egg-info: total 24 -rw-r--r-- 1 root root 1 Apr 6 21:55 dependency_links.txt -rw-r--r-- 1 root root 226 Apr 6 21:55 entry_points.txt -rw-r--r-- 1 root root 417 Apr 6 21:55 PKG-INFO -rw-r--r-- 1 root root 369 Apr 6 21:55 requires.txt -rw-r--r-- 1 root root 205 Apr 6 21:55 SOURCES.txt -rw-r--r-- 1 root root 1 Apr 6 21:55 top_level.txt
配置ElastAlert主組態檔:
##dingtalk插件的目錄下有安裝檔案和一些參考和說明檔案,在撰寫規則時可以進行參考,但是我們能進行撰寫即可,一般不用
[root@localhost ~]# cd dingtalk/
[root@localhost dingtalk]# ll
total 12
-rw-r--r-- 1 root root 1762 Sep 15 2017 config.yaml
drwxr-xr-x 2 root root 50 Sep 15 2017 elastalert_modules
-rw-r--r-- 1 root root 688 Sep 15 2017 README.md
-rw-r--r-- 1 root root 92 Sep 15 2017 requirements.txt
drwxr-xr-x 2 root root 57 Sep 15 2017 rules
##ElastAlert目錄下原本只有主組態檔模板,我們要復制創建一個主組態檔
[root@localhost ~]# cd elastalert
[root@localhost elastalert]# cp config.yaml.example config.yaml
##之后我們進行主組態檔的調整
[root@localhost elastalert]# vim config.yaml
##這個是我們配置的規則檔案的目錄,相對路徑即可,這里我沒有變動,一會規則檔案都塞到這里面
3 rules_folder: example_rules
##
7 run_every:
8 minutes: 1
##
12 buffer_time:
13 minutes: 15
##我們elasticsearch的地址,我這里設定的是IP
17 es_host: 192.168.85.10
##端接我這里沒有變化,若自行設定請修改正確
20 es_port: 9200
##這里是elasticsearhc的認證用戶及密碼,若設定了就需要填寫,沒有則可以#注釋,不使用
46 es_username: elastic
47 es_password: elastic
##這個是我們為ElastAlert創建的索引,默認即可,一會后面我們會進行創建,若自定義請填寫正確
59 writeback_index: elastalert_status
60 writeback_alias: elastalert_alerts
##
64 alert_time_limit:
65 days: 2
在Elasticsearch創建索引:
##這里提醒一下,一般情況下創建 elastalert-create-index --index elastalert_status 和 elastalert-create-index 兩個命令都是可以的 ##若一個不行可以用另一個試一試,我在洗掉索引之后重建使用 elastalert-create-index 不能創建,后面必須加 --index elastalert_status 才可以執行成功 ##創建成功后,在Kibana中就可以看到該條索引了 [root@localhost elastalert]# elastalert-create-index --index elastalert_status Elastic Version: 7.11.2 Reading Elastic 6 index mappings: Reading index mapping 'es_mappings/6/silence.json' Reading index mapping 'es_mappings/6/elastalert_status.json' Reading index mapping 'es_mappings/6/elastalert.json' Reading index mapping 'es_mappings/6/past_elastalert.json' Reading index mapping 'es_mappings/6/elastalert_error.json' New index elastalert_status created Done!
到這里安裝基本完成,可以使用,接下來比較重要,撰寫并除錯規則配置,這個是發送報警的重點;
--回傳目錄--
ElastAlert規則組態檔撰寫及除錯說明
規則組態檔這里單獨拿出來做一個部分,因為這個需要經常進行創建、調整配置(我個人在這個地方卡了將近一個星期,當時我由于各種原因極其煩躁,靜不下來);
撰寫報警規則:
##到我們的規則檔案目錄里來 [root@localhost example_rules]# pwd /root/elastalert/example_rules ##我們需要編輯一個規則檔案,可以從現有的模板進行一次復制 [root@localhost example_rules]# cp example_frequency.yaml syslog10.yaml ##編輯規則檔案 [root@localhost example_rules]# vim syslog200.yaml # Alert when the rate of events exceeds a threshold # (Optional) # Elasticsearch host ##這里是elasticsearch的地址 es_host: 192.168.85.10 # (Optional) # Elasticsearch port ##這里是elasticsearch的埠 es_port: 9200 # (OptionaL) Connect with SSL to Elasticsearch ##這里是設定開啟ssl,注釋掉,我們沒有開啟 #use_ssl: True # (Optional) basic-auth username and password for Elasticsearch ##elasticsearch的認證賬戶密碼,正確填寫 es_username: elastic es_password: elastic # (Required) # Rule name, must be unique ##這里是我們發送的訊息的標題 name: 發送訊息 # (Required) # Type of alert. # the frequency rule type alerts when num_events events occur with timeframe time ##這里是規則型別,frequency是根據我們設定的頻率匹配,后面設定; ##詳細參考:https://elastalert.readthedocs.io/en/latest/ruletypes.html#ruletypes type: frequency # (Required) # Index to search, wildcard supported ##這里是我們要匹配的索引,可以用 * 來匹配 index: nginx* # (Required, frequency specific) # Alert when this many documents matching the query occur within a timeframe ##這里設定在我們規定的時間內出現了多少頻次可匹配到的規則就需要進行報警 num_events: 1 # (Required, frequency specific) # num_events must occur within this amount of time to trigger an alert ##這里接上面,規定的時間,單位有hours、minutes、seconds(時、分、秒) timeframe: minutes: 1 # (Required) # A list of Elasticsearch filters used for find events # These filters are joined with AND and nested in a filtered query # For more info: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html ##這里是我們重點,匹配的規則,翻譯為過濾器,我們暫時設定匹配ERROR的message訊息來進行測驗 ##詳情參考:https://elastalert.readthedocs.io/en/latest/recipes/writing_filters.html#writingfilters filter: - query: query_string: query: "message: ERROR" # (Required) # The alert is use when a match is found ##這里是加載模塊,注意!暫時我們要寫這個樣子,有的帖子只寫dingtalk的,甚至插件的github頁面也是這么寫,可能僅代表標簽,只寫dingtalk寫會報錯 alert: - "elastalert_modules.dingtalk_alert.DingTalkAlerter" ##下面是我們釘釘機器人的地址,和檔案型別 dingtalk_webhook: "https://oapi.dingtalk.com/robot/send?access_token=************************************" dingtalk_msgtype: text ##這下面是郵箱發送的一些配置,我們本次不使用郵箱,所以把他注釋上 ##比如我使用163郵箱發送,這個需要開通smtp服務 #smtp_host: smtp.163.com ##這個埠是非ssl的,若你開了ssl需要更換,163、qq的都不一樣,網上可以查到 #smtp_port: 25 ##這個是用來發送的郵箱的賬號和認證密碼存放檔案的絕對路徑,在 #smtp_auth_file: /root/elastalert/example_rules/testauth.yaml ##用來接收郵件的郵箱地址 #email_reply_to: *****@qq.com ##用來發送郵件的郵箱地址 #from_addr: *****@163.com # (required, email specific) # a list of email addresses to send alerts to # email: # - "[email protected]"
郵箱認證檔案(用釘釘可以不配置,郵箱需要):
[root@localhost example_rules]# vim testauth.yaml ##用來發郵件的郵箱地址 user: ***********@163.com ##這個密碼是你開通smtp功能后官方給你的密碼,是一串字母,可不是你自己設定的郵箱密碼 password: sdgkljsklgslvgb
運行進行測驗:
這里提醒一下:運行的時候也是會大量報錯的,原因可能是你某些模塊沒有安裝,也可能是你的yaml檔案格式不對;
還是要仔細看報錯資訊,打開你的谷歌翻譯去翻譯一下,里面的資訊很重要,很多可以幫你解決問題的命令都在里面;
[root@localhost elastalert]# python3 -m elastalert.elastalert --verbose --config ./config.yaml --rule ./example_rules/syslog10.yaml 1 rules loaded INFO:elastalert:Starting up INFO:elastalert:Disabled rules are: [] INFO:elastalert:Sleeping for 59.999936 seconds INFO:elastalert:Queried rule 發送訊息 from 2021-04-10 12:38 CST to 2021-04-10 12:41 CST: 0 / 0 hits INFO:elastalert:Ran 發送訊息 from 2021-04-10 12:38 CST to 2021-04-10 12:41 CST: 0 query hits (0 already seen), 0 matches, 0 alerts sent INFO:elastalert:Background configuration change check run at 2021-04-10 12:42 CST INFO:elastalert:Disabled rules are: [] INFO:elastalert:Sleeping for 59.999764 seconds INFO:elastalert:Background alerts thread 0 pending alerts sent at 2021-04-10 12:42 CST INFO:elastalert:Queried rule 發送訊息 from 2021-04-10 12:38 CST to 2021-04-10 12:42 CST: 72 / 72 hits INFO:elastalert:Ignoring match for silenced rule 發送訊息 INFO:elastalert:Ignoring match for silenced rule 發送訊息 INFO:elastalert:Ignoring match for silenced rule 發送訊息 INFO:elastalert:Ignoring match for silenced rule 發送訊息 ...... ......
之后我的釘釘收到了訊息,不過比較長,把整個一條的索引資訊全部發過來了;
但是這已經證明我們的配置是成功的,之后就是對發送資訊的調整了;

生產環境正常運行:
##需要使用相對路徑,絕對路徑會報錯,所以要先進入檔案目錄; [root@localhost ~]# cd /root/elastalert ##將其放入后臺 [root@localhost elastalert]# nohup python3 -m elastalert.elastalert --verbose --config ./config.yaml --rule ./example_rules/syslog10.yaml & ##如果有很多組態檔,那么把可以用的放到我們配置的規則檔案目錄下(配置不正確的規則檔案會導致報錯,要移出規目錄),然后就不用指定引數 --rule 了,同理 --config 的引數也不是必須的,但是切到目錄下是必須的 [root@localhost elastalert]# nohup python3 -m elastalert.elastalert --verbose
--回傳目錄--
ElastAlert安裝及使用中遇到的報錯問題
這里總結一些報錯資訊,供大家參考:
1、安裝時的報錯:
這個問題一般出現在 python setup.py install 和 pip install -r requirements.txt 的時候,我在安裝的時候就有點納悶,為什么我安裝時用python(自帶的2.7)來安裝,而我運行缺需要用python3來運行;但是
討論這些大多數情況都是浪費時間,我們的首要目標應該是讓它可以正常使用,所以當下只管安裝上即可,至于什么原理,為什么,我們完成之后再去做分析考慮;
還是我之前說的,python 和 python3 要切換著用,因為情況亂,我并沒有記錄下所有的指令和報錯資訊,無法全部復現,所以大家要多嘗試一下,因為我自己就是在 python、python3、python3.6 和 pip、pip3、
pip3.6 中來回切換才最終安裝成功的,有時候python可以安裝,但是它不能運行,python3可以運行,但是它不能安裝;
(我安了2.7.5、3.6、3.7三個版本,我甚至在生產環境把自帶的2.7.5卸載而導致yum和python均不能使用,害我一上午就在那修復了,鬧死心了);
2、找不到模塊:
##找不到模塊,下面兩種都可能出現,那么我們安裝相應模塊即可,但是一般正確安裝了elastalert和dingtalk插件的基本不會提示; ##但也有特殊情況,我們的安裝主要以python36為主,因為我們運行需要它,常用 pip3 來確定自己是不是真的沒有安裝包,還是只是它找不到路徑 File "/root/elastalert/elastalert/elastalert.py", line 21, in <module> import pytz ImportError: No module named pytz ModuleNotFoundError: No module named 'deeptools'
3、語法錯誤:
##語法錯誤其實是因為你自己的配置和使用的python和pip的版本問題,本身程式的代碼沒有問題,但是當python3的代碼放到python2中那么就是語法錯誤,反之一樣,這里展示的是我組態檔指向錯誤; ##你可能還會遇到一個問題,就是提示你a sync= False ,這個也是python3和2的使用錯誤,用3就會報錯,這個是安裝時候的提示,那么你直接用python,或python2.7就安裝成功了
raise EAException("Error querying for dashboard: %s" % (e)), None, sys.exc_info()[2] ^ SyntaxError: invalid syntax
4、釘釘的模塊找不到:
##仔細看里面: elastalert_modules.dingtalk_alert.DingTalkAlerter 找不到,之前說了,規則檔案里面 alert 一定寫對,而且 elastalert_modules 一定要復制到 elastalert 目錄下
File "/root/elastalert/elastalert/loaders.py", line 126, in load raise EAException('Error loading file %s: %s' % (rule_file, e)) elastalert.util.EAException: Error loading file ./example_rules/syslog200.yaml: Error initia ting alert ['elastalert_modules.dingtalk_alert.DingTalkAlerter']: Could not import module el astalert_modules.dingtalk_alert.DingTalkAlerter: No module named 'elastalert_modules'
--回傳目錄--
Kibana提示許可過期,不能對索引等功能進行操作
有的時候,會發生一些例外:我在測驗的時候昨天晚上剛剛安裝完,今天晚上用居然出現不能進行索引的相關你操作,提示我認證過期;
但是......,你不是說 “您的許可證永不會過期”,逗我?假的么?但是不管怎樣,不能用了是真的,所以需要解決它;
許可證是可以在官網進行注冊獲取的,注冊成功的郵件中有下載地址;但是問題也很奇怪,我反復的安裝過很多次ELK,6.6.1、7.6.1、7.11.2,
這么多版本只有一次說我認證過期,而其他的測驗,還有生產在用的都沒有提示過我,不過既然出現過這種情況,可以解決,就沒問題,

--回傳目錄--
Elasticsearch與Kibana之間開啟TLS以滿足“告警和操作”的設定環境(商用功能)
這個部分說來尷尬,其實當時是看到居然自帶了“告警和操作”這個專案,以為可以使用,但是開啟之后發現免費只有2個模塊,想要用的webhook居然是商用,沒辦法,直接放棄轉向了ElastAlert;
若你使用的是商用的各種會員(看看官方價格表,我就是沒看商用功能的價格表,白忙活一天),可以使用這些商用功能,可以簡單參考一下,來滿足最基本的設定環境,詳細的參考官方檔案;
7.11.2中的”告警和操作“功能需要我們在為elasticsearch開啟加密傳輸(E、L、K和filebeat都可以開啟加密傳輸模式);
官方檔案參考太多了,直接從他的頁面點擊進入去參考;最開始會提示你建立TLS,當你TLS建立完畢之后,會提示你需要API密鑰;
配置錯誤會直接導致elasticsearch和kibana啟動失敗,使用 journalctl -xe 查看服務啟動失敗的原因進行調整即可,若是elasticsearch啟動正常,但是提示kibana沒有準備好,就是沒有連接到elasticsearch,去看看http是否改了https;
elasticsearch服務一般啟動慢,但是若是錯誤會提示,查看status也可以,但是kibana是一定要持續觀察一下,因為kibana必定啟動,但是不一定能一直正常運行,很可能啟動成功,1、20秒后,status狀態會變為失敗,一定好好排查;
還有就是,當你配置完了,啟動也正常了,但是依然不能訪問kibana的時候,是因為你的kibana訪問地址是https的,記得瀏覽器地址修改前綴;
進入 ”告警和操作“ 的界面,我們首先是被告知:必須在 Kibana 和 Elasticsearch 之間啟用傳輸層安全并在 kibana.yml 檔案中配置加密密鑰,那么我們接下來就對其進行配置:

按照我當前測驗環境的配置,需要的檔案如下,有了這些檔案就可以開啟Elasticsearch和Kibana直接的安全傳輸了;
當然我的串列里面有很多是原來的組態檔,記得區分:
Elasticsearch需要
[root@VM_0_7_centos elasticsearch]# pwd /etc/elasticsearch [root@VM_0_7_centos elasticsearch]# ll total 76 -rw------- 1 elasticsearch root 3443 Apr 6 10:51 elastic-certificates.p12 -rw-rw---- 1 root elasticsearch 199 Mar 29 18:04 elasticsearch.keystore -rw-rw---- 1 root elasticsearch 3545 Apr 6 09:14 elasticsearch.yml -rw------- 1 elasticsearch root 2527 Apr 6 10:51 elastic-stack-ca.p12 -rw-r--r-- 1 elasticsearch root 3451 Apr 6 11:01 http.p12 -rw-rw---- 1 root elasticsearch 3182 Mar 6 14:03 jvm.options drwxr-s--- 2 root elasticsearch 4096 Mar 6 14:06 jvm.options.d -rw-r--r-- 1 root elasticsearch 1397 Apr 6 11:36 kibana-ca.crt -rw-rw---- 1 root elasticsearch 18612 Mar 6 14:03 log4j2.properties -rw-r--r-- 1 root root 1306 Apr 6 11:01 README.txt -rw-rw---- 1 root elasticsearch 473 Mar 6 14:03 role_mapping.yml -rw-rw---- 1 root elasticsearch 197 Mar 6 14:03 roles.yml -rw-r--r-- 1 root root 658 Apr 6 11:01 sample-elasticsearch.yml -rw-r--r-- 1 root root 1057 Apr 6 11:01 sample-kibana.yml -rw-rw---- 1 root elasticsearch 0 Mar 6 14:03 users -rw-rw---- 1 root elasticsearch 0 Mar 6 14:03 users_roles
Kibana需要
[root@VM_0_7_centos elasticsearch]# cd /etc/kibana/ [root@VM_0_7_centos kibana]# ll total 20 -rw-r--r-- 1 kibana root 1200 Apr 6 11:08 elasticsearch-ca.pem -rw-rw---- 1 root kibana 5466 Apr 6 11:53 kibana.yml -rw-r--r-- 1 root kibana 216 Mar 6 13:11 node.options
然后我們根據我們的需要去生成檔案:
生成CA證書:
[root@localhost ~]# /usr/share/elasticsearch/bin/elasticsearch-certutil ca This tool assists you in the generation of X.509 certificates and certificate signing requests for use with SSL/TLS in the Elastic stack. ......
......
be a zip file containing individual files for the CA certificate and private key Please enter the desired output file [elastic-stack-ca.p12]: 默認即可,直接回車確定 Enter password for elastic-stack-ca.p12 : 可自行設定密碼,不設定直接回車也可以 ##查看生成檔案 [root@localhost ~]# ls -l /usr/share/elasticsearch/elastic-stack-ca.p12 -rw------- 1 root root 2527 Apr 5 13:38 /usr/share/elasticsearch/elastic-stack-ca.p12
生成節點的證書及私鑰:
[root@localhost ~]# /usr/share/elasticsearch/bin/elasticsearch-certutil cert --ca /usr/share/elasticsearch/elastic-stack-ca.p12 This tool assists you in the generation of X.509 certificates and certificate signing requests for use with SSL/TLS in the Elastic stack. ......
......
* -keep-ca-key (retain generated CA key) * -multiple (generate multiple certificates) * -in (generate certificates from an input file) then the output will be be a zip file containing individual certificate/key files Enter password for CA (/usr/share/elasticsearch/elastic-stack-ca.p12) : 指定好的檔案,直接回車即可 Please enter the desired output file [elastic-certificates.p12]: 直接回車 Enter password for elastic-certificates.p12 : 直接回車 Certificates written to /usr/share/elasticsearch/elastic-certificates.p12 ......
......
configure the client to trust this certificate. ##查看生成的檔案 [root@localhost ~]# ls -l /usr/share/elasticsearch/elastic-certificates.p12 -rw------- 1 root root 3451 Apr 5 13:41 /usr/share/elasticsearch/elastic-certificates.p12
Elasticsearch加密HTTP客戶端通信:
[root@localhost ~]# /usr/share/elasticsearch/bin/elasticsearch-certutil http ## Elasticsearch HTTP Certificate Utility The 'http' command guides you through the process of generating certificates ......
...... configure all your clients to trust that custom CA. Generate a CSR? [y/N] n 輸入n,選否 ## Do you have an existing Certificate Authority (CA) key-pair that you wish to use to sign your certificate? If you have an existing CA certificate and key, then you can use that CA to sign your new http certificate. This allows you to use the same CA across multiple Elasticsearch clusters which can make it easier to configure clients, and may be easier for you to manage. If you do not have an existing CA, one will be generated for you. Use an existing CA? [y/N] y 輸入y,選是 ## What is the path to your CA? Please enter the full pathname to the Certificate Authority that you wish to use for signing your new http certificate. This can be in PKCS#12 (.p12), JKS (.jks) or PEM (.crt, .key, .pem) format. CA Path: /usr/share/elasticsearch/elastic-stack-ca.p12 這個是我們之前生成的檔案 Reading a PKCS12 keystore requires a password. It is possible for the keystore's password to be blank, in which case you can simply press <ENTER> at the prompt Password for elastic-stack-ca.p12: 直接回車 ## How long should your certificates be valid? Every certificate has an expiry date. When the expiry date is reached clients will stop trusting your certificate and TLS connections will fail. Best practice suggests that you should either: (a) set this to a short duration (90 - 120 days) and have automatic processes to generate a new certificate before the old one expires, or (b) set it to a longer duration (3 - 5 years) and then perform a manual update a few months before it expires. You may enter the validity period in years (e.g. 3Y), months (e.g. 18M), or days (e.g. 90D) For how long should your certificate be valid? [5y] 這里是有效時間,Y是年,M是月,D是天,根據實際情況寫,默認5Y,我直接回車了 ## Do you wish to generate one certificate per node? If you have multiple nodes in your cluster, then you may choose to generate a separate certificate for each of these nodes. Each certificate will have its own private key, and will be issued for a specific hostname or IP address. Alternatively, you may wish to generate a single certificate that is valid across all the hostnames or addresses in your cluster. If all of your nodes will be accessed through a single domain (e.g. node01.es.example.com, node02.es.example.com, etc) then you may find it simpler to generate one certificate with a wildcard hostname (*.es.example.com) and use that across all of your nodes. However, if you do not have a common domain name, and you expect to add additional nodes to your cluster in the future, then you should generate a certificate per node so that you can more easily generate new certificates when you provision new nodes. Generate a certificate per node? [y/N] n 這里我選擇不為每個節點設定,因為我是單機配置,若是集群,需要每個去配置,然后分發檔案 ## Which hostnames will be used to connect to your nodes? These hostnames will be added as "DNS" names in the "Subject Alternative Name" (SAN) field in your certificate. You should list every hostname and variant that people will use to connect to your cluster over http. Do not list IP addresses here, you will be asked to enter them later. If you wish to use a wildcard certificate (for example *.es.example.com) you can enter that here. Enter all the hostnames that you need, one per line. When you are done, press <ENTER> once more to move on to the next step. 這里提示我直接回車,不填寫主機的hostname,因為我是單機 You did not enter any hostnames. Clients are likely to encounter TLS hostname verification errors if they connect to your cluster using a DNS name. Is this correct [Y/n]y 是否使用當前配置,選擇y是 ## Which IP addresses will be used to connect to your nodes? If your clients will ever connect to your nodes by numeric IP address, then you can list these as valid IP "Subject Alternative Name" (SAN) fields in your certificate. If you do not have fixed IP addresses, or not wish to support direct IP access to your cluster then you can just press <ENTER> to skip this step. Enter all the IP addresses that you need, one per line. When you are done, press <ENTER> once more to move on to the next step. 192.168.85.10 這里輸入本機的IP地址,回車一次,輸入一條,然后再點回車確定即可 You entered the following IP addresses. - 192.168.85.10 Is this correct [Y/n]y 是否使用當前配置,選擇y是 ## Other certificate options The generated certificate will have the following additional configuration values. These values have been selected based on a combination of the information you have provided above and secure defaults. You should not need to change these values unless you have specific requirements. Key Name: elasticsearch Subject DN: CN=elasticsearch Key Size: 2048 Do you wish to change any of these options? [y/N]n 是否要修改之前的選擇,選n否,我們配置沒有問題就可以 ## What password do you want for your private key(s)? Your private key(s) will be stored in a PKCS#12 keystore file named "http.p12". This type of keystore is always password protected, but it is possible to use a blank password. If you wish to use a blank password, simply press <enter> at the prompt below. Provide a password for the "http.p12" file: [<ENTER> for none]
根據提示,回車就可以了,我們的配置已經完成,之后只是提示和生成檔案了 ## Where should we save the generated files? A number of files will be generated including your private key(s), public certificate(s), and sample configuration options for Elastic Stack products. These files will be included in a single zip archive. What filename should be used for the output zip file? [/usr/share/elasticsearch/elasticsearch-ssl-http.zip] Zip file written to /usr/share/elasticsearch/elasticsearch-ssl-http.zip ##生成的檔案是zip格式的,若沒有unzip,請yum install -y unzip [root@localhost ~]# unzip /usr/share/elasticsearch/elasticsearch-ssl-http.zip -d /root Archive: /usr/share/elasticsearch/elasticsearch-ssl-http.zip creating: /root/elasticsearch/ inflating: /root/elasticsearch/README.txt inflating: /root/elasticsearch/http.p12 inflating: /root/elasticsearch/sample-elasticsearch.yml creating: /root/kibana/ inflating: /root/kibana/README.txt inflating: /root/kibana/elasticsearch-ca.pem inflating: /root/kibana/sample-kibana.yml
調整Elasticsearch的組態檔:
[root@localhost ~]# vim /etc/elasticsearch/elasticsearch.yml ##在最后面添加即可,檔案的path是相對路徑,是放到了/etc/elasticsearch下才可行 92 xpack.security.transport.ssl.verification_mode: certificate 93 xpack.security.transport.ssl.client_authentication: required 94 xpack.security.transport.ssl.keystore.path: elastic-certificates.p12 95 xpack.security.transport.ssl.truststore.path: elastic-certificates.p12 96 xpack.security.http.ssl.enabled: true 97 xpack.security.http.ssl.keystore.path: http.p12
調整Kibana的組態檔:
[root@localhost ~]# vim /etc/kibana/kibana.yml ##開啟了SSL,所以elasticsearch的訪問地址已經變成了https了 32 elasticsearch.hosts: ["https://192.168.85.10:9200"] ##下面直接添加一個elasticsearch的ca證書,指向檔案位置 34 elasticsearch.ssl.certificateAuthorities: [ "/etc/kibana/elasticsearch-ca.pem" ] ##直接在結尾添加就可以,字串是隨機自定義的就可以,至少32位,下面是kibana的會話超時時間和生命周期 118 xpack.encryptedSavedObjects.encryptionKey: 'afdjlk_@rsedfdgnfkl_asd_flkfjselj_faelikj@##' 119 xpack.security.session.idleTimeout: "1h" 120 xpack.security.session.lifespan: "30d"
再次訪問 ”告警和操作“ ,可以看到已經可以建立告警了:

下面內容是 filebeat 和 logstash 的相關設定,畢竟 http:// 已經連接不上了;
這里說明一下,我這里配置只是為了能夠設定告警,而只開了elasticsearch和kibana相關的配置;
但本身沒有開logstash和filebeat的HTTPS,所以,它們只需要可以連接到開啟HTTPS的elasticsearch和kibana即可;
若你需要將全部應用的服務均開啟安全傳輸,那么請參考官網:https://www.elastic.co/guide/en/elasticsearch/reference/7.11/ssl-tls.html
filebeat 組態檔有兩種方法:
第一種需要將原來的http改為https,并增加一個CA證書;
[root@postgreSQL ~]# vim /etc/filebeat/filebeat.yml ##Elasticsearch Output部分的hosts給地址加上https://即可 hosts: ["https://192.168.85.10:9200"] ##檔案是來自elasticsearch的主機的CA證書,權限root即可 ssl.certificate_authorities: ["/etc/filebeat/elasticsearch-ca.pem"]
第二種需要在原來 output.elasticsearch 加兩個選項;
[root@postgreSQL ~]# vim /etc/filebeat/filebeat.yml ##在Elasticsearch Output部分添加這兩行,使用https protocol: "https" ssl.verification_mode: none ##之后重啟服務 [root@postgreSQL ~]# systemctl restart filebeat
沒有報錯,服務狀態正常,去 Kibana 的可視化界面驗證下是否重繪日志;
logstash 組態檔:
##首先需要CA證書,這里仿照Kibana的方法可以生成p12檔案 [root@localhost ~]# /usr/share/elasticsearch/bin/elasticsearch-certutil cert -ca /etc/elasticsearch/elastic-stack-ca.p12 -name logstash-client This tool assists you in the generation of X.509 certificates and certificate signing requests for use with SSL/TLS in the Elastic stack. ......
......
* -multiple (generate multiple certificates) * -in (generate certificates from an input file) then the output will be be a zip file containing individual certificate/key files Enter password for CA (/etc/elasticsearch/elastic-stack-ca.p12) : 直接回車 Please enter the desired output file [logstash-client.p12]: 還是回車 Enter password for logstash-client.p12 : 依舊回車 ......
......
configure the client to trust this certificate. ##根據生成的p12檔案,生成我們需要的CA證書 [root@localhost ~]# openssl pkcs12 -in /usr/share/elasticsearch/logstash-client.p12 -cacerts -nokeys -out logstash-ca.crt Enter Import Password: MAC verified OK ##之后檔案放入logstash的組態檔目錄,權限原來的root即可,不用修改 [root@localhost ~]# mv logstash-ca.crt /etc/logstash/
修改組態檔:
[root@localhost ~]# vim /etc/logstash/conf.d/logstash.conf # Sample Logstash configuration for creating a simple # Beats -> Logstash -> Elasticsearch pipeline. input { beats { port => 5044 } } output { elasticsearch { hosts => ["https://192.168.180.200:9200"] ##開啟ssl ssl => true ##指定剛才我們生成的CA證書檔案 cacert => "/etc/logstash/logstash-ca.crt" index => "%{[fields][index]}-%{[@metadata][beat]}-%{+YYYY.MM.dd}" user => "elastic" password => "elastic" } }
之后我們啟動(或重啟)logstash 和 filebeat 服務,然后到Kibana可視化界面查看是否重繪了日志;
相應的,filebeat 服務的組態檔是無需修改的;
--回傳目錄--
希望有幫到你,哈哈!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/274690.html
標籤:其他
