mcpField
K
kubernetes-deploying

kubernetes-deploying

github.com/spencerpauly云与 DevOps868372 次安装13 周前更新
代码仓库

关于 kubernetes-deploying

这项技能用于在 Kubernetes 上部署和管理应用,涵盖常见资源、发布策略、健康检查、自动扩缩容及运维实践。

主要功能

  1. 01Deployment、Service 和 Ingress 配置
  2. 02ConfigMap 与 Secret 指引
  3. 03滚动、蓝绿和金丝雀发布
  4. 04存活与就绪探针
  5. 05配置 Horizontal Pod Autoscaler
  6. 06资源与命名空间实践

适用场景

  1. 01编写 Kubernetes 应用配置
  2. 02选择应用发布策略
  3. 03配置工作负载健康检查
  4. 04使用 HPA 扩缩工作负载

安装

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

$npx skills add https://github.com/spencerpauly/awesome-cursor-skills --skill kubernetes-deploying

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

详细介绍

Kubernetes 部署

在 Kubernetes 上部署和管理应用程序。

核心资源

Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  labels:
    app: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-app
          image: my-registry/my-app:v1.2.3
          ports:
            - containerPort: 3000
          resources:
            requests:
              cpu: 100m
              memory: 128Mi
            limits:
              cpu: 500m
              memory: 512Mi
          livenessProbe:
            httpGet:
              path: /healthz
              port: 3000
            initialDelaySeconds: 10
            periodSeconds: 30
          readinessProbe:
            httpGet:
              path: /ready
              port: 3000
            initialDelaySeconds: 5
            periodSeconds: 10
          env:
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: my-app-secrets
                  key: database-url
            - name: NODE_ENV
              value: production
Service
apiVersion: v1
kind: Service
metadata:
  name: my-app
spec:
  selector:
    app: my-app
  ports:
    - port: 80
      targetPort: 3000
  type: ClusterIP
Ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-app
  annotations:
    cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
  tls:
    - hosts:
        - app.example.com
      secretName: my-app-tls
  rules:
    - host: app.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: my-app
                port:
                  number: 80
ConfigMap 与 Secret
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-app-config
data:
  LOG_LEVEL: info
  FEATURE_FLAGS: '{"darkMode": true}'
---
apiVersion: v1
kind: Secret
metadata:
  name: my-app-secrets
type: Opaque
stringData:
  database-url: postgresql://user:pass@host:5432/db
  api-key: sk-abc123

常用命令

# Apply manifests
kubectl apply -f k8s/

# Check deployment status
kubectl rollout status deployment/my-app

# View pods
kubectl get pods -l app=my-app

# View logs
kubectl logs -f deployment/my-app

# Execute into a pod
kubectl exec -it <pod-name> -- /bin/sh

# Scale
kubectl scale deployment/my-app --replicas=5

# Rollback
kubectl rollout undo deployment/my-app

# Port forward for local debugging
kubectl port-forward svc/my-app 3000:80

部署策略

策略方式适用场景
滚动更新(默认)一次替换一个 Pod大多数部署
重建终止所有旧 Pod,然后启动新 Pod无法同时运行两个版本时
蓝绿部署运行两套完整环境,然后切换流量需要即时回滚时
金丝雀发布将少量流量路由到新版本高风险变更

滚动更新配置:

spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0

健康检查

始终设置以下两项:

  • livenessProbe:“进程是否健康?”——失败时重启 Pod
  • readinessProbe:“是否可以处理流量?”——失败时将其从 Service 中移除

常见探针类型:

  • httpGet:访问 HTTP 端点(最常用)
  • exec:在容器中运行命令
  • tcpSocket:检查端口是否开放

水平 Pod 自动伸缩器

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: my-app
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: my-app
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70

提示

  • 始终设置资源请求和限制
  • 使用命名空间隔离环境(dev、staging、prod)
  • 绝不要将明文 Secret 放入提交到 git 的 YAML 文件中——请使用 Sealed Secrets、SOPS 或外部 Secret 管理器
  • 使用特定版本标记镜像,生产环境中绝不要使用 :latest
  • 为高可用工作负载设置 PodDisruptionBudget

评论

支持 Markdown