Bladeren bron

节点分配

martin 2 jaren geleden
bovenliggende
commit
1ec058efec

+ 3 - 0
api-common/src/main/java/api/common/util/StringUtil.java

@@ -5,6 +5,9 @@ import java.util.UUID;
 
 public class StringUtil {
 
+    public static String[] splitByBlank(String string) {
+        return string.split("\\s+");
+    }
 
     public static String getRandomUUID() {
         return UUID.randomUUID().toString().replace("-", "");

+ 1 - 1
simulation-oauth-server/src/main/resources/logback-spring.xml

@@ -11,7 +11,7 @@
         <property name="LOG_HOME" value="/opt/simulation-cloud/simulation-oauth-server/log"/>
     </springProfile>
     <springProfile name="test">
-        <property name="LOG_HOME" value="/opt/simulation-cloud/simulation-oauth-server/log"/>
+        <property name="LOG_HOME" value="/opt/simulation-cloud/simulation-oauth-server-test/log"/>
     </springProfile>
     <!--输出到控制台-->
     <appender name="console" class="ch.qos.logback.core.ConsoleAppender">

+ 1 - 1
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/consumer/ProjectConsumer.java

@@ -190,7 +190,7 @@ public class ProjectConsumer {
         //1 读取 kafka 的 project 信息
         ProjectMessageDTO projectMessageDTO = JsonUtil.jsonToBean(projectJson, ProjectMessageDTO.class);
         String projectId = projectMessageDTO.getProjectId();    // 项目 id
-        String projectType = projectMessageDTO.getType();    // 项目 id
+        String projectType = projectMessageDTO.getType();    // 项目 类型
         String packageId = projectMessageDTO.getScenePackageId();   // 场景测试包 id
         Long maxSimulationTime = projectMessageDTO.getMaxSimulationTime(); // 最大仿真时间,即生成视频的时间长度
         String vehicleConfigId = projectMessageDTO.getVehicleConfigId();// 模型配置 id

+ 19 - 11
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/service/ProjectService.java

@@ -18,6 +18,8 @@ import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.http.client.config.RequestConfig;
 import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.sshd.client.SshClient;
+import org.apache.sshd.client.session.ClientSession;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.redis.core.StringRedisTemplate;
@@ -26,10 +28,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
 import java.util.stream.Collectors;
 
 @Service
@@ -366,13 +365,22 @@ public class ProjectService {
         String replace7 = replace6.replace("apiVers1on", "apiVersion");
         String replace8 = replace7.replace("1atch/v1", "batch/v1");
         // 根据 kubernetes 的 node 分配 job
-        //1 获取 node 列表
-        //2 获取 node 的 pod 列表
-        //3 获取 pod 数量最少的 node
-        //4 将 job 指定 node 执行
-
-
-        String finalYaml = replace8;
+        SshClient client = SshUtil.getClient();
+        ClientSession session = SshUtil.getSession(client, hostname, username, password);
+        String nodeListString = SshUtil.execute(session, "kubectl get node");
+        String podListString = SshUtil.execute(session, "kubectl get pod -o wide");
+        String[] lineArray = nodeListString.split("\n");
+        String minNodeName = "master";
+        long minPodNumber = 9999L;
+        for (int i = 1; i < lineArray.length; i++) {
+            String[] attributeArray = StringUtil.splitByBlank(lineArray[i]);
+            String nodeName = attributeArray[0];
+            long podNumberOfNode = Arrays.stream(podListString.split("\n")).filter(string -> string.contains(nodeName)).count();
+            if (podNumberOfNode < minPodNumber) {
+                minNodeName = nodeName;
+            }
+        }
+        String finalYaml = replace8.replace("node-name", minNodeName);
         log.info("ProjectConsumer--parseManualProject 开始执行 yaml 文件" + finalYaml);
         FileUtil.writeStringToLocalFile(finalYaml, jobTemplateYamlPathTarget);
         //  启动

+ 1 - 0
simulation-resource-scheduler/src/main/resources/kubernetes/template/job/job-template.yaml

@@ -6,6 +6,7 @@ metadata:
   labels:
     user: EY
 spec:
+  nodeName: node-name
   completions: completions-number
   parallelism: parallelism-number
   template: