合作机构:阿里云 / 腾讯云 / 亚马逊云 / DreamHost / NameSilo / INWX / GODADDY / 百度统计
大家好,我是蜗牛,今天介绍一款Kubernetes的别名工具,可以让你高效管理k8s 集群,下面是相关介绍
我们在管理 Kubernetes集群和执行指定任务是比较复杂和费时的。但是如果使用正确的kubectl别名集,可以大幅简化Kubernetes管理并提高我们的工作效率。在本文中,我们将分享一些有用的 kubectl 别名,它们可以帮助我们更快、更轻松地执行常见任务。从查询资源信息到对 Pod 进行故障排除和管理节点,这些别名将成为 Kubernetes 工具包中的宝贵工具。因此,让我们深入研究并发现简化 Kubernetes 管理所需的 kubectl 别名!
在开始使用这些实用的 kubectl 别名之前,需要确保我们的电脑满足以下前置条件:
图片
把以下别名命令添加到环境变量中,并执行source 命令进行生效
# autocomplete kubectl & helm
source <(kubectl completion zsh)
source <(helm completion zsh)
alias k=kubectl
# when using below aliases, print kubectl command and then execute it
function kctl() { echo "+ kubectl $@" && command kubectl $@ }
# add aliases collection like 'kgpo' for 'kubectl get pods` from https://github.com/ahmetb/kubectl-aliases
[ ! -f ~/.kube/aliases.sh ] && curl -fsSL "https://raw.githubusercontent.com/ahmetb/kubectl-aliases/master/.kubectl_aliases" > ~/.kube/aliases.sh && sed -i -e 's/kubectl/kctl/g' ~/.kube/aliases.sh
source ~/.kube/aliases.sh
# set default namespace
alias kn='kctl config set-context --current --namespace'
# get events sorted by last timestamp
alias kgel='kctl get events --sort-by=.lastTimestamp'
# get events sorted by creation timestamp
alias kgec='kctl get events --sort-by=.metadata.creationTimestamp'
# get pod's descending events
function kger() { kctl get events --sort-by=.lastTimestamp --field-selector involvedObject.name="$@" }
# get 'real' all
alias kgworld='kctl get $(kubectl api-resources --verbs=list --namespaced -o name | paste -sd ",")'
# display all nodes resources request and limits
alias kgnr="k get nodes --no-headers | awk '{print \$1}' | xargs -I {} sh -c 'echo {} ; kubectl describe node {} | grep Allocated -A 5 | grep -ve Event -ve Allocated -ve percent -ve -- ; echo '"
# start a debug pod (including lots of troubleshooting tools)
alias kdebug="kctl -n default run debug-pod --rm -it --tty --image leodotcloud/swiss-army-knife:v0.12 --image-pull-policy=IfNotPresent -- bash"
# get pod's containers list
function kgpc() { kctl get pod -o jsnotallow="{.spec.containers[*].name}" "$@" && echo "" }
# ping a service, ex: 'kping whoami:8080'
alias kping='kctl run httping -it --image bretfisher/httping --image-pull-policy=IfNotPresent --rm=true --'
# get existing pod's yaml without forbidden fields, ex: 'kyaml pod whoami'
function kyaml() { kubectl get "$@" -o yaml | kubectl-neat }
# display and delete failed pods in current namespace
alias krmfailed='kctl delete pods --field-selector=status.phase=Failed'
TOP