通过ansible k8s模块删除kubernetes中的所有NOT READY Pod

时间:2020-06-19 08:51:46

标签: kubernetes ansible

要求要做的是

kubectl get pods -n default | grep "0/1" | awk {'print $1'} | xargs kubectl delete pod -n default --force --grace-period=0

解释这里发生的事情-

  • 在默认名称空间中获取所有Pod
  • 夹持处于未就绪状态的
  • 仅获取第一列,即窗格的名称
  • 最后使用kubectl delete命令强行删除那些POD

我尝试过的解决方案 cron-job.yaml

- name: remove dev not ready pods
  hosts: localhost
  remote_user: ubuntu
  become: true
  vars:
    env: "dev"
  roles:
    - { role: k8s-app-cron,            tags: [ 'k8s-app-cron'   ]  }

main.yaml的角色k8s-app-cron

---
- name: get list of pods and remove the not ready ones
  shell: "noOfPODs=`kubectl get pods -n {{item}} | grep 0/1 | wc -l`; if [ $noOfPODs -gt 0 ]; then kubectl get pods -n {{item}} | grep 0/1 | awk {'print $1'}  | xargs kubectl delete pod -n {{item}} --force --grace-period=0; else echo 'No Pods in NOT READY state'; fi"

#  no_log: false
  environment:
    KUBECONFIG: "./_kubeconfig/{{ env }}/kubeconfig"
  register: result
  loop:
    - default
    - front
    - back

0 个答案:

没有答案