KubeSphere 接入外部 Elasticsearch 最佳实践

news2025/4/10 22:42:32

作者:张坚,科大讯飞开发工程师,云原生爱好者。

大家好,我是张坚。今天来聊聊如何在 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

日志接收器:

查询审计日志:

本文由博客一文多发平台 OpenWrite 发布!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/74583.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

索引优化学习

背景 最近做查询优化&#xff0c;学到的。字段长度&#xff0c;索引长度联合索引计算是否使用范围查询使用索引 字段长度&#xff08;varchar&#xff09; 只谈论varchar&#xff1a;首先我们建表varchar(20) 中的20是字符数。看你的数据库编码 执行&#xff1a;show creat…

支持多种网关类型!米尔基于Zynq-7010/20开发平台工业网关设计应用

随着工业物联网的飞速的发展&#xff0c;5G时代的到来&#xff0c;工业控制系统在生产领域应用越来越广泛&#xff0c;工业物联网为未来工业控制系统灵活性和可扩展性的需求提供了支持。工业物联网使我们的生产数据可以进行规模化集中存储&#xff0c;并利用高速采集、云计算等…

ChatGPT国产平替出现了:APP商店就能下载,还可给AI加人设,背后公司刚成立3个月...

明敏 发自 凹非寺量子位 | 公众号 QbitAIChatGPT太火爆谁不想上手试试&#xff1f;但注册复杂、服务器拥挤……着实有点麻烦。不过很快就有极客网友指路&#xff0c;说国内其实已经有类似的APP上线了&#xff0c;也是上知天文下知地理的那种。比如聊聊《三体》&#xff0c;还会…

Transformer 训练优化

前言 自 BERT 出现以来&#xff0c;NLP 领域已经进入了大模型的时代&#xff0c;大模型虽然效果好&#xff0c;但是毕竟不是人人都有着丰富的 GPU 资源&#xff0c;在训练时往往就捉襟见肘&#xff0c;出现显存 out of memory 的问题&#xff0c;或者训练时间非常非常的久&…

web期末大作业:基于html+css+js制作深圳大学网站(13页) 学校班级网页制作模板 学生静态HTML网页源码

&#x1f389;精彩专栏推荐 &#x1f4ad;文末获取联系 ✍️ 作者简介: 一个热爱把逻辑思维转变为代码的技术博主 &#x1f482; 作者主页: 【主页——&#x1f680;获取更多优质源码】 &#x1f393; web前端期末大作业&#xff1a; 【&#x1f4da;毕设项目精品实战案例 (10…

【easypoi 模板导出嵌套 list 问题】

easypoi 模板导出多 list 问题1背景1.1 espoi 模板定义1.2 导出结果1.3发现共享单车的数据没有显示&#xff0c;手动操作取消共享单车的单元格合并1.4手动取消单元格合并后数据&#xff0c;正常再把框线画好1.5 代码操作&#xff1a;用 esaypoi 处理到1.2&#xff0c;用基础 po…

聊聊与前端工程师天然互补的 Serverless

作为前端工程师&#xff0c;我们的使命是为用户提供良好的前端用户体验。随着云原生时代的到来&#xff0c;显而易见的&#xff0c;我们能做的更多了。Serverless 产品的特点是免运维、按量付费和自适应弹性&#xff0c;所以我们可以利用云上的各种 Serverless 能力&#xff0c…

《程序员的自我修养》程序实现的两大环境

学习内容 翻译环境 预处理 编译 汇编 链接 执行环境 在标准C的任何一种实现中&#xff0c;都存在两个不同的环境&#xff1a; 1.翻译环境&#xff1a;在这个环境中&#xff0c;源代码被翻译成为可执行的机器指令。 2.执行环境&#xff1a;用于执行实际的代码 在VS2022中&…

字符串的读入方式

文章目录1、scanf2、fgets()3、cin4、cin.getline()5、getline()1、scanf scanf只能读入不带空格的字符串&#xff0c;遇到空格则结束。scanf只能读入字符数组&#xff0c;不能读入string。scanf在读入的时候&#xff0c;会自动在字符串的末尾加上’\0’。定义字符数组长度时&…

虚表指针在C++类的继承/多态与重载中的基本逻辑

文章目录前言重载继承虚函数虚函数表动态绑定的实现析构函数构造函数多态子类直接继承父类的方法&#xff0c;不覆盖多重继承纯虚函数前言 C在C语言的基础上增加了类的概念&#xff0c;而类的最关键的特性就是三个&#xff1a; 继承多态重载 这篇文章想接着上两篇C相关的文章…

[附源码]Python计算机毕业设计SSM基于推荐算法的汽车租赁系统(程序+LW)

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

2023最新SSM计算机毕业设计选题大全(附源码+LW)之java动物爱心救助平台s9dza

首先选择计算机题目的时候先看定什么主题&#xff0c;一般的话都选择当年最热门的话题进行组题&#xff0c;就比如说&#xff0c;今年的热门话题有奥运会&#xff0c;全运会&#xff0c;残运会&#xff0c;或者疫情相关的&#xff0c;这些都是热门话题&#xff0c;所以你就可以…

HTML做一个抗疫逆行者感动人物页面(纯html代码)

&#x1f389;精彩专栏推荐 &#x1f4ad;文末获取联系 ✍️ 作者简介: 一个热爱把逻辑思维转变为代码的技术博主 &#x1f482; 作者主页: 【主页——&#x1f680;获取更多优质源码】 &#x1f393; web前端期末大作业&#xff1a; 【&#x1f4da;毕设项目精品实战案例 (10…

AI公平性研究进展(2022)

最近AI公平性方面出了不少新的研究成果&#xff0c;如有遗漏&#xff0c;欢迎补充↓↓↓↓ 公平性提升 MAAT: A Novel Ensemble Approach to Addressing Fairness and Performance Bugs for Machine Learning Software, FSE, 2022. 利用模型集成的方式获得公平的预测结果&am…

Windows下如何查看某个端口被谁占用

开发时经常遇到端口被占用的情况&#xff0c;这个时候我们就需要找出被占用端口的程序&#xff0c;然后结束它&#xff0c;本文为大家介绍如何查找被占用的端口。 1、打开命令窗口(以管理员身份运行) 开始—->运行—->cmd&#xff0c;或者是 windowR 组合键&#xff0c…

含冰蓄冷空调的冷热电联供型微网多时间尺度优化调度(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

开关电源 - LLC电路的谐振工作模态浅析

LLC电路的谐振工作模态浅析 LLC谐振电路是常用的拓扑&#xff0c;广泛应用在目前的热门应用中&#xff0c;本文主要从典型谐振状态来分析其基本工作过程&#xff0c;后续我们将逐步扩展到其它工作状态。 一、电路工作基本条件及电路组成 图1 电路主要组成部分 如上图1所示&a…

c#入门-运算符

可用运算符 值的运算也是自定义的&#xff0c;但是这个就看不出了。 要么翻源文件&#xff0c;要么翻说明书才知道一个类型能怎么运算。 但是一个默认情况下的类型&#xff0c;是没有定义任何运算符的。 也就是说你适用的运算一定是由人先写出来的&#xff0c;没有系统默认的…

MYSQL的索引事务

文章目录1&#xff1a;索引1.1&#xff1a;概念&#xff1a;1.2 作用&#xff1a;1.3 使用场景&#xff1a;1.4 使用&#xff1a;补充&#xff1a;**2&#xff1a;了解一下B树&#xff1a;****3&#xff1a;了解一下B树**4&#xff1a;为什么使用B树/B树来实现索引2&#xff1a…

通过IP地址如何查到用户真实个人信息

当人们使用服务时&#xff0c;互联网上所有科技巨头公司都会收集大量的数据&#xff0c;他们就是这样发财的。微博知道您的朋友是谁、您 “喜欢” 什么&#xff0c;以及您在新闻源上阅读的内容种类&#xff1b;百度知道您的想法、您想知道什么、以及任何时候的去向&#xff1b;…