Commit 247b02ab authored by 黎博's avatar 黎博

优化健康检查时间及/k8s/service/list接口

parent 5ec5bec0
...@@ -4,6 +4,7 @@ import cn.qg.holmes.entity.k8s.DockerProject; ...@@ -4,6 +4,7 @@ import cn.qg.holmes.entity.k8s.DockerProject;
import cn.qg.holmes.entity.k8s.ServiceCreateVo; import cn.qg.holmes.entity.k8s.ServiceCreateVo;
import cn.qg.holmes.utils.RedisUtils; import cn.qg.holmes.utils.RedisUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.fabric8.kubernetes.api.model.*; import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.api.model.apps.Deployment; import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentSpec; import io.fabric8.kubernetes.api.model.apps.DeploymentSpec;
...@@ -14,6 +15,7 @@ import io.fabric8.kubernetes.client.Config; ...@@ -14,6 +15,7 @@ import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.DefaultKubernetesClient; import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.KubernetesClient;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
...@@ -38,6 +40,9 @@ public class K8sService { ...@@ -38,6 +40,9 @@ public class K8sService {
private KubernetesClient kubernetesClient; private KubernetesClient kubernetesClient;
@Autowired
DockerProjectService dockerProjectService;
public K8sService() { public K8sService() {
try { try {
String configYAML = String.join("\n", readConfigFile("kube-config.yml")); String configYAML = String.join("\n", readConfigFile("kube-config.yml"));
...@@ -368,7 +373,8 @@ public class K8sService { ...@@ -368,7 +373,8 @@ public class K8sService {
*/ */
public Map<String, Object> formatPodInfo(Pod pod) { public Map<String, Object> formatPodInfo(Pod pod) {
Map<String, Object> podMap = new HashMap<>(); Map<String, Object> podMap = new HashMap<>();
podMap.put("serviceName", pod.getMetadata().getLabels().get("qcloud-app")); String serviceName = pod.getMetadata().getLabels().get("qcloud-app");
podMap.put("serviceName", serviceName);
podMap.put("podName", pod.getMetadata().getName()); podMap.put("podName", pod.getMetadata().getName());
podMap.put("status", getPodStatus(pod)); podMap.put("status", getPodStatus(pod));
podMap.put("podIp", pod.getStatus().getPodIP()); podMap.put("podIp", pod.getStatus().getPodIP());
...@@ -398,6 +404,15 @@ public class K8sService { ...@@ -398,6 +404,15 @@ public class K8sService {
} }
if (pod.getMetadata().getLabels() != null) { if (pod.getMetadata().getLabels() != null) {
podMap.put("labels", pod.getMetadata().getLabels()); podMap.put("labels", pod.getMetadata().getLabels());
// 对于非基础服务,新增服务描述,方便前端展示
if (!StringUtils.equals(pod.getMetadata().getLabels().get("type"), "base")) {
QueryWrapper<DockerProject> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("project_name", serviceName);
DockerProject dockerProject = dockerProjectService.getOne(queryWrapper);
if (dockerProject != null) {
podMap.put("desc", dockerProject.getDesc());
}
}
} }
return podMap; return podMap;
} }
...@@ -675,7 +690,7 @@ public class K8sService { ...@@ -675,7 +690,7 @@ public class K8sService {
Probe livenessProbe = new Probe(); Probe livenessProbe = new Probe();
livenessProbe.setExec(execAction); livenessProbe.setExec(execAction);
// 启动延时,容器延时启动健康检查的时间 // 启动延时,容器延时启动健康检查的时间
livenessProbe.setInitialDelaySeconds(600); livenessProbe.setInitialDelaySeconds(300);
// 间隔时间,进行健康检查的时间间隔 // 间隔时间,进行健康检查的时间间隔
livenessProbe.setPeriodSeconds(60); livenessProbe.setPeriodSeconds(60);
// 响应超时,每次健康检查响应的最大超时时间 // 响应超时,每次健康检查响应的最大超时时间
...@@ -683,22 +698,22 @@ public class K8sService { ...@@ -683,22 +698,22 @@ public class K8sService {
// 健康阈值,表示后端容器从失败到成功的连续健康检查成功次数 // 健康阈值,表示后端容器从失败到成功的连续健康检查成功次数
livenessProbe.setSuccessThreshold(1); livenessProbe.setSuccessThreshold(1);
// 不健康阈值,表示后端容器从成功到失败的连续健康检查成功次数 // 不健康阈值,表示后端容器从成功到失败的连续健康检查成功次数
livenessProbe.setFailureThreshold(3); livenessProbe.setFailureThreshold(5);
container.setLivenessProbe(livenessProbe); container.setLivenessProbe(livenessProbe);
//readinessProbe 就绪检查,检查容器是否就绪,不就绪则停止转发流量到当前实例 //readinessProbe 就绪检查,检查容器是否就绪,不就绪则停止转发流量到当前实例
Probe readinessProbe = new Probe(); Probe readinessProbe = new Probe();
readinessProbe.setExec(execAction); readinessProbe.setExec(execAction);
// 启动延时 // 启动延时
readinessProbe.setInitialDelaySeconds(600); readinessProbe.setInitialDelaySeconds(60);
// 响应超时 // 响应超时
readinessProbe.setTimeoutSeconds(5); readinessProbe.setTimeoutSeconds(5);
// 间隔时间 // 间隔时间
readinessProbe.setPeriodSeconds(30); readinessProbe.setPeriodSeconds(10);
// 健康阈值 // 健康阈值
readinessProbe.setSuccessThreshold(1); readinessProbe.setSuccessThreshold(1);
// 不健康阈值 // 不健康阈值,有些服务启动较慢,因此设置大一点
readinessProbe.setFailureThreshold(3); readinessProbe.setFailureThreshold(30);
container.setReadinessProbe(readinessProbe); container.setReadinessProbe(readinessProbe);
} }
...@@ -889,7 +904,7 @@ public class K8sService { ...@@ -889,7 +904,7 @@ public class K8sService {
commandList.add("/home/quant_group/readyCheck.sh"); commandList.add("/home/quant_group/readyCheck.sh");
execAction.setCommand(commandList); execAction.setCommand(commandList);
livenessProbe.setExec(execAction); livenessProbe.setExec(execAction);
livenessProbe.setInitialDelaySeconds(600); livenessProbe.setInitialDelaySeconds(300);
livenessProbe.setSuccessThreshold(1); livenessProbe.setSuccessThreshold(1);
livenessProbe.setFailureThreshold(5); livenessProbe.setFailureThreshold(5);
livenessProbe.setPeriodSeconds(60); livenessProbe.setPeriodSeconds(60);
...@@ -898,9 +913,10 @@ public class K8sService { ...@@ -898,9 +913,10 @@ public class K8sService {
//readinessProbe //readinessProbe
Probe readinessProbe = new Probe(); Probe readinessProbe = new Probe();
readinessProbe.setExec(execAction); readinessProbe.setExec(execAction);
readinessProbe.setInitialDelaySeconds(600); readinessProbe.setInitialDelaySeconds(60);
readinessProbe.setTimeoutSeconds(2); readinessProbe.setTimeoutSeconds(5);
readinessProbe.setPeriodSeconds(15); readinessProbe.setPeriodSeconds(10);
readinessProbe.setFailureThreshold(30);
container.setReadinessProbe(readinessProbe); container.setReadinessProbe(readinessProbe);
containerList.add(container); containerList.add(container);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment