我正在尝试使用以下代码使用 Fabric8 java API 获取 Kubernetes 服务的可用 Pod
ConnectionExample.java:
package examples;
import java.util.*;
import io.fabric8.kubernetes.api.KubernetesClient;
import io.fabric8.kubernetes.api.KubernetesFactory;
import io.fabric8.kubernetes.api.model.IntOrString;
import io.fabric8.kubernetes.api.model.Service;
public class ConnectionExample {
private String ip;
private String port;
public ConnectionExample(String ip, String port) {
this.ip= ip;
this.port = port;
}
public KubernetesClient getConnection() {
final String URI = "http://" + ip+ ":" + port;
final KubernetesClient kubernetes = new KubernetesClient(new KubernetesFactory(URI));
return kubernetes;
}
}
应用程序.java
package examples;
/**
* Hello world!
*
*/
public class App {
public static void main( String[] args ) {
System.out.println( "Hello World!" );
ConnectionExample connectionExample = new ConnectionExample("XXX.XXX.XXX.XX", "1234");
System.out.println("Retrun: "+connectionExample.getConnection());
System.out.println("List of Pods: "+connectionExample.getConnection().getPods());
//connectionExample.getConnection().createService(entity, namespace)
}
}
我遇到以下错误
2017-10-26 15:09:04 WARN PhaseInterceptorChain:452 - Interceptor for
WebClient has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: Could not send Message.
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:64)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:307)
at org.apache.cxf.jaxrs.client.AbstractClient.doRunInterceptorChain(AbstractClient.java:619)
at org.apache.cxf.jaxrs.client.ClientProxyImpl.doChainedInvocation(ClientProxyImpl.java:674)
at org.apache.cxf.jaxrs.client.ClientProxyImpl.invoke(ClientProxyImpl.java:224)
at com.sun.proxy.$Proxy19.getPods(Unknown Source)
at io.fabric8.kubernetes.api.KubernetesClient.getPods(KubernetesClient.java:154)
at io.fabric8.kubernetes.api.KubernetesClient.getPods(KubernetesClient.java:149)
还想知道如何传递 kubernetes 服务的用户名和密码
我正在尝试使用fabric8 java API从java类启动kubernetes服务
最佳答案
public static void main(String[] args) throws Exception{
try {
String url = "cluster_endpoint";
String oathToken = "serviceAccountToken";
Config config = new ConfigBuilder()
.withMasterUrl("")
.withTrustCerts(true)
.withOauthToken(oathToken.replaceAll("(\\r|\\n)", ""))
.build();
KubernetesClient client = new DefaultKubernetesClient(config);
System.out.println(client.pods().inNamespace("default").list());
} catch (KubernetesClientException kce) {
logger.error("KubernetesClientException : {}, {}", KubernetesErrorUtil.getErrorMsg(kce), kce);
} catch (Exception e){
logger.error("Exception :");
e.printStackTrace();
}
}
这将使客户端使用 serviceAccountToken。您可以通过创建 ClusterRole 或 Role 以及 ClusterRoleBinding 或 RoleBinding 来创建服务帐户并为其授予所需的权限。
您还可以通过其他方式获得客户:
- 1) 使用 kubeconfig 文件
2) 使用 api-server 中使用的证书
配置配置 = new ConfigBuilder() .withMasterUrl(masterURL) .withClientCertFile(certFile) .build();
或证书文件的内容-
配置配置 = new ConfigBuilder() .withMasterUrl(masterURL) .withClientCertData(certFile) .build();
关于Java Fabric 8 - 使用 Fabric8 Java API 获取 Kubernetes 服务的 Pod 并启动服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46963106/