Kubectl随记
来自三线的随记
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 api-resources --api-group=networking.k8s.io
get groupVersion when a resource exists in multiple api groups
explain api-resource with sprcific api version
kubectl api-resources --api-group=networking.k8s.io -v8 2>&1| grep ingress
kubectl explain ingress --api-version="networking.k8s.io/v1beta1"
get service account token faster
kubectl get secret sa-secret -o jsonpath={.data.token} | base64 -d
kubectl get resources with custom column
利用custom-columns自定义输出字段(语法相对go-template简单)
kubectl get pods -o custom-columns='namespace:metadata.namespace,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
只获取deployment名字和image
kubectl get deployments -o go-template --template '{{range .items}}{{.metadata.name}}{{" -- "}}{{range .spec.template.spec.containers}}{{.image}}{{" "}}{{end}}{{"\n"}}{{end}}'
Pods - image
只获取pods名字和image
kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{" -- "}}{{range .spec.containers}}{{.image}}{{end}}{{"\n"}}{{end}}'
Get the ip address of the specified node
只获取节点名字和Internal ip地址
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
只获取节点名字和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}}'