有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从:[/var/run/secrets/kubernetes.io/servicecomport/token]读取服务帐户令牌时出错。忽略

当我运行此代码时 公共类test2{

public static void main(String[] args) {
    // TODO Auto-generated method stub


      String podName = "xrdpprocan";
      String namespace = "default";
      String master = "https://my_ip_adress"; 

      Config config = new ConfigBuilder().withMasterUrl(master).withTrustCerts(true).build();
      try (final KubernetesClient client = new DefaultKubernetesClient(config)) {

        String log = client.pods().inNamespace(namespace).withName(podName).getLog(true);
        System.out.println("Log of pod " + podName + " in " + namespace + " is:");
        System.out.println("------------------");
        System.out.println(log);

      } catch (KubernetesClientException e) {
       System.out.println(e.getMessage());
      }
}

我从:[/var/run/secrets/kubernetes.io/servicecomport/token]读取服务帐户令牌时出错。忽略


共 (1) 个答案

  1. # 1 楼答案

    问题出在哪里:当前类型的客户端配置不完整,您缺少客户端身份验证设置/数据部分

    当您从集群外部运行代码时,请注意 (这种类型的客户机配置称为集群外客户机配置),您需要明确指定从外部成功连接到Kubernetes控制平面的最小值

    1. Kubernetes主URL
    2. 至少一种用于用户身份验证的方法可以是:
    • 客户端证书
    • 不记名代币
    • HTTP基本身份验证

    你看到问题了吗-您没有从>> user <<身份验证的第二个条件中指定任何一个(这里是一个关键字:user

    现在Java Kubernetes客户端回到了基于服务帐户的身份验证策略,认为您不是人类而是机器人(Pod在服务帐户上下文中运行)

    从技术上讲,客户现在正在解决最后的选择:

    KUBERNETES_AUTH_TRYSERVICEACCOUNT

    4th在fabric8io/kubernetes客户机支持的配置选项列表中,选中下面的选项)

    这涉及读取放置在Pod容器内文件系统中的服务内帐户令牌,路径如下:

    /var/run/secrets/kubernetes.io/serviceaccount/token


    官方fabric8io/kubernetes-clientjava客户端支持以下配置客户端的方法:

    This will use settings from different sources in the following order of priority:

    • System properties
    • Environment variables
    • Kube config file
    • Service account token & mounted CA certificate <== you client code tries this

    System properties are preferred over environment variables. The following system properties & environment variables can be used for configuration

    最简单的解决方案是依靠Kube config file选项从外部访问集群,例如:

    public class KubeConfigFileClientExample {
      public static void main(String[] args) throws IOException, ApiException {
    
        // file path to your KubeConfig
    
        String kubeConfigPath = System.getenv("HOME") + "/.kube/config";
    
        // loading the out-of-cluster config, a kubeconfig from file-system
        ApiClient client =
            ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath))).build();
    
        // set the global default api-client to the in-cluster one from above
        Configuration.setDefaultApiClient(client);
    
        // the CoreV1Api loads default api-client from global configuration.
        CoreV1Api api = new CoreV1Api();
    
        // invokes the CoreV1Api client
        V1PodList list =
            api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null);
        for (V1Pod item : list.getItems()) {
          System.out.println(item.getMetadata().getName());
        }
      }
    }
    

    完整的代码示例可以在here找到