最近需要用java连接kubernets api操作k8s的service、rc等,做个记录
1.https://github.com/kubernetes-client/java/下载开源k8s客户端
2.k8s server api默认监听6443端口,上代码:
ApiClient apiClient = new ApiClient();
apiClient.setBasePath(“https://12.6.89.6:6443″);//api server地址
apiClient.setVerifyingSsl(false);//设置不验证ssl证书,因为自己的证书都是不可信任的
String token = “sghkdsjgheruighkj”;//k8s server的token
apiClient.addDefaultHeader(“Authorization”, “Bearer “+token);
api = new CoreV1Api(apiClient);//获得登录连接api
//创建ReplicationController
api.createNamespacedReplicationController(“default”, body, “false”, null, null);
//创建service
api.createNamespacedService(“default”, body, “false”, null, null);
这样就能连接k8s server api 进行远程操作。