分类
devops

kubectl总结


Last Updated on 2022-11-20

kubectl patch更改svc为NodePort

kubectl patch  svc my-eureka --type='json' -p '[{"op":"replace","path":"/spec/type","value":"NodePort"},{"op":"add","path":"/spec/ports/0/nodePort","value":30030}]'

kubectl patch svc my-eureka --type='json' -p '[{"op":"remove","path":"/spec/ports/0/nodePort"},{"op":"replace","path":"/spec/type","value":"ClusterIP"}]'

or

kubectl patch svc my-eureka -p '{"spec": {"type": "NodePort"}}'

重启所有kube-proxy的pod(kubeadm安装的)20220929

date +%s | xargs -I@@ kubectl patch -n kube-system ds kube-proxy -p '{"spec":{"template":{"metadata":{"annotations":{"commit_sha":"@@"}}}}}'

20221004 新增

kubectl rollout restart -n kube-system daemonset kube-proxy

approve所有csr证书请求

( kubectl get csr --no-headers | grep -i 'pending' | awk '{print $1}' ) | xargs -r kubectl certificate approve

设置default storageclass

kubectl patch storageclass nfs-client -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'

查看集群的Service CIDR

kubectl get cm -o yaml -n kube-system kubeadm-config | grep Subnet

或者

echo '{"apiVersion":"v1","kind":"Service","metadata":{"name":"tst"},"spec":{"clusterIP":"1.1.1.1","ports":[{"port":443}]}}' | kubectl apply -f - 2>&1 | sed 's/.*valid IPs is //'

获取某个节点的podCIDR

kubectl get nodes <NODE> -o jsonpath='{.items[*].spec.podCIDR}'

循环删除CrashLoopBackOff、Evicted、Terminating的pod

for ns in $(kubectl get ns -o json | jq -r '[.items[] | .metadata.name ] | join(" ")'); do
  #echo $ns;
  kubectl -n "$ns" get po | grep -E "Evicted|Terminating|CrashLoopBackOff" | awk '{print $1}'| xargs --no-run-if-empty -I% kubectl -n "$ns" delete pod % --force --grace-period=0
done

获取所有空间

kubectl get ns --template '{{range.items}}{{.metadata.name}}{{"n"}}{{end}}'

或者

kubectl get ns -o jsonpath='{range .items[*]}{.metadata.name}{"n"}{end}'
kubectl get ns -o jsonpath='{.items[*].metadata.name}'

获取所有pod的namespace、名字、podID

kubectl get pods -A -o jsonpath='{range .items[*]}{.metadata.namespace}{"/"}{.metadata.name}{","}{.status.podIP}{"n"}{end}'

可以通过增加 -l 通过label参数过滤选择某一类pod

kubelet查看错误

# 查看 kubelet 运行状态及其日志有无错误信息提示。
systemctl status kubelet -l --no-pager
journalctl -xeu kubelet --no-pager

# 查看 Conatinerd 运行状态及其日志有无错误信息提示。
systemctl status containerd -l --no-pager
journalctl -xeu containerd --no-pager

# 查看 docker 运行状态及其日志有无错误信息提示。
systemctl status docker -l --no-pager
journalctl -xeu docker --no-pager