Files
ljwx-k8s/deploy-observability-enhancement.sh
2026-02-20 22:50:45 +08:00

253 lines
7.9 KiB
Bash
Executable File

#!/bin/bash
# 可观测性增强组件一键部署脚本
# 自动部署 Postgres/Redis Exporter + 更新 Prometheus/Grafana 配置
set -euo pipefail
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# 配置
MONITORING_NS="${MONITORING_NS:-monitoring}"
INFRA_NS="${INFRA_NS:-infra}"
# 辅助函数
print_header() {
echo ""
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}$1${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
}
print_step() {
echo -e "${GREEN}==>${NC} $1"
}
print_warn() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
print_error() {
echo -e "${RED}❌ $1${NC}"
}
# 检查命令
check_prerequisites() {
print_header "检查前置条件"
if ! command -v kubectl &> /dev/null; then
print_error "kubectl 未安装"
exit 1
fi
print_step "kubectl: ✓"
if ! kubectl cluster-info &> /dev/null; then
print_error "无法连接到 Kubernetes 集群"
exit 1
fi
print_step "Kubernetes 集群连接: ✓"
# 检查核心组件
if ! kubectl get deployment prometheus -n "$MONITORING_NS" &> /dev/null; then
print_error "Prometheus 未部署在 namespace $MONITORING_NS"
exit 1
fi
print_step "Prometheus: ✓"
if ! kubectl get deployment grafana -n "$MONITORING_NS" &> /dev/null; then
print_warn "Grafana 未部署,将跳过 Grafana 配置"
else
print_step "Grafana: ✓"
fi
}
# 部署 Exporters
deploy_exporters() {
print_header "部署 Exporters"
# Postgres Exporter
read -p "是否部署 Postgres Exporter? (y/N): " deploy_pg
if [[ $deploy_pg =~ ^[Yy]$ ]]; then
print_step "部署 Postgres Exporter..."
# 检查 Secret 是否存在
if kubectl get secret postgres-exporter-secret -n "$MONITORING_NS" &> /dev/null; then
print_warn "postgres-exporter-secret 已存在,跳过创建"
else
print_warn "请先编辑 infra/observability/postgres-secret-example.yaml 配置连接串"
read -p "按回车继续,或 Ctrl+C 取消..."
kubectl apply -f infra/observability/postgres-secret-example.yaml
fi
kubectl apply -f infra/observability/postgres-exporter.yaml
print_step "Postgres Exporter 部署完成"
fi
# Redis Exporter
read -p "是否部署 Redis Exporter? (y/N): " deploy_redis
if [[ $deploy_redis =~ ^[Yy]$ ]]; then
print_step "部署 Redis Exporter..."
if kubectl get secret redis-exporter-secret -n "$MONITORING_NS" &> /dev/null; then
print_warn "redis-exporter-secret 已存在,跳过创建"
else
print_warn "请先编辑 infra/observability/redis-secret-example.yaml 配置连接串"
read -p "按回车继续,或 Ctrl+C 取消..."
kubectl apply -f infra/observability/redis-secret-example.yaml
fi
kubectl apply -f infra/observability/redis-exporter.yaml
print_step "Redis Exporter 部署完成"
fi
}
# 更新 Prometheus 配置
update_prometheus() {
print_header "更新 Prometheus 配置"
read -p "是否更新 Prometheus ConfigMap? (Y/n): " update_prom
if [[ ! $update_prom =~ ^[Nn]$ ]]; then
print_step "备份现有 Prometheus ConfigMap..."
kubectl get configmap prometheus-config -n "$MONITORING_NS" -o yaml > \
"prometheus-config-backup-$(date +%Y%m%d-%H%M%S).yaml"
print_step "应用新的 Prometheus 配置..."
kubectl apply -f monitoring/prometheus/prometheus-configmap-enhanced.yaml
print_step "部署告警规则..."
# 创建规则 ConfigMaps
for rule_file in monitoring/prometheus/rules/*.yml; do
rule_name=$(basename "$rule_file" .yml)
print_step "部署规则: $rule_name"
kubectl create configmap "prometheus-rules-$rule_name" \
--from-file="$rule_file" \
-n "$MONITORING_NS" \
--dry-run=client -o yaml | kubectl apply -f -
done
print_step "重启 Prometheus 以加载新配置..."
kubectl rollout restart deployment/prometheus -n "$MONITORING_NS"
print_step "等待 Prometheus 就绪..."
kubectl rollout status deployment/prometheus -n "$MONITORING_NS" --timeout=120s
fi
}
# 更新 Grafana 配置
update_grafana() {
print_header "更新 Grafana 配置"
if ! kubectl get deployment grafana -n "$MONITORING_NS" &> /dev/null; then
print_warn "Grafana 未部署,跳过"
return
fi
read -p "是否更新 Grafana 数据源配置? (Y/n): " update_grafana
if [[ ! $update_grafana =~ ^[Nn]$ ]]; then
print_step "备份现有 Grafana 数据源配置..."
kubectl get configmap grafana-datasources -n "$MONITORING_NS" -o yaml > \
"grafana-datasources-backup-$(date +%Y%m%d-%H%M%S).yaml"
print_step "应用新的数据源配置..."
kubectl apply -f monitoring/grafana/datasources-loki-tempo-enhanced.yaml
print_step "重启 Grafana 以加载新配置..."
kubectl rollout restart deployment/grafana -n "$MONITORING_NS"
print_step "等待 Grafana 就绪..."
kubectl rollout status deployment/grafana -n "$MONITORING_NS" --timeout=120s
fi
}
# 导入 Dashboards
import_dashboards() {
print_header "导入 Grafana Dashboards"
if ! kubectl get deployment grafana -n "$MONITORING_NS" &> /dev/null; then
print_warn "Grafana 未部署,跳过"
return
fi
read -p "是否导入 Grafana Dashboards? (y/N): " import_dash
if [[ $import_dash =~ ^[Yy]$ ]]; then
print_step "执行 Dashboard 导入脚本..."
cd monitoring/grafana/dashboards
./import-dashboards.sh
cd -
fi
}
# 验收
run_verification() {
print_header "运行验收测试"
read -p "是否运行验收测试? (Y/n): " run_verify
if [[ ! $run_verify =~ ^[Nn]$ ]]; then
print_step "执行验收脚本..."
./verify-observability-enhanced.sh
fi
}
# 显示后续步骤
show_next_steps() {
print_header "部署完成!"
echo -e "${GREEN}✅ 可观测性增强组件部署成功${NC}"
echo ""
echo "后续步骤:"
echo ""
echo "1. 验证 Prometheus Targets:"
echo " kubectl port-forward -n $MONITORING_NS svc/prometheus 9090:9090"
echo " 浏览器打开: http://localhost:9090/targets"
echo ""
echo "2. 访问 Grafana Dashboards:"
echo " kubectl port-forward -n $MONITORING_NS svc/grafana 3000:3000"
echo " 浏览器打开: http://localhost:3000"
echo ""
echo "3. 启用 pg_stat_statements (如果使用 Postgres):"
echo " 参考文档: docs/observability/pg-stat-statements-setup.md"
echo ""
echo "4. 集成业务应用:"
echo " - Spring Boot: docs/observability/business-instrumentation-spring-boot.md"
echo " - Python: docs/observability/business-instrumentation-python.md"
echo ""
echo "5. 测试 Loki ↔ Tempo 跳转:"
echo " - 在 Grafana Explore 中查询包含 trace_id 的日志"
echo " - 点击 'View Trace' 链接验证跳转"
echo ""
echo "📚 完整文档: OBSERVABILITY-ENHANCEMENT-GUIDE.md"
echo ""
}
# 主函数
main() {
echo -e "${BLUE}"
cat << "EOF"
╔═══════════════════════════════════════════════════════╗
║ K8s 可观测性增强部署工具 ║
║ Postgres/Redis Monitoring + LGTM Integration ║
╚═══════════════════════════════════════════════════════╝
EOF
echo -e "${NC}"
check_prerequisites
deploy_exporters
update_prometheus
update_grafana
import_dashboards
run_verification
show_next_steps
}
# 执行
main "$@"