1、设置启动pod亲和性反亲和性

This commit is contained in:
chenzhihang 2024-08-28 14:27:19 +08:00
parent 88269061d9
commit cfeca85f6d
3 changed files with 45 additions and 7 deletions

View File

@ -151,7 +151,7 @@ public class ImageController extends BaseController {
@PostMapping("/saveImage") @PostMapping("/saveImage")
@ApiOperation(value = "保存环境为镜像", notes = "docker commit方式保存并推送到horbor") @ApiOperation(value = "保存环境为镜像", notes = "docker commit方式保存并推送到horbor")
public GenericsAjaxResult<String> saveImage(ImageVo imageVo){ public GenericsAjaxResult<String> saveImage(@RequestBody ImageVo imageVo){
return genericsSuccess(this.imageService.saveImage(imageVo)); return genericsSuccess(this.imageService.saveImage(imageVo));
} }
} }

View File

@ -377,12 +377,48 @@ public class K8sClientUtil {
// 创建配置好的Pod // 创建配置好的Pod
public Integer createConfiguredPod(String podName, String namespace, Integer port, String mountPath, V1PersistentVolumeClaim pvc, String image, String dataPvcName, String datasetPath, String modelPath) { public Integer createConfiguredPod(String podName, String namespace, Integer port, String mountPath, V1PersistentVolumeClaim pvc, String image, String dataPvcName, String datasetPath, String modelPath) {
Map<String, String> selector = new LinkedHashMap<>();
selector.put("k8s-jupyter", podName);
//设置选择节点pod反亲和性
Map<String, String> selector = new LinkedHashMap<>();
selector.put("k8s-jupyter", "CPU-GPU");
Map<String, String> nodeSelector = new LinkedHashMap<>(); Map<String, String> nodeSelector = new LinkedHashMap<>();
nodeSelector.put("resource-type", "CPU-GPU"); nodeSelector.put("resource-type", "CPU-GPU");
V1LabelSelectorRequirement labelSelectorRequirement = new V1LabelSelectorRequirement()
.key("k8s-jupyter").operator("NotIn").values(Collections.singletonList("CPU-GPU"));
V1LabelSelector labelSelector = new V1LabelSelector()
.matchExpressions(Collections.singletonList(labelSelectorRequirement));
V1PodAffinityTerm podAffinityTerm = new V1PodAffinityTerm()
.labelSelector(labelSelector)
.namespaces(Collections.singletonList(namespace))
.topologyKey("kubernetes.io/hostname");
V1PodAffinity podAffinity = new V1PodAffinity()
.requiredDuringSchedulingIgnoredDuringExecution(Collections.singletonList(podAffinityTerm));
V1LabelSelectorRequirement antiLabelSelectorRequirement = new V1LabelSelectorRequirement()
.key("k8s-jupyter").operator("In").values(Collections.singletonList("CPU-GPU"));
V1LabelSelector antiLabelSelector = new V1LabelSelector()
.matchExpressions(Collections.singletonList(antiLabelSelectorRequirement));
V1PodAffinityTerm antiPodAffinityTerm = new V1PodAffinityTerm()
.labelSelector(antiLabelSelector)
.namespaces(Collections.singletonList(namespace))
.topologyKey("kubernetes.io/hostname");
// V1WeightedPodAffinityTerm weightedPodAffinityTerm = new V1WeightedPodAffinityTerm().weight(100).podAffinityTerm(podAffinityTerm);
V1PodAntiAffinity podAntiAffinity = new V1PodAntiAffinity()
.requiredDuringSchedulingIgnoredDuringExecution(Collections.singletonList(antiPodAffinityTerm));
V1Affinity v1Affinity = new V1Affinity()
.podAffinity(podAffinity)
.podAntiAffinity(podAntiAffinity);
// 创建Pod
CoreV1Api api = new CoreV1Api(apiClient); CoreV1Api api = new CoreV1Api(apiClient);
V1PodList v1PodList = null; V1PodList v1PodList = null;
try { try {
@ -427,6 +463,7 @@ public class K8sClientUtil {
.endContainer() .endContainer()
.withVolumes(volumes) .withVolumes(volumes)
.withNodeSelector(nodeSelector) .withNodeSelector(nodeSelector)
.withAffinity(v1Affinity)
.endSpec() .endSpec()
.build(); .build();

View File

@ -24,7 +24,7 @@ public class ImageVo implements Serializable {
* 镜像类型 * 镜像类型
*/ */
@ApiModelProperty(name = "imageType") @ApiModelProperty(name = "image_type")
private Integer imageType; private Integer imageType;
@ -43,7 +43,7 @@ public class ImageVo implements Serializable {
/** /**
* 镜像tag名称 * 镜像tag名称
*/ */
@ApiModelProperty(name = "tagName") @ApiModelProperty(name = "tag_name")
private String tagName; private String tagName;
/** /**
@ -57,15 +57,16 @@ public class ImageVo implements Serializable {
@ApiModelProperty(name = "status") @ApiModelProperty(name = "status")
private String status; private String status;
@ApiModelProperty(value = "上传方式, 基于公网上传0基于本地上传1") @ApiModelProperty(name = "upload_type", value = "上传方式, 基于公网上传0基于本地上传1")
private Integer uploadType; private Integer uploadType;
@ApiModelProperty(value = "镜像上传路径") @ApiModelProperty(value = "镜像上传路径")
private String path; private String path;
@ApiModelProperty(value = "环境id") @ApiModelProperty(name = "dev_environment_id", value = "环境id")
private Integer devEnvironmentId; private Integer devEnvironmentId;
// public Integer getId() { // public Integer getId() {
// return id; // return id;
// } // }