From cc-best
Provides DevOps patterns including Docker containerization, CI/CD pipelines via GitHub Actions/GitLab CI, blue-green/rolling/canary deployments, 12-Factor App principles, and monitoring. Activates on Dockerfiles, workflows, or k8s files.
npx claudepluginhub xiaobei930/cc-best --plugin cc-bestThis skill is limited to using the following tools:
本技能提供 DevOps 实践的最佳实践和模式,支持多平台按需加载。
Guides Docker best practices including multi-stage builds, GitHub Actions CI/CD pipelines, deployment strategies, IaC like Terraform, and observability setup for production infrastructure.
Sets up CI/CD pipelines with GitHub Actions, containerizes apps using Docker and docker-compose, deploys to Kubernetes with Helm, and manages IaC with Terraform.
Sets up production DevOps infrastructure: Docker containerization with Dockerfiles and docker-compose, CI/CD pipelines, Terraform IaC for cloud provisioning, and monitoring. For deploying apps.
Share bugs, ideas, or general feedback.
本技能提供 DevOps 实践的最佳实践和模式,支持多平台按需加载。
根据项目需求,加载对应的平台专属文件:
| 平台 | 加载文件 | 内容 |
|---|---|---|
| Docker | docker.md | 容器化、Compose、镜像优化 |
| CI/CD | ci-cd.md | GitHub Actions、GitLab CI |
加载方式: 检测项目中的 Dockerfile/.github/workflows/k8s/ 等文件确定需求。
┌─────────────────────────────────────────────────────────────┐
│ 12-Factor App 核心原则 │
├─────────────────────────────────────────────────────────────┤
│ 1. Codebase 一个代码库,多个部署 │
│ 2. Dependencies 显式声明依赖 │
│ 3. Config 配置存储在环境变量中 │
│ 4. Backing Services 将后端服务视为附加资源 │
│ 5. Build/Release/Run 严格分离构建、发布、运行 │
│ 6. Processes 以无状态进程运行应用 │
│ 7. Port Binding 通过端口绑定导出服务 │
│ 8. Concurrency 通过进程模型扩展 │
│ 9. Disposability 快速启动和优雅终止 │
│ 10. Dev/Prod Parity 保持开发、预发、生产环境尽量相似 │
│ 11. Logs 将日志视为事件流 │
│ 12. Admin Processes 将管理任务作为一次性进程运行 │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ 环境流转 │
├─────────────────────────────────────────────────────────────┤
│ Development → Staging → Production │
│ ↓ ↓ ↓ │
│ 本地开发 预发验证 线上环境 │
│ .env.local .env.staging .env.production │
└─────────────────────────────────────────────────────────────┘
环境变量管理:
# .env.example(提交到 Git,作为模板)
DATABASE_URL=postgresql://user:password@localhost:5432/dbname
REDIS_URL=redis://localhost:6379
API_KEY=your-api-key-here
# .env.local(不提交,本地开发)
DATABASE_URL=postgresql://dev:dev@localhost:5432/myapp_dev
| 策略 | 特点 | 适用场景 |
|---|---|---|
| 蓝绿部署 | 双环境切换,零停机,回滚快 | 关键业务,零停机要求 |
| 滚动部署 | 逐实例替换,maxSurge/maxUnavailable 控制 | 标准 K8s 部署 |
| 金丝雀 | 5-10% 流量试验,渐进增量,监控错误率后全量 | 高风险功能发布 |
详细配置和流程图见 docker.md 和各云平台文档。
| 支柱 | 用途 | 工具 |
|---|---|---|
| Metrics | 聚合的数值数据 | Prometheus, Datadog |
| Logs | 离散的事件记录 | ELK, Loki |
| Traces | 请求的分布式追踪 | Jaeger, Zipkin |
┌─────────────────────────────────────────────────────────────┐
│ 四个黄金信号 │
├─────────────────────────────────────────────────────────────┤
│ Latency 响应时间 - p50, p95, p99 │
│ Traffic 流量 - QPS, 请求数/秒 │
│ Errors 错误率 - 5xx 比例, 失败请求 │
│ Saturation 饱和度 - CPU, 内存, 磁盘使用率 │
└─────────────────────────────────────────────────────────────┘
# Prometheus 告警规则
groups:
- name: application
rules:
# 高错误率告警
- alert: HighErrorRate
expr: rate(http_requests_total{status=~"5.."}[5m]) / rate(http_requests_total[5m]) > 0.05
for: 5m
labels:
severity: critical
annotations:
summary: "高错误率: {{ $value | humanizePercentage }}"
# 高延迟告警
- alert: HighLatency
expr: histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m])) > 1
for: 5m
labels:
severity: warning
annotations:
summary: "P95 延迟超过 1s"
| 工具 | 用途 | 关键原则 |
|---|---|---|
| Terraform | 基础设施编排 | 声明式、模块化、环境隔离 |
| Ansible | 配置管理 | 幂等性、Playbook 可复用 |
| Scripts | 部署/回滚 | deploy.sh + rollback.sh 成对 |
目录结构和 Terraform 示例详见 ci-cd.md。
# ✅ 使用特定版本,非 latest
FROM node:20-alpine
# ✅ 以非 root 用户运行
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser
# ✅ 扫描漏洞
# docker scan myimage:tag
| 方案 | 适用场景 | 工具 |
|---|---|---|
| 环境变量 | 开发/简单部署 | .env 文件 |
| 密钥管理服务 | 生产环境 | AWS Secrets Manager, Vault |
| K8s Secrets | Kubernetes | kubectl create secret |
# 创建 K8s Secret
kubectl create secret generic app-secrets \
--from-literal=db-password=mysecretpassword \
--from-literal=api-key=myapikey
# 网络策略示例 - 只允许特定来源访问
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-network-policy
spec:
podSelector:
matchLabels:
app: api
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- port: 8080
{
"timestamp": "2025-01-23T10:00:00Z",
"level": "info",
"service": "user-service",
"traceId": "abc123",
"message": "用户登录成功",
"userId": "user_456",
"duration": 45,
"environment": "production"
}
| 级别 | 用途 | 生产环境 |
|---|---|---|
| DEBUG | 详细调试信息 | 关闭 |
| INFO | 正常操作事件 | 开启 |
| WARN | 潜在问题 | 开启 |
| ERROR | 错误但可恢复 | 开启 |
| FATAL | 致命错误 | 开启 |
详细的平台专属实现请参考:
💡 Kubernetes 基础概念已包含在本文件中(部署策略、网络策略等),如需深入学习请参考官方文档。
记住: DevOps 的目标是自动化一切可重复的操作——手动步骤越少,出错概率越低。