要求要做的是
kubectl get pods -n default | grep "0/1" | awk {'print $1'} | xargs kubectl delete pod -n default --force --grace-period=0
解释这里发生的事情-
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