Commit 47240c7f authored by 黎博's avatar 黎博

新增一些接口

parent f5daa84e
...@@ -71,13 +71,82 @@ public class K8sController { ...@@ -71,13 +71,82 @@ public class K8sController {
try { try {
String serviceName = serviceCreateVo.getServiceName(); String serviceName = serviceCreateVo.getServiceName();
String type = serviceCreateVo.getType(); String type = serviceCreateVo.getType();
String namespace = serviceCreateVo.getNamespace();
String image = serviceCreateVo.getImage();
QueryWrapper<DockerProject> queryWrapper = new QueryWrapper<>(); QueryWrapper<DockerProject> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("project_name", serviceName); queryWrapper.eq("project_name", serviceName);
DockerProject dockerProject = dockerProjectService.getOne(queryWrapper); if (type.equals("base")) {
if (type.equals("java")) { // Service
tkeService.createJavaDeployment(serviceCreateVo, dockerProject); if (!tkeService.queryIfServiceExistByName(namespace, serviceName)) {
if (serviceName.equals("mysql")) {
tkeService.createMysqlService(namespace);
}
if (serviceName.equals("redis")) {
tkeService.createRedisService(namespace);
}
if (serviceName.equals("mongodb")) {
tkeService.createMongodbService(namespace);
}
if (serviceName.equals("zookeeper")) {
tkeService.createZookeeperService(namespace);
}
if (serviceName.equals("rabbitmq")) {
tkeService.createRabbitmqService(namespace);
}
}
// Pvc
if (!tkeService.queryIfPvcExistByName(namespace, serviceName)) {
if (serviceName.equals("mysql")) {
tkeService.createMysqlPvc(namespace);
}
if (serviceName.equals("redis")) {
tkeService.createRedisPvc(namespace);
}
if (serviceName.equals("mongodb")) {
tkeService.createMongodbPvc(namespace);
}
if (serviceName.equals("zookeeper")) {
tkeService.createZookeeperPvc(namespace);
}
if (serviceName.equals("rabbitmq")) {
tkeService.createRabbitmqPvc(namespace);
}
}
// deployment
if (!tkeService.queryIfDeploymentExistByName(namespace, serviceName)) {
if (serviceName.equals("mysql")) {
tkeService.createMysqlDeployment(namespace, image);
}
if (serviceName.equals("redis")) {
tkeService.createRedisDeployment(namespace, image);
}
if (serviceName.equals("mongodb")) {
tkeService.createMongoDbDeployment(namespace, image);
}
if (serviceName.equals("zookeeper")) {
tkeService.createZookeeperDeployment(namespace, image);
}
if (serviceName.equals("rabbitmq")) {
tkeService.createRabbitmqDeployment(namespace, image);
}
}
} else if (type.equals("java")) {
DockerProject dockerProject = dockerProjectService.getOne(queryWrapper);
if (!tkeService.queryIfServiceExistByName(namespace, serviceName)) {
tkeService.createJavaService(namespace, serviceName);
}
if (!tkeService.queryIfDeploymentExistByName(namespace, serviceName)) {
tkeService.createJavaDeployment(serviceCreateVo, dockerProject);
}
} else if (type.equals("ui") || type.equals("node")) { } else if (type.equals("ui") || type.equals("node")) {
tkeService.createUIAndNodeDeployment(serviceCreateVo, dockerProject); DockerProject dockerProject = dockerProjectService.getOne(queryWrapper);
String label = serviceCreateVo.getLabel();
if (!tkeService.queryIfServiceExistByName(namespace, serviceName)) {
tkeService.createUIAndNodeService(namespace, serviceName, type, label);
}
if (!tkeService.queryIfDeploymentExistByName(namespace, serviceName)) {
tkeService.createUIAndNodeDeployment(serviceCreateVo, dockerProject);
}
} else { } else {
return JsonResult.buildErrorStateResult("暂不支持!", false); return JsonResult.buildErrorStateResult("暂不支持!", false);
} }
...@@ -119,6 +188,6 @@ public class K8sController { ...@@ -119,6 +188,6 @@ public class K8sController {
*/ */
@PostMapping("/pod/delete") @PostMapping("/pod/delete")
public JsonResult deletePodByName(String namespace, String serviceName) { public JsonResult deletePodByName(String namespace, String serviceName) {
return JsonResult.buildSuccessResult(tkeService.deleteService(namespace, serviceName)); return JsonResult.buildSuccessResult(tkeService.deleteDeployment(namespace, serviceName));
} }
} }
...@@ -45,6 +45,7 @@ public class TkeService { ...@@ -45,6 +45,7 @@ public class TkeService {
/** /**
* 读取k8s配置文件 * 读取k8s配置文件
*
* @param file 配置文件名 * @param file 配置文件名
* @return * @return
* @throws IOException * @throws IOException
...@@ -61,14 +62,58 @@ public class TkeService { ...@@ -61,14 +62,58 @@ public class TkeService {
return result; return result;
} }
/**
* 构造ObjectMeta对象
*
* @param namespace 环境
* @param serviceName 服务名称
* @param type 类型,java、ui、base等等
* @return
*/
public ObjectMeta buildObjectMeta(String namespace, String serviceName, String type) {
ObjectMeta objectMeta = new ObjectMeta();
Map<String, String> labels = new HashMap<>();
labels.put("type", type);
labels.put("qcloud-app", serviceName);
objectMeta.setLabels(labels);
objectMeta.setName(serviceName);
objectMeta.setNamespace(namespace);
return objectMeta;
}
public ResourceRequirements buildResourceRequirements(String cpuRequestAmount, String cpuRequestFormat, String memRequestAmount, String memRequestFormat,
String cpuLimitAmount, String cpuLimitFormat, String memLimitAmount, String memLimitFormat) {
ResourceRequirements resourceRequirements = new ResourceRequirements();
Map<String, Quantity> requests = new HashMap<>();
Map<String, Quantity> limits = new HashMap<>();
Quantity cpuQuantity = new Quantity();
Quantity memoryQuantity = new Quantity();
Quantity cpuLimit = new Quantity();
Quantity memoryLimit = new Quantity();
cpuQuantity.setAmount(cpuRequestAmount);
cpuQuantity.setFormat(cpuRequestFormat);
memoryQuantity.setAmount(memRequestAmount);
memoryQuantity.setFormat(memRequestFormat);
cpuLimit.setAmount(cpuLimitAmount);
cpuLimit.setFormat(cpuLimitFormat);
memoryLimit.setAmount(memLimitAmount);
memoryLimit.setFormat(memLimitFormat);
requests.put("cpu", cpuQuantity);
requests.put("memory", memoryQuantity);
resourceRequirements.setRequests(requests);
resourceRequirements.setLimits(limits);
return resourceRequirements;
}
/** /**
* 获取namespace列表 * 获取namespace列表
*
* @return * @return
*/ */
public List<Map<String, Object>> getNamespaceList() { public List<Map<String, Object>> getNamespaceList() {
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) {
log.info(namespace.toString()); log.info(namespace.toString());
if (namespace.getMetadata().getAnnotations() != null) { if (namespace.getMetadata().getAnnotations() != null) {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
...@@ -76,33 +121,33 @@ public class TkeService { ...@@ -76,33 +121,33 @@ public class TkeService {
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("createdAt",namespace.getMetadata().getCreationTimestamp()); map.put("createdAt", namespace.getMetadata().getCreationTimestamp());
resultList.add(map); resultList.add(map);
} }
} }
return resultList; return resultList;
} }
/** /**
* 获取运行中的pod列表 * 获取运行中的pod列表
*
* @param namespace 环境 * @param namespace 环境
* @return * @return
*/ */
public List<Map<String, Object>> getPodList(String namespace) public List<Map<String, Object>> getPodList(String namespace) {
{
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) {
if (pod.getStatus().getPhase().equals("Running")) { if (pod.getStatus().getPhase().equals("Running")) {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
// 端口映射 // 端口映射
List<Map<String, Object>> portMappingList = new ArrayList<>(); List<Map<String, Object>> portMappingList = new ArrayList<>();
ObjectMeta podMetadata= pod.getMetadata(); ObjectMeta podMetadata = pod.getMetadata();
String serviceName = podMetadata.getLabels().get("qcloud-app"); String serviceName = podMetadata.getLabels().get("qcloud-app");
Service service = kubernetesClient.services().inNamespace(namespace).withName(serviceName).get(); Service service = kubernetesClient.services().inNamespace(namespace).withName(serviceName).get();
List<ServicePort> servicePortList = service.getSpec().getPorts(); List<ServicePort> servicePortList = service.getSpec().getPorts();
if (servicePortList.size() > 0) { if (servicePortList.size() > 0) {
for (ServicePort servicePort: servicePortList) { for (ServicePort servicePort : servicePortList) {
if (servicePort.getNodePort() != null) { if (servicePort.getNodePort() != null) {
map.put("port_" + servicePort.getName(), servicePort.getNodePort()); map.put("port_" + servicePort.getName(), servicePort.getNodePort());
} }
...@@ -137,8 +182,9 @@ public class TkeService { ...@@ -137,8 +182,9 @@ public class TkeService {
/** /**
* 重置pod * 重置pod
*
* @param namespace 环境 * @param namespace 环境
* @param podName podName * @param podName podName
* @return * @return
*/ */
public boolean resetPod(String namespace, String podName) { public boolean resetPod(String namespace, String podName) {
...@@ -147,41 +193,33 @@ public class TkeService { ...@@ -147,41 +193,33 @@ public class TkeService {
/** /**
* 删除一个pod * 删除一个pod
* @param namespace 环境 * @param namespace 环境
* @param serviceName 服务名 * @param serviceName 服务名
* @return * @return
*/ */
public boolean deleteService(String namespace, String serviceName) { public boolean deleteDeployment(String namespace, String serviceName) {
// 删除service // 删除service
kubernetesClient.services().inNamespace(namespace).withName(serviceName).delete(); kubernetesClient.services().inNamespace(namespace).withName(serviceName).delete();
Map<String, String > map = new HashMap<>(); Map<String, String> map = new HashMap<>();
map.put("qcloud-app", serviceName); map.put("qcloud-app", serviceName);
LabelSelector labelSelector = new LabelSelector(); LabelSelector labelSelector = new LabelSelector();
labelSelector.setMatchLabels(map); labelSelector.setMatchLabels(map);
// 删除deployment // 删除deployment
boolean deploymentResult = kubernetesClient.apps().deployments().inNamespace(namespace).withName(serviceName).delete(); return kubernetesClient.apps().deployments().inNamespace(namespace).withName(serviceName).delete();
// 删除replicationControllers
boolean rpcResult = kubernetesClient.replicationControllers().inNamespace(namespace).withLabelSelector(labelSelector).delete();
// 删除pvc
boolean pvcResult = kubernetesClient.persistentVolumeClaims().inNamespace(namespace).withName(serviceName).delete();
// 删除service
boolean serviceResult = kubernetesClient.services().inNamespace(namespace).withLabelSelector(labelSelector).delete();
// 删除ingress
boolean ingressResult = kubernetesClient.extensions().ingresses().inNamespace(namespace).withName(serviceName).delete();
return deploymentResult && rpcResult && pvcResult && serviceResult && ingressResult;
} }
/** /**
* 部署Java服务 * 部署Java服务
*
* @return * @return
*/ */
public Service createJavaService(String namespace, String serviceName, String serviceType, String label) { public Service createJavaService(String namespace, String serviceName) {
Service service = new Service(); Service service = new Service();
ObjectMeta objectMeta = new ObjectMeta(); ObjectMeta objectMeta = new ObjectMeta();
ServiceSpec serviceSpec = new ServiceSpec(); ServiceSpec serviceSpec = new ServiceSpec();
// 设置meta // 设置meta
Map<String, String> labels = new HashMap<>(); Map<String, String> labels = new HashMap<>();
labels.put("type", label); labels.put("type", "java");
labels.put("qcloud-app", serviceName); labels.put("qcloud-app", serviceName);
objectMeta.setName(serviceName); objectMeta.setName(serviceName);
...@@ -206,7 +244,7 @@ public class TkeService { ...@@ -206,7 +244,7 @@ public class TkeService {
selector.put("qcloud-app", serviceName); selector.put("qcloud-app", serviceName);
// 设置serviceSpec // 设置serviceSpec
serviceSpec.setType(serviceType); serviceSpec.setType("java");
serviceSpec.setPorts(portList); serviceSpec.setPorts(portList);
serviceSpec.setSelector(selector); serviceSpec.setSelector(selector);
...@@ -220,6 +258,7 @@ public class TkeService { ...@@ -220,6 +258,7 @@ public class TkeService {
/** /**
* 部署Java Deployment * 部署Java Deployment
*
* @param serviceCreateVo * @param serviceCreateVo
* @param dockerProject * @param dockerProject
* @return * @return
...@@ -410,7 +449,8 @@ public class TkeService { ...@@ -410,7 +449,8 @@ public class TkeService {
/** /**
* 创建UI或Node 服务 * 创建UI或Node 服务
* @param namespace 环境 *
* @param namespace 环境
* @param serviceName 服务名 * @param serviceName 服务名
* @param serviceType 服务类型 * @param serviceType 服务类型
* @param label * @param label
...@@ -451,6 +491,7 @@ public class TkeService { ...@@ -451,6 +491,7 @@ public class TkeService {
/** /**
* 部署UI或Node deployment * 部署UI或Node deployment
*
* @param serviceCreateVo * @param serviceCreateVo
* @param dockerProject * @param dockerProject
* @return * @return
...@@ -627,17 +668,19 @@ public class TkeService { ...@@ -627,17 +668,19 @@ public class TkeService {
return kubernetesClient.apps().deployments().inNamespace(namespace).create(deployment); return kubernetesClient.apps().deployments().inNamespace(namespace).create(deployment);
} }
public Service createRedisService(String namespace, String serviceName) { public PersistentVolumeClaim createRedisPvc(String namespace) {
PersistentVolumeClaim redisPvc = new PersistentVolumeClaim();
return kubernetesClient.persistentVolumeClaims().inNamespace(namespace).create(redisPvc);
}
public Service createRedisService(String namespace) {
Service redisService = new Service(); Service redisService = new Service();
ObjectMeta objectMeta = new ObjectMeta();
ObjectMeta objectMeta = buildObjectMeta(namespace, "redis", "base");
ServiceSpec serviceSpec = new ServiceSpec(); ServiceSpec serviceSpec = new ServiceSpec();
// 设置meta
Map<String, String> labels = new HashMap<>();
labels.put("type", "base");
labels.put("qcloud-app", serviceName);
objectMeta.setName(serviceName);
objectMeta.setNamespace(namespace);
objectMeta.setLabels(labels);
// 设置spec // 设置spec
List<ServicePort> portList = new ArrayList<>(); List<ServicePort> portList = new ArrayList<>();
ServicePort servicePort1 = new ServicePort(); ServicePort servicePort1 = new ServicePort();
...@@ -661,7 +704,7 @@ public class TkeService { ...@@ -661,7 +704,7 @@ public class TkeService {
portList.add(servicePort4); portList.add(servicePort4);
portList.add(servicePort5); portList.add(servicePort5);
Map<String, String> selector = new HashMap<>(); Map<String, String> selector = new HashMap<>();
selector.put("qcloud-app", serviceName); selector.put("qcloud-app", "redis");
// 设置serviceSpec // 设置serviceSpec
serviceSpec.setType("NodePort"); serviceSpec.setType("NodePort");
...@@ -678,8 +721,9 @@ public class TkeService { ...@@ -678,8 +721,9 @@ public class TkeService {
/** /**
* 创建redis container * 创建redis container
*
* @param image 镜像名 * @param image 镜像名
* @param port 端口号 * @param port 端口号
* @return * @return
*/ */
public Container createRedisContainer(String image, String port) { public Container createRedisContainer(String image, String port) {
...@@ -744,24 +788,16 @@ public class TkeService { ...@@ -744,24 +788,16 @@ public class TkeService {
/** /**
* 创建redis deployment * 创建redis deployment
*
* @param namespace * @param namespace
* @param label
* @param serviceName
* @param type
* @param image * @param image
* @return * @return
*/ */
public Deployment createRedisDeployment(String namespace, String label, String serviceName, String type, String image) { public Deployment createRedisDeployment(String namespace, String image) {
Deployment redisDeployment = new Deployment(); Deployment redisDeployment = new Deployment();
// 设置meta // 设置meta
ObjectMeta objectMeta = new ObjectMeta(); ObjectMeta objectMeta = buildObjectMeta(namespace, "redis", "base");
Map<String, String> labelMap = new HashMap<>();
labelMap.put("type", label);
labelMap.put("qcloud-app", serviceName);
objectMeta.setName(serviceName);
objectMeta.setNamespace(namespace);
objectMeta.setLabels(labelMap);
// 设置spec // 设置spec
DeploymentSpec deploymentSpec = new DeploymentSpec(); DeploymentSpec deploymentSpec = new DeploymentSpec();
...@@ -769,7 +805,7 @@ public class TkeService { ...@@ -769,7 +805,7 @@ public class TkeService {
// 设置labelSelector // 设置labelSelector
LabelSelector labelSelector = new LabelSelector(); LabelSelector labelSelector = new LabelSelector();
Map<String, String> matchLabels = new HashMap<>(); Map<String, String> matchLabels = new HashMap<>();
matchLabels.put("qcloud-app", serviceName); matchLabels.put("qcloud-app", "redis");
labelSelector.setMatchLabels(matchLabels); labelSelector.setMatchLabels(matchLabels);
// 设置strategy // 设置strategy
...@@ -778,11 +814,6 @@ public class TkeService { ...@@ -778,11 +814,6 @@ public class TkeService {
// 设置pod Template // 设置pod Template
PodTemplateSpec podTemplateSpec = new PodTemplateSpec(); PodTemplateSpec podTemplateSpec = new PodTemplateSpec();
ObjectMeta templateObjectMeta = new ObjectMeta();
Map<String, String> templateLabels = new HashMap<>();
templateLabels.put("qcloud-app", serviceName);
templateLabels.put("type", "base");
templateObjectMeta.setLabels(templateLabels);
PodSpec podSpec = new PodSpec(); PodSpec podSpec = new PodSpec();
List<Container> containerList = new ArrayList<>(); List<Container> containerList = new ArrayList<>();
...@@ -814,7 +845,7 @@ public class TkeService { ...@@ -814,7 +845,7 @@ public class TkeService {
podSpec.setTerminationGracePeriodSeconds(30L); podSpec.setTerminationGracePeriodSeconds(30L);
// 设置PodTemplateSpec // 设置PodTemplateSpec
podTemplateSpec.setMetadata(templateObjectMeta); podTemplateSpec.setMetadata(objectMeta);
podTemplateSpec.setSpec(podSpec); podTemplateSpec.setSpec(podSpec);
// 设置Deployment Spec // 设置Deployment Spec
...@@ -838,6 +869,7 @@ public class TkeService { ...@@ -838,6 +869,7 @@ public class TkeService {
/** /**
* 创建mysql service * 创建mysql service
*
* @param namespace 环境 * @param namespace 环境
* @return * @return
*/ */
...@@ -877,6 +909,7 @@ public class TkeService { ...@@ -877,6 +909,7 @@ public class TkeService {
/** /**
* 创建mysql pvc * 创建mysql pvc
*
* @param namespace 环境 * @param namespace 环境
*/ */
public PersistentVolumeClaim createMysqlPvc(String namespace) { public PersistentVolumeClaim createMysqlPvc(String namespace) {
...@@ -911,70 +944,853 @@ public class TkeService { ...@@ -911,70 +944,853 @@ public class TkeService {
} }
/** /**
* * 创建Mysql deployment
*/ */
public Deployment createMysqlDeployment(String namespace) { public Deployment createMysqlDeployment(String namespace, String image) {
return null; Deployment mysqlDeployment = new Deployment();
// 设置metadata
ObjectMeta objectMeta = new ObjectMeta();
Map<String, String> labels = new HashMap<>();
labels.put("type", "base");
labels.put("qcloud-app", "mysql");
objectMeta.setLabels(labels);
objectMeta.setName("mysql");
objectMeta.setNamespace(namespace);
// 设置spec
DeploymentSpec deploymentSpec = new DeploymentSpec();
Map<String, String> matchLabels = new HashMap<>();
matchLabels.put("qcloud-app", "mysql");
LabelSelector labelSelector = new LabelSelector();
labelSelector.setMatchLabels(matchLabels);
DeploymentStrategy deploymentStrategy = new DeploymentStrategy();
deploymentStrategy.setType("Recreate");
// template的metadata
ObjectMeta specMeta = new ObjectMeta();
specMeta.setLabels(labels);
// template的spec
PodSpec podSpec = new PodSpec();
List<Container> containerList = new ArrayList<>();
Container container = new Container();
container.setImage("ccr.ccs.tencentyun.com/" + image);
container.setImagePullPolicy("Always");
container.setName("mysql");
// resources
ResourceRequirements resourceRequirements = new ResourceRequirements();
Map<String, Quantity> requests = new HashMap<>();
Map<String, Quantity> limits = new HashMap<>();
Quantity cpuQuantity = new Quantity();
Quantity memoryQuantity = new Quantity();
Quantity cpuLimit = new Quantity();
Quantity memoryLimit = new Quantity();
cpuQuantity.setAmount("200");
cpuQuantity.setFormat("m");
memoryQuantity.setAmount("1");
memoryQuantity.setFormat("Gi");
cpuLimit.setAmount("2");
cpuLimit.setFormat("");
memoryLimit.setAmount("1");
memoryLimit.setFormat("Gi");
requests.put("cpu", cpuQuantity);
requests.put("memory", memoryQuantity);
resourceRequirements.setRequests(requests);
resourceRequirements.setLimits(limits);
container.setResources(resourceRequirements);
// env
List<EnvVar> envVarList = new ArrayList<>();
EnvVar envVar1 = new EnvVar();
envVar1.setName("MYSQL_DATABASE");
envVar1.setValue("db");
EnvVar envVar2 = new EnvVar();
envVar2.setName("MYSQL_PASSWORD");
envVar2.setValue("qatest");
EnvVar envVar3 = new EnvVar();
envVar3.setName("MYSQL_ROOT_PASSWORD");
envVar3.setValue("Quantgroup2017");
EnvVar envVar4 = new EnvVar();
envVar4.setName("MYSQL_USER");
envVar4.setValue("qa");
envVarList.add(envVar1);
envVarList.add(envVar2);
envVarList.add(envVar3);
envVarList.add(envVar4);
container.setEnv(envVarList);
// livenessProbe
Probe livenessProbe = new Probe();
ExecAction execAction = new ExecAction();
List<String> commandList = new ArrayList<>();
commandList.add("mysql");
commandList.add("-uqa");
commandList.add("-pqatest");
commandList.add("-e");
commandList.add("SELECT 1");
execAction.setCommand(commandList);
livenessProbe.setExec(execAction);
livenessProbe.setInitialDelaySeconds(100);
livenessProbe.setSuccessThreshold(1);
container.setLivenessProbe(livenessProbe);
// readinessProbe
Probe readinessProbe = new Probe();
readinessProbe.setExec(execAction);
readinessProbe.setInitialDelaySeconds(5);
readinessProbe.setPeriodSeconds(5);
readinessProbe.setTimeoutSeconds(1);
container.setReadinessProbe(readinessProbe);
// volumeMounts
List<VolumeMount> volumeMountList = new ArrayList<>();
VolumeMount volumeMount = new VolumeMount();
volumeMount.setName("mysql");
volumeMount.setMountPath("/var/lib/mysql");
volumeMountList.add(volumeMount);
container.setVolumeMounts(volumeMountList);
// 设置container
containerList.add(container);
podSpec.setContainers(containerList);
// volumes
List<Volume> volumeList = new ArrayList<>();
Volume volume = new Volume();
PersistentVolumeClaimVolumeSource persistentVolumeClaimVolumeSource = new PersistentVolumeClaimVolumeSource();
persistentVolumeClaimVolumeSource.setClaimName("mysql-" + namespace);
volume.setName("mysql");
volume.setPersistentVolumeClaim(persistentVolumeClaimVolumeSource);
volumeList.add(volume);
podSpec.setVolumes(volumeList);
// imagePullSecrets
List<LocalObjectReference> objectReferences = new ArrayList<>();
LocalObjectReference objectReference1 = new LocalObjectReference();
objectReference1.setName("qcloudregistrykey");
LocalObjectReference objectReference2 = new LocalObjectReference();
objectReference2.setName("tencenthubkey");
objectReferences.add(objectReference1);
objectReferences.add(objectReference2);
podSpec.setImagePullSecrets(objectReferences);
// restartPolicy
podSpec.setRestartPolicy("Always");
podSpec.setTerminationGracePeriodSeconds(30L);
PodTemplateSpec podTemplateSpec = new PodTemplateSpec();
podTemplateSpec.setMetadata(specMeta);
podTemplateSpec.setSpec(podSpec);
deploymentSpec.setReplicas(1);
deploymentSpec.setRevisionHistoryLimit(1);
deploymentSpec.setSelector(labelSelector);
deploymentSpec.setStrategy(deploymentStrategy);
deploymentSpec.setTemplate(podTemplateSpec);
DeploymentStatus deploymentStatus = new DeploymentStatus();
mysqlDeployment.setApiVersion("apps/v1");
mysqlDeployment.setKind("Deployment");
mysqlDeployment.setMetadata(objectMeta);
mysqlDeployment.setSpec(deploymentSpec);
mysqlDeployment.setStatus(deploymentStatus);
return kubernetesClient.apps().deployments().inNamespace(namespace).create(mysqlDeployment);
} }
/** /**
* 更新部署pod * 创建mongodb Service
*
* @param namespace 环境 * @param namespace 环境
* @param serviceName 服务名
* @param image 镜像名
* @return * @return
*/ */
public Deployment updateDeployment(String namespace, String serviceName, String image) { public Service createMongodbService(String namespace) {
Deployment deployment = kubernetesClient.apps().deployments().inNamespace(namespace).withName(serviceName).get(); Service mongodbService = new Service();
List<Container> containerList = deployment.getSpec().getTemplate().getSpec().getContainers();
containerList.get(0).setImage("ccr.ccs.tencentyun.com/" + image); ObjectMeta objectMeta = buildObjectMeta("fe", "mongodb", "base");
deployment.getSpec().getTemplate().getSpec().setContainers(containerList);
return kubernetesClient.apps().deployments().inNamespace(namespace).withName(serviceName).createOrReplace(deployment); ServiceSpec serviceSpec = new ServiceSpec();
serviceSpec.setType("NodePort");
ServicePort servicePort = new ServicePort();
servicePort.setPort(27017);
List<ServicePort> servicePortList = new ArrayList<>();
servicePortList.add(servicePort);
serviceSpec.setPorts(servicePortList);
Map<String, String> selector = new HashMap<>();
selector.put("qcloud-app", "mongodb");
serviceSpec.setSelector(selector);
mongodbService.setApiVersion("v1");
mongodbService.setKind("Service");
mongodbService.setMetadata(objectMeta);
mongodbService.setSpec(serviceSpec);
return kubernetesClient.services().inNamespace(namespace).create(mongodbService);
} }
public static void main(String[] args) { /**
TkeService tkeService = new TkeService(); * 创建mongodb pvc
// List<Service> serviceList = k8sService.kubernetesClient.services().inNamespace("fe").list().getItems(); * @param namespace
// List<Pod> podList = k8sService.kubernetesClient.pods().inNamespace("fe").list().getItems(); * @return
// Pod pod = podList.get(22); */
public PersistentVolumeClaim createMongodbPvc(String namespace) {
PersistentVolumeClaim mongodbPvc = new PersistentVolumeClaim();
// System.out.println("Metadata: " + pod.getMetadata()); ObjectMeta objectMeta = new ObjectMeta();
// System.out.println("Spec: " + pod.getSpec()); objectMeta.setName("mongodb-" + namespace);
// System.out.println("Status: " + pod.getStatus()); objectMeta.setNamespace(namespace);
// System.out.println("Api: " + pod.getApiVersion());
// System.out.println(pod.getStatus().getContainerStatuses().get(0).getImage());
// System.out.println(pod.getStatus().getContainerStatuses().get(0).getImageID());
// System.out.println(pod.getStatus().getPhase());
// System.out.println(pod.getSpec().getContainers().get(0).getName());
// System.out.println(serviceList.get(19).getSpec().getPorts()); PersistentVolumeClaimSpec pvcSpec = new PersistentVolumeClaimSpec();
// Service service = k8sService.kubernetesClient.services().inNamespace("fe").withName("holmes").get(); List<String> accessModes = new ArrayList<>();
// System.out.println(service); accessModes.add("ReadWriteOnce");
pvcSpec.setAccessModes(accessModes);
// System.out.println(k8sService.resetPod("fe", "acm-ui-58864499d9-h47rl")); ResourceRequirements resourceRequirements = new ResourceRequirements();
// tkeService.deleteService("fe", "kdsp"); Map<String, Quantity> requests = new HashMap<>();
Quantity storage = new Quantity();
storage.setAmount("10");
storage.setFormat("Gi");
requests.put("storage", storage);
resourceRequirements.setRequests(requests);
pvcSpec.setResources(resourceRequirements);
//创建deployment mongodbPvc.setApiVersion("v1");
// ServiceCreateVo serviceCreateVo = new ServiceCreateVo(); mongodbPvc.setKind("PersistentVolumeClaim");
// serviceCreateVo.setServiceName("gu-bei"); mongodbPvc.setMetadata(objectMeta);
// serviceCreateVo.setNamespace("test1"); mongodbPvc.setSpec(pvcSpec);
// serviceCreateVo.setCluster("qa");
// serviceCreateVo.setMock(1);
// serviceCreateVo.setDebug(0);
// serviceCreateVo.setLabel("java");
// serviceCreateVo.setImage("qa-test/gu-bei:master-20210707165716636");
// DockerProject dockerProject = new DockerProject();
// dockerProject.setCpuRequest("200");
// dockerProject.setMemRequest("500");
// dockerProject.setCpuLimit("2000");
// dockerProject.setMemLimit("1000");
// tkeService.createJavaDeployment(serviceCreateVo, dockerProject);
// tkeService.updateDeployment("fe", "mo-clotho", "qa-test/mo-clotho:master-20210707143546990"); return kubernetesClient.persistentVolumeClaims().inNamespace(namespace).create(mongodbPvc);
// tkeService.createRedisService("fe", "redis"); }
// tkeService.createRedisDeployment("fe", "base", "redis", "redis", "qa-base/redis:3");
// tkeService.createJavaService("test1", "gu-bei", "NodePort", "java"); /**
// tkeService.createUIAndNodeService("tob", "new-op-optimized-ui", "ClusterIP", "ui"); * 创建mongodb deployment
* @param namespace
* @param image
* @return
*/
public Deployment createMongoDbDeployment(String namespace, String image) {
Deployment mongodbDeployment = new Deployment();
// 设置meta
ObjectMeta objectMeta = new ObjectMeta();
Map<String, String> labelMap = new HashMap<>();
labelMap.put("type", "base");
labelMap.put("qcloud-app", "mongodb");
objectMeta.setName("mongodb");
objectMeta.setNamespace(namespace);
objectMeta.setLabels(labelMap);
DeploymentSpec deploymentSpec = new DeploymentSpec();
Map<String, String> matchLabels = new HashMap<>();
matchLabels.put("qcloud-app", "mongodb");
LabelSelector labelSelector = new LabelSelector();
labelSelector.setMatchLabels(matchLabels);
DeploymentStrategy deploymentStrategy = new DeploymentStrategy();
deploymentStrategy.setType("Recreate");
// template spec
PodSpec podSpec = new PodSpec();
List<Container> containerList = new ArrayList<>();
Container container = new Container();
container.setImage("ccr.ccs.tencentyun.com/" + image);
container.setImagePullPolicy("Always");
container.setName("mongodb");
// resources
ResourceRequirements resourceRequirements = new ResourceRequirements();
Map<String, Quantity> requests = new HashMap<>();
Map<String, Quantity> limits = new HashMap<>();
Quantity cpuRequest = new Quantity();
Quantity memoryRequest = new Quantity();
Quantity cpuLimit = new Quantity();
Quantity memoryLimit = new Quantity();
cpuRequest.setAmount("256");
cpuRequest.setFormat("m");
memoryRequest.setAmount("256");
memoryRequest.setFormat("Mi");
cpuLimit.setAmount("250");
cpuLimit.setFormat("m");
memoryLimit.setAmount("512");
memoryLimit.setFormat("Mi");
requests.put("cpu", cpuRequest);
requests.put("memory", memoryRequest);
resourceRequirements.setRequests(requests);
resourceRequirements.setLimits(limits);
container.setResources(resourceRequirements);
// env
List<EnvVar> envVarList = new ArrayList<>();
EnvVar envVar1 = new EnvVar();
envVar1.setName("MONGO_INITDB_ROOT_USERNAME");
envVar1.setValue("qa");
EnvVar envVar2 = new EnvVar();
envVar2.setName("MONGO_INITDB_ROOT_PASSWORD");
envVar2.setValue("qatest");
envVarList.add(envVar1);
envVarList.add(envVar2);
container.setEnv(envVarList);
// livenessProbe
Probe livenessProbe = new Probe();
ExecAction execAction = new ExecAction();
List<String> commandList = new ArrayList<>();
commandList.add("mongo");
commandList.add("-uqa");
commandList.add("-pqatest");
commandList.add("admin");
execAction.setCommand(commandList);
livenessProbe.setExec(execAction);
livenessProbe.setInitialDelaySeconds(100);
livenessProbe.setSuccessThreshold(1);
container.setLivenessProbe(livenessProbe);
// readinessProbe
Probe readinessProbe = new Probe();
readinessProbe.setExec(execAction);
readinessProbe.setInitialDelaySeconds(5);
readinessProbe.setPeriodSeconds(5);
readinessProbe.setTimeoutSeconds(1);
container.setReadinessProbe(readinessProbe);
// volumeMounts
List<VolumeMount> volumeMountList = new ArrayList<>();
VolumeMount volumeMount = new VolumeMount();
volumeMount.setName("mongodb");
volumeMount.setMountPath("/var/lib/mongo");
volumeMountList.add(volumeMount);
container.setVolumeMounts(volumeMountList);
containerList.add(container);
// volumes
List<Volume> volumeList = new ArrayList<>();
PersistentVolumeClaimVolumeSource persistentVolumeClaimVolumeSource = new PersistentVolumeClaimVolumeSource();
persistentVolumeClaimVolumeSource.setClaimName("mongodb-" + namespace);
Volume volume = new Volume();
volume.setName("mongodb");
volume.setPersistentVolumeClaim(persistentVolumeClaimVolumeSource);
volumeList.add(volume);
// imagePullSecrets
List<LocalObjectReference> imagePullSecrets = new ArrayList<>();
LocalObjectReference localObjectReference1 = new LocalObjectReference();
localObjectReference1.setName("qcloudregistrykey");
LocalObjectReference localObjectReference2 = new LocalObjectReference();
localObjectReference2.setName("tencenthubkey");
imagePullSecrets.add(localObjectReference1);
imagePullSecrets.add(localObjectReference2);
podSpec.setContainers(containerList);
podSpec.setVolumes(volumeList);
podSpec.setImagePullSecrets(imagePullSecrets);
podSpec.setRestartPolicy("Always");
podSpec.setTerminationGracePeriodSeconds(30L);
// template
PodTemplateSpec podTemplateSpec = new PodTemplateSpec();
podTemplateSpec.setSpec(podSpec);
podTemplateSpec.setMetadata(objectMeta);
deploymentSpec.setReplicas(1);
deploymentSpec.setRevisionHistoryLimit(1);
deploymentSpec.setSelector(labelSelector);
deploymentSpec.setStrategy(deploymentStrategy);
deploymentSpec.setTemplate(podTemplateSpec);
DeploymentStatus deploymentStatus = new DeploymentStatus();
mongodbDeployment.setApiVersion("apps/v1");
mongodbDeployment.setKind("Deployment");
mongodbDeployment.setMetadata(objectMeta);
mongodbDeployment.setSpec(deploymentSpec);
mongodbDeployment.setStatus(deploymentStatus);
return kubernetesClient.apps().deployments().inNamespace(namespace).create(mongodbDeployment);
}
/**
* 创建zookeeper pvc
* @param namespace
* @return
*/
public PersistentVolumeClaim createZookeeperPvc(String namespace) {
PersistentVolumeClaim zookeeperPvc = new PersistentVolumeClaim();
ObjectMeta objectMeta = new ObjectMeta();
objectMeta.setName("zookeeper-" + namespace);
objectMeta.setNamespace(namespace);
PersistentVolumeClaimSpec pvcSpec = new PersistentVolumeClaimSpec();
List<String> accessModes = new ArrayList<>();
accessModes.add("ReadWriteOnce");
ResourceRequirements resourceRequirements = new ResourceRequirements();
Map<String, Quantity> requests = new HashMap<>();
Quantity storage = new Quantity();
storage.setAmount("10");
storage.setFormat("Gi");
requests.put("storage", storage);
resourceRequirements.setRequests(requests);
pvcSpec.setAccessModes(accessModes);
pvcSpec.setResources(resourceRequirements);
zookeeperPvc.setApiVersion("v1");
zookeeperPvc.setKind("PersistentVolumeClaim");
zookeeperPvc.setMetadata(objectMeta);
zookeeperPvc.setSpec(pvcSpec);
return kubernetesClient.persistentVolumeClaims().inNamespace(namespace).create(zookeeperPvc);
}
/**
* 创建zookeeper Service
* @param namespace
* @return
*/
public Service createZookeeperService(String namespace) {
Service zookeeperService = new Service();
// metadata
ObjectMeta objectMeta = new ObjectMeta();
Map<String, String> labels = new HashMap<>();
labels.put("type", "base");
labels.put("qcloud-app", "zookeeper");
objectMeta.setLabels(labels);
objectMeta.setName("zookeeper");
objectMeta.setNamespace(namespace);
// spec
ServiceSpec serviceSpec = new ServiceSpec();
serviceSpec.setType("NodePort");
List<ServicePort> servicePortList = new ArrayList<>();
ServicePort servicePort1 = new ServicePort();
servicePort1.setName("2181");
servicePort1.setPort(2181);
servicePortList.add(servicePort1);
serviceSpec.setPorts(servicePortList);
Map<String, String> selector = new HashMap<>();
selector.put("qcloud-app", "zookeeper");
serviceSpec.setSelector(selector);
// 设置Service
zookeeperService.setApiVersion("v1");
zookeeperService.setKind("Service");
zookeeperService.setMetadata(objectMeta);
zookeeperService.setSpec(serviceSpec);
return kubernetesClient.services().inNamespace(namespace).create(zookeeperService);
}
/**
* 创建zookeeper deployment
* @param namespace
* @param image
* @return
*/
public Deployment createZookeeperDeployment(String namespace, String image) {
Deployment zookeeperDeployment = new Deployment();
ObjectMeta objectMeta = new ObjectMeta();
Map<String, String> labels = new HashMap<>();
labels.put("type", "base");
labels.put("qcloud-app", "zookeeper");
objectMeta.setLabels(labels);
objectMeta.setName("zookeeper");
objectMeta.setNamespace(namespace);
// selector
LabelSelector labelSelector = new LabelSelector();
Map<String, String> matchLabels = new HashMap<>();
matchLabels.put("qcloud-app", "zookeeper");
labelSelector.setMatchLabels(matchLabels);
// strategy
DeploymentStrategy deploymentStrategy = new DeploymentStrategy();
deploymentStrategy.setType("Recreate");
// PodSpec
PodSpec podSpec = new PodSpec();
List<Container> containerList = new ArrayList<>();
Container container = new Container();
container.setImage("ccr.ccs.tencentyun.com/" + image);
container.setImagePullPolicy("Always");
container.setName("zookeeper");
ResourceRequirements resourceRequirements = new ResourceRequirements();
Map<String, Quantity> requests = new HashMap<>();
Quantity cpuRequest = new Quantity();
Quantity menRequest = new Quantity();
cpuRequest.setAmount("100");
cpuRequest.setFormat("m");
menRequest.setAmount("300");
menRequest.setFormat("Mi");
requests.put("cpu", cpuRequest);
requests.put("memory", menRequest);
Map<String, Quantity> limits = new HashMap<>();
Quantity cpuLimit = new Quantity();
Quantity memLimit = new Quantity();
cpuLimit.setAmount("200");
cpuLimit.setFormat("m");
memLimit.setAmount("300");
memLimit.setFormat("Mi");
limits.put("cpu", cpuLimit);
limits.put("memory", memLimit);
resourceRequirements.setRequests(requests);
resourceRequirements.setLimits(limits);
container.setResources(resourceRequirements);
// livenessProbe
Probe livenessProbe = new Probe();
ExecAction execAction = new ExecAction();
List<String> commandList = new ArrayList<>();
commandList.add("/zookeeper-3.4.13/readyCheck.sh");
execAction.setCommand(commandList);
livenessProbe.setExec(execAction);
livenessProbe.setInitialDelaySeconds(100);
livenessProbe.setSuccessThreshold(1);
livenessProbe.setFailureThreshold(3);
livenessProbe.setPeriodSeconds(10);
livenessProbe.setTimeoutSeconds(1);
container.setLivenessProbe(livenessProbe);
// readinessProbe
Probe readinessProbe = new Probe();
readinessProbe.setExec(execAction);
readinessProbe.setInitialDelaySeconds(5);
readinessProbe.setTimeoutSeconds(1);
readinessProbe.setPeriodSeconds(5);
readinessProbe.setFailureThreshold(3);
readinessProbe.setSuccessThreshold(1);
container.setReadinessProbe(readinessProbe);
// volumeMounts
List<VolumeMount> volumeMountList = new ArrayList<>();
VolumeMount volumeMount = new VolumeMount();
volumeMount.setName("zookeeper");
volumeMount.setMountPath("/var/lib/zookeeper");
volumeMountList.add(volumeMount);
container.setVolumeMounts(volumeMountList);
// env
List<EnvVar> envVarList = new ArrayList<>();
EnvVar envVar = new EnvVar();
envVar.setName("ZOO_USER");
envVar.setValue("zookeeper");
envVarList.add(envVar);
container.setEnv(envVarList);
container.setTerminationMessagePath("/dev/termination-log");
container.setTerminationMessagePolicy("File");
containerList.add(container);
// volumes
List<Volume> volumes = new ArrayList<>();
Volume volume = new Volume();
volume.setName("zookeeper");
PersistentVolumeClaimVolumeSource pvcSource = new PersistentVolumeClaimVolumeSource();
pvcSource.setClaimName("zookeeper-" + namespace);
volume.setPersistentVolumeClaim(pvcSource);
volumes.add(volume);
List<LocalObjectReference> localObjectReferenceList = new ArrayList<>();
LocalObjectReference localObjectReference1 = new LocalObjectReference();
LocalObjectReference localObjectReference2 = new LocalObjectReference();
localObjectReference1.setName("qcloudregistrykey");
localObjectReference2.setName("tencenthubkey");
localObjectReferenceList.add(localObjectReference1);
localObjectReferenceList.add(localObjectReference2);
podSpec.setContainers(containerList);
podSpec.setVolumes(volumes);
podSpec.setDnsPolicy("ClusterFirst");
podSpec.setImagePullSecrets(localObjectReferenceList);
podSpec.setRestartPolicy("Always");
podSpec.setTerminationGracePeriodSeconds(30L);
// template
PodTemplateSpec podTemplateSpec = new PodTemplateSpec();
podTemplateSpec.setMetadata(objectMeta);
podTemplateSpec.setSpec(podSpec);
DeploymentSpec deploymentSpec = new DeploymentSpec();
deploymentSpec.setReplicas(1);
deploymentSpec.setRevisionHistoryLimit(1);
deploymentSpec.setProgressDeadlineSeconds(600);
deploymentSpec.setSelector(labelSelector);
deploymentSpec.setStrategy(deploymentStrategy);
deploymentSpec.setTemplate(podTemplateSpec);
DeploymentStatus deploymentStatus = new DeploymentStatus();
zookeeperDeployment.setApiVersion("apps/v1");
zookeeperDeployment.setKind("Deployment");
zookeeperDeployment.setMetadata(objectMeta);
zookeeperDeployment.setSpec(deploymentSpec);
zookeeperDeployment.setStatus(deploymentStatus);
return kubernetesClient.apps().deployments().inNamespace(namespace).create(zookeeperDeployment);
}
/**
* 创建rabbitmq pvc
* @param namespace
* @return
*/
public PersistentVolumeClaim createRabbitmqPvc(String namespace) {
PersistentVolumeClaim rabbitmqPvc = new PersistentVolumeClaim();
ObjectMeta objectMeta = new ObjectMeta();
objectMeta.setName("rabbitmq-" + namespace);
objectMeta.setNamespace(namespace);
PersistentVolumeClaimSpec pvcSpec = new PersistentVolumeClaimSpec();
List<String> accessModes = new ArrayList<>();
accessModes.add("ReadWriteOnce");
pvcSpec.setAccessModes(accessModes);
ResourceRequirements resourceRequirements = new ResourceRequirements();
Map<String, Quantity> requests = new HashMap<>();
Quantity storage = new Quantity();
storage.setAmount("10");
storage.setFormat("Gi");
requests.put("storage", storage);
resourceRequirements.setRequests(requests);
pvcSpec.setResources(resourceRequirements);
rabbitmqPvc.setApiVersion("apps/v1");
rabbitmqPvc.setKind("PersistentVolumeClaim");
rabbitmqPvc.setMetadata(objectMeta);
rabbitmqPvc.setSpec(pvcSpec);
return kubernetesClient.persistentVolumeClaims().inNamespace(namespace).create(rabbitmqPvc);
}
/**
* 创建rabbitmq service
* @param namespace
* @return
*/
public Service createRabbitmqService(String namespace) {
Service rabbitmqService = new Service();
ObjectMeta objectMeta = new ObjectMeta();
Map<String, String> labels = new HashMap<>();
labels.put("type", "base");
labels.put("qcloud-app", "rabbitmq");
objectMeta.setLabels(labels);
objectMeta.setName("rabbitmq");
objectMeta.setNamespace(namespace);
ServiceSpec serviceSpec = new ServiceSpec();
serviceSpec.setType("NodePort");
List<ServicePort> servicePortList = new ArrayList<>();
ServicePort servicePort1 = new ServicePort();
servicePort1.setName("tcp-5672-5672");
servicePort1.setPort(5672);
ServicePort servicePort2 = new ServicePort();
servicePort2.setName("tcp-15672-15672");
servicePort2.setPort(15672);
servicePortList.add(servicePort1);
servicePortList.add(servicePort2);
serviceSpec.setPorts(servicePortList);
Map<String, String> selector = new HashMap<>();
selector.put("qcloud-app", "rabbitmq");
serviceSpec.setSelector(selector);
rabbitmqService.setApiVersion("apps/v1");
rabbitmqService.setKind("Service");
rabbitmqService.setMetadata(objectMeta);
rabbitmqService.setSpec(serviceSpec);
return kubernetesClient.services().inNamespace(namespace).create(rabbitmqService);
}
/**
* 创建rabbitmq deployment
* @param namespace
* @param image
* @return
*/
public Deployment createRabbitmqDeployment(String namespace, String image) {
Deployment rabbitmqDeployment = new Deployment();
ObjectMeta objectMeta = buildObjectMeta(namespace, "rabbitmq", "base");
LabelSelector labelSelector = new LabelSelector();
Map<String, String> matchLabels = new HashMap<>();
matchLabels.put("qcloud-app", "rabbitmq");
labelSelector.setMatchLabels(matchLabels);
DeploymentStrategy deploymentStrategy = new DeploymentStrategy();
deploymentStrategy.setType("Recreate");
ResourceRequirements resourceRequirements = buildResourceRequirements("100", "m",
"1500", "Mi", "200", "m", "1500", "Mi");
List<EnvVar> envVarList = new ArrayList<>();
EnvVar envVar1 = new EnvVar();
envVar1.setName("RABBITMQ_DEFAULT_USER");
envVar1.setValue("qa");
EnvVar envVar2 = new EnvVar();
envVar2.setName("RABBITMQ_DEFAULT_PASS");
envVar2.setValue("qatest");
envVarList.add(envVar1);
envVarList.add(envVar2);
// livenessProbe
Probe livenessProbe = new Probe();
List<Container> containerList = new ArrayList<>();
Container container = new Container();
container.setImage("ccr.ccs.tencentyun.com/" + image);
container.setImagePullPolicy("IfNotPresent");
container.setName("rabbitmq");
container.setResources(resourceRequirements);
container.setEnv(envVarList);
container.setLivenessProbe(livenessProbe);
List<Volume> volumeList = new ArrayList<>();
List<LocalObjectReference> localObjectReferenceList = new ArrayList<>();
PodSpec podSpec = new PodSpec();
podSpec.setHostname("rabbitmq-" + namespace);
podSpec.setContainers(containerList);
podSpec.setVolumes(volumeList);
podSpec.setImagePullSecrets(localObjectReferenceList);
podSpec.setRestartPolicy("Always");
podSpec.setTerminationGracePeriodSeconds(30L);
PodTemplateSpec podTemplateSpec = new PodTemplateSpec();
podTemplateSpec.setMetadata(objectMeta);
podTemplateSpec.setSpec(podSpec);
DeploymentSpec deploymentSpec = new DeploymentSpec();
deploymentSpec.setReplicas(1);
deploymentSpec.setRevisionHistoryLimit(1);
deploymentSpec.setSelector(labelSelector);
deploymentSpec.setStrategy(deploymentStrategy);
deploymentSpec.setTemplate(podTemplateSpec);
DeploymentStatus deploymentStatus = new DeploymentStatus();
rabbitmqDeployment.setApiVersion("apps/v1");
rabbitmqDeployment.setKind("Deployment");
rabbitmqDeployment.setMetadata(objectMeta);
rabbitmqDeployment.setSpec(deploymentSpec);
rabbitmqDeployment.setStatus(deploymentStatus);
return kubernetesClient.apps().deployments().inNamespace(namespace).create(rabbitmqDeployment);
}
/**
* 查询PersistentVolumeClaims是否存在
* @param namespace 环境
* @param pvcName pvc名称
* @return
*/
public boolean queryIfPvcExistByName(String namespace, String pvcName) {
return kubernetesClient.persistentVolumeClaims().inNamespace(namespace).withName(pvcName + "-" + namespace).get() != null;
}
/**
* 查询Deployment是否存在
* @param namespace 环境
* @param deploymentName Deployment名称
* @return
*/
public boolean queryIfDeploymentExistByName(String namespace, String deploymentName) {
return kubernetesClient.apps().deployments().inNamespace(namespace).withName(deploymentName).get() != null;
}
/**
* 查询Service是否存在
* @param namespace 环境
* @param serviceName Service名称
* @return
*/
public boolean queryIfServiceExistByName(String namespace, String serviceName) {
return kubernetesClient.services().inNamespace(namespace).withName(serviceName).get() != null;
}
/**
* 更新部署pod
*
* @param namespace 环境
* @param serviceName 服务名
* @param image 镜像名
* @return
*/
public Deployment updateDeployment(String namespace, String serviceName, String image) {
Deployment deployment = kubernetesClient.apps().deployments().inNamespace(namespace).withName(serviceName).get();
List<Container> containerList = deployment.getSpec().getTemplate().getSpec().getContainers();
containerList.get(0).setImage("ccr.ccs.tencentyun.com/" + image);
deployment.getSpec().getTemplate().getSpec().setContainers(containerList);
return kubernetesClient.apps().deployments().inNamespace(namespace).withName(serviceName).createOrReplace(deployment);
}
public static void main(String[] args) {
TkeService tkeService = new TkeService();
// List<Service> serviceList = k8sService.kubernetesClient.services().inNamespace("fe").list().getItems();
// List<Pod> podList = k8sService.kubernetesClient.pods().inNamespace("fe").list().getItems();
// Pod pod = podList.get(22);
// System.out.println("Metadata: " + pod.getMetadata());
// System.out.println("Spec: " + pod.getSpec());
// System.out.println("Status: " + pod.getStatus());
// System.out.println("Api: " + pod.getApiVersion());
// System.out.println(pod.getStatus().getContainerStatuses().get(0).getImage());
// System.out.println(pod.getStatus().getContainerStatuses().get(0).getImageID());
// System.out.println(pod.getStatus().getPhase());
// System.out.println(pod.getSpec().getContainers().get(0).getName());
// System.out.println(serviceList.get(19).getSpec().getPorts());
// Service service = k8sService.kubernetesClient.services().inNamespace("fe").withName("holmes").get();
// System.out.println(service);
// System.out.println(k8sService.resetPod("fe", "acm-ui-58864499d9-h47rl"));
// tkeService.deleteService("fe", "kdsp");
//创建deployment
// ServiceCreateVo serviceCreateVo = new ServiceCreateVo();
// serviceCreateVo.setServiceName("gu-bei");
// serviceCreateVo.setNamespace("test1");
// serviceCreateVo.setCluster("qa");
// serviceCreateVo.setMock(1);
// serviceCreateVo.setDebug(0);
// serviceCreateVo.setLabel("java");
// serviceCreateVo.setImage("qa-test/gu-bei:master-20210707165716636");
// DockerProject dockerProject = new DockerProject();
// dockerProject.setCpuRequest("200");
// dockerProject.setMemRequest("500");
// dockerProject.setCpuLimit("2000");
// dockerProject.setMemLimit("1000");
// tkeService.createJavaDeployment(serviceCreateVo, dockerProject);
// tkeService.updateDeployment("fe", "mo-clotho", "qa-test/mo-clotho:master-20210707143546990");
// tkeService.createRedisService("fe", "redis");
// tkeService.createRedisDeployment("fe", "base", "redis", "redis", "qa-base/redis:3");
// tkeService.createJavaService("test1", "gu-bei", "NodePort", "java");
// tkeService.createUIAndNodeService("tob", "new-op-optimized-ui", "ClusterIP", "ui");
// tkeService.createRedisPvc("fe");
// tkeService.createRedisService("fe");
// tkeService.createRedisDeployment("fe", "qa-base/redis:3");
// tkeService.createMysqlPvc("fe");
// tkeService.createMysqlService("fe");
// tkeService.createMysqlDeployment("fe", "qa-base/mysql:5.6");
// tkeService.createMongodbPvc("fe");
// tkeService.createMongodbService("fe");
// tkeService.createMongoDbDeployment("fe", "qa-base/mongodb:3.6");
// tkeService.createZookeeperPvc("fe");
// tkeService.createZookeeperService("fe");
// tkeService.createZookeeperDeployment("fe", "qa-base/zookeeper:3.4.13");
// System.out.println(tkeService.queryIfDeploymentExistByName("test4", "vcc-talos"));
// System.out.println(tkeService.queryIfPvcExistByName("test4", "mysql"));
// System.out.println(tkeService.queryIfServiceExistByName("test4", "vcc-talos"));
} }
......
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