想实现Java访问k8s apiserver接口服务,目前主要Java client 有基于Jersey以及基于Fabric8
https://jersey.github.io/
https://github.com/fabric8io/kubernetes-client
Jersey需要自己实现各种接口调用,Fabric8已经实现主要的api调用,研究了一下Fabric8,决定还是自己模拟https请求来调用k8s服务接口,现成的框架尽管便于使用,但是有时候不便于扩展,还有k8s更新太快,现有框架对于功能可能不支持,因此这里使用org.apache.http包来模拟https请求,访问k8s服务。
k8s apiserver服务端口 https://10.3.10.144:6443 内网IP 这里验证方式使用Bootstrap Token请求响应头方式,已经在上一篇给出Service Account的配置以及访问方式。
由于使用https通信,首先下载apiserver的证书到jdk中证书信息中。
点击证书信息 复制到文件,下载到本地 命名为k8s.cer
cd $JAVA_HOME/jre/lib/security
keytool -import -file C:\Users\chen\Desktop\k8s.cer -keystore cacerts -alias k8s -trustcacerts
输入口令 changeit
查看口令:
具体密钥库的管理命令可以查看keytool指令
首先保证当前操作者用户对于jdk所在的文件有读写权限,win10权限验证很严格
尽量到jdk中secure目录下进行操作,keystore路径中不能有空格,否则:
之前访问利用curl来完成:
curl -k https://192.168.1.15:6443/api/v1/services --header "Authorization: Bearer $token”
现在使用httpclient来模拟上述请求
构建sslclient,信任所有证书:
private static CloseableHttpClient createSSLClientDefault() {
SSLContext sslContext;
try {
sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
//信任所有
@Override
public boolean isTrusted(X509Certificate[] xcs, String string){
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (KeyStoreException ex) {
Logger.getLogger(HttpUtils.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(HttpUtils.class.getName()).log(Level.SEVERE, null, ex);
} catch (KeyManagementException ex) {
Logger.getLogger(HttpUtils.class.getName()).log(Level.SEVERE, null, ex);
}
return HttpClients.createDefault();
}
构建http get请求方法,来执行get请求:
public static String get(String url, Map param,
Map headers) {
HttpClient httpClient = getClient();
// HttpClient httpClient = new DefaultHttpClient();
HttpGet get = null;
get = new HttpGet(url);
appendHeaders(get, headers);
appendParams(get, param);
try {
HttpResponse httpResponse = httpClient.execute(get);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity();
return EntityUtils.toString(entity);
} else {
httpResponse.getEntity().getContent().close();
//System.out.println("return status code:" + httpResponse.getStatusLine().getStatusCode());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
long e = System.currentTimeMillis();
return null;
}
private static HttpClient getClient() {
// HttpClient hc = clients.get();
// if (hc == null) {
// hc = ThreadSafeHttpclientGetter.getNewInstance(1000, 5000, 40000);
// clients.set(hc);
// }
// return hc;
return createSSLClientDefault();
}
构建main方法进行初步测试,
public static void main( String[] args ){
String url = "https://10.3.10.144:6443/api/v1/services";
Map param = new HashMap<>();
Map headers = new HashMap<>();
headers.put("Authorization", ConfigUtils.getBootstrapTokenSecret());
String result = HttpUtils.get(url, param, headers);
System.out.println(result);
}
在请求头中添加Authorization中这一项,内容是Bootstrap_Token_Secret,之前生成的Service Account Secret
执行结果:返回所有的Services的列表
参考地址:
请求头地址:http://tools.jb51.net/table/http_header
github: https://github.com/chen1234567/k8s-java