Kubectl随记:修订间差异
来自三线的随记
小无编辑摘要 |
小无编辑摘要 |
||
第65行: | 第65行: | ||
==== Deployments - image / 只获取deployment名字和image==== | ==== Deployments - image / 只获取deployment名字和image==== | ||
<nowiki>kubectl get deployments -o go-template --template '{{range .items}}{{.metadata.name}}{{" -- "}}{{range .spec.template.spec.containers}}{{.image}}{{" "}}{{end}}{{"\n"}}{{end}}'</nowiki> | <nowiki>kubectl get deployments -o go-template --template '{{range .items}}{{.metadata.name}}{{" -- "}}{{range .spec.template.spec.containers}}{{.image}}{{" "}}{{end}}{{"\n"}}{{end}}'</nowiki> | ||
稍微格式化输出,输出deployment名字 + container名字 + 对应的image | |||
kubectl get | kubectl get deployments.apps -o go-template --template '<nowiki>{{range .items}}</nowiki><nowiki>{{printf "%-30s " .metadata.name}}</nowiki><nowiki>{{range .spec.template.spec.containers}}</nowiki><nowiki>{{.name}}</nowiki><nowiki>{{":"}}</nowiki><nowiki>{{.image}}</nowiki><nowiki>{{" "}}</nowiki><nowiki>{{end}}</nowiki><nowiki>{{"\n"}}</nowiki><nowiki>{{end}}</nowiki>' | ||
====Pods - image / 只获取pod名字和image==== | ====Pods - image / 只获取pod名字和image==== |
2023年5月6日 (六) 22:41的版本
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
Get pod 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 / 利用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}}'
稍微格式化输出,输出deployment名字 + container名字 + 对应的image
kubectl get deployments.apps -o go-template --template '{{range .items}}{{printf "%-30s " .metadata.name}}{{range .spec.template.spec.containers}}{{.name}}{{":"}}{{.image}}{{" "}}{{end}}{{"\n"}}{{end}}'
Pods - image / 只获取pod名字和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 / 根据对象进行过滤获取k8s事件
kubectl get event -n kube-system --field-selector involvedObject.name=${pod_name}