Files
nexus/wiki/concepts/Self-Healing System.md
2026-05-03 05:42:12 +08:00

94 lines
3.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "Self-Healing System"
type: concept
tags: [DevOps, Reliability, Automation]
sources: [engineering-devops-automator]
last_updated: 2026-05-01
---
# Self-Healing System
## 定义
自愈系统是指能够自动检测故障、触发恢复机制,在无需人工干预的情况下恢复到正常运行状态的系统。核心目标是减少 MTTRMean Time To Recovery提升系统可用性。
## 自愈机制层次
### 1. 基础设施层
- **自动扩缩容**:基于指标自动增加/减少实例
- **健康检查 + 自动替换**:检测到不健康实例后自动终止并启动新实例
- **多可用区部署**:单可用区故障不影响整体服务
### 2. 应用层
- **优雅降级**:部分功能不可用时,核心功能继续运行
- **熔断器Circuit Breaker**:防止故障级联传播
- **重试机制 + 指数退避**:临时故障自动恢复
### 3. 数据层
- **自动备份**:定期备份数据库
- **故障转移**:主库故障自动切换到备用库
- **数据一致性检查**:定期校验数据完整性
## 在 DevOps Automator 中的应用
DevOps Automator 的自愈设计:
```terraform
# AWS Auto Scaling Group 自愈配置
resource "aws_autoscaling_group" "app" {
health_check_type = "ELB" # ELB 健康检查
health_check_grace_period = 300 # 启动后 5 分钟内不进行健康检查
lifecycle {
create_before_destroy = true # 新实例就绪后再终止旧实例
}
}
# CloudWatch Alarm 自动恢复
resource "aws_cloudwatch_metric_alarm" "high_cpu" {
alarm_name = "app-high-cpu"
comparison_operator = "GreaterThanThreshold"
threshold = 80
alarm_actions = [aws_sns_topic.alerts.arn]
# 自动触发扩展或通知
}
```
## Kubernetes 自愈能力
- **ReplicaSet**:维持期望的 Pod 副本数Pod 崩溃自动重启
- **Liveness Probe**:检测应用无响应后自动重启容器
- **Readiness Probe**:标记 Pod 为不可用,自动从 Service 中移除
- **Horizontal Pod Autoscaler**:基于 CPU/内存自动扩缩容
## 自愈 vs 人工干预
| 场景 | 自愈 | 人工干预 |
|------|------|----------|
| 实例崩溃 | ✅ 自动重启/替换 | - |
| 内存泄漏(渐进式) | ✅ 自动扩展缓解 | 人工分析根因 |
| 配置错误 | ❌ 需回滚配置 | ✅ 人工修复 |
| 外部依赖故障 | ⚠️ 降级/重试 | ✅ 人工处理 |
| 灾难性故障 | ⚠️ 部分可恢复 | ✅ 必需 |
## 相关概念
- [[Observability]]:自愈的前提是能够检测故障
- [[Zero-Downtime Deployment]]:自愈能力支撑零停机部署
- [[Kubernetes]]K8s 提供丰富的自愈机制
## 最佳实践
1. **监控先行**:没有可观测性就没有自愈
2. **渐进式自愈**:从简单场景开始,逐步增加复杂度
3. **设置边界**:明确哪些情况必须人工介入
4. **记录和告警**:即使自愈成功,也要通知运维团队
5. **测试自愈机制**:定期演练故障场景
## 关键指标
- **MTTR**:平均恢复时间(越短越好)
- **自愈成功率**:自愈成功的故障数 / 总故障数
- **MTTA**:平均确认时间(检测到告警到开始处理)
## Aliases
- Self-Healing
- 自愈系统
- Automatic Recovery
- 自动恢复
- Fault Tolerance
- 容错