Ver Fonte

创建 pod 改成 linux 命令

martin há 2 anos atrás
pai
commit
99458e2678

+ 4 - 12
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/manager/TaskManager.java

@@ -19,8 +19,6 @@ import com.css.simulation.resource.scheduler.util.ProjectUtil;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import io.kubernetes.client.openapi.ApiClient;
 import io.kubernetes.client.openapi.ApiClient;
-import io.kubernetes.client.openapi.models.V1Pod;
-import io.kubernetes.client.util.Yaml;
 import io.minio.MinioClient;
 import io.minio.MinioClient;
 import lombok.SneakyThrows;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
@@ -30,7 +28,6 @@ import org.apache.sshd.client.SshClient;
 import org.apache.sshd.client.session.ClientSession;
 import org.apache.sshd.client.session.ClientSession;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.data.redis.core.StringRedisTemplate;
-import org.springframework.kafka.core.KafkaTemplate;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Component;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.annotation.Transactional;
 
 
@@ -73,8 +70,6 @@ public class TaskManager {
     @Resource
     @Resource
     AutoSubProjectMapper autoSubProjectMapper;
     AutoSubProjectMapper autoSubProjectMapper;
     @Resource
     @Resource
-    KafkaTemplate<String, String> kafkaTemplate;
-    @Resource
     TaskIndexManager taskIndexManager;
     TaskIndexManager taskIndexManager;
     @Resource
     @Resource
     IndexMapper indexMapper;
     IndexMapper indexMapper;
@@ -151,19 +146,16 @@ public class TaskManager {
     /**
     /**
      * 更改一个名字继续启动
      * 更改一个名字继续启动
      *
      *
-     * @param projectId
-     * @param podName
+     * @param projectId 项目 id
+     * @param podName   项目名称
      */
      */
     @SneakyThrows
     @SneakyThrows
     public void createNextPod(String projectId, String podName) {
     public void createNextPod(String projectId, String podName) {
         String lastPodString = FileUtil.read(podYamlDirectory + podName + ".yaml");
         String lastPodString = FileUtil.read(podYamlDirectory + podName + ".yaml");
         String nextPodName = "project-" + projectId + "-" + StringUtil.getRandomUUID();
         String nextPodName = "project-" + projectId + "-" + StringUtil.getRandomUUID();
-        String nextPodFileName = nextPodName + ".yaml";     // 模板文件名称
         String nextPodString = lastPodString.replace("pod-name", podName); // pod 名称包括 projectId 和 随机字符串
         String nextPodString = lastPodString.replace("pod-name", podName); // pod 名称包括 projectId 和 随机字符串
-        FileUtil.writeStringToLocalFile(nextPodString, podYamlDirectory + nextPodFileName);
-        V1Pod v1Pod = (V1Pod) Yaml.load(nextPodString);
-        //  启动
-        KubernetesUtil.createPod(apiClient, kubernetesNamespace, v1Pod);
+        String nextPodFileName = nextPodName + ".yaml";     // 实际执行 pod 的文件名称
+        projectUtil.createPod(nextPodString, nextPodFileName);
     }
     }
 
 
     public void prepareScore(String projectRunningKey) {
     public void prepareScore(String projectRunningKey) {

+ 17 - 48
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/service/ProjectService.java

@@ -9,12 +9,11 @@ import com.css.simulation.resource.scheduler.pojo.to.*;
 import com.css.simulation.resource.scheduler.util.GitUtil;
 import com.css.simulation.resource.scheduler.util.GitUtil;
 import com.css.simulation.resource.scheduler.util.KubernetesUtil;
 import com.css.simulation.resource.scheduler.util.KubernetesUtil;
 import com.css.simulation.resource.scheduler.util.MinioUtil;
 import com.css.simulation.resource.scheduler.util.MinioUtil;
+import com.css.simulation.resource.scheduler.util.ProjectUtil;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import io.kubernetes.client.openapi.ApiClient;
 import io.kubernetes.client.openapi.ApiClient;
-import io.kubernetes.client.openapi.models.V1Pod;
-import io.kubernetes.client.util.Yaml;
 import io.minio.MinioClient;
 import io.minio.MinioClient;
 import lombok.SneakyThrows;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
@@ -28,7 +27,6 @@ import org.springframework.transaction.annotation.Transactional;
 
 
 import javax.annotation.Resource;
 import javax.annotation.Resource;
 import java.io.File;
 import java.io.File;
-import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStream;
 import java.util.*;
 import java.util.*;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
@@ -77,6 +75,8 @@ public class ProjectService {
     AlgorithmMapper algorithmMapper;
     AlgorithmMapper algorithmMapper;
     @Resource
     @Resource
     ApiClient apiClient;
     ApiClient apiClient;
+    @Resource
+    ProjectUtil projectUtil;
 
 
 
 
     // -------------------------------- Comment --------------------------------
     // -------------------------------- Comment --------------------------------
@@ -107,13 +107,6 @@ public class ProjectService {
             }
             }
         }
         }
 
 
-//        //4 将项目状态修改为执行中(页面上已经改成了执行中,这里应该不需要)
-//        if (DictConstants.PROJECT_TYPE_MANUAL.equals(projectType)) {
-//            manualProjectMapper.updateNowRunStateById(DictConstants.PROJECT_RUNNING, projectId);
-//        } else if (DictConstants.PROJECT_TYPE_AUTO_SUB.equals(projectType)) {
-//            autoSubProjectMapper.updateNowRunStateById(DictConstants.PROJECT_RUNNING, projectId);
-//        }
-
         //5 将该 project 下所有旧的任务和指标得分删除。
         //5 将该 project 下所有旧的任务和指标得分删除。
         taskMapper.deleteByProject(projectId);
         taskMapper.deleteByProject(projectId);
         indexMapper.deleteFirstTargetScoreByProjectId(projectId);
         indexMapper.deleteFirstTargetScoreByProjectId(projectId);
@@ -285,9 +278,9 @@ public class ProjectService {
     /**
     /**
      * 将 master 节点设置成镜像仓库,导入镜像的同时 commit 到仓库当中,供其他节点 pull
      * 将 master 节点设置成镜像仓库,导入镜像的同时 commit 到仓库当中,供其他节点 pull
      *
      *
-     * @param projectId
-     * @param algorithmId
-     * @return
+     * @param projectId 项目 id
+     * @param algorithmId 算法 id
+     * @return 镜像名称
      */
      */
     @SneakyThrows
     @SneakyThrows
     @Transactional
     @Transactional
@@ -379,32 +372,6 @@ public class ProjectService {
         return dockerImage;
         return dockerImage;
     }
     }
 
 
-//    /**
-//     * 运行
-//     *
-//     * @param projectId            项目id
-//     * @param nodeMap              并行度
-//     * @param algorithmDockerImage 算法镜像
-//     */
-//    @SneakyThrows
-//    public void transferAndRunYaml(String projectId, Map<String, Integer> nodeMap, String algorithmDockerImage) {
-//        String podTemplate = FileUtil.read(podTemplateYaml);
-//        String replace0 = podTemplate.replace("vtd-container", "vtd-" + projectId);
-//        String replace1 = replace0.replace("algorithm-container", "algorithm-" + projectId);
-//        String replace2 = replace1.replace("algorithm-image", algorithmDockerImage);
-//        String replace3 = replace2.replace("kafkaTopic", projectId);     // 消息主题名称为 projectId
-//        nodeMap.forEach((nodeName, parallelism) -> {
-//            String tempPodNameSuffix = projectId + StringUtil.getRandomUUID();
-//            String tempReplace4 = replace3.replace("pod-name", "project-" + tempPodNameSuffix); // pod 名称包括 projectId 和 随机字符串
-//            String tempFinalYaml = tempReplace4.replace("node-name", nodeName);     // 指定 pod 运行节点
-//            log.info("ProjectConsumer--parseManualProject 在节点 " + nodeName + " 开始执行 pod:" + finalYaml);
-//            String tempFinalYamlTargetPath = podYamlDirectory + tempPodNameSuffix;
-//            FileUtil.writeStringToLocalFile(tempFinalYaml, tempFinalYamlTargetPath);
-//            //  启动
-//            KubernetesUtil.applyYaml(hostname, username, password, jobTemplateYamlPathTarget);
-//        });
-//    }
-
     /**
     /**
      * 运行 pod
      * 运行 pod
      *
      *
@@ -430,15 +397,17 @@ public class ProjectService {
             String tempReplace4 = podTemplateStringOfProject.replace("pod-name", podName); // pod 名称包括 projectId 和 随机字符串
             String tempReplace4 = podTemplateStringOfProject.replace("pod-name", podName); // pod 名称包括 projectId 和 随机字符串
             String tempPodString = tempReplace4.replace("node-name", nodeName);     // 指定 pod 运行节点
             String tempPodString = tempReplace4.replace("node-name", nodeName);     // 指定 pod 运行节点
             log.info("ProjectService--createPod 在节点 " + nodeName + " 开始执行 pod:" + tempPodString);
             log.info("ProjectService--createPod 在节点 " + nodeName + " 开始执行 pod:" + tempPodString);
-            V1Pod v1Pod;
-            try {
-                FileUtil.writeStringToLocalFile(tempPodString, podYamlDirectory + tempPodFileNameOfProject);
-                v1Pod = (V1Pod) Yaml.load(tempPodString);
-            } catch (IOException e) {
-                throw new RuntimeException("ProjectService--createPod 项目 " + projectId + " 创建 pod 失败,为:", e);
-            }
-            //  启动
-            KubernetesUtil.createPod(apiClient, kubernetesNamespace, v1Pod);
+            projectUtil.createPod(tempPodString,tempPodFileNameOfProject);
+
+
+//            V1Pod v1Pod;
+//            try {
+//                FileUtil.writeStringToLocalFile(tempPodString, podYamlDirectory + tempPodFileNameOfProject);
+//                v1Pod = (V1Pod) Yaml.load(tempPodString);
+//            } catch (IOException e) {
+//                throw new RuntimeException("ProjectService--createPod 项目 " + projectId + " 创建 pod 失败:", e);
+//            }
+//            KubernetesUtil.createPod(apiClient, kubernetesNamespace, v1Pod);
         });
         });
     }
     }
 
 

+ 10 - 3
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/util/KubernetesUtil.java

@@ -11,7 +11,6 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.sshd.client.SshClient;
 import org.apache.sshd.client.SshClient;
 import org.apache.sshd.client.session.ClientSession;
 import org.apache.sshd.client.session.ClientSession;
 
 
-import java.io.IOException;
 import java.util.List;
 import java.util.List;
 import java.util.Objects;
 import java.util.Objects;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
@@ -39,10 +38,18 @@ public class KubernetesUtil {
         return result;
         return result;
     }
     }
 
 
-    public static void applyYaml(String hostname, String username, String password, String jobTemplateYamlPathTarget) throws IOException {
+    /**
+     * 执行 yaml
+     * @param hostname 主机名
+     * @param username 用户名
+     * @param password 密码
+     * @param yamlPath yaml 文件路径
+     */
+    @SneakyThrows
+    public static void applyYaml(String hostname, String username, String password, String yamlPath){
         SshClient client = SshUtil.getClient();
         SshClient client = SshUtil.getClient();
         ClientSession session = SshUtil.getSession(client, hostname, username, password);
         ClientSession session = SshUtil.getSession(client, hostname, username, password);
-        SshUtil.execute(session, "kubectl apply -f " + jobTemplateYamlPathTarget);
+        SshUtil.execute(session, "kubectl apply -f " + yamlPath);
         session.close();
         session.close();
         client.stop();
         client.stop();
     }
     }

+ 22 - 0
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/util/ProjectUtil.java

@@ -3,6 +3,7 @@ package com.css.simulation.resource.scheduler.util;
 import api.common.pojo.constants.DictConstants;
 import api.common.pojo.constants.DictConstants;
 import api.common.pojo.dto.ProjectMessageDTO;
 import api.common.pojo.dto.ProjectMessageDTO;
 import api.common.util.CollectionUtil;
 import api.common.util.CollectionUtil;
+import api.common.util.FileUtil;
 import api.common.util.JsonUtil;
 import api.common.util.JsonUtil;
 import api.common.util.StringUtil;
 import api.common.util.StringUtil;
 import com.css.simulation.resource.scheduler.configuration.kubernetes.KubernetesConfiguration;
 import com.css.simulation.resource.scheduler.configuration.kubernetes.KubernetesConfiguration;
@@ -16,6 +17,7 @@ import com.css.simulation.resource.scheduler.pojo.to.KubernetesNodeTO;
 import com.css.simulation.resource.scheduler.pojo.to.PrefixTO;
 import com.css.simulation.resource.scheduler.pojo.to.PrefixTO;
 import lombok.SneakyThrows;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.kafka.core.KafkaTemplate;
 import org.springframework.kafka.core.KafkaTemplate;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Component;
@@ -31,6 +33,16 @@ import java.util.stream.Collectors;
 @Slf4j
 @Slf4j
 public class ProjectUtil {
 public class ProjectUtil {
 
 
+    @Value("${scheduler.linux-path.pod-yaml-directory}")
+    String podYamlDirectory;
+    @Value("${scheduler.host.hostname}")
+    String hostname;
+    @Value("${scheduler.host.username}")
+    String username;
+    @Value("${scheduler.host.password}")
+    String password;
+
+
     @Resource
     @Resource
     ManualProjectMapper manualProjectMapper;
     ManualProjectMapper manualProjectMapper;
     @Resource
     @Resource
@@ -46,6 +58,16 @@ public class ProjectUtil {
     @Resource
     @Resource
     StringRedisTemplate stringRedisTemplate;
     StringRedisTemplate stringRedisTemplate;
 
 
+    @SneakyThrows
+    public void createPod(String podYamlContent, String podYamlName) {
+        String podYamlPath = podYamlDirectory + podYamlName;
+        FileUtil.writeStringToLocalFile(podYamlContent, podYamlPath);
+        KubernetesUtil.applyYaml(hostname, username, password, podYamlPath);
+//        下面这种执行方法报错
+//        V1Pod v1Pod = (V1Pod) Yaml.load(nextPodString);
+//        KubernetesUtil.createPod(apiClient, kubernetesNamespace, v1Pod);
+    }
+
     public String getProjectTypeByProjectId(String projectId) {
     public String getProjectTypeByProjectId(String projectId) {
         String projectType = null;
         String projectType = null;
         ProjectPO manualProjectPO = manualProjectMapper.selectById(projectId);
         ProjectPO manualProjectPO = manualProjectMapper.selectById(projectId);