减小低资源上的副本大小

huangapple go评论53阅读模式
英文:

Reduce replica size on low resources

问题

我正在使用 Google Kubernetes Engine,我的一些部署比其他部署更重要(例如,暂存环境可以运行较少的副本,甚至在需要时停止)。

我希望根据可用资源,特别是内存,动态更改部署的副本数量。

理想情况下,我会设置一个默认的副本数量,通常使用,如果集群资源不足,它应该减少该部署的副本数量。

这应该适用于一些部署,但不适用于所有部署。

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: my-deployment
  name: my-deployment
  namespace: my-namespace
spec:
  replicas: 3 # 如果集群资源(内存或 CPU)不足,应该降低这个数量
  selector:
    matchLabels:
      app: my-deployment
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: my-deployment
    spec:
      containers:
      - image: my/image:version
        name: my-deployment

甚至应该可以将一些部署的副本数量减少到 0,但对于其他部署则不是这样。

请注意,我的部署分布在多个命名空间中(如果这很重要)。

英文:

I am using Google Kubernetes Engine and some of my deployments are more important than others (For example, the staging environment can run on less replicas or even stop if needed).

I want to dynamically change the amount of replicas of a deployment depending on the available resources, especially memory.

Ideally, I would set a default number of replicas which is used normally and if the cluster is running low on resources, it should reduce the number of replicas of that deployment.

This should happen for some deployments but not all of them.

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: my-deployment
  name: my-deployment
  namespace: my-namespace
spec:
  replicas: 3 # This should be lower if the cluster is running on low resources (memory or CPU)
  selector:
    matchLabels:
      app: my-deployment
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: my-deployment
    spec:
      containers:
      - image: my/image:version
        name: my-deployment

It should even be possible to reduce the number of replicas down to 0 for some deployments but not for others.

Note that my deployments are distributed across multiple namespaces (if that matters).

答案1

得分: 2

我想根据可用资源,特别是内存,动态更改部署的副本数量。

您可以使用Kubernetes HPA(水平Pod自动缩放),它会根据CPU/内存利用率动态更改副本的数量。

同时,您还可以为部署设置默认的副本数量,例如对于deployment,您可以将其设置为1个副本,扩展到3个副本;对于staging,您至少要有3个运行中的副本,扩展到5个副本;对于Prod,至少需要4个运行中的副本,扩展到10个副本等。

了解更多关于HPA的信息:https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/

示例

kubectl autoscale deployment <Deployment-name> --cpu-percent=50 --min=1 --max=10

示例参考:https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/

如果流量较少,还可以实现自动缩放至零,并且基于CPU使用情况,它可以在没有用户输入的情况下自动扩展Pods。

如果您不需要扩展,或者仅需要模板化,您可以忽略上面的回答,并使用scaffoldhelm来动态管理YAML模板。

Deployment.yaml

replicas: {{ .Values.replicas }}

values-dev.yaml

replicas=3

values-staging.yaml

replicas=5

根据环境,您可以传递相应的values-*.yaml副本数量给helm,它将为您创建模板。

英文:

> I want to dynamically change the amount of replicas of a deployment
> depending on the available resources, especially memory.

You can use the Kubernetes HPA (Horizonatal POD autoscaling), which which dynamically changes the number of replicas based on the CPU/Memory utilization.

While you can also set the default number of replicas for your deployment for deployment you have 1 scaling to 3 for staging you have a minimum 3 running scaling up to 5 and for Prod minimum running you 4 and scaling to 10 etc.

Read more about the HPA : https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/

Example

kubectl autoscale deployment &lt;Deployment-name&gt; --cpu-percent=50 --min=1 --max=10

Example ref : https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/

If there is less traffic auto-scale down to zero is also possible and based on CPU usage it can scale up pods auto without user input.

If you not looking for scaling or just for templating you are facing issues ignore above answer and you can use the scaffold or helm to mange the YAML templating dynamically.

Deployment.yaml

replicas: {{ .Values.replicas}}

values-dev.yaml

replicas=3

values-staging.yaml

replicas=5

based on the environment you can pass the values-*.yaml replica to helm and it will create the template for you.

huangapple
  • 本文由 发表于 2023年2月24日 17:38:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定