作者:張堅(jiān),科大訊飛開發(fā)工程師,云原生愛好者。
大家好,我是張堅(jiān)。今天來聊聊如何在 KubeSphere 中集成外置的 ES 組件。
KubeSphere 在安裝完成時(shí)候可以啟用日志組件,這樣會(huì)安裝 ES 組件并可以收集所有部署組件的日志,也可以收集審計(jì)日志,然后可以很方便的在 KubeSphere 平臺(tái)上進(jìn)行日志查詢。
但是在實(shí)際使用過程中發(fā)現(xiàn)使用 KubeSphere 自身的 ES 會(huì)很重,而且官方也建議我們將日志接入到外部的 ES 中減輕 Kubernetes 的壓力。
以下為操作實(shí)戰(zhàn)。
前置步驟
ES 集群需支持 http 協(xié)議
1?? 搭建好外部 ES 集群,使用 http 協(xié)議(非本文重點(diǎn))。
測試環(huán)境 IP: 172.30.10.226,172.30.10.191,172.30.10.184
port: 9200
username: elastic
password: changeme
2?? 對 ES 做負(fù)載均衡。
有三種常見的做法:
- 使用 nginx 做負(fù)載均衡;
- 單協(xié)調(diào)節(jié)點(diǎn);
- 通過自定義 service 和 endpoints 負(fù)載均衡。
本文檔基于第三種負(fù)載均衡方案(通過 endpoints 負(fù)載)做對接。
備份 ks-installer
管理員賬號登錄 KubeSphere,在平臺(tái)管理 - 集群管理 - CRD 中搜索 clusterconfiguration,在自定義資源中,點(diǎn)擊 ks-installer
選擇編輯 YAML ,復(fù)制備份。
關(guān)閉內(nèi)部 ES 對接外部 ES(如果未開啟日志則省略)
集群開啟了內(nèi)部 Elasticsearch,會(huì)存在如下系統(tǒng)組件和日志接收器
1?? 系統(tǒng)組件:
2?? 日志接收器:
容器日志、資源事件、審計(jì)日志(不開啟不會(huì)有日志接收器)
接收器地址為內(nèi)部 Elasticsearch 地址:elasticsearch-logging-data.kubesphere-logging-system.svc:9200
關(guān)閉內(nèi)部 ES 并卸載日志相關(guān)可插拔組件
執(zhí)行命令編輯 ks-installer:
$ kubectl edit cc ks-installer -n kubesphere-system
1?? 卸載日志系統(tǒng),將 ks-installer
參數(shù)的 logging.enabled
字段的值從 true
改為 false
。
2?? 卸載事件日志系統(tǒng),將 ks-installer
參數(shù)的 events.enabled
字段的值從 true
改為 false
。
3?? 卸載審計(jì)日志系統(tǒng),將 ks-installer
參數(shù)的 auditing.enabled
字段的值從 true
改為 false
。
4?? 配置文件的最后,刪除如下內(nèi)容:
es:
? enabledTime: 2022-08-16T10:33:18CST
? status: enabled
events:
? enabledTime: 2022-04-15T16:22:59CST
? status: enabled
fluentbit:
enabledTime: 2022-04-15T16:19:46CST
status: enabled
logging:
enabledTime: 2022-04-15T16:22:59CST
status: enabled
執(zhí)行命令檢查安裝過程:
$ kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l app=ks-install -o jsonpath='{.items[0].metadata.name}') -f
#日志出現(xiàn)以下內(nèi)容說明重啟成功
Collecting installation results ...
#####################################################
### Welcome to KubeSphere! ###
#####################################################
Console: http://172.30.9.xxx:30880
Account: admin
Password: P@88w0rd
NOTES:
1. After you log into the console, please check the
monitoring status of service components in
"Cluster Management". If any service is not
ready, please wait patiently until all components
are up and running.
2. Please change the default password after login.
#####################################################
https://kubesphere.io 2022-08-04 15:53:14
#####################################################
執(zhí)行命令卸載相關(guān)可插拔組件:
###### 卸載 KubeSphere 日志系統(tǒng)
$ kubectl delete inputs.logging.kubesphere.io -n kubesphere-logging-system tail
###### 卸載 KubeSphere 事件系統(tǒng)
$ helm delete ks-events -n kubesphere-logging-system
###### 卸載 KubeSphere 審計(jì)
$ helm uninstall kube-auditing -n kubesphere-logging-system
$ kubectl delete crd rules.auditing.kubesphere.io
$ kubectl delete crd webhooks.auditing.kubesphere.io
###### 卸載包括 Elasticsearch 的日志系統(tǒng)
$ kubectl delete crd fluentbitconfigs.logging.kubesphere.io
$ kubectl delete crd fluentbits.logging.kubesphere.io
$ kubectl delete crd inputs.logging.kubesphere.io
$ kubectl delete crd outputs.logging.kubesphere.io
$ kubectl delete crd parsers.logging.kubesphere.io
$ kubectl delete deployments.apps -n kubesphere-logging-system fluentbit-operator
$ helm uninstall elasticsearch-logging --namespace kubesphere-logging-system
$ kubectl delete deployment logsidecar-injector-deploy -n kubesphere-logging-system
$ kubectl delete ns kubesphere-logging-system
卸載過程中可能出現(xiàn)如下異常:
crd 資源刪除時(shí)出現(xiàn)問題,嘗試使用如下命令
$ kubectl patch crd/crd名稱 -p '{"metadata":{"finalizers":[]}}' --type=merge
創(chuàng)建 namespace
$ kubectl create ns kubesphere-logging-system
自定義 service 負(fù)載均衡 ES 節(jié)點(diǎn)
es-service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: es-service
name: es-service
namespace: kubesphere-logging-system
spec:
ports:
- port: 9200
name: es
protocol: TCP
targetPort: 9200
es-endpoints.yaml
ip 地址修改為真實(shí)要對接的 ES 集群節(jié)點(diǎn)的 IP 地址。
apiVersion: v1
kind: Endpoints
metadata:
labels:
app: es-service
name: es-service
namespace: kubesphere-logging-system
subsets:
- addresses:
- ip: 172.30.10.***
- ip: 172.30.10.***
- ip: 172.30.10.***
ports:
- port: 9200
name: es
protocol: TCP
執(zhí)行命令創(chuàng)建自定義 SVC:
$ kubectl apply -f es-service.yaml -n kubesphere-logging-system
$ kubectl apply -f es-endpoints.yaml -n kubesphere-logging-system
#查看svc
$ kubectl get svc -n kubesphere-logging-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
es-service ClusterIP 109.233.8.178 <none> 9200/TCP 10d
#查看endpoints
$ kubectl get ep -n kubesphere-logging-system
NAME ENDPOINTS AGE es-service 172.30.10.***:9200,172.30.10.***:9200,172.30.10.***:9200 10d
“平臺(tái)管理-集群管理-應(yīng)用負(fù)載-服務(wù)”搜索 es-service。
es-service 服務(wù)地址:es-service.kubesphere-logging-system.svc。
開啟日志并對接外部 ES
在平臺(tái)管理 - 集群管理 - CRD 中搜索 clusterconfiguration,在自定義資源中,點(diǎn)擊 ks-installer
,修改配置:
開啟容器日志、審計(jì)日志分別修改
logging.enabled: true
auditing.enabled: true
修改外部 ES 配置
es.basicAuth.enabled: true
es.basicAuth.password
es.basicAuth.username
es.externalElasticsearchUrl
es.externalElasticsearchPort
修改日志保存日期(默認(rèn) 7 天)
logMaxAge
修改日志索引前綴(默認(rèn) logstash)
elkPrefix(開發(fā):dev,測試:sit,生產(chǎn):prod)
apiVersion: installer.kubesphere.io/v1alpha1
kind: ClusterConfiguration
metadata:
labels:
version: v3.2.1
name: ks-installer
namespace: kubesphere-system
spec:
alerting:
enabled: false
auditing:
enabled: true # false改為true
...
es:
basicAuth:
enabled: true # false改為true
password: '****' # 密碼
username: '****' # 用戶名
data:
volumeSize: 20Gi
elkPrefix: sit #開發(fā):dev 測試:sit 生產(chǎn):prod
externalElasticsearchPort: '9200' # 端口
externalElasticsearchUrl: es-service.kubesphere-logging-system.svc # 修改es-service
logMaxAge: 7 #默認(rèn)7天即可
master:
volumeSize: 4Gi
...
執(zhí)行命令檢查安裝過程
$ kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l app=ks-install -o jsonpath='{.items[0].metadata.name}') -f
#日志出現(xiàn)以下內(nèi)容說明重啟成功
Collecting installation results ...
#####################################################
### Welcome to KubeSphere! ###
#####################################################
Console: http://172.30.9.xxx:30880
Account: admin
Password: P@88w0rd
NOTES:
1. After you log into the console, please check the
monitoring status of service components in
"Cluster Management". If any service is not
ready, please wait patiently until all components
are up and running.
2. Please change the default password after login.
#####################################################
https://kubesphere.io 2022-08-04 15:53:14
#####################################################
執(zhí)行命令,查看對應(yīng)的 ConfigMap 配置:
$ kubectl get configmap kubesphere-config -n kubesphere-system -o yaml
#重點(diǎn)如下,看es的配置是否已經(jīng)生效,host是否為自定義SVC,以及用戶名密碼索引是否正確
logging:
host: http://es-service.kubesphere-logging-system.svc:9200
basicAuth: True
username: "****" #此處為你填寫的正確用戶名
password: "****" #此處為你填寫的正確密碼
indexPrefix: ks-sit-log #不同環(huán)境對應(yīng)dev\sit\prod
auditing:
enable: true
webhookURL: https://kube-auditing-webhook-svc.kubesphere-logging-system.svc:6443/audit/webhook/event
host: http://es-service.kubesphere-logging-system.svc:9200
basicAuth: True
username: "****" #此處為你填寫的正確用戶名
password: "****" #此處為你填寫的正確密碼
indexPrefix: ks-sit-auditing #不同環(huán)境對應(yīng)dev\sit\prod
執(zhí)行命令編輯對應(yīng) output(如果已自動(dòng)修改則不需要手動(dòng)修改):
-
修改 host
-
修改索引 (開發(fā)、測試、生產(chǎn)前綴分別對應(yīng) ks-dev-、ks-sit-、ks-prod-)
#修改es的output
$ kubectl edit output es -n kubesphere-logging-system
#修改host 和 logstashPrefix
# host: es-service.kubesphere-logging-system.svc
# logstashPrefix: ks-對應(yīng)環(huán)境-log
#如下:
spec:
es:
generateID: true
host: es-service.kubesphere-logging-system.svc # host地址
httpPassword:
valueFrom:
secretKeyRef:
key: password
name: elasticsearch-credentials
httpUser:
valueFrom:
secretKeyRef:
key: username
name: elasticsearch-credentials
logstashFormat: true
logstashPrefix: ks-sit-log # 修改此處為對應(yīng)環(huán)境的日志索引
port: 9200
timeKey: '@timestamp'
matchRegex: '(?:kube|service)\.(.*)'
#修改es-auditing的output
$ kubectl edit output es-auditing -n kubesphere-logging-system
#修改host 和 logstashPrefix
# host: es-service.kubesphere-logging-system.svc
# logstashPrefix: ks-對應(yīng)環(huán)境-auditing
#如下
spec:
es:
generateID: true
host: es-service.kubesphere-logging-system.svc # host地址
httpPassword:
valueFrom:
secretKeyRef:
key: password
name: elasticsearch-credentials
httpUser:
valueFrom:
secretKeyRef:
key: username
name: elasticsearch-credentials
logstashFormat: true
logstashPrefix: ks-sit-auditing # 修改此處為對應(yīng)環(huán)境的日志索引
port: 9200
match: kube_auditing
重啟 ks-apiserver
$ kubectl rollout restart deployment ks-apiserver -n kubesphere-system
驗(yàn)證
$ kubectl get po -n kubesphere-logging-system
NAME READY STATUS RESTARTS AGE
elasticsearch-logging-curator-elasticsearch-curator-276864h2xt2 0/1 Error 0 38h
elasticsearch-logging-curator-elasticsearch-curator-276864wc6bs 0/1 Completed 0 38h
elasticsearch-logging-curator-elasticsearch-curator-276879865wl 0/1 Completed 0 14h
elasticsearch-logging-curator-elasticsearch-curator-276879l7xpf 0/1 Error 0 14h
fluent-bit-4vzq5 1/1 Running 0 47h
fluent-bit-6ckvm 1/1 Running 0 25h
fluent-bit-6jt8d 1/1 Running 0 47h
fluent-bit-88crg 1/1 Running 0 47h
fluent-bit-9ps6z 1/1 Running 0 47h
fluent-bit-djhtx 1/1 Running 0 47h
fluent-bit-dmpfv 1/1 Running 0 47h
fluent-bit-dtr7z 1/1 Running 0 47h
fluent-bit-flxbt 1/1 Running 0 47h
fluent-bit-fnxdk 1/1 Running 0 47h
fluent-bit-gqbrl 1/1 Running 0 47h
fluent-bit-kbzsj 1/1 Running 0 47h
fluent-bit-lbnnh 1/1 Running 0 47h
fluent-bit-nq4g8 1/1 Running 0 47h
fluent-bit-q5shz 1/1 Running 0 47h
fluent-bit-qrb7v 1/1 Running 0 47h
fluent-bit-r26fk 1/1 Running 0 47h
fluent-bit-rfrpd 1/1 Running 0 47h
fluent-bit-s8869 1/1 Running 0 47h
fluent-bit-sp5k4 1/1 Running 0 47h
fluent-bit-vjvhl 1/1 Running 0 47h
fluent-bit-xkksv 1/1 Running 0 47h
fluent-bit-xrlz4 1/1 Running 0 47h
fluentbit-operator-745bf5559f-vnz8w 1/1 Running 0 47h
kube-auditing-operator-84857bf967-ftbjr 1/1 Running 0 47h
kube-auditing-webhook-deploy-64cfb8c9f8-hf8g8 1/1 Running 0 47h
kube-auditing-webhook-deploy-64cfb8c9f8-zf4rd 1/1 Running 0 47h
logsidecar-injector-deploy-5fb6fdc6dd-fj5vm 2/2 Running 0 47h
logsidecar-injector-deploy-5fb6fdc6dd-qbhdg 2/2 Running 0 47h
日志接收器:
查詢審計(jì)日志:
文章來源:http://www.zghlxwxcb.cn/news/detail-430484.html
本文由博客一文多發(fā)平臺(tái) OpenWrite 發(fā)布!文章來源地址http://www.zghlxwxcb.cn/news/detail-430484.html
到了這里,關(guān)于KubeSphere 接入外部 Elasticsearch 最佳實(shí)踐的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!