java远程监控k8s各节点资源占用情况
java远程监控k8s各节点资源占用情况
·
一、环境要求
k8s必须安装metric-server组件
本文使用两种开源包监控方案:1.kubernetes client-java 2.fabric kubernetes-client 使用任意一种即可
二、kubernetes client-java
1.pom配置
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<version>12.0.0</version>
</dependency>
官方网址:GitHub - kubernetes-client/java: Official Java client library for kubernetes
2.java代码
ApiClient client = ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader("D:\\test\\kubernetes\\admin.conf"))).build();
Configuration.setDefaultApiClient(client); //admin.conf是k8s生成可以拉到本地,或者此处可以使用token连接方式,参见官方demo
GenericKubernetesApi<NodeMetrics, NodeMetricsList> nodeMetricsListGenericKubernetesApi = new GenericKubernetesApi<>(
NodeMetrics.class,
NodeMetricsList.class,
"metrics.k8s.io",
"v1beta1",
"nodes",
client);
NodeMetricsList nodeMetricsList = nodeMetricsListGenericKubernetesApi.list().getObject();
HashMap<String, Map<String, String>> usages = new HashMap<>();
for (NodeMetrics item : nodeMetricsList.getItems()) {
String name = item.getMetadata().getName();
Map<String, Quantity> usage = item.getUsage();
Map<String, String> quantity = new HashMap<>();
for (String s : usage.keySet()) {
Quantity quantity1 = usage.get(s);
String s1 = quantity1.toSuffixedString();
quantity.put(s,s1);
}
usages.put(name,quantity);
}
CoreV1Api api = new CoreV1Api();
V1NodeList viNodeList = api.listNode(null, false, null, null, null, 0, null, null, 0, false);
List<Cllocata> cList = new ArrayList<>();
for (V1Node v1Node :viNodeList.getItems()){
Cllocata cllocata = new Cllocata();
V1NodeStatus v1NodeStatus = v1Node.getStatus();
List<V1NodeAddress> v1NodeAddresss = v1NodeStatus.getAddresses();
for (V1NodeAddress v1NodeAddress:v1NodeAddresss){
String type = v1NodeAddress.getType();
String address = v1NodeAddress.getAddress();
if("InternalIP".equals(type)){
cllocata.setIp(address);
}else if("Hostname".equals(type)){
cllocata.setName(address);
}
}
Map<String, Quantity> map = v1NodeStatus.getAllocatable();
cllocata.setCpu(map.get("cpu").toSuffixedString());
cllocata.setMemory(map.get("memory").toSuffixedString());
cList.add(cllocata);
}
Map<String,Object> cMap = new HashMap<>();
cMap.put("component", ComponentEnum.K8S);
List<SystemAlarmParams> sapList = systemAlarmParamsMapper.selectByMap(cMap);
for (Cllocata cllocata :cList){
Map<String,String> map = usages.get(cllocata.getName());
if(map != null){
String cpu = map.get("cpu");
cpu = cpu.replace("n","");
String memory = map.get("memory");
memory = memory.replace("Ki","");
String aCpu = cllocata.getCpu();
String aMemory = cllocata.getMemory();
aMemory = aMemory.replace("Ki","");
int pCpu = Math.round(100*Long.parseLong(cpu)/1000/1000/1000/Long.parseLong(aCpu));
int pMemory = Math.round(100*Long.parseLong(memory)/Long.parseLong(aMemory));
String ip = cllocata.getIp();
}
}
三、fabric kubernetes-client
1.pom配置
<fabric8.version>5.12.2</fabric8.version>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-core</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-common</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-rbac</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-admissionregistration</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-apps</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-autoscaling</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-apiextensions</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-batch</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-certificates</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-coordination</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-discovery</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-events</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-extensions</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-flowcontrol</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-networking</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-metrics</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-policy</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-scheduling</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-storageclass</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-node</artifactId>
<version>${fabric8.version}</version>
</dependency>
这里使用最新的包,应该从5.0以上官方包内部依赖有问题,所以如上许多子包需要指定版本单独引入,否则默认会引入4.13版本,版本不对应会报许多方法找不到问题
官方地址:GitHub - fabric8io/kubernetes-client: Java client for Kubernetes & OpenShift
2.java代码
String adminConfData = IOUtils.toString(new FileInputStream(configPath));
Config config = Config.fromKubeconfig(adminConfData);
KubernetesClient client = new DefaultKubernetesClient(config);
NodeMetricsList nodeMetricsList = client.top().nodes().metrics();
HashMap<String, Map<String, String>> usages = new HashMap<>();
for (NodeMetrics item : nodeMetricsList.getItems()) {
String name = item.getMetadata().getName();
Map<String, Quantity> usage = item.getUsage();
Map<String, String> quantity = new HashMap<>();
for (String s : usage.keySet()) {
Quantity quantity1 = usage.get(s);
String s1 = quantity1.toString();
quantity.put(s,s1);
}
usages.put(name,quantity);
}
NodeList nodeList = k8sUtil.getNodeList();
List<Cllocata> cList = new ArrayList<>();
for (Node node :nodeList.getItems()){
Cllocata cllocata = new Cllocata();
NodeStatus nodeStatus = node.getStatus();
List<NodeAddress> nodeAddresss = nodeStatus.getAddresses();
for (NodeAddress nodeAddress:nodeAddresss){
String type = nodeAddress.getType();
String address = nodeAddress.getAddress();
if("InternalIP".equals(type)){
cllocata.setIp(address);
}else if("Hostname".equals(type)){
cllocata.setName(address);
}
}
Map<String, Quantity> map = nodeStatus.getAllocatable();
cllocata.setCpu(map.get("cpu").toString());
cllocata.setMemory(map.get("memory").toString());
cList.add(cllocata);
}
Map<String,Object> cMap = new HashMap<>();
cMap.put("component", ComponentEnum.K8S);
List<SystemAlarmParams> sapList = systemAlarmParamsMapper.selectByMap(cMap);
for (Cllocata cllocata :cList){
Map<String,String> map = usages.get(cllocata.getName());
if(map != null){
String cpu = map.get("cpu");
Long lCpu = 0L;
if(cpu.endsWith("n")){
cpu = cpu.replaceAll("n","");
lCpu = Long.parseLong(cpu);
}else if(cpu.endsWith("m")){
cpu = cpu.replaceAll("m","");
lCpu = Long.parseLong(cpu)*1000000;
}
String memory = map.get("memory");
Long lMemory = 0L;
if(memory.endsWith("Ki")){
memory = memory.replaceAll("Ki","");
lMemory = Long.parseLong(memory);
}else if(memory.endsWith("Mi")){
memory = memory.replaceAll("Mi","");
lMemory = Long.parseLong(memory)*1024;
}
String aCpu = cllocata.getCpu();
Long laCpu = 0L;
if(aCpu.endsWith("n")){
aCpu = aCpu.replaceAll("n","");
laCpu = Long.parseLong(aCpu);
}else if(aCpu.endsWith("m")){
aCpu = aCpu.replaceAll("m","");
laCpu = Long.parseLong(aCpu)*1000000;
}
String aMemory = cllocata.getMemory();
Long laMemory = 0L;
if(aMemory.endsWith("Ki")){
aMemory = aMemory.replaceAll("Ki","");
laMemory = Long.parseLong(aMemory);
}else if(aMemory.endsWith("Mi")){
aMemory = aMemory.replaceAll("Mi","");
laMemory = Long.parseLong(aMemory)*1024;
}
int pCpu = Math.round(100*lCpu/laCpu);
int pMemory = Math.round(100*lMemory/laMemory);
String ip = cllocata.getIp();
}
更多推荐
已为社区贡献4条内容
所有评论(0)