Kubectl随记:修订间差异

来自三线的随记
无编辑摘要
无编辑摘要
第94行: 第94行:
稍微处理一下,格式化对齐输出
稍微处理一下,格式化对齐输出
  kubectl get namespaces -o go-template='<nowiki>{{range .items}}</nowiki><nowiki>{{printf "%-30s " .metadata.name}}</nowiki><nowiki>{{if index .metadata.annotations "scheduler.alpha.kubernetes.io/node-selector"}}</nowiki><nowiki>{{index .metadata.annotations "scheduler.alpha.kubernetes.io/node-selector"}}</nowiki><nowiki>{{else}}</nowiki> <nowiki>{{end}}</nowiki><nowiki>{{"\n"}}</nowiki><nowiki>{{end}}</nowiki>'
  kubectl get namespaces -o go-template='<nowiki>{{range .items}}</nowiki><nowiki>{{printf "%-30s " .metadata.name}}</nowiki><nowiki>{{if index .metadata.annotations "scheduler.alpha.kubernetes.io/node-selector"}}</nowiki><nowiki>{{index .metadata.annotations "scheduler.alpha.kubernetes.io/node-selector"}}</nowiki><nowiki>{{else}}</nowiki> <nowiki>{{end}}</nowiki><nowiki>{{"\n"}}</nowiki><nowiki>{{end}}</nowiki>'
=== kubectl get event filter by pod name ===
kubectl get event -n kube-system --field-selector involvedObject.name=${pod_name}
[[分类:K8s]]
[[分类:K8s]]

2023年3月29日 (三) 11:48的版本

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简单)

通过custom-columns只输出pod所在租户,pod名字和pod uid
kubectl get pods -o custom-columns='namespace:metadata.namespace,pod:metadata.name,uid:metadata.uid'
通过custom-columns只输出namespace名字和ns annotation中定义的node selector
kubectl get namespaces -o custom-columns="NAMESPACE:.metadata.name, NODE_SELECTOR:.metadata.annotations.scheduler\.alpha\.kubernetes\.io/node-selector"

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}}'
获取namespace名字和ns annotation中定义的node selector
kubectl get namespaces -o go-template='{{range .items}}{{.metadata.name}}{{" "}}{{index .metadata.annotations "scheduler.alpha.kubernetes.io/node-selector" -}}{{"\n"}}{{end}}'

稍微处理一下,格式化对齐输出

kubectl get namespaces -o go-template='{{range .items}}{{printf "%-30s " .metadata.name}}{{if index .metadata.annotations "scheduler.alpha.kubernetes.io/node-selector"}}{{index .metadata.annotations "scheduler.alpha.kubernetes.io/node-selector"}}{{else}} {{end}}{{"\n"}}{{end}}'

kubectl get event filter by pod name

kubectl get event -n kube-system --field-selector involvedObject.name=${pod_name}