mcpField
D
devops-automation

devops-automation

github.com/rohitg00云与 DevOps2.8k267 次安装24 周前更新
代码仓库

关于 devops-automation

devops-automation 是为团队配置 CI/CD 与基础设施部署实践提供参考的技能。内容涵盖 GitHub Actions、Docker、Kubernetes、Helm、ArgoCD 和监控,并提供运维模式与安全建议。

主要功能

  1. 01组织 GitHub Actions 流水线
  2. 02采用 Docker 多阶段构建实践
  3. 03配置 Kubernetes 部署检查
  4. 04按环境组织 Helm chart
  5. 05应用 ArgoCD GitOps 模式
  6. 06规划监控指标与告警

适用场景

  1. 01设计带部署门禁的 CI/CD 流水线
  2. 02准备生产环境容器镜像
  3. 03设置部署探针和资源限制
  4. 04审查基础设施工作流的安全性

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/rohitg00/awesome-claude-code-toolkit --skill devops-automation

安装后在 Claude Code 中输入/devops-automation确认已加载。

详细介绍

DevOps 自动化

GitHub Actions 工作流结构

name: CI/CD
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: 'npm'
      - run: npm ci
      - run: npm run lint

  test:
    runs-on: ubuntu-latest
    needs: lint
    strategy:
      matrix:
        node-version: [20, 22]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'
      - run: npm ci
      - run: npm test -- --coverage
      - uses: actions/upload-artifact@v4
        with:
          name: coverage-${{ matrix.node-version }}
          path: coverage/

  deploy:
    runs-on: ubuntu-latest
    needs: test
    if: github.ref == 'refs/heads/main'
    environment: production
    steps:
      - uses: actions/checkout@v4
      - run: ./deploy.sh

关键模式:

  • 使用 concurrency 取消过时的运行
  • 使用 setup 操作的 cache 选项缓存依赖项
  • 使用 needs 声明作业依赖关系
  • 使用 environment 保护规则限制部署
  • 使用 matrix 进行跨版本测试

Docker 多阶段构建

FROM node:22-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --production

FROM node:22-alpine AS builder
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:22-alpine AS runner
WORKDIR /app
RUN addgroup -g 1001 appgroup && adduser -u 1001 -G appgroup -S appuser
COPY --from=deps /app/node_modules ./node_modules
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/package.json ./
USER appuser
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s CMD wget -qO- http://localhost:3000/health || exit 1
CMD ["node", "dist/server.js"]

规则:

  • 使用特定的镜像标签,切勿使用 latest
  • 以非 root 用户身份运行
  • 仅将必要文件复制到最终阶段
  • 添加 HEALTHCHECK,以便与编排器集成
  • 使用 .dockerignore 排除 node_modules、.git 和测试文件

Kubernetes 部署清单

apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-server
  labels:
    app: api-server
spec:
  replicas: 3
  selector:
    matchLabels:
      app: api-server
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: api-server
    spec:
      containers:
        - name: api
          image: registry.example.com/api:v1.2.3
          ports:
            - containerPort: 3000
          resources:
            requests:
              cpu: 100m
              memory: 128Mi
            limits:
              cpu: 500m
              memory: 512Mi
          readinessProbe:
            httpGet:
              path: /health
              port: 3000
            initialDelaySeconds: 5
            periodSeconds: 10
          livenessProbe:
            httpGet:
              path: /health
              port: 3000
            initialDelaySeconds: 15
            periodSeconds: 20
          env:
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: api-secrets
                  key: database-url

始终设置资源请求和限制。始终定义就绪探针和存活探针。使用 maxUnavailable: 0 实现零停机部署。

Helm Chart 结构

chart/
  Chart.yaml
  values.yaml
  values-staging.yaml
  values-production.yaml
  templates/
    deployment.yaml
    service.yaml
    ingress.yaml
    hpa.yaml
    _helpers.tpl
# values.yaml
replicaCount: 2
image:
  repository: registry.example.com/api
  tag: latest
  pullPolicy: IfNotPresent
resources:
  requests:
    cpu: 100m
    memory: 128Mi
  limits:
    cpu: 500m
    memory: 512Mi
ingress:
  enabled: true
  host: api.example.com
autoscaling:
  enabled: true
  minReplicas: 2
  maxReplicas: 10
  targetCPUUtilization: 70

为每个环境使用 values-{env}.yaml 覆盖配置。使用 helm lint 检查 Chart。部署前使用 helm template 测试。

ArgoCD GitOps 模式

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: api-server
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/org/k8s-manifests
    targetRevision: main
    path: apps/api-server
  destination:
    server: https://kubernetes.default.svc
    namespace: production
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
      - CreateNamespace=true

GitOps 原则:

  • Git 是集群状态的唯一事实来源
  • 所有变更都通过 PR 进行(生产环境中不使用 kubectl apply)
  • ArgoCD 自动将 Git 中的更改同步到集群
  • 启用 selfHeal,撤销对集群的手动更改
  • 将应用代码仓库与部署清单仓库分开

监控栈

# Prometheus ServiceMonitor
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: api-server
spec:
  selector:
    matchLabels:
      app: api-server
  endpoints:
    - port: metrics
      interval: 15s
      path: /metrics

要公开的关键指标:

  • http_request_duration_seconds(直方图)- 按路由和状态统计的请求延迟
  • http_requests_total(计数器)- 按路由和状态统计的请求数
  • process_resident_memory_bytes(量表)- 内存使用量
  • db_query_duration_seconds(直方图)- 数据库查询延迟

在以下情况触发告警:错误率 >1%、P99 延迟 >2s、内存使用量 >限制的 80%、10 分钟内 Pod 重启 >3 次。

流水线最佳实践

  1. 将 CI 控制在 10 分钟以内(并行运行作业,积极缓存)
  2. 在测试前运行 lint 和类型检查
  3. 使用临时环境预览 PR
  4. 将所有 Action 版本固定到 SHA,而不是标签
  5. 将密钥存储在 GitHub Secrets 中,切勿放在工作流文件里
  6. 使用 OIDC 进行云服务提供商身份验证(不使用长期密钥)
  7. 使用 git SHA 标记镜像,而不是 latest
  8. 在 CI 中对容器镜像运行安全扫描(Trivy、Snyk)

评论

支持 Markdown