大家好,我是飄渺,這是SpringCloud alibaba開發實戰系列文的第三十八篇,運維系列的第二篇,系列文章,歡迎持續關注,
文章會在流量變低時轉入收費專欄,且看且珍惜!
概述
在單體服務中我們需要查看日志只需直接在日志檔案中 grep、awk 就可以獲得自己想要的資訊,
但是在微服務架構中,不同的服務模塊一般會部署多個節點,日志散落在多個節點的日志檔案中,一旦出現問題,我們就需要登錄不同的服務節點分別查看日志,非常之繁瑣,所以在微服務架構中,我們是需要建立集中式日志收集系統,將所有節點上的日志統一收集,管理,訪問,
現在目前主流的分布式日志解決方案還是基于ELK(ElasticSearch、Logstash、Kibana),今天我們就動手搭建一個單機版的ELK日志收集系統并將我們的日志檔案集成進去,
ELK版本:7.13.4
ElasticSearch 安裝配置
說明:ElasticSearch7.13.4內置了JDK16,如果沒有特殊要求,直接使用默認JDK即可,本文中直接使用ElasticSearch的默認JDK16,
- 下載
下載地址:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.13.4-linux-x86_64.tar.gz
- 上傳檔案并解壓縮
將下載下來的ElasticSearch上傳至服務器目錄/app/elasticsearch,并將其解壓
cd /app/elasticsearch
tar -zxvf elasticsearch-7.13.4-linux-x86_64.tar.gz
解壓完成后的目錄結構如下:
ll
total 319484
drwxr-xr-x 9 elastic elastic 4096 Jul 15 02:37 elasticsearch-7.13.4
-rw-r--r-- 1 root root 327143992 Aug 3 14:26 elasticsearch-7.13.4-linux-x86_64.tar.gz
- 創建ElasticSearch啟動用戶
ElasticSearch不允許使用root用戶啟動,所以我們需要創建一個elastic用戶用于啟動ElasticSearch,并授予用戶對應權限,
useradd elastic
chown -R elastic:elastic /app/elasticsearch/
- 修改es配置
組態檔為:/app/elasticsearch/elasticsearch-7.13.4/config/elasticsearch.yml
關鍵配置:
cluster.name: elk-application
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["127.0.0.1"]
cluster.initial_master_nodes: ["node-1"]
- 修改es的JVM引數
組態檔為:/app/elasticsearch/elasticsearch-7.13.4/config/jvm.options
配置內容:
-Xms4g
-Xmx4g
- 修改系統組態檔
- 在
/etc/sysctl.conf中增加如下配置
vm.max_map_count = 655360
- 在
/etc/security/limits.conf后面增加如下配置
* soft memlock unlimited
* hard memlock unlimited
* hard nofile 65536
* soft nofile 65536
修改完成后需要使用命令 sysctl -p 重新加載配置,
- 切換elastic用戶并啟動ElasticSearch
su - elastic
cd /app/elasticsearch/elasticsearch-7.13.4/bin
./elasticsearch # 正常啟動,可以看到啟動日志
或者 ./elasticsearch -d # 后臺啟動,看不到啟動日志
- 驗證安裝
在服務器執行curl 127.0.0.1:9200命令,查看輸出結果,如果輸出如下json資料,則表示啟動成功,
{
"name" : "node-1",
"cluster_name" : "elk-application",
"cluster_uuid" : "qvggIOwbTk6pVlxFlulqQg",
"version" : {
"number" : "7.13.4",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "c5f60e894ca0c61cdbae4f5a686d9f08bcefc942",
"build_date" : "2021-07-14T18:33:36.673943207Z",
"build_snapshot" : false,
"lucene_version" : "8.8.2",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
如果你服務器已經安裝好了JDK環境并且想使用自己的JDK,可以修改配置
/app/elasticsearch/elasticsearch-7.13.4/bin中的elasticsearch-env,在配置ES_CLASSPATH="$ES_HOME/lib/*"之后加入
ES_JAVA_HOME=/app/elasticsearch/elasticsearch-7.13.4/jdk # 修改為你自己的jdk地址
Kibana 安裝配置
- 下載
下載地址:https://artifacts.elastic.co/downloads/kibana/kibana-7.13.4-linux-x86_64.tar.gz
- 上傳檔案并解壓縮
將下載下來的Kibana上傳至服務器目錄/app/Kibana,并將其解壓
cd /app/kibana
tar -zxvf kibana-7.13.4-linux-x86_64.tar.gz
- 修改組態檔
/kibana/kibana-7.13.4-linux-x86_64/config/kibana.yml
# 埠
server.port: 5601
# 指定本機ip讓外部能訪問
server.host: "0.0.0.0"
# 請求資料指向的elasticsearch服務器
elasticsearch.hosts: ["http://ip:9200"]
# 中文漢化
i18n.locale: "zh-CN"
這幾個配置在組態檔中被注釋掉了,直接放開注釋做相應的修改即可,
- 創建kibana啟動用戶
Kibana默認情況下也不允許使用root用戶啟動,你可以選擇在啟動命令后加上--allow-root引數,不過為了規范,我們還是創建一個獨立用戶用于啟動Kibana,并授予用戶對應權限,
useradd kibana
chown -R kibana:elastic /app/kibana/
- 切換用戶并使用啟動kibana
su - kibana # 切換用戶
./kibana
或者 ./kibana &
或者 ./kibana --allow-root # 未設定獨立用戶,不建議使用
- 訪問
通過瀏覽器輸入http://ip:5601/即可訪問kibana

安全加固
我們剛剛部署的Kibana是不需要密碼就可以登錄的,這樣誰都可以kibana訪問并且更改索引資料,在生產環境中為了保證資料的安全,我們必須得給kibana加上密碼,保證用戶登錄后可進行操作,
主要是利用elasticsearch自帶的xpack作為權限驗證功能,操作步驟如下:
1. 修改ES配置開啟 X-PACK
修改ElasticSearch的組態檔,/app/elasticsearch/elasticsearch-7.13.4/config/elasticsearch.yml,開啟x-pack
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
2. 重啟ElasticSearch
./bin/elasticsearch -d
3. 初始化用戶密碼
cd /app/elasticsearch/elasticsearch-7.13.4/bin
./elasticsearch-setup-passwords interactive
執行后會出現下面的內容,讓你設定對應的密碼,輸入 y 繼續
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
...
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]
4. 設定證書
啟用x-pack后ElasticSearch的啟動日志會報Caused by: javax.net.ssl.SSLHandshakeException: No available authentication scheme的例外,原因是因為缺少CA證書,所以我們需要給其生成一個,
./bin/elasticsearch-certutil ca
看到提示后直接回車即可,不用設定密碼
If you elect to generate PEM format certificates (the -pem option), then the output will
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 : # 直接回車
之后我們在ElasticSearch的安裝目錄下會看到這個證書檔案elastic-stack-ca.p12

現在我們借助生成的這個證書檔案生成p12秘鑰
./bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
看到提示后還是直接回車,不設定密碼
Enter password for CA (elastic-stack-ca.p12) : # 直接回車
Please enter the desired output file [elastic-certificates.p12]: # 回車
Enter password for elastic-certificates.p12 : # 回車
# 檔案路徑
Certificates written to /app/elasticserach/elasticsearch-7.13.4/elastic-certificates.p12
此時檔案如下:

在config目錄下創建certs目錄,并將生成的秘鑰檔案拷貝進去
mkdir certs
cp ../elastic-certificates.p12 certs/elastic-certificates.p12
再次修改ElasticSearch的組態檔elasticsearch.yml中xpack相關配置
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12
接著再次重啟ElasticSearch后錯誤消失,
5. 修改Kibana配置
修改kibana組態檔/app/kibana/kibana-7.13.4-linux-x86_64/config/kibana.yml
elasticsearch.username: "elastic"
elasticsearch.password: "changeMe"
5. 重新啟動kibana
6. 驗證
使用curl訪問ElasticSearch提示需要密碼
{
"error": {
"root_cause": [
{
"type": "security_exception",
"reason": "missing authentication credentials for REST request [/]",
"header": {
"WWW-Authenticate": "Basic realm=\"security\" charset=\"UTF-8\""
}
}
],
"type": "security_exception",
"reason": "missing authentication credentials for REST request [/]",
"header": {
"WWW-Authenticate": "Basic realm=\"security\" charset=\"UTF-8\""
}
},
"status": 401
}
帶上密碼后重新呼叫,顯示正常
curl --user elastic:password http://ip:9200/
{
"name" : "node-1",
"cluster_name" : "efk-application",
"cluster_uuid" : "qvggIOwbTk6pVlxFlulqQg",
"version" : {
"number" : "7.13.4",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "c5f60e894ca0c61cdbae4f5a686d9f08bcefc942",
"build_date" : "2021-07-14T18:33:36.673943207Z",
"build_snapshot" : false,
"lucene_version" : "8.8.2",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
重新登錄kibana需要使用密碼,需要使用elastic賬戶登錄

登錄后在kibana中可以對用戶和角色進行管理,

Logstash安裝配置
- 下載
下載地址:https://artifacts.elastic.co/downloads/logstash/logstash-7.13.4-linux-x86_64.tar.gz
- 上傳檔案并解壓縮
將下載下來的logstash上傳至服務器目錄/app/logstash,并將其解壓
cd /app/logstash
tar -zxvf logstash-7.13.4-linux-x86_64.tar.gz
- 修改組態檔
在/app/logstash/logstash-7.13.4/config中復制logstash-sample.conf為logstash.conf,并修改里面的內容:
cp logstash-sample.conf logstash.conf
input {
beats {
port => 5044
}
}
output {
elasticsearch {
hosts => ["http://ip:9200"]
index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
user => "elastic"
password => "password"
}
}
主要修改host、user、password的屬性值,
- 啟動logstash
./logstash -f ../config/logstash.conf
通過上面的安裝配置我們已經將ELK環境搭建完畢,接下來需要將日志集成進來,
集成日志
日志接入到ELK有多種形式,最常見的就是使用FileBeat,此時Filebeat扮演日志代理的角色,安裝在生成日志檔案的計算機上,跟蹤它們,并將資料轉發到Logstash以進行更高級的處理,或者直接轉發到Elasticsearch進行索引,

這種方式實作比較簡單,在我 “運維監控” 系列文章中有詳細介紹,感興趣的同學可以移步查看,
ELK實戰二 - 日志集成
今天我們介紹另外一種方式,使用logstash-logback-encoder直接將logback日志推送到logstash,
配置 logstash 輸出到 elasticsearch
操作步驟如下:
- 引入logstash插件包
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>6.6</version>
</dependency>
- 修改logback-spring.xml,配置logstash到appender
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<!--定義日志檔案的存盤地址 -->
<property name="LOG_HOME" value="./logs" />
<property name="LOG_NAME" value="cloud_gateway" />
<springProperty scope="context" name="applicationName" source="spring.application.name"/>
...
<!--LogStash-->
<appender name="logstash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<!--logstash的請求地址-->
<destination>172.xx.0.xxx:5064</destination>
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder">
<customFields>{"serverName":"${applicationName}"}</customFields>
</encoder>
</appender>
<!-- 日志輸出級別 -->
<root level="INFO">
...
<appender-ref ref="logstash"/>
</root>
</configuration>
檔案中洗掉了不相關的配置,值得注意的是這里定義了一個靜態常量appllicationName,從組態檔中讀取spring.application.name的值,并通過customFields自定義欄位serverName傳輸到了logstash,
- 修改logstash的組態檔,logstash.conf
input {
tcp {
port => 5064
codec => json_lines
type => "cloud_alibaba"
}
}
output {
# 如果不需要列印可以直接洗掉
stdout{
codec => rubydebug
}
# 通過type用于區分不同來源的日志
if [type] == "cloud_alibaba"{
elasticsearch {
hosts => ["http://localhost:9200"]
index => "%{[serverName]}-%{+YYYY.MM.dd}"
user => "elastic"
password => "changeMe"
}
}
}
logstash需要使用tcp協議接受logstash傳來的日志,并使用customFields中定義的serverName動態建立索引,
- 重新啟動logstash 以及后端服務

通過logstash的啟動面板可以看到日志已經傳輸到了ElasticSearch,并且已經使用cloud-gateway作為索引名,
kibana操作
日志傳輸到ElasticSearch后我們就需要借助kibana將日志展示出來,
首先我們需要登錄kibana,創建索引模式

使用cloud-gateway-*作為索引名稱,然后點下一步并保存,
然后就可以在Discover中查看日志了,

可以通過左側添加對應的欄位,
當然也可以流式傳輸實時查看日志

至于kibana的其他功能就需要各位自己去摸索了,
logback動態讀取logstash的路徑
上面我們配置logstash輸出到elasticsearch的時候是直接寫死了destination的配置
<destination>172.xx.0.xxx:5064</destination>
那在實際開發的時候我們肯定不能這樣,環境不同logstash的路徑也不同,所以我們需要從nacos配置中心讀取對應logstash的地址,
小提示,springboot讀取組態檔是有優先級的,如果使用默認的logback.xml或者logback-spring.xml為組態檔名則會讀取不到nacos上的配置,他會先于配置中心加載,所以我們這里需要使用自定義組態檔,
- 修改nacos配置中心service對應的組態檔,配置自定義日志名稱以及logstash的路徑
logging:
config: classpath:logback-custom.xml
logstash:
destination: 172.xx.0.xxx:5064
-
修改日志組態檔為
logback-custom.xml -
在logback-custom.xml中定義常量,從配置中心讀取配置,并修改
destination屬性的值
<springProperty scope="context" name="logstashDestination" source="logstash.destination"/>
...
<destination>${logstashDestination}</destination>
通過上面三步操作,我們的logback就可以直接從nacos組態檔中加載logstash的相關配置了,
小結
ELK集中式日志是分布式架構中必不可少的一個組件,本文詳細介紹了ELK基礎環境的安裝配置,希望對各位有所幫助,最后,我是飄渺Jam,一名寫代碼的架構師,做架構的程式員,期待您的轉發與關注,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/294898.html
標籤:其他
