yas*_*ine 7 java fabric8 kubernetes
当我运行此代码 public class 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());
}
}
Run Code Online (Sandbox Code Playgroud)
我从 [/var/run/secrets/kubernetes.io/serviceaccount/token] 读取服务帐户令牌时收到此错误。无视。
问题出在哪里:您当前的客户端配置类型不完整,您缺少客户端身份验证设置/数据部分。
请注意,当您从集群外部运行代码时(这种类型的客户端配置称为集群外客户端配置),您需要显式指定从外部成功连接到 Kubernetes 控制平面的最低限度。
你看到问题了吗?- 您没有指定第二个身份验证条件中的任何一个>> user <<(这是此处的关键字user:)
现在, Java Kubernetes 客户端退回到基于服务帐户的身份验证策略,认为您不是人类而是机器人(Pod 在服务帐户的上下文中运行)。
从技术上讲,客户现在正在解决最后的选择:
(fabric8io/kubernetes-client 支持的配置选项列表中的第四个,请在下面检查)
其中涉及读取放入 Pod 容器内文件系统中的服务帐户令牌,路径如下:
/var/run/secrets/kubernetes.io/serviceaccount/token
Fabric8io/kubernetes-client java客户端官方支持以下几种配置客户端的方式:
这将按以下优先级顺序使用来自不同来源的设置:
- 系统属性
- 环境变量
- 库贝配置文件
- 服务帐户令牌和安装的 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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
完整的代码示例可以在这里找到。
| 归档时间: |
|
| 查看次数: |
4815 次 |
| 最近记录: |