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

时间:2017-04-12 12:51:32

标签: java kubernetes fabric8

当我运行此代码时 公共课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 / services / count /token]。忽略。

1 个答案:

答案 0 :(得分:1)

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

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

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

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

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

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

<块引用>

KUBERNETES_AUTH_TRYSERVICEACCOUNT

(fabric8io/kubernetes-client 支持的配置选项列表中的第 4 个,请在下面查看)

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

<块引用>

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


官方fabric8io/kubernetes-client java客户端支持以下配置客户端的方式:

<块引用>

这将按以下顺序使用来自不同来源的设置 优先级:

  • 系统属性
  • 环境变量
  • Kube 配置文件
  • 服务帐户令牌和挂载的 CA 证书 <== 您的客户端代码会尝试此操作

系统属性优先于环境变量。这 以下系统属性和环境变量可用于 配置

最简单的解决方案是依靠 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找到完整的代码示例。

相关问题