当我运行此代码时,公共类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/serviceaccount/token.获得了读取服务帐户令牌的错误无视。
发布于 2021-03-12 06:13:52
的问题是:当前类型的客户端配置是不完整的,您缺少客户端身份验证设置/数据部分。
请注意,当您在集群外部运行代码时(这种类型的客户端配置称为-out-群集客户端配置),您需要显式地指定从外部成功连接到Kubernetes控件平面的最小值。
您看到问题了吗? -您没有从>> user <<
身份验证的第二个条件中指定这些条件(这里的关键字是:user
)
现在 Java客户端回到了基于服务帐户的身份验证策略,认为您不是人而是机器人(Pod运行在服务帐户的上下文中)。
从技术上讲,客户现在正在解决最后的选择:
(8 on /kubernetes列表中的第4-客户端支持的配置选项,请参阅下面的代码)
这包括读取放置在Pod容器中的文件系统中的服务帐户令牌,路径如下:
/var/run/secrets/kubernetes.io/serviceaccount/token
8/kubernetes-客户 java客户端支持以下配置客户端的方式:
这将按以下优先顺序使用来自不同来源的设置:
系统属性优于环境变量。可以使用下列系统属性和环境变量进行配置
最简单的解决方案是依靠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());
}
}
}
完整的代码示例可以找到这里。
https://stackoverflow.com/questions/43370090
复制相似问题