java调用k8s客户端api更新副本并且读取Deployment信息(1.14和1.16版本)

 我做的需求是界面设置了时间和副本数量,后台定时判断时间范围,在时间范围内设置最大副本数量,超过时间就设置最小副本量,但是一个问题就是定时以后就会频繁的去一直更改副本,后来想是不是需要获取k8s副本数量对比,一样的时候不更新副本,不一样的时候在更新。然后接下来就把读取副本也就是获取部署信息的接口和更新副本的Api给大家列在这里。

对了不同版本调用的接口有的是不一样的,我就入坑了,现场开发是1.14,本地是1.16,然后就发现调接口一直报404,后来想到应该是版本问题,还好都解决了。那接下来就讲两个版本的调用,再插一句,我maven用的k8s之前是5.0.0发现用不了更新副本的接口,后来改成了6.0.1就可以了。

1 1.14读取部署信息的代码

    public ExtensionsV1beta1Deployment readNamespacedDeployment14(String namespace, String name) {
        ExtensionsV1beta1Api apiInstance = new ExtensionsV1beta1Api(k8sInit.getConnection());
        ExtensionsV1beta1Deployment result = null;
        try {
            result = apiInstance.readNamespacedDeployment(name, namespace, null, null, null);
        } catch (ApiException e) {
            System.err.println("Exception when calling ExtensionsV1beta1Api#readNamespacedDeployment");
            System.err.println("Status code: " + e.getCode());
            System.err.println("Reason: " + e.getResponseBody());
            System.err.println("Response headers: " + e.getResponseHeaders());
            e.printStackTrace();
        }
        return result;
    }

2 1.16读取部署信息的代码

  /**
     * 获取指定部署信息
     * k8s 1.16 版本获取
     *
     * @param namespace
     * @param name
     * @return
     */
    @Override
    public V1Deployment readNamespacedDeployment16(String namespace, String name) {
        AppsV1Api apiInstance = new AppsV1Api(k8sInit.getConnection());
        try {
            V1Deployment result = apiInstance.readNamespacedDeployment(name, namespace, null, null, null);
            return result;
        } catch (ApiException e) {
            System.err.println("Exception when calling AppsV1Api#readNamespacedDeployment");
            System.err.println("Status code: " + e.getCode());
            System.err.println("Reason: " + e.getResponseBody());
            System.err.println("Response headers: " + e.getResponseHeaders());
            e.printStackTrace();
        }
        return null;

    }

就会返回当前的deployments部署信息

3 更新副本1.14版本代码

 /**
     * 更新Deployment副本,k8s是1.14版本
     */
    public boolean patchNamespacedDeployment14(String namespace, String name, int replicas) {
        ExtensionsV1beta1Api apiInstance = new ExtensionsV1beta1Api(k8sInit.getConnection());
        String jsonPatchStr = "[{\"op\":\"replace\",\"path\":\"/spec/replicas\", \"value\": " + replicas + " }]";
        V1Patch body = new V1Patch(jsonPatchStr);
        try {
            ExtensionsV1beta1Deployment result1 = apiInstance.patchNamespacedDeployment(name, namespace, body, null, null, null, null);
            return true;
        } catch (ApiException e) {
            e.printStackTrace();
            log.error("k8s副本更新失败!");
            return false;
        }
    }

4.更新副本1.16版本代码

 /**
     * 更新Deployment副本,k8s是1.16版本
     */
    public boolean patchNamespacedDeployment16(String namespace, String name, int replicas) {
        AppsV1Api apiInstance = new AppsV1Api(k8sInit.getConnection());
        // 更新副本的json串
        String jsonPatchStr = "[{\"op\":\"replace\",\"path\":\"/spec/replicas\", \"value\": " + replicas + " }]";
        V1Patch body = new V1Patch(jsonPatchStr);
        try {
            V1Deployment result1 = apiInstance.patchNamespacedDeployment(name, namespace, body, null, null, null, null);
            return true;
        } catch (ApiException e) {
            e.printStackTrace();
            log.error("k8s副本更新失败!");
            return false;
        }
    }

这样就可以更改副本了,亲测可用!

 

 

 

 

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值