因为我们公司现有的项目部署在k8s集群中,然后做的项目是想要查看自己部署集群的项目以及容器信息和运行情况,然后发现k8s其实有对外展示的接口,并且也支持java开发,但是无奈这部分的内容网上偏少但好在最后还是顺利的解决问题,在此说明,我们k8s的可视化使用的是在k8s之上封装的插件或者说是软件叫kuboard,这个可以不用我们部署再自己打yaml文件了,甚至部署应用也可以进行分层是相当的方便,真的推荐使用。
下面说下介绍下具体怎么调用K8s的官方提供的接口
1.java调用api需要引入jar包
io.kubernetes
client-java
5.0.0
compile
2 我是用restapi实现的也就是https://ip:6443就可以去获取k8s的一系列的接口,但是前提需要权限的,因为kuboard查看界面需要token,那个token就能有k8s获取接口的权限,我是把相应的信息放入了配置文件,那么上代码
k8s:
api-http: https://192.168.7.102:6443
token: eyJhbGciOiJSUzI1NiIsImtpZCI6IlBFRUUtalNxT3FIX0c1aDd3ckRiNFlIVGIxM3lReFBtbzdmWXJ0V3hxTzQifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdWJvYXJkLXVzZXItdG9rZW4tY3E1dGIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoia3Vib2FyZC11c2VyIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiZGQ1YmVmNmUtNjNmMC00ODgyLWI2MjItOWY4YzNlNWJjNjJhIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmt1Ym9hcmQtdXNlciJ9.MK9WUHJ50DxwqlPqPW6aH-U_Ww0qWNm-HX8PPejBjEPaG4YO3McjdviStT4p8t80iSIYq9NlrLUDcqghmJhxkF-z4Of4cqVwmgSbQ7WML07f0ynSD0UDIWV-YEwBsuMBHVfWGquy1aghlEU8V4gknImwU1_DkvDHZXQFYgGXaqUSf1DyRzkkcZahhlVvx-96ajKZ_4ciVYgxWeTig07bercWrLcMzh8GQoNOjEj_uAjXfoSstR1baK41MU_2qmvpSKu0lacZkInurXjbQ5IXpNQ8XCFiNzCbVGXU5JldQQp5T1DZBKy58TmyRCN0OMaxkf0qNsiNjh0bdJdYc651XQ
3. 开始初始化连接k8s的客户端,只要需要获取接口都需要使用,那么我就封装到工具类里了
package gsa.service.overview.util;
import io.kubernetes.client.ApiClient;
import io.kubernetes.client.Configuration;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.credentials.AccessTokenAuthentication;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* k8s的初始化
*
* @Author df
* @Date 2019/11/1 15:39
* @Version 1.0
*/
@Component
public class K8sInit {
@Value("${k8s.api-http}")
private String httpApi;
@Value("${k8s.token}")
private String token;
/**
* k8s的初始化
*
* @return
*/
public ApiClient getConnection() {
ApiClient client = new ClientBuilder().
setBasePath(httpApi).setVerifyingSsl(false).
setAuthentication(new AccessTokenAuthentication(token)).build();
Configuration.setDefaultApiClient(client);
return client;
}
}
4.然后咱们示例获取应用空间列表的接口
public V1NamespaceList getNamespace() {
ApiClient client = k8sInit.getConnection();
CoreV1Api apiInstance = new CoreV1Api();
String pretty = "true";
V1NamespaceList namespaceList = new V1NamespaceList();
try {
namespaceList = apiInstance.listNamespace(null, pretty, null, null, null, null, null, null, null);
} catch (ApiException e) {
System.err.println("Exception when calling CoreV1Api#listNamespace");
e.printStackTrace();
}
return namespaceList;
}
5.然后controller运行以后咱们postman来运行一下获取k8s集群部署的应用空间名称
{
"msg": "成功",
"code": 200,
"data": {
"apiVersion": "v1",
"items": [
{
"metadata": {
"creationTimestamp": {
"year": 2019,
"dayOfMonth": 23,
"dayOfWeek": 3,
"era": 1,
"dayOfYear": 296,
"weekOfWeekyear": 43,
"yearOfCentury": 19,
"weekyear": 2019,
"monthOfYear": 10,
"centuryOfEra": 20,
"yearOfEra": 2019,
"hourOfDay": 15,
"minuteOfHour": 38,
"millisOfSecond": 0,
"secondOfMinute": 9,
"millisOfDay": 56289000,
"secondOfDay": 56289,
"minuteOfDay": 938,
"millis": 1571816289000,
"zone": {
"fixed": false,
"uncachedZone": {
"fixed": false,
"cachable": true,
"id": "Asia/Shanghai"
},
"id": "Asia/Shanghai"
},
"chronology": {
"zone": {
"fixed": false,
"uncachedZone": {
"fixed": false,
"cachable": true,
"id": "Asia/Shanghai"
},
"id": "Asia/Shanghai"
}
},
"afterNow": false,
"equalNow": false,
"beforeNow": true
},
"name": "default",
"resourceVersion": "153",
"selfLink": "/api/v1/namespaces/default",
"uid": "0f647b80-f568-11e9-ac35-5254001afee7"
},
"spec": {
"finalizers": [
"kubernetes"
]
},
"status": {
"phase": "Active"
}
},
{
"metadata": {
"creationTimestamp": {
"year": 2019,
"dayOfMonth": 23,
"dayOfWeek": 3,
"era": 1,
"dayOfYear": 296,
"weekOfWeekyear": 43,
"yearOfCentury": 19,
"weekyear": 2019,
"monthOfYear": 10,
"centuryOfEra": 20,
"yearOfEra": 2019,
"hourOfDay": 15,
"minuteOfHour": 38,
"millisOfSecond": 0,
"secondOfMinute": 6,
"millisOfDay": 56286000,
"secondOfDay": 56286,
"minuteOfDay": 938,
"millis": 1571816286000,
"zone": {
"fixed": false,
"uncachedZone": {
"fixed": false,
"cachable": true,
"id": "Asia/Shanghai"
},
"id": "Asia/Shanghai"
},
"chronology": {
"zone": {
"fixed": false,
"uncachedZone": {
"fixed": false,
"cachable": true,
"id": "Asia/Shanghai"
},
"id": "Asia/Shanghai"
}
},
"afterNow": false,
"equalNow": false,
"beforeNow": true
},
"name": "kube-node-lease",
"resourceVersion": "6",
"selfLink": "/api/v1/namespaces/kube-node-lease",
"uid": "0d7d0cda-f568-11e9-ac35-5254001afee7"
},
"spec": {
"finalizers": [
"kubernetes"
]
},
"status": {
"phase": "Active"
}
},
{
"metadata": {
"creationTimestamp": {
"year": 2019,
"dayOfMonth": 23,
"dayOfWeek": 3,
"era": 1,
"dayOfYear": 296,
"weekOfWeekyear": 43,
"yearOfCentury": 19,
"weekyear": 2019,
"monthOfYear": 10,
"centuryOfEra": 20,
"yearOfEra": 2019,
"hourOfDay": 15,
"minuteOfHour": 38,
"millisOfSecond": 0,
"secondOfMinute": 6,
"millisOfDay": 56286000,
"secondOfDay": 56286,
"minuteOfDay": 938,
"millis": 1571816286000,
"zone": {
"fixed": false,
"uncachedZone": {
"fixed": false,
"cachable": true,
"id": "Asia/Shanghai"
},
"id": "Asia/Shanghai"
},
"chronology": {
"zone": {
"fixed": false,
"uncachedZone": {
"fixed": false,
"cachable": true,
"id": "Asia/Shanghai"
},
"id": "Asia/Shanghai"
}
},
"afterNow": false,
"equalNow": false,
"beforeNow": true
},
"name": "kube-public",
"resourceVersion": "5",
"selfLink": "/api/v1/namespaces/kube-public",
"uid": "0d7ca439-f568-11e9-ac35-5254001afee7"
},
"spec": {
"finalizers": [
"kubernetes"
]
},
"status": {
"phase": "Active"
}
},
{
"metadata": {
"creationTimestamp": {
"year": 2019,
"dayOfMonth": 23,
"dayOfWeek": 3,
"era": 1,
"dayOfYear": 296,
"weekOfWeekyear": 43,
"yearOfCentury": 19,
"weekyear": 2019,
"monthOfYear": 10,
"centuryOfEra": 20,
"yearOfEra": 2019,
"hourOfDay": 15,
"minuteOfHour": 38,
"millisOfSecond": 0,
"secondOfMinute": 6,
"millisOfDay": 56286000,
"secondOfDay": 56286,
"minuteOfDay": 938,
"millis": 1571816286000,
"zone": {
"fixed": false,
"uncachedZone": {
"fixed": false,
"cachable": true,
"id": "Asia/Shanghai"
},
"id": "Asia/Shanghai"
},
"chronology": {
"zone": {
"fixed": false,
"uncachedZone": {
"fixed": false,
"cachable": true,
"id": "Asia/Shanghai"
},
"id": "Asia/Shanghai"
}
},
"afterNow": false,
"equalNow": false,
"beforeNow": true
},
"name": "kube-system",
"resourceVersion": "4",
"selfLink": "/api/v1/namespaces/kube-system",
"uid": "0d7be3a0-f568-11e9-ac35-5254001afee7"
},
"spec": {
"finalizers": [
"kubernetes"
]
},
"status": {
"phase": "Active"
}
},
{
"metadata": {
"creationTimestamp": {
"year": 2019,
"dayOfMonth": 28,
"dayOfWeek": 1,
"era": 1,
"dayOfYear": 301,
"weekOfWeekyear": 44,
"yearOfCentury": 19,
"weekyear": 2019,
"monthOfYear": 10,
"centuryOfEra": 20,
"yearOfEra": 2019,
"hourOfDay": 17,
"minuteOfHour": 26,
"millisOfSecond": 0,
"secondOfMinute": 9,
"millisOfDay": 62769000,
"secondOfDay": 62769,
"minuteOfDay": 1046,
"millis": 1572254769000,
"zone": {
"fixed": false,
"uncachedZone": {
"fixed": false,
"cachable": true,
"id": "Asia/Shanghai"
},
"id": "Asia/Shanghai"
},
"chronology": {
"zone": {
"fixed": false,
"uncachedZone": {
"fixed": false,
"cachable": true,
"id": "Asia/Shanghai"
},
"id": "Asia/Shanghai"
}
},
"afterNow": false,
"equalNow": false,
"beforeNow": true
},
"name": "test",
"resourceVersion": "534065",
"selfLink": "/api/v1/namespaces/test",
"uid": "f9eca43c-f964-11e9-ac35-5254001afee7"
},
"spec": {
"finalizers": [
"kubernetes"
]
},
"status": {
"phase": "Active"
}
}
],
"kind": "NamespaceList",
"metadata": {
"resourceVersion": "1078870",
"selfLink": "/api/v1/namespaces"
}
}
}
6.那么如果想获取pod容器信息呢!通过以下这个接口获取容器的信息
package gsa.service.overview.service.impl.k8s;
import gsa.service.overview.service.k8s.K8sPodApiService;
import gsa.service.overview.util.K8sInit;
import io.kubernetes.client.ApiClient;
import io.kubernetes.client.ApiException;
import io.kubernetes.client.apis.CoreV1Api;
import io.kubernetes.client.models.V1PodList;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @Author df
* @Date 2019/11/1 17:24
* @Version 1.0
*/
@Service
public class K8sPodApiServiceImpl implements K8sPodApiService {
@Resource
private K8sInit k8sInit;
@Override
public V1PodList getPodByNamePace(String namespace) {
ApiClient client = k8sInit.getConnection();
CoreV1Api apiInstance = new CoreV1Api();
namespace = namespace;
String pretty = "pretty_example";
V1PodList result = new V1PodList();
try {
result = apiInstance.listNamespacedPod(namespace, null, pretty, null, null, null, null, null, null, null);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling CoreV1Api#listNamespacedPod");
e.printStackTrace();
}
return result;
}
}
7.然后结果是这样的
然后还想看更多的k8s调用信息请看官方提供的api文档可以看这个文档
https://github.com/kubernetes-client/java/tree/master/kubernetes/docs
里边的api后缀全部都是k8s提供的接口
点击进去查看具体都是怎么调用接口的都可以看到
比如我说查询某个应用空间下的所有service