1. 首页 > KubeSphere教程 > 正文

KubeSphere教程FG018-KubeSphere自定义监控面板与指标配置实战

内容简介

本文档详细介绍KubeSphere自定义监控面板与指标配置的实战操作,包括监控面板的核心概念、KubeSphere监控系统架构、自定义监控面板的创建与配置、自定义指标的采集与配置等内容。风哥教程参考KubeSphere官方文档监控相关章节,将官方内容转化为生产实用指南。

通过本文的学习,读者将掌握如何在KubeSphere中创建和配置自定义监控面板,以及如何配置自定义指标,实现对集群和应用的全面监控,提高系统的可靠性和稳定性。

目录大纲

Part01-基础概念与理论知识

1.1 监控面板核心概念

监控面板是可视化监控数据的重要工具,核心概念包括:

  • 监控面板:展示监控数据的界面,由多个图表组成
  • 图表:展示监控数据的可视化组件,如折线图、柱状图、饼图等
  • 数据源:提供监控数据的来源,如Prometheus、Elasticsearch等
  • 查询:从数据源获取数据的语句,如PromQL查询
  • 变量:用于动态过滤和参数化查询的变量
  • 仪表盘:多个监控面板的集合

1.2 KubeSphere监控系统架构

KubeSphere监控系统架构包括以下组件:

  • Prometheus:负责监控指标的采集和存储
  • Grafana:负责监控数据的可视化
  • Node Exporter:负责采集节点级别的监控指标
  • Kube State Metrics:负责采集Kubernetes资源的监控指标
  • Custom Metrics:负责采集自定义应用的监控指标
  • KubeSphere监控组件:提供统一的监控管理界面

1.3 监控指标类型与采集方式

监控指标类型与采集方式:

  • 监控指标类型:
    • 计数器(Counter):单调递增的指标,如请求数、错误数等
    • 仪表盘(Gauge):可以上下浮动的指标,如CPU使用率、内存使用量等
    • 直方图(Histogram):分布在不同 buckets 中的观测值,如响应时间分布
    • 摘要(Summary):观测值的分位数,如P95、P99响应时间
  • 采集方式:
    • 拉取(Pull):监控系统主动从目标获取指标,如Prometheus
    • 推送(Push):目标主动将指标推送到监控系统,如StatsD
    • 代理(Agent):在目标上部署代理,采集指标后发送到监控系统

Part02-生产环境规划与建议

2.1 监控面板规划

监控面板规划需要考虑以下因素:

  • 面板类型:
    • 全局监控面板:展示整个集群的状态
    • 节点监控面板:展示单个节点的状态
    • 应用监控面板:展示单个应用的状态
    • 业务监控面板:展示业务指标的状态
  • 面板内容:
    • 关键指标:如CPU、内存、磁盘、网络等
    • 业务指标:如请求数、响应时间、错误率等
    • 自定义指标:根据应用特性定义的指标
  • 面板布局:
    • 按功能分组:将相关指标放在一起
    • 按重要性排序:将重要指标放在显眼位置
    • 保持简洁:避免面板过于复杂

2.2 自定义指标规划

自定义指标规划需要考虑以下因素:

  • 指标选择:
    • 业务关键指标:如订单量、用户数等
    • 应用性能指标:如响应时间、QPS等
    • 资源使用指标:如连接池使用率、缓存命中率等
  • 指标命名:
    • 使用统一的命名规范:如{service}_{metric}_{unit}
    • 避免使用特殊字符:只使用字母、数字和下划线
    • 保持命名简洁明了:便于理解和查询
  • 采集频率:
    • 高频指标:15-60秒采集一次
    • 中频指标:1-5分钟采集一次
    • 低频指标:5-15分钟采集一次

2.3 性能与存储规划

性能与存储规划需要考虑以下因素: 风哥提示:

  • 存储需求:
    • 根据指标数量和采集频率计算存储需求
    • 设置合理的数据保留策略
    • 考虑使用远程存储,如Thanos、Cortex等
  • 性能优化:
    • 使用合适的采集频率,避免过度采集
    • 使用指标聚合,减少存储和查询压力
    • 优化查询语句,提高查询性能
  • 高可用性:
    • 部署高可用的监控系统
    • 配置数据备份,确保数据安全
    • 设置监控系统自身的监控

Part03-生产环境项目实施方案

3.1 自定义监控面板创建

自定义监控面板创建的步骤如下:

# 步骤1:登录KubeSphere控制台
# 步骤2:进入项目
# 步骤3:选择监控与告警 -> 监控面板
# 步骤4:点击创建
# 步骤5:填写面板信息
# 名称:自定义应用监控面板
# 描述:应用性能监控面板
# 点击确定
# 步骤6:添加图表
# 点击添加图表
# 填写图表信息
# 名称:CPU使用率
# 数据源:Prometheus
# 查询:sum(rate(container_cpu_usage_seconds_total{namespace=”fgedu-project”,pod=”fgedu-app”}[5m])) by (pod)
# 图表类型:折线图
# 点击保存

3.2 自定义指标配置

自定义指标配置的步骤如下: 学习交流加群风哥微信: itpux-com

# 步骤1:编写自定义指标采集代码
# 以Python为例
cat > custom-metrics-exporter.py << EOF
#!/usr/bin/env python3
from prometheus_client import start_http_server, Counter, Gauge
import time
import random
from:www.itpux.com.qq113257174.wx:itpux-com
web: `http://www.fgedu.net.cn`
# 创建指标
REQUEST_COUNT = Counter(‘fgedu_app_requests_total’, ‘Total number of requests’)
REQUEST_LATENCY = Gauge(‘fgedu_app_request_latency_seconds’, ‘Request latency in seconds’)
ACTIVE_USERS = Gauge(‘fgedu_app_active_users’, ‘Number of active users’)
def generate_metrics():
while True:
# 模拟请求
REQUEST_COUNT.inc()
# 模拟响应时间
latency = random.uniform(0.1, 1.0)
REQUEST_LATENCY.set(latency)
# 模拟活跃用户
active_users = random.randint(10, 100)
ACTIVE_USERS.set(active_users)
time.sleep(1)
if __name__ == ‘__main__’:
# 启动HTTP服务器
start_http_server(8000)
# 生成指标
generate_metrics()
EOF

# 步骤2:部署自定义指标采集器
cat > custom-metrics-exporter.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: custom-metrics-exporter
namespace: fgedu-project
spec:
replicas: 1
selector:
matchLabels:
app: custom-metrics-exporter
template:
metadata:
labels:
app: custom-metrics-exporter
annotations:
prometheus.io/scrape: “true”
prometheus.io/port: “8000”
spec:
containers:
– name: custom-metrics-exporter
image: python:3.9
command: [“python”, “/app/custom-metrics-exporter.py”]
volumeMounts:
– name: app
mountPath: /app
volumes:
– name: app
configMap:
name: custom-metrics-exporter

apiVersion: v1
kind: ConfigMap
metadata:
name: custom-metrics-exporter
namespace: fgedu-project
data:
custom-metrics-exporter.py: |
#!/usr/bin/env python3
from prometheus_client import start_http_server, Counter, Gauge
import time
import random
# 创建指标
REQUEST_COUNT = Counter(‘fgedu_app_requests_total’, ‘Total number of requests’)
REQUEST_LATENCY = Gauge(‘fgedu_app_request_latency_seconds’, ‘Request latency in seconds’)
ACTIVE_USERS = Gauge(‘fgedu_app_active_users’, ‘Number of active users’)
def generate_metrics():
while True:
# 模拟请求
REQUEST_COUNT.inc()
# 模拟响应时间
latency = random.uniform(0.1, 1.0)
REQUEST_LATENCY.set(latency)
# 模拟活跃用户
active_users = random.randint(10, 100)
ACTIVE_USERS.set(active_users)
time.sleep(1)
if __name__ == ‘__main__’:
# 启动HTTP服务器
start_http_server(8000)
# 生成指标
generate_metrics()

apiVersion: v1
kind: Service
metadata:
name: custom-metrics-exporter
namespace: fgedu-project
spec:
selector:
app: custom-metrics-exporter
ports:
– port: 8000
targetPort: 8000
EOF

kubectl apply -f custom-metrics-exporter.yaml
deployment.apps/custom-metrics-exporter created
configmap/custom-metrics-exporter created
service/custom-metrics-exporter created

3.3 监控面板布局与设计

监控面板布局与设计的步骤如下:

# 步骤1:登录KubeSphere控制台
# 步骤2:进入项目
# 步骤3:选择监控与告警 -> 监控面板
# 步骤4:选择要编辑的面板
# 步骤5:调整面板布局
# 拖拽图表调整位置
# 调整图表大小
# 步骤6:添加变量
# 点击变量设置
# 添加变量:namespace
# 数据源:Prometheus
# 查询:kubernetes_namespace_info
# 步骤7:更新图表查询
# 将查询中的namespace固定值替换为变量:${namespace}
# 步骤8:保存面板

Part04-生产案例与实战讲解

4.1 自定义监控面板创建实战

自定义监控面板创建的实战案例: 学习交流加群风哥QQ113257174

# 1. 创建全局监控面板
# 步骤:登录KubeSphere控制台 -> 集群管理 -> 监控告警 -> 监控面板 -> 创建
# 名称:集群全局监控
# 描述:集群整体状态监控

# 2. 添加集群资源使用图表
# 点击添加图表
# 名称:集群CPU使用率
# 数据源:Prometheus
# 查询:sum(rate(node_cpu_seconds_total{mode!=”idle”}[5m])) / sum(rate(node_cpu_seconds_total[5m])) * 100
# 图表类型:折线图
# 点击保存

# 3. 添加集群内存使用率图表
# 点击添加图表
# 名称:集群内存使用率
# 数据源:Prometheus
# 查询:sum(node_memory_MemTotal_bytes) – sum(node_memory_MemAvailable_bytes) / sum(node_memory_MemTotal_bytes) * 100
# 图表类型:折线图
# 点击保存

# 4. 添加集群磁盘使用率图表
# 点击添加图表
# 名称:集群磁盘使用率
# 数据源:Prometheus
# 查询:sum(node_filesystem_size_bytes{mountpoint=”/”}) – sum(node_filesystem_free_bytes{mountpoint=”/”}) / sum(node_filesystem_size_bytes{mountpoint=”/”}) * 100
# 图表类型:折线图
# 点击保存

4.2 自定义指标配置实战

自定义指标配置的实战案例: 更多视频教程www.fgedu.net.cn

# 1. 部署MySQL指标采集器
cat > mysql-exporter.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-exporter
namespace: fgedu-db
spec:
replicas: 1
selector:
matchLabels:
app: mysql-exporter
template:
metadata:
labels:
app: mysql-exporter
annotations:
prometheus.io/scrape: “true”
prometheus.io/port: “9104”
spec:
containers:
– name: mysql-exporter
image: prom/mysqld_exporter:v0.14.0
ports:
– containerPort: 9104
env:
– name: DATA_SOURCE_NAME
value: “exporter:exporter123@(fgedu-db:3306)/”

apiVersion: v1
kind: Service
metadata:
name: mysql-exporter
namespace: fgedu-db
spec:
selector:
app: mysql-exporter
ports:
– port: 9104
targetPort: 9104
EOF

kubectl apply -f mysql-exporter.yaml
deployment.apps/mysql-exporter created
service/mysql-exporter created

# 2. 验证指标采集
kubectl port-forward svc/mysql-exporter -n fgedu-db 9104:9104
Forwarding from 127.0.0.1:9104 -> 9104
Forwarding from [::1]:9104 -> 9104

# 访问指标端点
curl http://localhost:9104/metrics | grep mysql
# HELP mysql_global_status_commands_total Total number of executed commands
# TYPE mysql_global_status_commands_total counter
mysql_global_status_commands_total{command=”admin_commands”} 0
mysql_global_status_commands_total{command=”alter_db”} 0
mysql_global_status_commands_total{command=”alter_event”} 0
mysql_global_status_commands_total{command=”alter_function”} 0
mysql_global_status_commands_total{command=”alter_procedure”} 0
mysql_global_status_commands_total{command=”alter_server”} 0
mysql_global_status_commands_total{command=”alter_table”} 0
mysql_global_status_commands_total{command=”alter_tablespace”} 0
mysql_global_status_commands_total{command=”alter_user”} 0
mysql_global_status_commands_total{command=”analyze”} 0
mysql_global_status_commands_total{command=”begin”} 0
mysql_global_status_commands_total{command=”binlog”} 0
mysql_global_status_commands_total{command=”call_procedure”} 0
mysql_global_status_commands_total{command=”change_db”} 0
mysql_global_status_commands_total{command=”check”} 0
mysql_global_status_commands_total{command=”checksum”} 0
mysql_global_status_commands_total{command=”commit”} 0
mysql_global_status_commands_total{command=”create_db”} 0
mysql_global_status_commands_total{command=”create_event”} 0
mysql_global_status_commands_total{command=”create_function”} 0
mysql_global_status_commands_total{command=”create_index”} 0
mysql_global_status_commands_total{command=”create_procedure”} 0
mysql_global_status_commands_total{command=”create_server”} 0
mysql_global_status_commands_total{command=”create_table”} 0
mysql_global_status_commands_total{command=”create_trigger”} 0
mysql_global_status_commands_total{command=”create_udf”} 0
mysql_global_status_commands_total{command=”create_user”} 0
mysql_global_status_commands_total{command=”create_view”} 0
mysql_global_status_commands_total{command=”delete”} 0
mysql_global_status_commands_total{command=”drop_db”} 0
mysql_global_status_commands_total{command=”drop_event”} 0
mysql_global_status_commands_total{command=”drop_function”} 0
mysql_global_status_commands_total{command=”drop_index”} 0
mysql_global_status_commands_total{command=”drop_procedure”} 0
mysql_global_status_commands_total{command=”drop_server”} 0
mysql_global_status_commands_total{command=”drop_table”} 0
mysql_global_status_commands_total{command=”drop_trigger”} 0
mysql_global_status_commands_total{command=”drop_user”} 0
mysql_global_status_commands_total{command=”drop_view”} 0
mysql_global_status_commands_total{command=”execute”} 0
mysql_global_status_commands_total{command=”flush”} 0
mysql_global_status_commands_total{command=”grant”} 0
mysql_global_status_commands_total{command=”insert”} 0
mysql_global_status_commands_total{command=”kill”} 0
mysql_global_status_commands_total{command=”load”} 0
mysql_global_status_commands_total{command=”lock_tables”} 0
mysql_global_status_commands_total{command=”optimize”} 0
mysql_global_status_commands_total{command=”prepare”} 0
mysql_global_status_commands_total{command=”purge”} 0
mysql_global_status_commands_total{command=”rename_table”} 0
mysql_global_status_commands_total{command=”rename_user”} 0
mysql_global_status_commands_total{command=”repair”} 0
mysql_global_status_commands_total{command=”replace”} 0
mysql_global_status_commands_total{command=”reset”} 0
mysql_global_status_commands_total{command=”revoke”} 0
mysql_global_status_commands_total{command=”rollback”} 0
mysql_global_status_commands_total{command=”savepoint”} 0
mysql_global_status_commands_total{command=”select”} 0
mysql_global_status_commands_total{command=”set_option”} 0
mysql_global_status_commands_total{command=”show_binlogs”} 0
mysql_global_status_commands_total{command=”show_charsets”} 0
mysql_global_status_commands_total{command=”show_collations”} 0
mysql_global_status_commands_total{command=”show_create_db”} 0
mysql_global_status_commands_total{command=”show_create_event”} 0
mysql_global_status_commands_total{command=”show_create_func”} 0
mysql_global_status_commands_total{command=”show_create_proc”} 0
mysql_global_status_commands_total{command=”show_create_table”} 0
mysql_global_status_commands_total{command=”show_create_trigger”} 0
mysql_global_status_commands_total{command=”show_databases”} 0
mysql_global_status_commands_total{command=”show_engine_logs”} 0
mysql_global_status_commands_total{command=”show_engine_mutex”} 0
mysql_global_status_commands_total{command=”show_engine_status”} 0
mysql_global_status_commands_total{command=”show_events”} 0
mysql_global_status_commands_total{command=”show_function_status”} 0
mysql_global_status_commands_total{command=”show_grants”} 0
mysql_global_status_commands_total{command=”show_keys”} 0
mysql_global_status_commands_total{command=”show_master_status”} 0
mysql_global_status_commands_total{command=”show_open_tables”} 0
mysql_global_status_commands_total{command=”show_plugins”} 0
mysql_global_status_commands_total{command=”show_privileges”} 0
mysql_global_status_commands_total{command=”show_procedure_status”} 0
mysql_global_status_commands_total{command=”show_processlist”} 0
mysql_global_status_commands_total{command=”show_slave_hosts”} 0
mysql_global_status_commands_total{command=”show_slave_status”} 0
mysql_global_status_commands_total{command=”show_status”} 0
mysql_global_status_commands_total{command=”show_table_status”} 0
mysql_global_status_commands_total{command=”show_tables”} 0
mysql_global_status_commands_total{command=”show_triggers”} 0
mysql_global_status_commands_total{command=”show_variables”} 0
mysql_global_status_commands_total{command=”show_warnings”} 0
mysql_global_status_commands_total{command=”slave_start”} 0
mysql_global_status_commands_total{command=”slave_stop”} 0
mysql_global_status_commands_total{command=”truncate”} 0
mysql_global_status_commands_total{command=”unlock_tables”} 0
mysql_global_status_commands_total{command=”update”} 0
mysql_global_status_commands_total{command=”xa_commit”} 0
mysql_global_status_commands_total{command=”xa_end”} 0
mysql_global_status_commands_total{command=”xa_prepare”} 0
mysql_global_status_commands_total{command=”xa_recover”} 0
mysql_global_status_commands_total{command=”xa_rollback”} 0
mysql_global_status_commands_total{command=”xa_start”} 0

4.3 监控面板布局与设计实战

监控面板布局与设计的实战案例: 更多学习教程公众号风哥教程itpux_com

# 1. 创建应用监控面板
# 步骤:登录KubeSphere控制台 -> 进入项目 -> 监控与告警 -> 监控面板 -> 创建
# 名称:应用监控面板
# 描述:fgedu应用性能监控

# 2. 添加应用请求数图表
# 点击添加图表
# 名称:应用请求数
# 数据源:Prometheus
# 查询:sum(rate(nginx_http_requests_total{namespace=”fgedu-project”}[5m])) by (service)
# 图表类型:柱状图
# 点击保存

# 3. 添加应用响应时间图表
# 点击添加图表
# 名称:应用响应时间
# 数据源:Prometheus
# 查询:sum(rate(nginx_http_request_duration_seconds_sum{namespace=”fgedu-project”}[5m])) / sum(rate(nginx_http_request_duration_seconds_count{namespace=”fgedu-project”}[5m]))
# 图表类型:折线图
# 点击保存

# 4. 添加应用错误率图表
# 点击添加图表
# 名称:应用错误率
# 数据源:Prometheus
# 查询:sum(rate(nginx_http_requests_total{namespace=”fgedu-project”,status=~”5..”}[5m])) / sum(rate(nginx_http_requests_total{namespace=”fgedu-project”}[5m])) * 100
# 图表类型:折线图
# 点击保存

# 5. 调整面板布局
# 拖拽图表调整位置,使面板布局美观
# 调整图表大小,使数据展示清晰

4.4 多面板集成实战

多面板集成的实战案例:

# 1. 创建仪表盘
# 步骤:登录KubeSphere控制台 -> 监控与告警 -> 仪表盘 -> 创建
# 名称:系统监控仪表盘
# 描述:系统整体监控

# 2. 添加面板到仪表盘
# 点击添加面板
# 选择之前创建的面板:集群全局监控
# 点击确定

# 3. 添加应用监控面板到仪表盘
# 点击添加面板
# 选择之前创建的面板:应用监控面板
# 点击确定

# 4. 添加数据库监控面板到仪表盘
# 点击添加面板
# 选择之前创建的面板:数据库监控面板
# 点击确定

# 5. 查看仪表盘
# 点击仪表盘名称,查看集成的面板

4.5 KubeSphere数据库监控面板实战案例

KubeSphere数据库监控面板的实战案例:

# 1. 创建数据库监控面板
# 步骤:登录KubeSphere控制台 -> 进入项目 -> 监控与告警 -> 监控面板 -> 创建
# 名称:数据库监控面板
# 描述:MySQL数据库性能监控

# 2. 添加数据库连接数图表
# 点击添加图表
# 名称:数据库连接数
# 数据源:Prometheus
# 查询:mysql_global_status_threads_connected{instance=”fgedu-db:3306″}
# 图表类型:折线图
# 点击保存

# 3. 添加数据库QPS图表
# 点击添加图表
# 名称:数据库QPS
# 数据源:Prometheus
# 查询:sum(rate(mysql_global_status_commands_total{instance=”fgedu-db:3306″,command=~”select|insert|update|delete”}[5m])) by (command)
# 图表类型:柱状图
# 点击保存

# 4. 添加数据库缓存命中率图表
# 点击添加图表
# 名称:数据库缓存命中率
# 数据源:Prometheus
# 查询:(sum(mysql_global_status_qcache_hits{instance=”fgedu-db:3306″}) / (sum(mysql_global_status_qcache_hits{instance=”fgedu-db:3306″}) + sum(mysql_global_status_qcache_inserts{instance=”fgedu-db:3306″}))) * 100
# 图表类型:折线图
# 点击保存

# 5. 添加数据库慢查询图表
# 点击添加图表
# 名称:数据库慢查询
# 数据源:Prometheus
# 查询:sum(rate(mysql_global_status_slow_queries{instance=”fgedu-db:3306″}[5m]))
# 图表类型:折线图
# 点击保存

Part05-风哥经验总结与分享

5.1 自定义监控面板最佳实践

在创建和配置自定义监控面板时,应根据实际需求设计面板内容和布局,确保监控数据的可读性和实用性。 from K8S+DB视频:www.itpux.com

  • 根据监控目标设计面板内容,避免面板过于复杂
  • 使用合适的图表类型,如折线图适合趋势分析,柱状图适合比较分析
  • 设置合理的时间范围,便于观察不同时间段的监控数据
  • 使用变量功能,提高面板的灵活性和可重用性
  • 定期更新面板内容,确保监控数据的时效性
  • 分享面板给相关团队成员,提高团队协作效率

5.2 自定义指标采集最佳实践

  • 指标设计:
    • 选择与业务相关的关键指标
    • 使用统一的命名规范
    • 避免采集过多的指标,影响性能
  • 采集配置:
    • 设置合理的采集频率
    • 使用轻量级的采集工具
    • 配置适当的指标过滤规则
  • 存储与查询:
    • 设置合理的数据保留策略
    • 使用指标聚合,减少存储压力
    • 优化查询语句,提高查询性能

5.3 常见问题与解决方案

在配置和使用自定义监控面板与指标时,常见的问题包括指标采集失败、面板显示异常、查询性能慢等,需要根据具体情况进行排查和解决。

  • 问题:指标采集失败
    • 解决方案:检查采集器配置;检查网络连接;检查指标端点是否可访问
  • 问题:面板显示异常
    • 解决方案:检查查询语句是否正确;检查数据源配置;检查网络连接
  • 问题:查询性能慢
    • 解决方案:优化查询语句;增加查询时间范围;使用指标聚合
  • 问题:指标数据不准确
    • 解决方案:检查采集器配置;检查指标定义;检查数据源
  • 问题:面板加载缓慢
    • 解决方案:减少面板中的图表数量;优化查询语句;增加监控系统资源

,自定义监控面板与指标配置是监控系统的重要组成部分,建议根据实际需求设计和配置,确保监控数据的准确性和实用性。

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息