KubeSphere 接入外部 Elasticsearch 最佳實踐

KubeSphere發表於2022-12-15
作者:張堅,科大訊飛開發工程師,雲原生愛好者。

大家好,我是張堅。今天來聊聊如何在 KubeSphere 中整合外接的 ES 元件。

KubeSphere 在安裝完成時候可以啟用日誌元件,這樣會安裝 ES 元件並可以收集所有部署元件的日誌,也可以收集審計日誌,然後可以很方便的在 KubeSphere 平臺上進行日誌查詢。

但是在實際使用過程中發現使用 KubeSphere 自身的 ES 會很重,而且官方也建議我們將日誌接入到外部的 ES 中減輕 Kubernetes 的壓力。

以下為操作實戰。

前置步驟

ES 叢集需支援 http 協議

1️⃣ 搭建好外部 ES 叢集,使用 http 協議(非本文重點)。

測試環境 IP: 172.30.10.226,172.30.10.191,172.30.10.184

port: 9200

username: elastic

password: changeme

2️⃣ 對 ES 做負載均衡。

有三種常見的做法:

  1. 使用 nginx 做負載均衡;
  2. 單協調節點;
  3. 透過自定義 service 和 endpoints 負載均衡。
本文件基於第三種負載均衡方案(透過 endpoints 負載)做對接。

備份 ks-installer

管理員賬號登入 KubeSphere,在平臺管理 - 叢集管理 - CRD 中搜尋 clusterconfiguration,在自定義資源中,點選 ks-installer 選擇編輯 YAML ,複製備份。

關閉內部 ES 對接外部 ES(如果未開啟日誌則省略)

叢集開啟了內部 Elasticsearch,會存在如下系統元件和日誌接收器

1️⃣ 系統元件:

2️⃣ 日誌接收器:

容器日誌、資源事件、審計日誌(不開啟不會有日誌接收器)

接收器地址為內部 Elasticsearch 地址:elasticsearch-logging-data.kubesphere-logging-system.svc:9200

關閉內部 ES 並解除安裝日誌相關可插拔元件

執行命令編輯 ks-installer:

$ kubectl edit cc ks-installer -n kubesphere-system

1️⃣ 解除安裝日誌系統,將 ks-installer 引數的 logging.enabled 欄位的值從 true 改為 false

2️⃣ 解除安裝事件日誌系統,將 ks-installer 引數的 events.enabled 欄位的值從 true 改為 false

3️⃣ 解除安裝審計日誌系統,將 ks-installer 引數的 auditing.enabled 欄位的值從 true 改為 false

4️⃣ 配置檔案的最後,刪除如下內容:

 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

執行命令檢查安裝過程:

$ kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l app=ks-install -o jsonpath='{.items[0].metadata.name}') -f
#日誌出現以下內容說明重啟成功

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
#####################################################

執行命令解除安裝相關可插拔元件:

###### 解除安裝 KubeSphere 日誌系統
$ kubectl delete inputs.logging.kubesphere.io -n kubesphere-logging-system tail

###### 解除安裝 KubeSphere 事件系統
$ helm delete ks-events -n kubesphere-logging-system

###### 解除安裝 KubeSphere 審計
$ helm uninstall kube-auditing -n kubesphere-logging-system

$ kubectl delete crd rules.auditing.kubesphere.io

$ kubectl delete crd webhooks.auditing.kubesphere.io

###### 解除安裝包括 Elasticsearch 的日誌系統
$ 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

解除安裝過程中可能出現如下異常:

crd 資源刪除時出現問題,嘗試使用如下命令

$ kubectl patch crd/crd名稱 -p '{"metadata":{"finalizers":[]}}' --type=merge

建立 namespace

$ kubectl create ns kubesphere-logging-system

自定義 service 負載均衡 ES 節點

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 地址修改為真實要對接的 ES 叢集節點的 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

執行命令建立自定義 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

“平臺管理-叢集管理-應用負載-服務”搜尋 es-service。

es-service 服務地址:es-service.kubesphere-logging-system.svc

開啟日誌並對接外部 ES

在平臺管理 - 叢集管理 - CRD 中搜尋 clusterconfiguration,在自定義資源中,點選 ks-installer,修改配置:

開啟容器日誌、審計日誌分別修改

logging.enabled: true

auditing.enabled: true

修改外部 ES 配置

es.basicAuth.enabled: true

es.basicAuth.password

es.basicAuth.username

es.externalElasticsearchUrl

es.externalElasticsearchPort

修改日誌儲存日期(預設 7 天)

logMaxAge

修改日誌索引字首(預設 logstash)

elkPrefix(開發:dev,測試:sit,生產: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           #開發:dev 測試:sit 生產:prod
      externalElasticsearchPort: '9200' # 埠
      externalElasticsearchUrl: es-service.kubesphere-logging-system.svc # 修改es-service
      logMaxAge: 7             #預設7天即可          
      master:
        volumeSize: 4Gi
    ...

執行命令檢查安裝過程

$ kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l app=ks-install -o jsonpath='{.items[0].metadata.name}') -f

#日誌出現以下內容說明重啟成功

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
#####################################################

執行命令,檢視對應的 ConfigMap 配置:

$ kubectl get configmap kubesphere-config -n kubesphere-system -o yaml  
#重點如下,看es的配置是否已經生效,host是否為自定義SVC,以及使用者名稱密碼索引是否正確
    logging:
      host: http://es-service.kubesphere-logging-system.svc:9200
      basicAuth: True
      username: "****"  #此處為你填寫的正確使用者名稱
      password: "****"  #此處為你填寫的正確密碼
      indexPrefix: ks-sit-log  #不同環境對應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 #不同環境對應dev\sit\prod

執行命令編輯對應 output(如果已自動修改則不需要手動修改):

  1. 修改 host
  2. 修改索引 (開發、測試、生產字首分別對應 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-對應環境-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  # 修改此處為對應環境的日誌索引
    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-對應環境-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   # 修改此處為對應環境的日誌索引
    port: 9200
  match: kube_auditing

重啟 ks-apiserver

$ kubectl rollout restart deployment ks-apiserver -n kubesphere-system

驗證

$ 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

日誌接收器:

查詢審計日誌:

相關文章