k8s pod 重启到达一定次数,自动删除策略

  • 可以获取pod下面的容器的restart count参数,传入容器变量,容器里写个脚本来读值判断
  • 写个 controller ,监听 restartcount

最后实操

删除 haidene命名空间中重启超过100的pod

$ cat  /opt/tools/pod_auto_delete.sh

#!/bin/bash
restart_pods=`kubectl get po -n haidene |sort -k 4 -n -r | head -1`
restart_pod_second=`echo $restart_pods |awk '{print $4}'`
restart_pod_name=`echo $restart_pods |awk '{print $1}'`
[ $restart_pod_second -ge 100 ] && kubectl delete -n haidene pod $restart_pod_name
# [ $restart_pod_second -ge 10 ] && kubectl get -n haidene pod $restart_pod_name

添加定时任务,每分钟执行一次

$ crontab -l

* * * * * /opt/tools/pod_auto_delete.sh >/tmp/pod_auto_delete.log 2>&1

后续优化

可以将此任务放到job控制器中,运行一个带有kubectl命令的容器,其中kubeconfig放一个权限较小只针对个别命名空间有删除权限的token即可。。。

本文作者:鸣昊

本文链接:https://www.cnblogs.com/x602/p/16532177.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   鸣昊  阅读(1135)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
🔑
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.