本文发布于Cylon的收藏册,转载请著名原文链接~


Pod

检查 Pod 就绪探针

kubectl get pods <pod-name> -n <namespace> -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}'

查看 Pod 事件

kubectl get events -n <namespace> --field-selector involvedObject.name=<pod-name>

获取 Pod Affinity 和 Anti-Affinity

kubectl get pod <pod-name> -n <namespace> -o=jsonpath='{.spec.affinity}'

列出 Pod 的 anti-affinity 规则

kubectl get pod <pod-name> -n <namespace> -o=jsonpath='{.spec.affinity.podAntiAffinity}'

Pod Network

运行 Debug Pod 进行调试

kubectl run -it --rm --restart=Never --image=busybox net-debug-pod -- /bin/sh

测试从 Pod 到 Endpoint 的连接性

kubectl exec -it <pod-name> -n <namespace> -- curl <endpoint-url>

trace 一个 Pod 到另一个 Pod 的网络

kubectl exec -it <source-pod-name> -n <namespace> -- traceroute <destination-pod-ip>

检查 Pod DNS配置

kubectl exec -it <pod-name> -n <namespace> -- cat /etc/resolv.conf

Deployment

查看 rollout 状态

kubectl rollout status deployment/<deployment-name> -n <namespace>

查看 rollout 历史记录

kubectl rollout history deployment/<deployment-name> -n <namespace>

调整 deployment 数量

kubectl scale deployment <deployment-name> --replicas=<replica-count> -n <namespace>

设置 deployment 的自动缩放

kubectl autoscale deployment <deployment-name> \
	--min=<min-pods> \
	--max=<max-pods> \
	--cpu-percent=<cpu-percent> \
	-n <namespace>

检查 HAP 状态

kubectl get hpa -n <namespace>

Networking

显示命名空间中 Pod 的 IP 地址:

kubectl get pods -n <namespace -o custom-columns=POD:metadata.name,IP:status.podIP --no-headers

Node

获取特定 Node 上运行的 Pod 列表

kubectl get pods --field-selector spec.nodeName=<node-name> -n <namespace>
kubectl get pod -n {ns} -o \ 
    jsonpath='{range $.items[?(@.spec.nodeName == "xxxx")]}{.metadata.name}{"\n"}'

获取 Node 的 IP

kubectl get pod -n {ns} -o \ 
    -o=jsonpath='{range $.items[*]}{.metadata.name}{"\t"}{.status.addresses[0].address}{"\n"}{end}'

获取 Node 上 指定标签

kubectl get nodes \
    -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.metadata.labels.<xxx>}{"\n"}{end}'

查找属于这个标签 Node 的数量

kubectl get node -l xxx=xxxx --no-headers | wc -l

查看node上所有的标签,json格式

kubectl get node -o json | jq '.items[] | {name: .metadata.name, labels: .metadata.labels }' --compact-output | jq -r

根据 NodeSelector 查询 application

kubectl get deployment -A \
    -o=jsonpath='{range .items[*]}{@.metadata.name}{"\t"}{range @.spec.template.spec.nodeSelector}{.xxxx}{"\n"}{end}{end}'

根据条件列出 Node 1.17+

kubectl get nodes -o \
	custom-columns=NODE:.metadata.name,READY:.status.conditions[?(@.type=="Ready")].status -l 'node-role.kubernetes.io/worker=xxx'

获取 Node 的操作系统信息

kubectl get node <node-name> -o jsonpath='{.status.nodeInfo.osImage}'

查询应用 (Deployment) 使用的 Node

kubectl deployment -n {ns} -o \
    jsonpath='{range $.item[*]}{"Deployment: "}{.metadata.name}{"\n"}{"\t"}{.spec.template.spec.nodeSelector.srv-grp}{"\n"}{end}'

仅获取 Node 的指定标签和 Pod 名称

k get nodes -o \
jsonpath='{range .items[*]}{.metadata.labels.nodeSelector.xxxxx}{"\t"}{.status.addresses[?(@.type=="InternalIP")].address}{"\n"}{end}'

仅获取 Node IP

k get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="InternalIP")].address}'

获取指定标签的 Node,并输出他的标签和 IP

k get nodes -o \
	jsonpath='{range .items[?(@.metadata.labels.xxxx=="xxxx")]}{.metadata.labels.srv-grp}{"\t"}{.status.addresses[?(@.type=="InternalIP")].address}{"\n"}{end}'

获取指定 Role 的 Node 列表,只输出他的主机名

k get node --all-namespaces \
	-o=jsonpath='{range .items[?(@.metadata.labels.kubernetes.io/role=="xxxx")]}{.metadata.name}{"\n"}{end}'

修改指定 Role 的 Node 的角色值 (jq)

# 假设将 k8s role 为 aaa 的主机修改为 role=bbb
for n in `k get nodes -o json |jq -r '.items[] | select(.metadata.labels."kubernetes.io/role" == "aaa") | .metadata.name'`;
do 
	k label node $n kubernetes.io/role=bbb --overwrite
done

修改指定 Role 的 Node 的角色值 (jsonpath)

# 假设将 k8s node 标签 xxx 为 aaa 的主机修改为 xxx=bbb
for n in `k get nodes -o jsonpath='{range .items[?(@.metadata.labels.xxx=="aaa")]}{.metadata.name}{"\n"}{end}'`;\
do
	k label node $n xxx=bbb  --overwrite
done

Resource Quotas

列出命名空间中的资源配额

kubectl get resourcequotas -n <namespace>

查看资源配额详情

kubectl describe resourcequota <resource-quota-name> -n <namespace>

Volumes

按容量排序的列出PV

kubectl get pv --sort-by=.spec.capacity.storage

检查 PV reclaim policy

kubectl get pv <pv-name> -o=jsonpath='{.spec.persistentVolumeReclaimPolicy}'

Ephemeral Containers

1.18+

运行一个 ephemeral debugging 容器

kubectl debug -it <pod-name> -n <namespace> --image=<debug-image> -- /bin/sh

Pod Disruption Budget (PDB)

列出一个ns内的PDB

kubectl get pdb -n <namespace>

ConfigMap

批量备份 configmap

Secret

从一个 namespace 导出所有 secret 到另一个 namespace

for n in `kubectl get secret -n xxxx -o jsonpath='{range $.items[*]}{.metadata.name}{"\n"}{end}'`;do
    kubectl get secret -n xxxx $n -o yaml | \
     sed -e '/resourceVersion:/d;/uid:/d; /selfLink:/d; /creationTimestamp:/d;' \
         -e 's/namespace: <xxxxxx>/namespace: <xxxxx>/' \
    kubectl create -f -
done

查看证书是否过期

ns=xxx
secret_name=xxx
keywords=xxx
_command=xxxx
openssl x509 -in <(
    ${_command} get secret -n $ns $secret_name -ojson | \
    jq --arg secret_key $(
        ${_command} get secret -n ${ns} ${secret_name} -ojson | \
        jq -r '.data | keys[]' | \
        awk -v keywords=${keywords} '$0 ~ keywords { print }'
    ) -r '.data | .[$secret_key]' | base64 -d
) -text -noout | \
grep -i "subject: "

批量备份集群内指定secret命令

# 指定域名的secret
keywords=xxx
_command=xxxx
for ns in `${_command} get ns -o jsonpath='{range $.items[*]}{.metadata.name}{"\n"}{"end"}'`
do
    for secret_name in `${_command} get secret -n $ns -o json|jq -r '.items[] | select( .type == "kubernetes.io/tls") | .metadata.name'`
    do  
        openssl x509 -in <(
            ${_command} get secret -n ${ns} ${secret_name} -ojson | \
            jq --arg secret_key $(
                    ${_command} get secret -n ${ns} ${secret_name} -ojson | 
                    jq -r '.data | keys[]' | \
                    awk '$0 ~ /crt/ { print }'
                ) -r '.data|.[$secret_key]'|base64 -d
        ) -text -noout | \
        grep -i "subject: "| \
        awk -v ns=${ns} -v secret_name=${secret_name} -v cmd=${_command} -v kw=${keywords} \
            '$0 ~ kw { system(
                cmd" get secret -n "ns" "secret_name" -oyaml > primetive/"ns"."secret_name".yaml"
            )}'
    done
done

查询 tls 签发域名,并进行替换

keywords=xxx
_command=xxxx
for ns in `${_command} get ns -o jsonpath='range $.items[*]}{.metadata.name}{"\n"}{"end"}'
do
    for secret_name in `${_command} get secret -n $ns -o json|jq -r '.items[] | select( .type == "kubernetes.io/tls") | .metadta.name'`
    do  
        openssl x509 -n <(
            ${_command} get secret -n ${ns} ${secret_name} -ojson | \
            jq --arg secret_key $(${_command} get secret -n ${ns} ${secret_name} -ojson | \
            jq -r '.data | .keys[]' | \
            awk '$0 ~ /crt/ { print }' -r '.data|.[$secret_key]'|base64 -d
        ) -text -noout | \
        grep -i "subject: "| \
        awk -v ns=${ns} -v secret_name=${secret_name} -v commond=${_command}\
            '$0 ~ ${keywords} { system(
                command" create secret tls -n "ns" "secret_name" --cert=ca.crt --key=key.key --dry-run -oyaml|command" replace -f -"
            )}'
    done
done

本文发布于Cylon的收藏册,转载请著名原文链接~

链接:https://www.oomkill.com/2024/04/kubernetes-kubectl.md/

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」 许可协议进行许可。