每天凌晨4点清除僵尸pod

This commit is contained in:
wangwei10061 2018-06-26 15:40:31 +08:00
parent 7b0d516741
commit 413b1f4bbf
5 changed files with 51 additions and 42 deletions

12
pom.xml
View File

@ -201,6 +201,18 @@
</exclusions>
</dependency>
<!-- quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
<build>

View File

@ -60,47 +60,6 @@ public class DelPodThread implements Runnable {
GameService gameService = (GameService) BeanFactory.getObejct("GameService");
gameService.executeWaitingTask();
// 删除存活时间超过半小时的pod
deleteTimeoutPod(30 * 60);
}
/**
* 删除存活时间超过一定时间(s)的pod
* @param maxAliveTime
*/
private void deleteTimeoutPod(int maxAliveTime) {
// 找所有pod与其存活的时间
JSONObject result = ShellUtil.executeAndGetExitStatus("kubectl get pods | awk -F' ' '{print $1, $5}' | grep -v NAME");
if (result.getInteger("exitStatus") != 0) {
return;
}
try {
String out = result.getString("out");
String[] pods = out.split(System.lineSeparator());
for (String pod : pods) {
String[] podDetail = pod.split(" ");
int podAge = 0;
if (podDetail[1].endsWith("s")) {
podAge = Integer.parseInt(podDetail[1].substring(0, podDetail[1].length() - 1));
} else if (podDetail[1].endsWith("m")) {
podAge = Integer.parseInt(podDetail[1].substring(0, podDetail[1].length() - 1)) * 60;
} else if (podDetail[1].endsWith("h")) {
podAge = Integer.parseInt(podDetail[1].substring(0, podDetail[1].length() - 1)) * 60 * 60;
} else {
podAge = Integer.parseInt(podDetail[1].substring(0, podDetail[1].length() - 1)) * 60 * 60 * 24;
}
if (podAge > maxAliveTime) {
logger.warn("删除存活超过半个小时的pod{}", podDetail[0]);
ShellUtil.execute("kubectl delete pod " + podDetail[0] + " --now");
}
}
} catch (Exception e) {
// 非重要操作将异常捕捉并不做处理
logger.debug(e.toString());
}
}
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2017-2018 Educoder Group.
*/
package com.educoder.bridge.game.thread;
import com.educoder.bridge.common.utils.ShellUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* 清理僵尸pod的任务
*
* @author weishao
*/
@Component
public class DelZombiePodTask {
private final static Logger logger = LoggerFactory.getLogger(DelZombiePodTask.class);
@Scheduled(cron = "0 0 4 * * ?")
public void work() {
logger.info("定时任务: 开始删除所有pod");
ShellUtil.execute("kubectl delete pod --all");
logger.info("定时任务: 删除所有pod完毕");
}
}

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<task:annotation-driven/>
</beans>

View File

@ -9,7 +9,7 @@
<!-- Spring 上下文参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
<param-value>classpath:applicationContext.xml,classpath:quartz.xml</param-value>
</context-param>
<listener>