Commit cba3c864 authored by 黎博's avatar 黎博

优化获取namespace列表以及健康检查

parent 613c012b
...@@ -2,7 +2,6 @@ package cn.qg.holmes.service.k8s; ...@@ -2,7 +2,6 @@ package cn.qg.holmes.service.k8s;
import cn.qg.holmes.entity.k8s.DockerProject; import cn.qg.holmes.entity.k8s.DockerProject;
import cn.qg.holmes.entity.k8s.ServiceCreateVo; import cn.qg.holmes.entity.k8s.ServiceCreateVo;
import com.alibaba.fastjson.JSON;
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;
...@@ -13,6 +12,7 @@ import io.fabric8.kubernetes.client.Config; ...@@ -13,6 +12,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.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -27,6 +27,9 @@ import java.util.*; ...@@ -27,6 +27,9 @@ import java.util.*;
@Component @Component
public class K8sService { public class K8sService {
@Value("${no.healthcheck.service}")
private String noHealthCheckService;
private KubernetesClient kubernetesClient; private KubernetesClient kubernetesClient;
public K8sService() { public K8sService() {
...@@ -111,14 +114,66 @@ public class K8sService { ...@@ -111,14 +114,66 @@ public class K8sService {
List<Map<String, Object>> resultList = new ArrayList<>(); List<Map<String, Object>> resultList = new ArrayList<>();
List<Namespace> namespaceList = kubernetesClient.namespaces().list().getItems(); List<Namespace> namespaceList = kubernetesClient.namespaces().list().getItems();
for (Namespace namespace : namespaceList) { for (Namespace namespace : namespaceList) {
if (namespace.getMetadata().getAnnotations() != null && namespace.getMetadata().getAnnotations().get("description").equals(env)) { if (namespace.getMetadata().getAnnotations() != null && env.equals("all")) {
String name = namespace.getMetadata().getName();
// 获取Service
Service service = kubernetesClient.services().inNamespace(name).withName("mysql").get();
// 获取Pod
Map<String, String> labels = new HashMap<>();
labels.put("qcloud-app", "mysql");
labels.put("type", "base");
List<Pod> podList = kubernetesClient.pods().inNamespace(name).withLabels(labels).list().getItems();
Integer port = null;
String host = null;
if (service != null) {
port = service.getSpec().getPorts().get(0).getNodePort();
}
if (podList.size() > 0) {
host = podList.get(0).getStatus().getHostIP();
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
Map<String, Object> map = new HashMap<>();
map.put("name", name);
map.put("description", namespace.getMetadata().getAnnotations().get("description"));
map.put("owner", namespace.getMetadata().getAnnotations().get("owner"));
map.put("status", namespace.getStatus().getPhase());
map.put("mysqlHost", host);
map.put("mysqlPort", port);
try {
map.put("createdAt", df.parse(namespace.getMetadata().getCreationTimestamp()));
} catch (Exception e) {
log.info("时间解析异常!");
e.printStackTrace();
map.put("createdAt", namespace.getMetadata().getCreationTimestamp());
}
resultList.add(map);
} else if (namespace.getMetadata().getAnnotations() != null && namespace.getMetadata().getAnnotations().get("description").equals(env)) {
String name = namespace.getMetadata().getName();
// 获取Service
Service service = kubernetesClient.services().inNamespace(name).withName("mysql").get();
// 获取Pod
Map<String, String> labels = new HashMap<>();
labels.put("qcloud-app", "mysql");
labels.put("type", "base");
List<Pod> podList = kubernetesClient.pods().inNamespace(name).withLabels(labels).list().getItems();
Integer port = null;
String host = null;
if (service != null) {
port = service.getSpec().getPorts().get(0).getNodePort();
}
if (podList.size() > 0) {
host = podList.get(0).getStatus().getHostIP();
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(TimeZone.getTimeZone("UTC")); df.setTimeZone(TimeZone.getTimeZone("UTC"));
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("name", namespace.getMetadata().getName()); map.put("name", name);
map.put("description", namespace.getMetadata().getAnnotations().get("description")); map.put("description", namespace.getMetadata().getAnnotations().get("description"));
map.put("owner", namespace.getMetadata().getAnnotations().get("owner")); map.put("owner", namespace.getMetadata().getAnnotations().get("owner"));
map.put("status", namespace.getStatus().getPhase()); map.put("status", namespace.getStatus().getPhase());
map.put("mysqlHost", host);
map.put("mysqlPort", port);
try { try {
map.put("createdAt", df.parse(namespace.getMetadata().getCreationTimestamp())); map.put("createdAt", df.parse(namespace.getMetadata().getCreationTimestamp()));
} catch (Exception e) { } catch (Exception e) {
...@@ -141,6 +196,7 @@ public class K8sService { ...@@ -141,6 +196,7 @@ public class K8sService {
List<Pod> podList = kubernetesClient.pods().inNamespace(namespace).list().getItems(); List<Pod> podList = kubernetesClient.pods().inNamespace(namespace).list().getItems();
List<Map<String, Object>> result = new ArrayList<>(); List<Map<String, Object>> result = new ArrayList<>();
for (Pod pod : podList) { for (Pod pod : podList) {
System.out.println(pod);
if (pod.getStatus().getPhase().equals("Running") || pod.getStatus().getPhase().equals("Pending")) { if (pod.getStatus().getPhase().equals("Running") || pod.getStatus().getPhase().equals("Pending")) {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
// 端口映射 // 端口映射
...@@ -419,25 +475,29 @@ public class K8sService { ...@@ -419,25 +475,29 @@ public class K8sService {
resourceRequirements.setLimits(limits); resourceRequirements.setLimits(limits);
container.setResources(resourceRequirements); container.setResources(resourceRequirements);
// livenessProbe // 需要做健康检查的不创建存活检查和就绪检查
Probe livenessProbe = new Probe(); List<String> noHealthCheckServiceList = Arrays.asList(noHealthCheckService.split(","));
ExecAction execAction = new ExecAction(); if (!noHealthCheckServiceList.contains(serviceName)) {
List<String> commandList = new ArrayList<>(); // livenessProbe
commandList.add("/home/quant_group/readyCheck.sh"); Probe livenessProbe = new Probe();
execAction.setCommand(commandList); ExecAction execAction = new ExecAction();
livenessProbe.setExec(execAction); List<String> commandList = new ArrayList<>();
livenessProbe.setInitialDelaySeconds(200); commandList.add("/home/quant_group/readyCheck.sh");
livenessProbe.setSuccessThreshold(1); execAction.setCommand(commandList);
livenessProbe.setFailureThreshold(5); livenessProbe.setExec(execAction);
container.setLivenessProbe(livenessProbe); livenessProbe.setInitialDelaySeconds(200);
livenessProbe.setSuccessThreshold(1);
//readinessProbe livenessProbe.setFailureThreshold(5);
Probe readinessProbe = new Probe(); container.setLivenessProbe(livenessProbe);
readinessProbe.setExec(execAction);
readinessProbe.setInitialDelaySeconds(30); //readinessProbe
readinessProbe.setTimeoutSeconds(2); Probe readinessProbe = new Probe();
readinessProbe.setPeriodSeconds(5); readinessProbe.setExec(execAction);
container.setReadinessProbe(readinessProbe); readinessProbe.setInitialDelaySeconds(30);
readinessProbe.setTimeoutSeconds(2);
readinessProbe.setPeriodSeconds(5);
container.setReadinessProbe(readinessProbe);
}
containerList.add(container); containerList.add(container);
...@@ -1971,18 +2031,18 @@ public class K8sService { ...@@ -1971,18 +2031,18 @@ public class K8sService {
} }
public void podTest() { public void podTest() {
List<Pod> podList = kubernetesClient.pods().inNamespace("k8s").list().getItems(); Map<String, String> labels = new HashMap<>();
for (Pod pod: podList) { labels.put("qcloud-app", "mysql");
System.out.println(pod.getMetadata().getName()); labels.put("type", "base");
System.out.println(pod.getMetadata()); Pod pod = kubernetesClient.pods().inNamespace("k8s").withLabels(labels).list().getItems().get(0);
System.out.println(JSON.toJSON(pod.getSpec())); System.out.println(pod);
System.out.println(JSON.toJSON(pod.getStatus()));
}
} }
public static void main(String[] args) { public static void main(String[] args) {
K8sService k8sService = new K8sService(); K8sService k8sService = new K8sService();
// k8sService.createNewNamespace("test6", "dev", "黎博"); // k8sService.createNewNamespace("test6", "dev", "黎博");
// k8sService.podTest();
// k8sService.getPodList("k8s");
k8sService.podTest(); k8sService.podTest();
} }
} }
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