k8s 命令常用批量操作

用一行命令搞定:
kubectl get pods --all-namespaces -o wide | grep Evicted | awk '{print $1,$2}' | xargs -L1 kubectl delete pod -n

如:

kubectl get pods --all-namespaces -o wide | grep Evicted | awk '{print $1,$2}' | xargs -L1 kubectl delete pod -n 
pod "public-fe-tweeter-node-online-65fc7889-tjgn9" deleted
pod "flink-taskmanager-java-online-76b8c459f-5xqtv" deleted
pod "flink-taskmanager-java-online-76b8c459f-7hfdw" deleted
pod "flink-taskmanager-java-online-76b8c459f-jkb8l" deleted
pod "flink-taskmanager-java-online-76b8c459f-nwls4" deleted
pod "flink-taskmanager-java-online-76b8c459f-t7xxk" deleted
pod "saas-jcpt-saas-uc-base-tomcat-online-67bdb8585c-skzz5" deleted
pod "saas-jcpt-saas-uc-base-tomcat-online-69996b44-kcnqp" deleted
pod "saas-jcpt-saas-uc-base-tomcat-online-6cb9c6cb5-qjfj4" deleted
pod "saas-jcpt-saas-uc-base-tomcat-online-6cb9c6cb5-rr9nf" deleted
pod "saas-jcpt-saas-uc-base-tomcat-online-77948c97c5-2m4jf" deleted
pod "saas-jcpt-saas-uc-base-tomcat-online-77948c97c5-qjgh5" deleted
pod "saas-jcpt-saas-uc-base-tomcat-online-b4c456646-qr5wl" deleted
pod "saas-jcpt-saas-uc-base-tomcat-online-b4c456646-sshlr" deleted
pod "saas-jcpt-saas-uc-base-tomcat-online-b4c456646-wqnpq" deleted

也可以把Evicted换成OutOfcpu等其它状态使用。

批量加标签:

for node in `kubectl get node | grep node | awk '{print $1}'`; do kubectl label node $node edug=traefik ; done 

停用deployment:
kubectl patch deployment -p '{"spec":{"replicas":0}}' -n public-devops-tomcat-dev public-devops-oomtest-tomcat-dev

启用deployment:
kubectl patch deployment -p '{"spec":{"replicas":1}}' -n public-devops-tomcat-dev public-devops-oomtest-tomcat-dev

批量停用deployment:
kubectl get deployment --all-namespaces -o wide | grep public-devops-tomcat-dev | awk '{print $1,$2}' | xargs -L1 kubectl patch deployment -p '{"spec":{"replicas":0}}' -n

另一种批量停启deployment的方法:

  • 先从保存好现有的pod数信息
    kubectl get deployments --all-namespaces | grep -v "NAMESPACE" | grep -v "kube-system" | grep -v "default" | grep -v "prometheus" > deployments.txt

  • 再批量配置所有deployment的pod数为0
    kubectl get deployments --all-namespaces | grep -v "NAMESPACE" | grep -v "kube-system" | grep -v "default" | grep -v "prometheus" | awk '{print $1,$2}' | xargs -L1 kubectl scale --replicas=0 deployment -n

  • 执行以下脚本从保存好的配置中恢复配置:

#!/bin/bash

# kubectl get deployments --all-namespaces | grep -v "NAMESPACE" | grep -v "kube-system" | grep -v "default" | grep -v "prometheus"  > deployments.txt


# set all app replicas to 0
# kubectl get deployments --all-namespaces | grep -v "NAMESPACE" | grep -v "kube-system" | grep -v "default" | grep -v "prometheus"  | awk '{print $1,$2}' | xargs -L1 kubectl scale --replicas=0 deployment -n

# delete all pod
# kubectl get pod --all-namespaces | grep -v "NAMESPACE" | grep -v "kube-system" | grep -v "default" | grep -v "prometheus"  | awk '{print $1,$2}' | xargs -L1 kubectl delete pod -n

cat deployments.txt | while read namespace name x1 replicas x2 x3; do
  echo "kubectl scale --replicas=$replicas deployment -n $namespace $name"
  kubectl scale --replicas=$replicas deployment -n $namespace $name
done