备份资源文件到本地
在做集群维护中,备份很重要。以下virtualservice为例。
kubectl --kubeconfig=/Users/xxx/Documents/devops/xxx get virtualservice -n xxx -o=name | xargs -I {} sh -c 'kubectl --kubeconfig=/Users/xxx/Documents/devops/xxx -n xxx get {} -o=yaml > $(basename {}).yaml'
批量调整副本集
kubectl --kubeconfig=xxxx get deployments -n xxxx --no-headers | awk '{print $1}' | xargs -I {} kubectl --kubeconfig=xxxx scale deployment -n xxxx {} --replicas=1
deployment批量增加 label
kubectl --kubeconfig=xxx get deployments -n xxx -o name \
| xargs -I {} kubectl --kubeconfig=xxx patch {} -n xxx -p '{"spec":{"template":{"metadata":{"labels":{"ASM_TRAFFIC_TAG":"version-base"}}}}}' --type merge
关键字查询输出资源名称
kubectl get crd -n xxx -o json --kubeconfig=xxx | jq -r '.items[] | select(. | tostring | contains("process")) | .metadata.name'
以上命令就是过滤 资源 crd 中包含 process 关键字,并输出 crd 的资源名称。
过滤副本集为0的资源
kubectl get deployments --all-namespaces -o jsonpath='{range .items[?(@.spec.replicas==0)]}{.metadata.namespace} {.metadata.name}{"\n"}{end}' --kubeconfig=<config_path>
如果需要指定 namespace 就把–all-namespaces改成-n <target_namespace>。
指定 config ,命名空间获取 deployment
kubectl get deployments -n aaa -o jsonpath='{.items[*].metadata.name}' --kubeconfig=xxx -o wide
查询项目的CRD
kubectl get crd -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' --kubeconfig=xxx -n xxx | grep app_name
过滤 deployments
kubectl get deployments --kubeconfig=xxxx -n a,b,c -o json | jq 'select(.spec.template.metadata.labels.version!="base")'
先查crd再删
kubectl get crd -n xxx --kubeconfig=xxx |grep 关键字 | awk '{print $1}' | xargs kubectl delete crd -n xxx --kubeconfig=xxx