1.30 k8s pods查询

This commit is contained in:
dengqichen 2025-12-13 22:57:15 +08:00
parent 7e1d78a898
commit b7fd18876c
3 changed files with 31 additions and 11 deletions

View File

@ -121,14 +121,18 @@ public class K8sDeploymentApiController extends BaseController<K8sDeployment, K8
return Response.success(k8sPodService.getPodDetailByDeployment(deploymentId, podName)); return Response.success(k8sPodService.getPodDetailByDeployment(deploymentId, podName));
} }
@Operation(summary = "查询Pod日志", description = "查询指定Pod的日志内容") @Operation(
summary = "查询Pod日志",
description = "查询指定Pod的日志内容。默认返回最后500行、最近1小时的日志最大10MB。" +
"注意由于K8s API限制无法实现传统分页建议使用下载功能获取完整日志。"
)
@GetMapping("/{deploymentId}/pods/{podName}/logs") @GetMapping("/{deploymentId}/pods/{podName}/logs")
public Response<String> getPodLogs( public Response<String> getPodLogs(
@Parameter(description = "Deployment ID", required = true) @PathVariable Long deploymentId, @Parameter(description = "Deployment ID", required = true) @PathVariable Long deploymentId,
@Parameter(description = "Pod名称", required = true) @PathVariable String podName, @Parameter(description = "Pod名称", required = true) @PathVariable String podName,
@Parameter(description = "容器名称(可选,默认第一个容器)") @RequestParam(required = false) String container, @Parameter(description = "容器名称(可选,默认第一个容器)") @RequestParam(required = false) String container,
@Parameter(description = "返回最后N行日志可选") @RequestParam(required = false) Integer tail, @Parameter(description = "返回最后N行日志可选默认500行最大10000行") @RequestParam(required = false) Integer tail,
@Parameter(description = "返回最近N秒的日志可选") @RequestParam(required = false) Integer sinceSeconds @Parameter(description = "返回最近N秒的日志可选默认3600秒即1小时最大86400秒即24小时") @RequestParam(required = false) Integer sinceSeconds
) { ) {
return Response.success(k8sPodService.getPodLogs(deploymentId, podName, container, tail, sinceSeconds)); return Response.success(k8sPodService.getPodLogs(deploymentId, podName, container, tail, sinceSeconds));
} }

View File

@ -107,7 +107,7 @@ public class K8sServiceIntegrationImpl extends BaseExternalSystemIntegration imp
// 创建K8S ApiClient并测试连接直接使用config作为kubeconfig // 创建K8S ApiClient并测试连接直接使用config作为kubeconfig
ApiClient client = Config.fromConfig(new StringReader(config)); ApiClient client = Config.fromConfig(new StringReader(config));
client.setConnectTimeout(15000); // 15秒连接超时 client.setConnectTimeout(15000); // 15秒连接超时
client.setReadTimeout(30000); // 30秒读取超时 client.setReadTimeout(120000); // 120秒读取超时优化日志查询等耗时操作
VersionApi versionApi = new VersionApi(client); VersionApi versionApi = new VersionApi(client);
VersionInfo version = versionApi.getCode(); VersionInfo version = versionApi.getCode();
@ -601,13 +601,20 @@ public class K8sServiceIntegrationImpl extends BaseExternalSystemIntegration imp
@Override @Override
public String getPodLogs(ExternalSystem externalSystem, String namespace, String podName, public String getPodLogs(ExternalSystem externalSystem, String namespace, String podName,
String container, Integer tail, Integer sinceSeconds, Boolean follow) { String container, Integer tail, Integer sinceSeconds, Boolean follow) {
log.info("查询K8S Pod日志集群: {}, 命名空间: {}, Pod: {}, 容器: {}", // 智能默认参数优化性能避免超时
externalSystem.getName(), namespace, podName, container); Integer effectiveTail = tail != null ? tail : 500; // 默认最后500行
Integer effectiveSinceSeconds = sinceSeconds != null ? sinceSeconds : 3600; // 默认最近1小时
log.info("查询K8S Pod日志集群: {}, 命名空间: {}, Pod: {}, 容器: {}, tail: {}, sinceSeconds: {}",
externalSystem.getName(), namespace, podName, container, effectiveTail, effectiveSinceSeconds);
try { try {
K8sApiClientCache cache = getApiClientCache(externalSystem); K8sApiClientCache cache = getApiClientCache(externalSystem);
CoreV1Api api = new CoreV1Api(cache.apiClient); CoreV1Api api = new CoreV1Api(cache.apiClient);
// 日志大小限制10MB防止OOM
Integer limitBytes = 10 * 1024 * 1024;
// 查询Pod日志 // 查询Pod日志
String logs = api.readNamespacedPodLog( String logs = api.readNamespacedPodLog(
podName, // Pod名称 podName, // Pod名称
@ -615,15 +622,16 @@ public class K8sServiceIntegrationImpl extends BaseExternalSystemIntegration imp
container, // 容器名称可选 container, // 容器名称可选
follow != null && follow, // 是否持续输出 follow != null && follow, // 是否持续输出
null, // insecureSkipTLSVerifyBackend null, // insecureSkipTLSVerifyBackend
null, // limitBytes limitBytes, // limitBytes10MB限制
"false", // pretty "false", // pretty
false, // previous是否查询上一个容器的日志 false, // previous是否查询上一个容器的日志
sinceSeconds, // sinceSeconds effectiveSinceSeconds, // sinceSeconds使用智能默认值
tail, // tail effectiveTail, // tail使用智能默认值
false // timestamps false // timestamps
); );
log.info("查询Pod日志成功日志长度: {}", logs != null ? logs.length() : 0); int logLength = logs != null ? logs.length() : 0;
log.info("查询Pod日志成功日志长度: {} bytes, 行数约: {}", logLength, logLength > 0 ? logs.split("\n").length : 0);
return logs != null ? logs : ""; return logs != null ? logs : "";
} catch (ApiException e) { } catch (ApiException e) {
@ -875,7 +883,7 @@ public class K8sServiceIntegrationImpl extends BaseExternalSystemIntegration imp
// 直接使用config作为kubeconfig内容 // 直接使用config作为kubeconfig内容
ApiClient client = Config.fromConfig(new StringReader(config)); ApiClient client = Config.fromConfig(new StringReader(config));
client.setConnectTimeout(15000); // 15秒连接超时 client.setConnectTimeout(15000); // 15秒连接超时
client.setReadTimeout(30000); // 30秒读取超时 client.setReadTimeout(120000); // 120秒读取超时优化日志查询等耗时操作
return client; return client;
} }
} }

View File

@ -135,6 +135,14 @@ public class K8sPodServiceImpl implements IK8sPodService {
log.info("查询Pod日志deploymentId: {}, podName: {}, container: {}, tail: {}, sinceSeconds: {}", log.info("查询Pod日志deploymentId: {}, podName: {}, container: {}, tail: {}, sinceSeconds: {}",
deploymentId, podName, container, tail, sinceSeconds); deploymentId, podName, container, tail, sinceSeconds);
// 参数校验
if (tail != null && (tail < 1 || tail > 10000)) {
throw new BusinessException(ResponseCode.INVALID_PARAM, new Object[]{"tail参数范围1-10000行"});
}
if (sinceSeconds != null && (sinceSeconds < 1 || sinceSeconds > 86400)) {
throw new BusinessException(ResponseCode.INVALID_PARAM, new Object[]{"sinceSeconds参数范围1-86400秒24小时"});
}
// 1. 查询K8sDeployment // 1. 查询K8sDeployment
K8sDeployment deployment = k8sDeploymentRepository.findById(deploymentId) K8sDeployment deployment = k8sDeploymentRepository.findById(deploymentId)
.orElseThrow(() -> new BusinessException(ResponseCode.K8S_RESOURCE_NOT_FOUND)); .orElseThrow(() -> new BusinessException(ResponseCode.K8S_RESOURCE_NOT_FOUND));