kubernetes-deploying

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Kubernetes Deploying

Kubernetes 应用部署

Deploy and manage applications on Kubernetes.
在Kubernetes上部署和管理应用。

Core Resources

核心资源

Deployment

Deployment

yaml
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
yaml
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

Service

yaml
apiVersion: v1
kind: Service
metadata:
  name: my-app
spec:
  selector:
    app: my-app
  ports:
    - port: 80
      targetPort: 3000
  type: ClusterIP
yaml
apiVersion: v1
kind: Service
metadata:
  name: my-app
spec:
  selector:
    app: my-app
  ports:
    - port: 80
      targetPort: 3000
  type: ClusterIP

Ingress

Ingress

yaml
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
yaml
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

ConfigMap & Secret

yaml
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
yaml
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

Common Commands

常用命令

bash
undefined
bash
undefined

Apply manifests

Apply manifests

kubectl apply -f k8s/
kubectl apply -f k8s/

Check deployment status

Check deployment status

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

View pods

View pods

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

View logs

View logs

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

Execute into a pod

Execute into a pod

kubectl exec -it <pod-name> -- /bin/sh
kubectl exec -it <pod-name> -- /bin/sh

Scale

Scale

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

Rollback

Rollback

kubectl rollout undo deployment/my-app
kubectl rollout undo deployment/my-app

Port forward for local debugging

Port forward for local debugging

kubectl port-forward svc/my-app 3000:80
undefined
kubectl port-forward svc/my-app 3000:80
undefined

Deployment Strategies

部署策略

StrategyHowWhen
Rolling update (default)Replace pods one at a timeMost deployments
RecreateKill all old pods, start new onesWhen you can't run two versions simultaneously
Blue/greenRun two full environments, switch trafficNeed instant rollback
CanaryRoute small % of traffic to new versionHigh-risk changes
Rolling update config:
yaml
spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
策略实现方式适用场景
Rolling update (默认)逐个替换Pod大多数部署场景
Recreate先终止所有旧Pod,再启动新Pod无法同时运行两个版本的场景
Blue/green运行两套完整环境,切换流量路由需要即时回滚的场景
Canary将小比例流量路由到新版本高风险变更场景
滚动更新配置:
yaml
spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0

Health Checks

健康检查

Always set both:
  • livenessProbe: "Is the process healthy?" — restarts the pod if it fails
  • readinessProbe: "Can it handle traffic?" — removes from service if it fails
Common probe types:
  • httpGet
    : Hit an HTTP endpoint (most common)
  • exec
    : Run a command in the container
  • tcpSocket
    : Check if a port is open
始终同时设置以下两种检查:
  • livenessProbe:"进程是否健康?"——如果检查失败,将重启Pod
  • readinessProbe:"是否可以处理流量?"——如果检查失败,将从服务中移除该Pod
常见的探针类型:
  • httpGet
    :访问HTTP端点(最常用)
  • exec
    :在容器内执行命令
  • tcpSocket
    :检查端口是否开放

Horizontal Pod Autoscaler

水平Pod自动扩缩器(Horizontal Pod Autoscaler)

yaml
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
yaml
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

Tips

小贴士

  • Always set resource requests and limits
  • Use namespaces to isolate environments (
    dev
    ,
    staging
    ,
    prod
    )
  • Never put secrets in plaintext YAML committed to git — use Sealed Secrets, SOPS, or external secret managers
  • Tag images with specific versions, never use
    :latest
    in production
  • Set
    PodDisruptionBudget
    for high-availability workloads
  • 始终设置资源请求(requests)和限制(limits)
  • 使用命名空间(namespaces)隔离环境(
    dev
    、
    staging
    、
    prod
    )
  • 切勿将明文Secret提交到Git仓库——使用Sealed Secrets、SOPS或外部密钥管理器
  • 为镜像打上具体版本标签,生产环境中绝不使用
    :latest
  • 为高可用工作负载设置
    PodDisruptionBudget