简体   繁体   English

GCP 上部署的水平扩展 Kubernetes

[英]Horizontal scaling of a deployment on GCP Kubernetes

I have a cluster where I have deployed multiple applications and I want to horizontally scale one of the deployment.我有一个集群,我在其中部署了多个应用程序,并且我想水平扩展其中一个部署。 Following is my Yaml for the deployment, how can I achieve it?以下是我的 Yaml 用于部署,我该如何实现呢? Note: I have tried changing the replicas to more than 1 and applied the new config and restarted the deployment but want to know if I need to add any policies, specs, etc to achieve the right horizontal scaling.注意:我尝试将副本更改为超过 1 个并应用新配置并重新启动部署,但想知道是否需要添加任何策略、规范等以实现正确的水平扩展。

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

In GKE, you can achieve this with Horizontal Pod Autoscaler (HPA) .在 GKE 中,您可以使用 Horizontal Pod Autoscaler (HPA)来实现这一点。 The autoscaling event can be configured to be triggered by system (eg. cpu or memory) or custom metrics (eg. pubsub queued messages count).自动缩放事件可以配置为由系统(例如 cpu 或内存)或自定义指标(例如 pubsub 排队消息计数)触发。 You can also set the minimum and maximum number of pods to scale up to.您还可以设置要向上扩展的 pod 的最小和最大数量。

Here is a link from GCP for a sample HPA yaml file这是来自 GCP 的链接,其中包含示例 HPA yaml 文件

Menu > GKE > Workloads > click on your deployment > 3 dots (more actions) > Actions > Autoscale > set metrics > Save菜单 > GKE > 工作负载 > 单击您的部署 > 3 个点(更多操作)> 操作 > 自动缩放 > 设置指标 > 保存

You can use the HPA (Horizontal Pod Autoscaler).您可以使用 HPA(Horizontal Pod Autoscaler)。 Here is what the typical yaml configuration looks like.这是典型的 yaml 配置的样子。

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: hpa_name
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: deployment_name_to_autoscale
  minReplicas: 1
  maxReplicas: 3
  targetCPUUtilizationPercentage: 80

You can monitor the scaling using kubectl get hpa您可以使用kubectl get hpa监控缩放

Here is another example for using HPA with external metric (eg cloud pub/sub):这是另一个将 HPA 与外部指标(例如云发布/订阅)结合使用的示例:

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: your-hpa-name
spec:
  minReplicas: 1
  maxReplicas: 4
  metrics:
  - external:
      metric:
       name: pubsub.googleapis.com|subscription|num_undelivered_messages
       selector:
         matchLabels:
           resource.labels.subscription_id: your-pubsub-subscirbtion-name
      target:
        type: AverageValue
        averageValue: 200
    type: External
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: your-deployment-name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM