Commit cba3c864 authored by 黎博's avatar 黎博

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

parent 613c012b
......@@ -2,7 +2,6 @@ package cn.qg.holmes.service.k8s;
import cn.qg.holmes.entity.k8s.DockerProject;
import cn.qg.holmes.entity.k8s.ServiceCreateVo;
import com.alibaba.fastjson.JSON;
import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentSpec;
......@@ -13,6 +12,7 @@ import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
......@@ -27,6 +27,9 @@ import java.util.*;
@Component
public class K8sService {
@Value("${no.healthcheck.service}")
private String noHealthCheckService;
private KubernetesClient kubernetesClient;
public K8sService() {
......@@ -111,14 +114,66 @@ public class K8sService {
List<Map<String, Object>> resultList = new ArrayList<>();
List<Namespace> namespaceList = kubernetesClient.namespaces().list().getItems();
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'");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
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("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) {
......@@ -141,6 +196,7 @@ public class K8sService {
List<Pod> podList = kubernetesClient.pods().inNamespace(namespace).list().getItems();
List<Map<String, Object>> result = new ArrayList<>();
for (Pod pod : podList) {
System.out.println(pod);
if (pod.getStatus().getPhase().equals("Running") || pod.getStatus().getPhase().equals("Pending")) {
Map<String, Object> map = new HashMap<>();
// 端口映射
......@@ -419,6 +475,9 @@ public class K8sService {
resourceRequirements.setLimits(limits);
container.setResources(resourceRequirements);
// 需要做健康检查的不创建存活检查和就绪检查
List<String> noHealthCheckServiceList = Arrays.asList(noHealthCheckService.split(","));
if (!noHealthCheckServiceList.contains(serviceName)) {
// livenessProbe
Probe livenessProbe = new Probe();
ExecAction execAction = new ExecAction();
......@@ -438,6 +497,7 @@ public class K8sService {
readinessProbe.setTimeoutSeconds(2);
readinessProbe.setPeriodSeconds(5);
container.setReadinessProbe(readinessProbe);
}
containerList.add(container);
......@@ -1971,18 +2031,18 @@ public class K8sService {
}
public void podTest() {
List<Pod> podList = kubernetesClient.pods().inNamespace("k8s").list().getItems();
for (Pod pod: podList) {
System.out.println(pod.getMetadata().getName());
System.out.println(pod.getMetadata());
System.out.println(JSON.toJSON(pod.getSpec()));
System.out.println(JSON.toJSON(pod.getStatus()));
}
Map<String, String> labels = new HashMap<>();
labels.put("qcloud-app", "mysql");
labels.put("type", "base");
Pod pod = kubernetesClient.pods().inNamespace("k8s").withLabels(labels).list().getItems().get(0);
System.out.println(pod);
}
public static void main(String[] args) {
K8sService k8sService = new K8sService();
// k8sService.createNewNamespace("test6", "dev", "黎博");
// k8sService.podTest();
// k8sService.getPodList("k8s");
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