您现在的位置是:首页 > 编程语言学习 > 前端编程语言 > 文章正文 前端编程语言

KubeSphere接入外部Elasticsearch实战示例

2022-12-13 11:16:23 前端编程语言

简介在安装完成时候可以启用日志组件,这样会安装 ES 组件并可以收集所有部署组件的日志,也可以收集审计日志,然后可以很方便的在 KubeSphe...

在安装完成时候可以启用日志组件,这样会安装 ES 组件并可以收集所有部署组件的日志,也可以收集审计日志,然后可以很方便的在 KubeSphere 平台上进行日志查询。

但是在实际使用过程中发现使用 KubeSphere 自身的 ES 会很重,而且官方也建议我们将日志接入到外部的 ES 中减轻 Kubernetes 的压力。

以下为操作实战。

前置步骤

ES 集群需支持 http 协议

1️⃣ 搭建好外部 ES 集群,使用 http 协议(非本文重点)。

测试环境 IP: 172.30.10.226,172.30.10.191,172.30.10.184port: 9200username: elasticpassword: changeme

2️⃣ 对 ES 做负载均衡。

有三种常见的做法:

  • 使用 nginx 做负载均衡;
  • 单协调节点;
  • 通过自定义 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:


  1. $ 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️⃣ 配置文件的最后,删除如下内容:

  1.  es:   
  2. enabledTime: 2022-08-16T10:33:18CST   
  3. status: enabled   
  4.   events:   
  5. enabledTime: 2022-04-15T16:22:59CST   
  6. status: enabled   
  7.   fluentbit:   
  8. enabledTime: 2022-04-15T16:19:46CST   
  9. status: enabled   
  10.   logging:   
  11. enabledTime: 2022-04-15T16:22:59CST   
  12. status: enabled 

执行命令检查安装过程:

  1. $ kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l app=ks-install -o jsonpath='{.items[0].metadata.name}') -f 
  2. #日志出现以下内容说明重启成功 
  3. Collecting installation results ... 
  4. ##################################################### 
  5. ###  Welcome to KubeSphere!   ### 
  6. ##################################################### 
  7. Console: http://172.30.9.xxx:30880 
  8. Account: admin 
  9. Password: P@88w0rd 
  10. NOTES: 
  11.   1. After you log into the console, please check the 
  12.  monitoring status of service components in 
  13.  "Cluster Management". If any service is not 
  14.  ready, please wait patiently until all components  
  15.  are up and running. 
  16.   2. Please change the default password after login. 
  17. ##################################################### 
  18. https://kubesphere.io 2022-08-04 15:53:14 
  19. ##################################################### 

执行命令卸载相关可插拔组件:

  1. ###### 卸载 KubeSphere 日志系统 
  2. $ kubectl delete inputs.logging.kubesphere.io -n kubesphere-logging-system tail 
  3. ###### 卸载 KubeSphere 事件系统 
  4. $ helm delete ks-events -n kubesphere-logging-system 
  5. ###### 卸载 KubeSphere 审计 
  6. $ helm uninstall kube-auditing -n kubesphere-logging-system 
  7. $ kubectl delete crd rules.auditing.kubesphere.io 
  8. $ kubectl delete crd webhooks.auditing.kubesphere.io 
  9. ###### 卸载包括 Elasticsearch 的日志系统 
  10. $ kubectl delete crd fluentbitconfigs.logging.kubesphere.io 
  11. $ kubectl delete crd fluentbits.logging.kubesphere.io 
  12. $ kubectl delete crd inputs.logging.kubesphere.io 
  13. $ kubectl delete crd outputs.logging.kubesphere.io 
  14. $ kubectl delete crd parsers.logging.kubesphere.io 
  15. $ kubectl delete deployments.apps -n kubesphere-logging-system fluentbit-operator 
  16. $ helm uninstall elasticsearch-logging --namespace kubesphere-logging-system 
  17. $ kubectl delete deployment logsidecar-injector-deploy -n kubesphere-logging-system 
  18. $ kubectl delete ns kubesphere-logging-system 

卸载过程中可能出现如下异常:

crd 资源删除时出现问题,尝试使用如下命令


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

创建 namespace


  1. $ kubectl create ns kubesphere-logging-system 

自定义 service 负载均衡 ES 节点

es-service.yaml

  1. apiVersion: v1 
  2. kind: Service 
  3. metadata: 
  4.   labels: 
  5. app: es-service 
  6.   name: es-service 
  7.   namespace: kubesphere-logging-system 
  8. spec: 
  9.   ports: 
  10.   - port: 9200 
  11. name: es 
  12. protocol: TCP 
  13. targetPort: 9200 

es-endpoints.yaml

ip 地址修改为真实要对接的 ES 集群节点的 IP 地址。

  1. apiVersion: v1 
  2. kind: Endpoints 
  3. metadata: 
  4.   labels: 
  5. app: es-service 
  6.   name: es-service 
  7.   namespace: kubesphere-logging-system 
  8. subsets: 
  9. - addresses: 
  10.   - ip: 172.30.10.*** 
  11.   - ip: 172.30.10.*** 
  12.   - ip: 172.30.10.*** 
  13.   ports: 
  14.   - port: 9200 
  15. name: es 
  16. protocol: TCP 

执行命令创建自定义 SVC:

  1. $ kubectl apply -f es-service.yaml -n kubesphere-logging-system 
  2. $ kubectl apply -f es-endpoints.yaml -n kubesphere-logging-system 
  3. #查看svc 
  4. $ kubectl get svc -n kubesphere-logging-system  
  5. NAME  TYPECLUSTER-IP   EXTERNAL-IP   PORT(S)AGE 
  6. es-serviceClusterIP   109.233.8.178<none>9200/TCP   10d 
  7. #查看endpoints 
  8. $ kubectl get ep -n kubesphere-logging-system  
  9. NAMEENDPOINTS  AGE   es-service172.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)

  1. apiVersion: installer.kubesphere.io/v1alpha1 
  2. kind: ClusterConfiguration 
  3. metadata: 
  4.   labels: 
  5. version: v3.2.1 
  6.   name: ks-installer 
  7.   namespace: kubesphere-system 
  8. spec: 
  9.   alerting: 
  10. enabled: false 
  11.   auditing: 
  12. enabled: truefalse改为true 
  13.   ... 
  14. es: 
  15.   basicAuth: 
  16. enabled: true  # false改为true 
  17. password: '****'   # 密码 
  18. username: '****'   # 用户名 
  19.   data: 
  20. volumeSize: 20Gi 
  21.   elkPrefix: sit   #开发:dev 测试:sit 生产:prod 
  22.   externalElasticsearchPort: '9200' # 端口 
  23.   externalElasticsearchUrl: es-service.kubesphere-logging-system.svc # 修改es-service 
  24.   logMaxAge: 7 #默认7天即可   
  25.   master: 
  26. volumeSize: 4Gi 
  27. ... 

执行命令检查安装过程

  1. $ kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l app=ks-install -o jsonpath='{.items[0].metadata.name}') -f 
  2. #日志出现以下内容说明重启成功 
  3. Collecting installation results ... 
  4. ##################################################### 
  5. ###  Welcome to KubeSphere!   ### 
  6. ##################################################### 
  7. Console: http://172.30.9.xxx:30880 
  8. Account: admin 
  9. Password: P@88w0rd 
  10. NOTES: 
  11.   1. After you log into the console, please check the 
  12.  monitoring status of service components in 
  13.  "Cluster Management". If any service is not 
  14.  ready, please wait patiently until all components  
  15.  are up and running. 
  16.   2. Please change the default password after login. 
  17. ##################################################### 
  18. https://kubesphere.io 2022-08-04 15:53:14 
  19. ##################################################### 

执行命令,查看对应的 ConfigMap 配置:

  1. $ kubectl get configmap kubesphere-config -n kubesphere-system -o yaml   
  2. #重点如下,看es的配置是否已经生效,host是否为自定义SVC,以及用户名密码索引是否正确 
  3. logging: 
  4.   host: http://es-service.kubesphere-logging-system.svc:9200 
  5.   basicAuth: True 
  6.   username: "****"  #此处为你填写的正确用户名 
  7.   password: "****"  #此处为你填写的正确密码 
  8.   indexPrefix: ks-sit-log  #不同环境对应dev\sit\prod 
  9. auditing: 
  10.   enable: true 
  11.   webhookURL: https://kube-auditing-webhook-svc.kubesphere-logging-system.svc:6443/audit/webhook/event 
  12.   host: http://es-service.kubesphere-logging-system.svc:9200 
  13.   basicAuth: True 
  14.   username: "****"  #此处为你填写的正确用户名 
  15.   password: "****"  #此处为你填写的正确密码 
  16.   indexPrefix: ks-sit-auditing #不同环境对应dev\sit\prod 

执行命令编辑对应 output(如果已自动修改则不需要手动修改):

  • 修改 host
  • 修改索引 (开发、测试、生产前缀分别对应 ks-dev-、ks-sit-、ks-prod-)
  1. #修改es的output 
  2. $ kubectl edit output es -n kubesphere-logging-system 
  3. #修改host 和 logstashPrefix 
  4. # host: es-service.kubesphere-logging-system.svc 
  5. # logstashPrefix: ks-对应环境-log 
  6. #如下: 
  7. spec: 
  8.   es: 
  9. generateID: true 
  10. host: es-service.kubesphere-logging-system.svc  # host地址 
  11. httpPassword: 
  12.   valueFrom: 
  13. secretKeyRef: 
  14.   key: password 
  15.   name: elasticsearch-credentials 
  16. httpUser: 
  17.   valueFrom: 
  18. secretKeyRef: 
  19.   key: username 
  20.   name: elasticsearch-credentials 
  21. logstashFormat: true 
  22. logstashPrefix: ks-sit-log  # 修改此处为对应环境的日志索引 
  23. port: 9200 
  24. timeKey: '@timestamp' 
  25.   matchRegex: '(?:kube|service)\.(.*)' 
  26. #修改es-auditing的output 
  27. $ kubectl edit output es-auditing -n kubesphere-logging-system 
  28. #修改host 和 logstashPrefix 
  29. # host: es-service.kubesphere-logging-system.svc 
  30. # logstashPrefix: ks-对应环境-auditing 
  31. #如下 
  32. spec: 
  33.   es: 
  34. generateID: true 
  35. host: es-service.kubesphere-logging-system.svc  # host地址 
  36. httpPassword: 
  37.   valueFrom: 
  38. secretKeyRef: 
  39.   key: password 
  40.   name: elasticsearch-credentials 
  41. httpUser: 
  42.   valueFrom: 
  43. secretKeyRef: 
  44.   key: username 
  45.   name: elasticsearch-credentials 
  46. logstashFormat: true 
  47. logstashPrefix: ks-sit-auditing   # 修改此处为对应环境的日志索引 
  48. port: 9200 
  49.   match: kube_auditing 

重启 ks-apiserver


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

验证

  1. $ kubectl get po -n kubesphere-logging-system  
  2. NAME  READY   STATUS  RESTARTS   AGE 
  3. elasticsearch-logging-curator-elasticsearch-curator-276864h2xt2   0/1 Error   0  38h 
  4. elasticsearch-logging-curator-elasticsearch-curator-276864wc6bs   0/1 Completed   0  38h 
  5. elasticsearch-logging-curator-elasticsearch-curator-276879865wl   0/1 Completed   0  14h 
  6. elasticsearch-logging-curator-elasticsearch-curator-276879l7xpf   0/1 Error   0  14h 
  7. fluent-bit-4vzq5  1/1 Running 0  47h 
  8. fluent-bit-6ckvm  1/1 Running 0  25h 
  9. fluent-bit-6jt8d  1/1 Running 0  47h 
  10. fluent-bit-88crg  1/1 Running 0  47h 
  11. fluent-bit-9ps6z  1/1 Running 0  47h 
  12. fluent-bit-djhtx  1/1 Running 0  47h 
  13. fluent-bit-dmpfv  1/1 Running 0  47h 
  14. fluent-bit-dtr7z  1/1 Running 0  47h 
  15. fluent-bit-flxbt  1/1 Running 0  47h 
  16. fluent-bit-fnxdk  1/1 Running 0  47h 
  17. fluent-bit-gqbrl  1/1 Running 0  47h 
  18. fluent-bit-kbzsj  1/1 Running 0  47h 
  19. fluent-bit-lbnnh  1/1 Running 0  47h 
  20. fluent-bit-nq4g8  1/1 Running 0  47h 
  21. fluent-bit-q5shz  1/1 Running 0  47h 
  22. fluent-bit-qrb7v  1/1 Running 0  47h 
  23. fluent-bit-r26fk  1/1 Running 0  47h 
  24. fluent-bit-rfrpd  1/1 Running 0  47h 
  25. fluent-bit-s8869  1/1 Running 0  47h 
  26. fluent-bit-sp5k4  1/1 Running 0  47h 
  27. fluent-bit-vjvhl  1/1 Running 0  47h 
  28. fluent-bit-xkksv  1/1 Running 0  47h 
  29. fluent-bit-xrlz4  1/1 Running 0  47h 
  30. fluentbit-operator-745bf5559f-vnz8w   1/1 Running 0  47h 
  31. kube-auditing-operator-84857bf967-ftbjr   1/1 Running 0  47h 
  32. kube-auditing-webhook-deploy-64cfb8c9f8-hf8g8 1/1 Running 0  47h 
  33. kube-auditing-webhook-deploy-64cfb8c9f8-zf4rd 1/1 Running 0  47h 
  34. logsidecar-injector-deploy-5fb6fdc6dd-fj5vm   2/2 Running 0  47h 
  35. logsidecar-injector-deploy-5fb6fdc6dd-qbhdg   2/2 Running 0  47h 

日志接收器:

查询审计日志:

相关文章

站点信息