架构层级 | 核心组件 |
---|---|
控制平面层 | etcd、API Server、Scheduler、Controller Manager |
工作节点层 | Kubelet、Kube-proxy、CRI(容器运行时接口)、CNI(网络插件)、CSI(存储插件) |
资源对象层 | Pod、Deployment、StatefulSet、Horizontal Pod Autoscaler |
扩展插件层 | CoreDNS、Ingress Controller、KEDA(事件驱动自动扩缩)、Argo Rollouts |
核心设计要点:
route:
group_by: [appid, alertname]
group_wait: 30s
group_interval: 5m
repeat_interval: 3h
receiver: 'default-receiver'
关键实践:
# 添加 Helm 仓库
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
# 下载 Chart 包(注意 k8s 和 Prometheus-Operator 的对应关系)
helm pull prometheus-community/kube-prometheus-stack --version 69.8.2
tar -xvf kube-prometheus-stack-69.8.2.tgz
cd kube-prometheus-stack/
# 镜像加速
chmod +x update_registry.sh
./update_registry.sh
# 安装
helm -n monitoring install kube-prometheus-stack ./ --create-namespace
镜像加速方案:
完整脚本见附录
#!/bin/bash
# 自动化镜像地址替换脚本 demo
find ./ -type f -name "*.yaml" -exec sed -i \
-e 's|registry.k8s.io|m.daocloud.io/registry.k8s.io|g' \
-e 's|quay.io|m.daocloud.io/quay.io|g' \
-e 's|docker.io|m.daocloud.io/docker.io|g' {} \;
采集架构:
PrometheusServiceMonitorServiceEndpointPod
故障排查路径:
主要都是 kube-state-metrics
收集的, K8s 内置的资源对象 , 只需要添加启动参数即可
- --metric-labels-allowlist=nodes=[env],deployments=[appid],pods=[appid],services=[appid]
Argo Rollouts 指标采集配置:
# customresourcestate-argo.yaml
resources:
- groupVersionKind:
group: argoproj.io
version: v1alpha1
kind: Rollout
metrics:
- name: argo_rollout_appid
help: "Argo Rollout application identifier"
each:
type: Info
info:
labelsFromPath:
exported_namespace: [metadata, namespace]
metricLabels:
appid: .metadata.labels.appid
实施步骤:
1. 创建 ConfigMap 存储采集配置
kubectl -n monitoring create configmap customresourcestate-config --from-file=customresourcestate-argo.yaml
2. 扩展 Kube-State-Metrics RBAC 权限
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: kube-state-metrics-argo
rules:
- apiGroups: ["apiextensions.k8s.io"]
resources: ["customresourcedefinitions"]
verbs: ["list", "watch"]
- apiGroups: ["argoproj.io"]
resources: ["rollouts"]
verbs: ["list", "watch"]
3. 挂载配置文件到 KSM Pod,添加启动参数 --custom-resource-state-config-file
containers:
- args:
- --custom-resource-state-config-file=/etc/config/customresourcestate-argo.yaml
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: customresourcestate-config
关联查询示例:
# 获取带有 AppID 的 Rollout 副本可用率
kube_argo_rollouts_status_replicas_available
* on(namespace, rollout_name) group_left(appid)
kube_customresource_argo_rollout_appid
核心功能模块:
关键 PromQL 集锦:
使用的关键 promql 函数 count
、unless
、sum
、 group_left
、sum
、max
、label_replace
、rate
、avg
、min_over_time
监控维度:
可视化设计原则:
每日数据量 = 指标数量 × 采集频率 × 24h × 保留天数
通过以上架构设计和实践方案,可构建覆盖基础设施、Kubernetes 核心组件、业务应用的立体化监控体系,为容器化业务提供全方位可观测性保障。
附录:
镜像加速脚本
#!/bin/bash
# 检测操作系统类型
if [[ "$(uname)" == "Darwin" ]]; then
# macOS
SED_CMD="sed -i ''"
else
# Linux 和其他
SED_CMD="sed -i"
fi
# 查找当前目录及子目录下的所有 YAML 文件
find . -type f -name "values.yaml" -o -name "values.yml" | while read yaml_file; do
echo "处理文件: $yaml_file"
# 使用 awk 处理整个文件,以处理隔行的 registry 和 repository
awk -v file="$yaml_file" -v sed_cmd="$SED_CMD" '
BEGIN { registry = ""; in_block = 0; }
/registry:/ {
# 提取 registry 值
for (i=1; i<=NF; i++) {
if ($i == "registry:") {
registry = $(i+1);
gsub(/[",]/, "", registry); # 移除可能的引号和逗号
in_block = 1;
print "找到 registry:", registry, "在文件", file;
}
}
}
/repository:/ {
if (in_block && registry != "") {
# 提取 repository 值
for (i=1; i<=NF; i++) {
if ($i == "repository:") {
repo = $(i+1);
gsub(/[",]/, "", repo); # 移除可能的引号和逗号
print "找到匹配的 repository:", repo, "在文件", file;
# 构建并执行 sed 命令
cmd = sed_cmd " '\''s|repository: " repo "|repository: " registry "/" repo "|g'\'' " file;
system(cmd);
# 重置状态
in_block = 0;
registry = "";
}
}
}
}
# 如果遇到新的块开始,重置状态
/^[^ ]/ {
if ($1 != "registry:" && $1 != "repository:") {
in_block = 0;
registry = "";
}
}
' "$yaml_file"
# 然后替换所有 registry 地址
$SED_CMD 's|registry: docker.io|registry: m.daocloud.io|g' "$yaml_file"
$SED_CMD 's|registry: registry.k8s.io|registry: m.daocloud.io|g' "$yaml_file"
$SED_CMD 's|registry: quay.io|registry: m.daocloud.io|g' "$yaml_file"
$SED_CMD 's|registry: ghcr.io|registry: m.daocloud.io|g' "$yaml_file"
echo "完成处理: $yaml_file"
done
echo "所有 YAML 文件处理完成!"
我是 Clay, 下期见~
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有