kubernetes 如何在k8s golang客户端中应用HorizontalPodAutoscalers

wj8zmpe1  于 2023-01-04  发布在  Kubernetes
关注(0)|答案(1)|浏览(200)

我使用的是k8s Go客户端,我想在Deployment上应用HorizontalPodAutoscalers,我尝试了多次,但总是返回

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

这是我的代码:

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

}

我已经检查了kubectl get,部署确实存在。
我试过使用kubectl autoscale创建自动定标器,它工作正常,我认为问题在于代码,但我不确定哪里出错了。
我找不到任何关于如何使用go-client创建水平自动分频器的文档,上面的代码是通过引用本文档中yaml的字段创建的:https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/
如果您以前在k8s go-client中应用过HorizontalAutoScaler,请分享您的知识。

jogvjijk

jogvjijk1#

再次检查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"}
  • 我试图为内存和CPU创建一个自动定标器,根据Kubernetes官方文件,这只能从kubernetes v1.23实现。
  • 我为自动定标器应用了不正确的版本和名称。

经过一些修改后,工作代码如下所示

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
}

相关问题