1. 目的:簡單監(jiān)控pod
我想使用java監(jiān)控k8s pod的資源的簡單使用情況,但是k8s內(nèi)部并沒有采集資源的實現(xiàn)。
但是k8s提供了一套k8s的對接標(biāo)準(zhǔn),只要適配這套標(biāo)準(zhǔn),就可以通過kubelet采集資源數(shù)據(jù),并且通過k8s api服務(wù)器輸出。這些對于故障排查以及自動伸縮至關(guān)重要。
2. 部署指南
metrics-server是Kubernetes的一個集群范圍內(nèi)的聚合器,用于收集所有節(jié)點和Pods的資源使用信息,如CPU和內(nèi)存。metrics指標(biāo)統(tǒng)計器(github)
- 如果證書沒問題,部署最新的metrics-server
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.4.1/components.yaml
- 如果你的集群使用自簽名的證書,你可能需要添加參數(shù)來忽略 TLS 驗證錯誤
把commponents.yaml wget下來后, 在 components.yaml 文件中,找到 metrics-server 的 Deployment 部分,并添加以下參數(shù)到 args:
- --kubelet-insecure-tls
- 查看是否部署成功
kubectl get deployment metrics-server -n kube-system
# 查看所有命名空間的pod資源
kubectl top pods -A
top 命令就可以使用了。
3. java Api調(diào)用
在官方提供的客戶端 java庫里是沒有直接調(diào)用metrics的方法的。我們只能使用restful api進行訪問,
- 添加maven依賴(獲取tls上下文,如果只是過去資源可以不添加)
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<version>18.0.0</version>
</dependency>
-
測試restful api(測試,可不做)
我這里是手動把證書導(dǎo)入到了postman(k8s的權(quán)限認證非常關(guān)鍵,沒有權(quán)限是無法訪問api的) -
java訪問 restapi
我這里吧k8s 客戶端和 rest http前后文都注入spring了,這樣調(diào)用起來比較方便
注入k8s客戶端
在這里插入代碼片@Configuration
public class KubernetesClientConfig {
@Bean
public AppsV1Api appsV1Api(ApiClient apiClient) {
return new AppsV1Api(apiClient);
}
}
注入8sRestTemplate
@Configuration
public class RestTemplateConfig {
private final ApiClient apiClient;
public RestTemplateConfig(ApiClient apiClient) {
this.apiClient = apiClient;
}
@Bean
public RestTemplate extendK8sRestTemplate() {
return new RestTemplate(new OkHttp3ClientHttpRequestFactory(apiClient.getHttpClient()));
}
}
4. 定時任務(wù)讀取該數(shù)據(jù),存儲到redis,保留60分鐘,可以根據(jù)自動目的設(shè)置。
里面代碼看起來比較雜亂,整體思路是使用上面注入的extendK8sRestTemplate訪問api,然后存入redis,只保留30分鐘文章來源:http://www.zghlxwxcb.cn/news/detail-827117.html
@Service
public class LogResourceTaskService implements TaskService {
@Resource
private final RedisService redisService;
@Resource
private final RestTemplate extendK8sRestTemplate;
@Resource
private final KubernetesServiceImpl kubernetesServiceImpl;
@Resource
private final ApiClient apiClient;
public LogResourceTaskService(RedisService redisService,RestTemplate extendK8sRestTemplate, KubernetesServiceImpl kubernetesServiceImpl,ApiClient apiClient) {
this.redisService = redisService;
this.extendK8sRestTemplate = extendK8sRestTemplate;
this.apiClient = apiClient;
this.kubernetesServiceImpl = kubernetesServiceImpl;
}
@Override
public void work(String guid) {
String mapKey0 = "metrics_cpu:"+PodDataSynConfig.CURRENT_POD;
String mapKey1 = "metrics_memory:"+PodDataSynConfig.CURRENT_POD;
String url = String.format("%s/apis/metrics.k8s.io/v1beta1/namespaces/%s/pods/%s", apiClient.getBasePath(),kubernetesServiceImpl.getNamespace(), PodDataSynConfig.CURRENT_POD);
ResponseEntity<PodMetrics> response = extendK8sRestTemplate.getForEntity(url, PodMetrics.class);
if (response.getBody() != null && response.hasBody()){
if(response.getBody().getContainers() != null && response.getBody().getContainers().get(0) != null){
String cpu = response.getBody().getContainers().get(0).getUsage().getCpu();
String memory = response.getBody().getContainers().get(0).getUsage().getMemory();
long timestamp = Instant.now().getEpochSecond();
redisService.getRedisTemplate().opsForZSet().add(mapKey0, timestamp+":"+cpu,timestamp);
redisService.getRedisTemplate().opsForZSet().add(mapKey1, timestamp+":"+memory,timestamp);
}
}
removeOldData(mapKey0,mapKey1);
}
public void removeOldData(String key0, String key1) {
long cutoffTimestamp = Instant.now().getEpochSecond() - 3600; // 60分鐘前
redisService.getRedisTemplate().opsForZSet().removeRangeByScore(key0, -Double.MAX_VALUE, cutoffTimestamp);
redisService.getRedisTemplate().opsForZSet().removeRangeByScore(key1, -Double.MAX_VALUE, cutoffTimestamp);
}
}
4. 完工:
這樣簡單讀取pod資源的任務(wù)就完成了,主要步驟就是 metrics server服務(wù)器的部署,然后使用restful api讀取信息。文章來源地址http://www.zghlxwxcb.cn/news/detail-827117.html
到了這里,關(guān)于通過MetricsAPI監(jiān)控pod資源使用情況(k8s資源監(jiān)控,java)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!