You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GCP Kubernetes中单个Deployment的水平扩缩容实现及配置补充咨询

How to Perform Horizontal Scaling for a Kubernetes Deployment in GCP (and Do I Need Extra Configs?)

Question:

I've deployed multiple apps in my GCP Kubernetes cluster and need to horizontally scale one specific Deployment. Here's its YAML config:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: preview
  namespace: default
  resourceVersion: {}
  uid: {}
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: preview
  strategy:
    type: Recreate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: preview
    spec:
      containers:
      - image: gcr.io/{project name}/{image name}
        imagePullPolicy: Always
        name: preview
        resources:
          requests:
            cpu: 10m
            memory: 450Mi
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /app/data
          name: data
        - mountPath: /app/conf
          name: config
          readOnly: true
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: preview
      - name: config
        secret:
          defaultMode: 420
          secretName: preview-secrets

I already tried changing the replicas value to something greater than 1, applied the new config, and restarted the Deployment—but I'm not sure if I need to add extra policies, specs, or other configs to make sure the scaling works correctly.


Answer:

Hey there! Let's clear this up for you—you're already on the right track, and most of what you've done is correct. Here's a breakdown:

1. Your Core Action is Correct (No Extra Config Needed for Manual Scaling)

Changing the replicas field in your Deployment YAML is exactly how you perform manual horizontal scaling. Kubernetes' Deployment controller will automatically create or terminate Pods to match the replica count you set. You don't need to add any extra policies or spec changes to make this work—your current config is fully sufficient for manual scaling.

A quick note on your strategy: Recreate setting: this only affects how the Deployment rolls out updates (it kills all old Pods before creating new ones), not horizontal scaling. When you scale up, Kubernetes will just spin up new Pods alongside the existing ones, so this strategy doesn't interfere with your scaling operation. Also, you don't need to manually restart the Deployment after applying the updated YAML—kubectl apply will trigger the controller to adjust the replica count automatically.

2. Two Easy Ways to Scale Your Deployment

You can use either of these methods (they do the exact same thing under the hood):

  • Edit and apply the YAML: Update the replicas value in your file, then run:
    kubectl apply -f your-deployment-file.yaml -n default
    
  • Use kubectl scale for a quicker fix: Skip editing the YAML entirely with this command (replace 3 with your desired replica count):
    kubectl scale deployment/preview --replicas=3 -n default
    

3. Verify the Scaling Worked

To confirm everything's running as expected, run these commands:

  • Check the Deployment status (look for the READY column to match your target replica count):
    kubectl get deployment preview -n default
    
  • List the Pods to see the new replicas:
    kubectl get pods -n default -l app=preview
    
    You should see all Pods in a Running state once scaling is complete.

4. Bonus: Automatic Scaling (If You Need It)

If you want Kubernetes to automatically scale the Deployment based on resource usage (like CPU or memory), you'll need to add a Horizontal Pod Autoscaler (HPA). This is the only "extra config" you might need, and it's optional.

For example, to create an HPA that scales between 1-5 replicas when CPU usage hits 70%, run:

kubectl autoscale deployment preview -n default --min=1 --max=5 --cpu-percent=70

Or use this YAML config for more control:

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: preview-hpa
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: preview
  minReplicas: 1
  maxReplicas: 5
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70

Apply it with:

kubectl apply -f hpa-config.yaml -n default

内容的提问来源于stack exchange,提问作者Jae

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 17:47:33