如何高效使用kubectl?

Posted futuretea

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何高效使用kubectl?相关的知识,希望对你有一定的参考价值。

alias如下

alias kcn="kubens"

alias kc="kubectl"

# krew
alias kck="kc krew"
alias kcki="kck install"
alias kckr="kck remove"
alias kckl="kck list"
alias kcks="kck search"
alias kcku="kck update"

# kubectl plugins
alias kce="kc iexec"
alias kct="kc tail"
alias kcs="kc sniff"

# kubectl log
alias kcl="kc logs"
alias kclf="kc logs -f"

# kubectl get
alias kcg="kc get"
alias kcga="kcg all"
alias kcgn="kcg node -o wide"
alias kcgns="kcg namespaces"
alias kcgs="kcg svc -o wide"
alias kcgd="kcg deploy"
alias kcgds="kcg ds"
alias kcge="kcg ev --sort-by=.metadata.creationTimestamp -w"

# kubectl get deploy
alias kcgdc="kcgd -o custom-columns='NAME:metadata.name,REPLICAS:spec.replicas,AVAILABLE:status.availableReplicas,CONTAINER:spec.template.spec.containers[*].name'"
alias kcgdci="kcgd -o custom-columns='NAME:metadata.name,REPLICAS:spec.replicas,AVAILABLE:status.availableReplicas,INITCONTAINER:spec.template.spec.initContainers[*].name'"
alias kcgdn="kcgd -o custom-columns='NAME:metadata.name,REPLICAS:spec.replicas,AVAILABLE:status.availableReplicas,DNSPOLICY:spec.template.spec.dnsPolicy,HOSTNETWORK:spec.template.spec.hostNetwork'"

# kubectl get pod
alias kcgp='kcgp(){if [ -z $RUN ];then;kcg pod $@;else;if [ $RUN -eq 0 ];then;kcg pod --field-selector=status.phase!=Running $@;else;kcg pod --field-selector=status.phase==Running $@;fi;fi;};kcgp $@'
alias kcgpn="kcgp -o custom-columns='NAME:metadata.name,NODE:spec.nodeName,DNSPOLICY:spec.dnsPolicy,HOSTNETWORK:spec.hostNetwork,HOSTPORT:spec.containers[*].ports[*].hostPort'"
alias kcgpc="kcgp -o custom-columns='NAME:metadata.name,UID:metadata.uid,CONTAINER:spec.containers[*].name'"
alias kcgpci="kcgp -o custom-columns='NAME:metadata.name,UID:metadata.uid,INITCONTAINER:spec.initContainers[*].name'"
alias kcgps="kcgp -o custom-columns='NAME:metadata.name,SERVICEACCOUNT:spec.serviceAccountName'"
alias kcgpv="kcgp -o custom-columns='NAME:metadata.name,NODE:spec.nodeName,VOLUMES:spec.volumes[*].name'"
alias kcgpi="kcgp -o custom-columns='NAME:metadata.name,IMAGES:spec.containers[*].image'"
alias kcgpip="kcgp -o custom-columns='NAME:metadata.name,PULLPOLICY:spec.containers[*].imagePullPolicy'"
alias kcgpil="kcgp -o custom-columns='IMAGES:spec.containers[*].image' --no-headers | tr ',' '
' | sort -u"
alias kcgpila="kcgp --all-namespaces -o custom-columns='IMAGES:spec.containers[*].image' --no-headers | tr ',' '
' | sort -u"

# others
alias kcdpe="kc delete pod --field-selector=status.phase!=Running"
alias kctpc="kc top pod --containers"
alias kcwpr="kc wait --for condition=ready pod --all"
alias kcapi="kc api-resources"
alias kcdns="pod2xsv | xsv search -s HOSTNETWORK true | xsv search -v -s DNSPOLICY ClusterFirstWithHostNet | xsv select NAME,NAMESPACE,HOSTNETWORK,DNSPOLICY | xsv table"
alias kcgcs="kc config view --minify --output 'jsonpath={..server}'"
alias kcgcn="kc config view --minify --output 'jsonpath={..namespace}'"
alias ?='echo "[cluster] $(kcgcs)"; echo "[namespace] $(kcgcn)";'
alias kubecurl='kc run -i --rm --restart=Never dummy --image=dockerqa/curl:ubuntu-trusty --command -- curl --silent'

# context
alias ccc='cf ${HOME}/.kube/config $(ls ${HOME}/.kube/config.* | cut -d "." -f 2 | fzf)'

# set
alias kcs='kubectl set'
alias kcse='kubectl set env'
alias kcsi='kubectl set image'
alias kcsr='kubectl set resources'
alias kcss='kubectl set selector'
# rollout
alias kcr='kubectl rollout'
alias kcru='kubectl rollout undo'
alias kcrs='kubectl rollout status'
alias kcrr='kubectl rollout restart'
alias kcrh='kubectl rollout history'
alias kcrp='kubectl rollout pause'
# run
alias kuberun='kuberun(){kubectl run $1 --image=$1;kubectl expose deploy $1 --port=$2 --target-port=$2;};kuberun $@'

以上是关于如何高效使用kubectl?的主要内容,如果未能解决你的问题,请参考以下文章

高效Web开发的10个jQuery代码片段

万字硬核干货!6大技巧,极速提升kubectl的生产力!

使用 nsenter 进入 netns 抓包

使用 nsenter 进入 netns 抓包

使用 nsenter 进入 netns 抓包

如何使用 kubectl 命令在 Kubernetes 中关闭自动缩放?