KHI无代理部署:终极指南教你快速配置和使用
KHI无代理部署:终极指南教你快速配置和使用
【免费下载链接】khiA log viewer for Kubernetes troubleshooting项目地址: https://gitcode.com/gh_mirrors/kh/khi
Kubernetes History Inspector(KHI)是一款专为Kubernetes故障排查设计的日志查看工具,支持无代理模式部署,让你无需复杂的集群侵入即可实现高效日志分析。本文将带你通过简单步骤完成KHI的本地部署与使用,轻松掌握Kubernetes资源变更追踪与问题诊断技巧。
准备工作:部署前的环境检查 📋
在开始KHI部署前,请确保你的系统已安装以下工具:
- Docker 或 Podman 容器运行环境
- kind 用于创建本地Kubernetes集群
- Helm 包管理工具
- LogCLI Loki日志查询工具
这些工具将帮助你快速搭建测试环境并收集必要的Kubernetes审计日志,为KHI分析提供数据基础。
第一步:创建带审计日志的Kubernetes集群 ⚙️
配置审计策略文件
首先创建审计策略目录和配置文件,定义Kubernetes API服务器需要记录的日志级别:
# audit-policy/audit-policy.yaml apiVersion: audit.k8s.io/v1 kind: Policy rules: - level: Metadata resources: - group: "" # core API group resources: ["configmaps", "secrets"] - level: RequestResponse此配置将记录敏感资源的元数据和所有其他资源的完整请求/响应数据,为KHI提供丰富的分析素材。
使用kind创建集群
创建kind配置文件kind-config.yaml,挂载审计策略并配置API服务器日志路径:
# kind-config.yaml kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 nodes: - role: control-plane extraMounts: - hostPath: audit-policy/ containerPath: /etc/kubernetes/audit readOnly: true kubeadmConfigPatches: - | apiVersion: kubeadm.k8s.io/v1beta3 kind: ClusterConfiguration apiServer: extraArgs: audit-policy-file: "/etc/kubernetes/audit/audit-policy.yaml" audit-log-path: "/var/log/kubernetes/audit.log" extraVolumes: - name: audit-config hostPath: /etc/kubernetes/audit mountPath: /etc/kubernetes/audit readOnly: true - name: audit-logs hostPath: /var/log/kubernetes mountPath: /var/log/kubernetes readOnly: false - role: worker - role: worker - role: worker执行以下命令创建集群:
kind create cluster --config kind-config.yaml第二步:部署Loki与Fluent Bit日志收集 📊
安装Loki日志存储
创建Loki配置文件loki-values.yaml,使用Helm快速部署:
# loki-values.yaml loki: commonConfig: replication_factor: 1 schemaConfig: configs: - from: "2025-01-01" store: tsdb object_store: s3 schema: v13 index: prefix: loki_index_ period: 24h minio: enabled: true deploymentMode: SingleBinary singleBinary: replicas: 1执行部署命令:
helm repo add grafana https://grafana.github.io/helm-charts helm repo update helm install loki grafana/loki -f loki-values.yaml --namespace khi --create-namespace配置Fluent Bit日志收集
创建Fluent Bit配置文件fluentbit-values.yaml,确保收集控制平面审计日志:
# fluentbit-values.yaml tolerations: - key: node-role.kubernetes.io/control-plane operator: Exists effect: NoSchedule config: inputs: | [INPUT] Name tail Path /var/log/containers/*.log multiline.parser cri Tag kube.* [INPUT] Name tail Tag kubevar.audit Path /mnt/audit/audit.log Parser json filters: | [FILTER] Name modify Match kubevar.audit Add job audit outputs: | [OUTPUT] Name loki Match * Host loki-gateway.khi.svc.cluster.local Port 80 Label_Keys $job extraVolumes: - name: auditlog hostPath: path: /var/log/kubernetes extraVolumeMounts: - name: auditlog mountPath: /mnt/audit readOnly: true执行部署命令:
helm repo add fluent https://fluent.github.io/helm-charts helm install fluentbit fluent/fluent-bit --values fluentbit-values.yaml --namespace khi第三步:生成测试数据并导出日志 📝
执行以下Kubernetes操作生成审计日志:
kubectl create deployment nginx --image nginx --replicas 3 kubectl scale deployment nginx --replicas 5 kubectl scale deployment nginx --replicas 1 kubectl delete deployment nginx通过LogCLI导出日志到文件:
kubectl port-forward --namespace khi service/loki-gateway 8000:80 & logcli query '{job="audit"}' \ --org-id=KHI \ --from="2025-04-08T00:00:00Z" \ --to="2025-04-09T00:00:00Z" \ --output=raw \ --limit=0 \ --addr=http://localhost:8000 \ -q > audit_log_export.jsonl第四步:启动KHI并上传日志分析 🚀
运行KHI容器
使用Docker快速启动KHI服务:
docker run --rm -p 127.0.0.1:8080:8080 gcr.io/kubernetes-history-inspector/release:latest访问 http://localhost:8080 打开KHI界面,点击"New Inspection"开始新分析。
创建无代理检查任务
在新建检查页面选择"OSS Kubernetes Log Files"类型,这是专为无代理环境设计的日志分析模式:
配置检查参数
在参数输入页面上传之前导出的audit_log_export.jsonl文件,设置时间范围并点击"Run"开始分析:
第五步:探索KHI的强大功能 🔍
时间线视图分析
检查完成后,KHI会生成直观的资源时间线,展示Nginx部署从创建到删除的完整生命周期:
时间线视图提供多资源宏观历史视角,可通过过滤控件聚焦关注的资源类型,轻松追踪集群变更轨迹。
日志详情查看
点击时间线上的事件可查看详细日志信息,包括事件类型、严重级别和原始日志内容:
日志视图采用三栏布局,左侧为日志列表,中间显示选中日志的元数据,右侧展示完整日志内容,帮助你快速定位问题根源。
清理与总结 🧹
完成测试后,使用以下命令清理环境:
kind delete clusterKHI的无代理部署模式为Kubernetes故障排查提供了灵活轻量的解决方案,特别适合无法在集群内部署代理的场景。通过本文介绍的步骤,你可以在本地环境快速搭建完整的KHI分析平台,利用Kubernetes审计日志实现高效的资源变更追踪和问题诊断。
官方文档:docs/en/setup-guide/oss-kubernetes-clusters.md
【免费下载链接】khiA log viewer for Kubernetes troubleshooting项目地址: https://gitcode.com/gh_mirrors/kh/khi
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
