简体   繁体   English

如何在 k8s golang 客户端上应用 HorizontalPodAutoscalers

[英]How to apply HorizontalPodAutoscalers with k8s golang client

I'm using k8s Go client and I want to apply HorizontalPodAutoscalers on a Deployment .我正在使用 k8s Go 客户端,我想在Deployment上应用HorizontalPodAutoscalers ntalPodAutoscalers。 I have tried multiple attempts but it always returns我尝试了多次尝试,但它总是返回

{
    "error": "the server could not find the requested resource"
}

This is my code:这是我的代码:

func (k *KubeClient) createAutoScaler(deploymentName string) (*v22.HorizontalPodAutoscaler, error) {
autoscaler := &v22.HorizontalPodAutoscaler{
        Spec: v22.HorizontalPodAutoscalerSpec{
            ScaleTargetRef: v22.CrossVersionObjectReference{
                Kind:       "Deployment",
                Name:       deploymentName,
                APIVersion: "apps/v1",
            },
            MinReplicas: pointer.Int32(2),
            MaxReplicas: 5,
            Metrics: []v22.MetricSpec{
                {
                    Type: v22.ResourceMetricSourceType,
                    Resource: &v22.ResourceMetricSource{
                        Name: "cpu",
                        Target: v22.MetricTarget{
                            Type:               "Utilization",
                            AverageUtilization: pointer.Int32(70),
                        },
                    },
                },
                //{
                //  Type: v22.ResourceMetricSourceType,
                //  Resource: &v22.ResourceMetricSource{
                //      Name: "memory",
                //      Target: v22.MetricTarget{
                //          Type:               "Utilization",
                //          AverageUtilization: pointer.Int32(70),
                //      },
                //  },
                //},
            },
            //Behavior: &v22.HorizontalPodAutoscalerBehavior{
            //  ScaleDown: &v22.HPAScalingRules{
            //      StabilizationWindowSeconds: pointer.Int32(120),
            //      Policies: []v22.HPAScalingPolicy{
            //          {
            //              Type:          v22.PodsScalingPolicy,
            //              Value:         1,
            //              PeriodSeconds: 60,
            //          },
            //      },
            //  },
            //},
        },
    }

    apply, err := k.client.AutoscalingV2().HorizontalPodAutoscalers("mlu-showroom-test").
        Create(context.Background(), autoscaler, apimetav1.CreateOptions{
            TypeMeta: apimetav1.TypeMeta{
                Kind:       "Pod",
                APIVersion: "apps/v1",
            },
            FieldValidation: "Ignore",
        })

    if err != nil {
        return nil, err
    }

    return apply, nil

}

I have checked with kubectl get , the deployment does exist.我已经用kubectl get检查过,部署确实存在。

I have tried creating the autoscaler using kubectl autoscale and it works, I think the problem relies in the code but I'm not sure what goes wrong.我已经尝试使用kubectl autoscale创建自动缩放器并且它有效,我认为问题在于代码但我不确定出了什么问题。

I cannot find any document on how to create a horizontal autoscaler using go-client, the code above is created by referencing the fields in yaml format in this document: https://kube.netes.io/docs/tasks/run-application/horizontal-pod-autoscale/我找不到任何关于如何使用 go-client 创建水平自动缩放器的文档,上面的代码是通过引用本文档中yaml格式的字段创建的: https://kube.netes.io/docs/tasks/run-application /水平吊舱自动缩放/

If you have applied HorizontalAutoScaler with k8s go-client before, please share your knowledge.如果您之前使用过 k8s go-client 应用过 HorizontalAutoScaler,请分享您的知识。

After checking K8S version again, I understand there were two problems:再次查看K8S版本,发现有两个问题:

kubectl --kubeconfig=kubeconfig version
Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.7", GitCommit:"1dd5338295409edcfff11505e7bb246f0d325d15", GitTreeState:"clean", BuildDate:"2021-01-13T13:23:52Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.5", GitCommit:"5c99e2ac2ff9a3c549d9ca665e7bc05a3e18f07e", GitTreeState:"clean", BuildDate:"2021-12-16T08:32:32Z", GoVersion:"go1.16.12", Compiler:"gc", Platform:"linux/amd64"}
  • I was trying to create an autoscaler for both memory and cpu, which can only be achieved from kube.netes v1.23 according to Kube.netes official document.我试图为 memory 和 cpu 创建一个自动缩放器,根据 Kube.netes 官方文档,这只能从kube.netes v1.23实现。
  • I applied incorrect version and name for the auto scaler.我为自动缩放器应用了错误的版本和名称。

After some modification, the working code looks like this经过一些修改后,工作代码如下所示

func (k *KubeClient) createAutoScaler(deploymentName string, namespace string) (*v12.HorizontalPodAutoscaler, error) {
    autoscaler := &v12.HorizontalPodAutoscaler{
        TypeMeta: apimetav1.TypeMeta{
            Kind:       "Pod",
            APIVersion: "autoscaling/v1",
        },
        ObjectMeta: apimetav1.ObjectMeta{
            Name:      deploymentName,
            Namespace: namespace,
        },
        Spec: v12.HorizontalPodAutoscalerSpec{
            ScaleTargetRef: v12.CrossVersionObjectReference{
                Kind:       "Deployment",
                Name:       deploymentName,
                APIVersion: "apps/v1",
            },
            MinReplicas:                    pointer.Int32(2),
            MaxReplicas:                    5,
            TargetCPUUtilizationPercentage: pointer.Int32(70),
        },
    }

    apply, err := k.client.AutoscalingV1().HorizontalPodAutoscalers(namespace).
        Create(context.Background(), autoscaler, apimetav1.CreateOptions{})

    if err != nil {
        return nil, err
    }

    return apply, nil
}

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

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