简体   繁体   English

如何使用 java io.fabric8 kubernetes-client 库覆盖默认 kubernetes 配置文件

[英]How to override default kubernetes config file using java io.fabric8 kubernetes-client library

 import java.io.File;
 import org.springframework.util.ResourceUtils;
 import io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetrics;
 import io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetricsList;
 import io.fabric8.kubernetes.client.Config;
 import io.fabric8.kubernetes.client.DefaultKubernetesClient;
 import io.fabric8.kubernetes.client.KubernetesClient;

public class Tdd {

 public static void main(String[] args) throws Exception {
    
    File file=ResourceUtils.getFile("classpath:kubernetes_config");
    
    Config config=Config.autoConfigure(file.getAbsolutePath());
    
    try (KubernetesClient k8s = new DefaultKubernetesClient(config)) {

        NodeMetricsList nodeMetricsList = k8s.top().nodes().metrics();
        for (NodeMetrics nodeMetrics : nodeMetricsList.getItems()) {        
            System.out.println(nodeMetrics.getMetadata().getName());
            System.out.println(nodeMetrics.getUsage().get("cpu"));
            System.out.println(nodeMetrics.getUsage().get("memory"));
            
        }
    }

 }

}

when I try to override kubernetes config file it will not be working,当我尝试覆盖 kubernetes 配置文件时,它将无法正常工作,
it is looking ".kube/config" file.它正在寻找“.kube/config”文件。
I have added a config file inside my project,我在我的项目中添加了一个配置文件,
I want to use my customize config file.我想使用我的自定义配置文件。
how can I do it?我该怎么做?

The Config class, which you've already found, has many setters to override specific settings;您已经找到的Config class 有许多设置器来覆盖特定设置; but it looks you want load config from another path.但看起来你想从另一个路径加载配置。 In that case, you can use fromKubeconfig method.在这种情况下,您可以使用fromKubeconfig方法。

File file=ResourceUtils.getFile("classpath:kubernetes_config");
    
    String kubeconfigContents=Files.readString(file.toPath());
    Config config=Config.fromKubeconfig(kubeconfigContents);
    
    try (KubernetesClient k8s = new DefaultKubernetesClient(config)) {

        NodeMetricsList nodeMetricsList = k8s.top().nodes().metrics();
        for (NodeMetrics nodeMetrics : nodeMetricsList.getItems()) {        
            System.out.println(nodeMetrics.getMetadata().getName());
            System.out.println(nodeMetrics.getUsage().get("cpu"));
            System.out.println(nodeMetrics.getUsage().get("memory"));
            
        }
    }

暂无
暂无

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

相关问题 如何使用 java io.fabric8 kubernetes-client 库获取节点 CPU% 和 MEMORY% 值 - how to get the Node CPU% and MEMORY% value using java io.fabric8 kubernetes-client library 如何在 java 中使用 kubernetes-client 获取部署状态 - How to get the status of deployment using kubernetes-client in java 使用fabric8io kubernetes-client; 是否可以删除[自定义]观察者? - Using fabric8io kubernetes-client; is it possible to remove a [custom] watcher? io.fabric8 插件生成 kubernetes 和 openshift yaml - io.fabric8 plugin generates kubernetes and openshift yaml 通过 Fabric8io kubernetes-client 加载 yaml Kafka 配置时出现 JsonMappingException - JsonMappingException while loading yaml Kafka configuration via Fabric8io kubernetes-client 了解Kubernetes监视(kubernetes-client / java api) - Understanding Kubernetes Watch (kubernetes-client/java api) 使用Java kubernetes-client从kubernetes入口删除路径 - Deleting path from kubernetes ingress with java kubernetes-client 如何使用 fabric8 kubernetes java 客户端从部署的 pod 中读取文件? - How do I read a file from deployment's pods using fabric8 kubernetes java client? io.fabric8.kubernetes api (用于 kubernetes 的 Java 客户端)中是否有类似的 --server-dry-run 选项? - Is there an analog of --server-dry-run option in io.fabric8.kubernetes api (Java client for kubernetes)? 无法使用io.fabric8客户端创建velero.io/v1-BackupStorageLocation - Failed to create the velero.io/v1-BackupStorageLocation using io.fabric8 client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM