Commit 0dcbec57 authored by 黎博's avatar 黎博

新增redis 哨兵以及优化部分方法

parent 4276634a
...@@ -112,6 +112,21 @@ public class K8sService { ...@@ -112,6 +112,21 @@ public class K8sService {
return resourceRequirements; return resourceRequirements;
} }
/**
* 构建imagePullSecrets
* @return
*/
public List<LocalObjectReference> buildImagePullSecrets() {
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);
return imagePullSecrets;
}
/** /**
* 获取namespace列表 * 获取namespace列表
* @return * @return
...@@ -378,6 +393,11 @@ public class K8sService { ...@@ -378,6 +393,11 @@ public class K8sService {
} }
} }
/**
* 格式化k8s Service
* @param service
* @return
*/
public Map<String, Object> formatServiceInfo(Service service) { public Map<String, Object> formatServiceInfo(Service service) {
Map<String, Object> serviceMap = new HashMap<>(); Map<String, Object> serviceMap = new HashMap<>();
serviceMap.put("serviceType", service.getSpec().getType()); serviceMap.put("serviceType", service.getSpec().getType());
...@@ -403,6 +423,11 @@ public class K8sService { ...@@ -403,6 +423,11 @@ public class K8sService {
return serviceMap; return serviceMap;
} }
/**
* 格式化k8s Ingress
* @param ingress
* @return
*/
public Map<String, Object> formatIngressInfo(Ingress ingress) { public Map<String, Object> formatIngressInfo(Ingress ingress) {
Map<String, Object> ingressMap = new HashMap<>(); Map<String, Object> ingressMap = new HashMap<>();
if (ingress.getSpec().getRules() != null && ingress.getSpec().getRules().size() > 0) { if (ingress.getSpec().getRules() != null && ingress.getSpec().getRules().size() > 0) {
...@@ -653,13 +678,7 @@ public class K8sService { ...@@ -653,13 +678,7 @@ public class K8sService {
volumeList.add(volume); volumeList.add(volume);
// imagePullSecrets // imagePullSecrets
List<LocalObjectReference> referenceList = new ArrayList<>(); List<LocalObjectReference> imagePullSecrets = buildImagePullSecrets();
LocalObjectReference reference1 = new LocalObjectReference();
LocalObjectReference reference2 = new LocalObjectReference();
reference1.setName("qcloudregistrykey");
reference2.setName("tencenthubkey");
referenceList.add(reference1);
referenceList.add(reference2);
// 设置podSpec // 设置podSpec
podSpec.setTerminationGracePeriodSeconds(0L); podSpec.setTerminationGracePeriodSeconds(0L);
...@@ -667,7 +686,7 @@ public class K8sService { ...@@ -667,7 +686,7 @@ public class K8sService {
podSpec.setVolumes(volumeList); podSpec.setVolumes(volumeList);
podSpec.setRestartPolicy("Always"); podSpec.setRestartPolicy("Always");
podSpec.setDnsPolicy("ClusterFirst"); podSpec.setDnsPolicy("ClusterFirst");
podSpec.setImagePullSecrets(referenceList); podSpec.setImagePullSecrets(imagePullSecrets);
// 设置PodTemplateSpec // 设置PodTemplateSpec
podTemplateSpec.setMetadata(templateObjectMeta); podTemplateSpec.setMetadata(templateObjectMeta);
...@@ -822,27 +841,9 @@ public class K8sService { ...@@ -822,27 +841,9 @@ public class K8sService {
container.setEnv(envVarList); container.setEnv(envVarList);
// resources // resources
ResourceRequirements resourceRequirements = new ResourceRequirements(); ResourceRequirements resourceRequirements = buildResourceRequirements(
Map<String, Quantity> requests = new HashMap<>(); dockerProject.getCpuRequest(), "m", dockerProject.getMemRequest(), "Mi",
Map<String, Quantity> limits = new HashMap<>(); dockerProject.getCpuLimit(), "m", dockerProject.getMemLimit(), "Mi");
Quantity cpuQuantity = new Quantity();
Quantity memoryQuantity = new Quantity();
Quantity cpuLimit = new Quantity();
Quantity memoryLimit = new Quantity();
cpuQuantity.setAmount(dockerProject.getCpuRequest());
cpuQuantity.setFormat("m");
memoryQuantity.setAmount(dockerProject.getMemRequest());
memoryQuantity.setFormat("Mi");
cpuLimit.setAmount(dockerProject.getCpuLimit());
cpuLimit.setFormat("m");
memoryLimit.setAmount(dockerProject.getMemLimit());
memoryLimit.setFormat("Mi");
requests.put("cpu", cpuQuantity);
requests.put("memory", memoryQuantity);
limits.put("cpu", cpuLimit);
limits.put("memory", memoryLimit);
resourceRequirements.setRequests(requests);
resourceRequirements.setLimits(limits);
container.setResources(resourceRequirements); container.setResources(resourceRequirements);
// livenessProbe // livenessProbe
...@@ -877,13 +878,7 @@ public class K8sService { ...@@ -877,13 +878,7 @@ public class K8sService {
volumeList.add(volume); volumeList.add(volume);
// imagePullSecrets // imagePullSecrets
List<LocalObjectReference> referenceList = new ArrayList<>(); List<LocalObjectReference> imagePullSecrets = buildImagePullSecrets();
LocalObjectReference reference1 = new LocalObjectReference();
LocalObjectReference reference2 = new LocalObjectReference();
reference1.setName("qcloudregistrykey");
reference2.setName("tencenthubkey");
referenceList.add(reference1);
referenceList.add(reference2);
// 设置podSpec // 设置podSpec
podSpec.setTerminationGracePeriodSeconds(0L); podSpec.setTerminationGracePeriodSeconds(0L);
...@@ -891,7 +886,7 @@ public class K8sService { ...@@ -891,7 +886,7 @@ public class K8sService {
podSpec.setVolumes(volumeList); podSpec.setVolumes(volumeList);
podSpec.setRestartPolicy("Always"); podSpec.setRestartPolicy("Always");
podSpec.setDnsPolicy("ClusterFirst"); podSpec.setDnsPolicy("ClusterFirst");
podSpec.setImagePullSecrets(referenceList); podSpec.setImagePullSecrets(imagePullSecrets);
// 设置PodTemplateSpec // 设置PodTemplateSpec
podTemplateSpec.setMetadata(templateObjectMeta); podTemplateSpec.setMetadata(templateObjectMeta);
...@@ -913,13 +908,6 @@ public class K8sService { ...@@ -913,13 +908,6 @@ public class K8sService {
return kubernetesClient.apps().deployments().inNamespace(namespace).create(deployment); return kubernetesClient.apps().deployments().inNamespace(namespace).create(deployment);
} }
public PersistentVolumeClaim createRedisPvc(String namespace) {
PersistentVolumeClaim redisPvc = new PersistentVolumeClaim();
log.info("开始创建redis pvc:{}", redisPvc.toString());
return kubernetesClient.persistentVolumeClaims().inNamespace(namespace).create(redisPvc);
}
public Service createRedisService(String namespace) { public Service createRedisService(String namespace) {
Service redisService = new Service(); Service redisService = new Service();
...@@ -1079,17 +1067,11 @@ public class K8sService { ...@@ -1079,17 +1067,11 @@ public class K8sService {
containerList.add(container5); containerList.add(container5);
// imagePullSecrets // imagePullSecrets
List<LocalObjectReference> referenceList = new ArrayList<>(); List<LocalObjectReference> imagePullSecrets = buildImagePullSecrets();
LocalObjectReference reference1 = new LocalObjectReference();
LocalObjectReference reference2 = new LocalObjectReference();
reference1.setName("qcloudregistrykey");
reference2.setName("tencenthubkey");
referenceList.add(reference1);
referenceList.add(reference2);
podSpec.setContainers(containerList); podSpec.setContainers(containerList);
podSpec.setDnsPolicy("ClusterFirst"); podSpec.setDnsPolicy("ClusterFirst");
podSpec.setImagePullSecrets(referenceList); podSpec.setImagePullSecrets(imagePullSecrets);
podSpec.setRestartPolicy("Always"); podSpec.setRestartPolicy("Always");
podSpec.setTerminationGracePeriodSeconds(30L); podSpec.setTerminationGracePeriodSeconds(30L);
...@@ -1117,6 +1099,105 @@ public class K8sService { ...@@ -1117,6 +1099,105 @@ public class K8sService {
return kubernetesClient.apps().deployments().inNamespace(namespace).create(redisDeployment); return kubernetesClient.apps().deployments().inNamespace(namespace).create(redisDeployment);
} }
/**
* 创建redis哨兵模式的Deployment
* @param namespace
* @param image
* @return
*/
public Deployment createRedisSentinelDeployment(String namespace, String image) {
Deployment redisSentinelDeployment = new Deployment();
ObjectMeta objectMeta = buildObjectMeta(namespace, "redis-sentinel", "base");
DeploymentSpec deploymentSpec = new DeploymentSpec();
deploymentSpec.setReplicas(1);
deploymentSpec.setRevisionHistoryLimit(1);
LabelSelector labelSelector = new LabelSelector();
Map<String, String> matchLabels = new HashMap<>();
matchLabels.put("qcloud-app", "redis-sentinel");
labelSelector.setMatchLabels(matchLabels);
deploymentSpec.setSelector(labelSelector);
DeploymentStrategy deploymentStrategy = new DeploymentStrategy();
deploymentStrategy.setType("Recreate");
deploymentSpec.setStrategy(deploymentStrategy);
PodTemplateSpec podTemplateSpec = new PodTemplateSpec();
PodSpec podSpec = new PodSpec();
List<Container> containerList = new ArrayList<>();
Container container = new Container();
container.setImage(image);
container.setImagePullPolicy("Always");
container.setName("redis-sentinel");
ResourceRequirements resourceRequirements = buildResourceRequirements("100", "m",
"1000", "Mi", "200", "m", "2000", "Mi");
container.setResources(resourceRequirements);
containerList.add(container);
List<LocalObjectReference> imagePullSecrets = buildImagePullSecrets();
podSpec.setImagePullSecrets(imagePullSecrets);
podSpec.setRestartPolicy("Always");
podSpec.setTerminationGracePeriodSeconds(30L);
podTemplateSpec.setMetadata(objectMeta);
podTemplateSpec.setSpec(podSpec);
deploymentSpec.setTemplate(podTemplateSpec);
DeploymentStatus deploymentStatus = new DeploymentStatus();
redisSentinelDeployment.setApiVersion("apps/v1");
redisSentinelDeployment.setKind("Deployment");
redisSentinelDeployment.setMetadata(objectMeta);
redisSentinelDeployment.setSpec(deploymentSpec);
redisSentinelDeployment.setStatus(deploymentStatus);
return kubernetesClient.apps().deployments().inNamespace(namespace).create(redisSentinelDeployment);
}
/**
* redis哨兵模式 Service
* @param namespace
* @return
*/
public Service createRedisSentinelService(String namespace) {
Service redisSentinelService = new Service();
ObjectMeta objectMeta = buildObjectMeta(namespace, "redis-sentinel", "base");
ServiceSpec serviceSpec = new ServiceSpec();
List<ServicePort> servicePortList = new ArrayList<>();
ServicePort servicePort6379 = new ServicePort();
ServicePort servicePort6380 = new ServicePort();
ServicePort servicePort6381 = new ServicePort();
ServicePort servicePort26379 = new ServicePort();
servicePort6379.setName("6379");
servicePort6379.setPort(6379);
servicePort6380.setName("6380");
servicePort6380.setPort(6380);
servicePort6381.setName("6381");
servicePort6381.setPort(6381);
servicePort26379.setName("26379");
servicePort26379.setPort(26379);
servicePortList.add(servicePort6379);
servicePortList.add(servicePort6380);
servicePortList.add(servicePort6381);
servicePortList.add(servicePort26379);
Map<String, String> selectorMap = new HashMap<>();
selectorMap.put("qcloud-app", "redis-sentinel");
serviceSpec.setType("NodePort");
serviceSpec.setPorts(servicePortList);
serviceSpec.setSelector(selectorMap);
redisSentinelService.setApiVersion("v1");
redisSentinelService.setKind("Service");
redisSentinelService.setMetadata(objectMeta);
redisSentinelService.setSpec(serviceSpec);
return kubernetesClient.services().inNamespace(namespace).create(redisSentinelService);
}
/** /**
* 创建mysql service * 创建mysql service
* @param namespace 环境 * @param namespace 环境
...@@ -1157,6 +1238,7 @@ public class K8sService { ...@@ -1157,6 +1238,7 @@ public class K8sService {
return kubernetesClient.services().inNamespace(namespace).create(mysqlService); return kubernetesClient.services().inNamespace(namespace).create(mysqlService);
} }
/** /**
* 创建mysql pvc * 创建mysql pvc
* *
......
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