Kubectl随记

来自三线的随记
Admin讨论 | 贡献2022年6月23日 (四) 18:58的版本

kubectl set default editor

export  KUBE_EDITOR=/usr/bin/vim

kubectl completion bash not working 排障随记

需要已经安装 bash-completion

如果提示 _get_comp_words_by_ref: command not found 错误的话,需要执行

source /usr/share/bash-completion/bash_completion

执行命令 set 看一下当前环境的相关配置有没有kube相关项

set | grep -i kube

没有的话需要执行

source <(kubectl completion bash)

正常的话,打开 kubectl debug

__kubectl_debug() {
   if [[ -n ${BASH_COMP_DEBUG_FILE} ]]; then
       echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
   fi
}
export BASH_COMP_DEBUG_FILE=****

根据debug file里面的记录去排查出错点

use kubectl to get all api resource types

kubectl api-resources  -o name --cached --request-timeout=5s --verbs=get


kubectl get resources with custom column

利用custom-columns自定义输出字段(语法相对go-template简单)

kubectl get pods -o custom-columns='pod:metadata.name,uid:metadata.uid'

get node info order by node ip address

根据ip地址排序获取节点信息

kubectl get nodes -owide --sort-by status.addresses[0].address

get pods order by running node name

根据pod运行节点排序获取pod信息

kubectl get pods -owide --sort-by .spec.nodeName
kubectl get pods -owide --sort-by spec.nodeName

sort / order by pod create time

根据pod创建时间排序获取pod

kubectl get pods -n efk-system --sort-by status.startTime
kubectl get pods -n efk-system --sort-by status.startTime| grep filebeat
kubectl get pods -n efk-system --sort-by status.startTime| grep filebeat|tac

kubectl get resources with go-template

Deployments - image

kubectl get deployments. -n dsp-system -o go-template --template '{{range .items}}{{.metadata.name}}{{" -- "}}{{range .spec.template.spec.containers}}{{.image}}{{end}}{{"\n"}}{{end}}'

Pods - image

kubectl get pods -n dsp-system -o go-template --template '{{range .items}}{{.metadata.name}}{{" -- "}}{{range .spec.containers}}{{.image}}{{end}}{{"\n"}}{{end}}'

Get the ip address of the specified node

kubectl get nodes -l kubernetes.io/hostname=nodename -o go-template --template '{{range .items}}{{range .status.addresses}}{{ if eq .type "InternalIP" }}{{.address}}{{end}}{{end}}{{end}}{{"\n"}}'

Get node taints

kubectl get nodes  -o go-template --template '{{range .items}}{{.metadata.name}}{{":\n"}}{{range .spec.taints}}{{.key}}{{"="}}{{.value}}{{":"}}{{.effect}}{{" "}}{{end}}{{"\n\n"}}{{end}}'

kubectl get nodes -l kubernetes.io/hostname=nodename -o go-template --template '{{range .items}}{{.metadata.name}}{{":\n"}}{{range .spec.taints}}{{.key}}{{"="}}{{.value}}{{":"}}{{.effect}}{{" "}}{{end}}{{"\n\n"}}{{end}}'