【问题标题】:How to delete HorizontalPodAutoscaler using Fabric8 k8s java client (version: 6.0.0)如何使用 Fabric8 k8s java 客户端删除 Horizo​​ntalPodAutoscaler(版本:6.0.0)
【发布时间】:2022-09-30 13:04:36
【问题描述】:

看起来不支持使用 fabric8 的 K8S Java 客户端版本:6.0.0 删除 Horizo​​ntalPodAutoscaler。

虽然使用 fabric8 的 K8S Java 客户端 ver:6.0.0 创建 Horizo​​ntalPodAutoscaler 很简单。

例如。

 HorizontalPodAutoscalerStatus hpaStatus = k8sClient.resource(createHPA())
                .inNamespace(namespace)
                .createOrReplace().getStatus();
public HorizontalPodAutoscaler createHPA(){
return new HorizontalPodAutoscalerBuilder()
                .withNewMetadata()
                    .withName(applicationName)
                    .addToLabels(\"name\", applicationName)
                .endMetadata()
                .withNewSpec()
                    .withNewScaleTargetRef()
                        .withApiVersion(hpaApiVersion)
                        .withKind(\"Deployment\")
                        .withName(applicationName)
                    .endScaleTargetRef()
                    .withMinReplicas(minReplica)
                    .withMaxReplicas(maxReplica)
                    .addNewMetric()
                        .withType(\"Resource\")
                        .withNewResource()
                            .withName(\"cpu\")
                            .withNewTarget()
                                .withType(\"Utilization\")
                                .withAverageUtilization(cpuAverageUtilization)
                            .endTarget()
                        .endResource()
                    .endMetric()
                    .addNewMetric()
                        .withType(\"Resource\")
                        .withNewResource()
                            .withName(\"memory\")
                            .withNewTarget() 
                                .withType(\"AverageValue\")
                                .withAverageValue(new Quantity(memoryAverageValue))
                            .endTarget()
                        .endResource()
                    .endMetric()
                    .withNewBehavior()
                        .withNewScaleDown()
                            .addNewPolicy()
                                .withType(\"Pods\")
                                .withValue(podScaleDownValue)
                                .withPeriodSeconds(podScaleDownPeriod)
                            .endPolicy()
                            .withStabilizationWindowSeconds(podScaledStabaliztionWindow)
                        .endScaleDown()
                    .endBehavior()
                .endSpec().build();
}

任何使用 fabric8\'s K8S Java 客户端版本:6.0.0 删除 Horizo​​ntalPodAutoscaler 的解决方案都将得到应用。

  • client.autoscaling().v1().horizontalPodAutoscalers().resource(hpa).delete(); 不工作吗?

标签: java linux kubernetes fabric8 fabric8-maven-plugin


【解决方案1】:

KubernetesHPAProducer.doDeleteHPA :要删除 HPA Autoscaler Pod,请遵循 link

以下可能对您有所帮助:

protected void doDeleteHPA(Exchange exchange, String operation) throws Exception {

   String hpaName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_HPA_NAME, String.class);

   String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);

   if (ObjectHelper.isEmpty(hpaName)) {

     LOG.error("Delete a specific hpa require specify a hpa name");

     throw new IllegalArgumentException("Delete a specific hpa require specify a hpa name");

   }

   if (ObjectHelper.isEmpty(namespaceName)) {

     LOG.error("Delete a specific hpa require specify a namespace name");

     throw new IllegalArgumentException("Delete a specific hpa require specify a namespace name");

   }

   boolean hpaDeleted = getEndpoint().getKubernetesClient().autoscaling().horizontalPodAutoscalers().inNamespace(namespaceName).withName(hpaName).delete();



   MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);

   exchange.getOut().setBody(hpaDeleted);

 }

}

【讨论】:

  • 如果答案有用,请将答案标记为已接受,以提高社区的知名度,如果答案有一些有用的信息,请点赞。
【解决方案2】:

首先,需要根据同一个 API 组确定部署创建期间使用了哪个 API 组 (v1, v2beta1, v2beta2),需要调用自动缩放函数来获取 HPA 实例,然后继续对该 HPA 实例执行任何操作。

在我的例子中,部署是使用 v2beta2 API 组创建的,下面的代码片段帮助我从提供的名称空间中删除了 Horizo​​ntalPodAutoscaler 对象。

k8sClient.autoscaling().v2beta2().horizontalPodAutoscalers().inNamespace("test").withName("myhpa").delete()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2018-12-27
    • 1970-01-01
    相关资源
    最近更新 更多