Kubectl tips and tricks

You can use this also

set expandtab
set tabstop=2
set shiftwidth=2
set autoindent
set smartindent
set bg=dark
set nowrap
set paste

Hi Krzysztof_Tkacz,
I saw this syntax being used in a few tutorials.
kubectl get pod nginx{1…3}
but I can’t find anything similar in kubectl official documents.
Do you have any reference to some docs that explain it more? I was wondering what other things you can do with this type of expansion.

Good question :slight_smile:, probably I found this one in some training. I think it could be good stackoverflow question.

1 Like

My .vimrc

autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab number

source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
alias k=kubectl
complete -F __start_kubectl k
export do="–dry-run=client -o yaml"
vi ~/.vimrc
set ts=2 sw=2 expandtab ruler
retab! # To fix tabs

this will help to print all name spaces.
kubectl get pods --all-namespaces -o wide --no-headers | grep -iv running | awk ‘{print $1"/"$2}’

Couple of tips consolidated from various posts:
bashrc:

alias k=kubectl
alias kn=“kubectl config set-context --current --namespace”
source <(kubectl completion bash)
complete -F __start_kubectl k
alias k=‘kubectl’
alias kn=‘kubectl config set-context --current --namespace’
export do="–dry-run -o yaml"
export fk="–force --grace-period=0"
alias kgd=“k get deploy -o wide”
alias kgp=“k get pods -o wide”
alias kgn=“k get nodes -o wide”
alias kgs=“k get svc -o wide”
alias kge=“k get events - sort-by=’.metadata.creationTimestamp’ |tail -8”
export nks="-n kube-system"

vimrc:

set expandtab
set tabstop=2
set shiftwidth=2
set autoindent
set smartindent
set bg=dark
set nowrap
set paste
“map :%s/\t/ /g”
“set pastetoggle=”

======================MY BASHRC==============================

# Alias's to modified commands
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -iv'
alias mkdir='mkdir -p'
alias ps='ps auxf'
alias ping='ping -c 10'
alias less='less -R'
alias cls='clear'
alias apt-get='sudo apt-get'
alias multitail='multitail --no-repeat -c'
alias freshclam='sudo freshclam'
alias vi='vim'
alias svi='sudo vi'
alias vis='vim "+set si"'

# Change directory aliases
alias home='cd ~'
alias cd..='cd ..'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'

# cd into the old directory
alias bd='cd "$OLDPWD"'

# Remove a directory and all files
alias rmd='/bin/rm  --recursive --force --verbose '

# Alias's for multiple directory listing commands
alias la='ls -Alh' # show hidden files
alias ls='ls -aFh --color=always' # add colors and file type extensions
alias lx='ls -lXBh' # sort by extension
alias lk='ls -lSrh' # sort by size
alias lc='ls -lcrh' # sort by change time
alias lu='ls -lurh' # sort by access time
alias lr='ls -lRh' # recursive ls
alias lt='ls -ltrh' # sort by date
alias lm='ls -alh |more' # pipe through 'more'
alias lw='ls -xAh' # wide listing format
alias ll='ls -Fls' # long listing format
alias labc='ls -lap' #alphabetical sort
alias lf="ls -l | egrep -v '^d'" # files only
alias ldir="ls -l | egrep '^d'" # directories only

# alias chmod commands
alias mx='chmod a+x'
alias 000='chmod -R 000'
alias 644='chmod -R 644'
alias 666='chmod -R 666'
alias 755='chmod -R 755'
alias 777='chmod -R 777'

# Search command line history
alias h="history | grep "

# Search running processes
alias p="ps aux | grep "
alias topcpu="/bin/ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10"

# Search files in the current folder
alias f="find . | grep "

# Count all files (recursively) in the current folder
alias countfiles="for t in files links directories; do echo \`find . -type \${t:0:1} | wc -l\` \$t; done 2> /dev/null"

# To see if a command is aliased, a file, or a built-in command
alias checkcommand="type -t"

# Show current network connections to the server
alias ipview="netstat -anpl | grep :80 | awk {'print \$5'} | cut -d\":\" -f1 | sort | uniq -c | sort -n | sed -e 's/^ *//' -e 's/ *\$//'"

# Show open ports
alias openports='netstat -nape --inet'

# Alias's for safe and forced reboots
alias rebootsafe='sudo shutdown -r now'
alias rebootforce='sudo shutdown -r -n now'

# Alias's to show disk space and space used in a folder
alias diskspace="du -S | sort -n -r |more"
alias folders='du -h --max-depth=1'
alias folderssort='find . -maxdepth 1 -type d -print0 | xargs -0 du -sk | sort -rn'
alias tree='tree -CAhF --dirsfirst'
alias treed='tree -CAFd'
alias mountedinfo='df -hT'

# Alias's for archives
alias mktar='tar -cvf'
alias mkbz2='tar -cvjf'
alias mkgz='tar -cvzf'
alias untar='tar -xvf'
alias unbz2='tar -xvjf'
alias ungz='tar -xvzf'

# Show all logs in /var/log
alias logs="sudo find /var/log -type f -exec file {} \; | grep 'text' | cut -d' ' -f1 | sed -e's/:$//g' | grep -v '[0-9]$' | xargs tail -f"

# SHA1
alias sha1='openssl sha1'

alias lsrat='ls -rathil'
# Calculator

    alias bc="bc -l"

# Clear

    alias c="clear"
    alias cl="clear"
    alias ckear="clear"
    alias clr="clear"



    alias k="kubectl"
    alias kc="kubectl create"
    alias kcd="kubectl create deploy"
    alias kdd="kubectl delete deploy"
    alias kdp="kubectl delete pod"
    alias ka="kubectl apply -f"
    alias kpa="kubectl patch -f"
    alias ked="kubectl edit"
    alias ksc="kubectl scale"
    alias kex="kubectl exec -i -t"
    alias kg="kubectl get"
    alias kga="kubectl get all"
    alias kgall="kubectl get all --all-namespaces"
    alias kinfo="kubectl cluster-info"
    alias kdesc="kubectl describe"
    alias ktp="kubectl top"
    alias klo="kubectl logs -f"
    alias kn="kubectl get nodes"
    alias kpv="kubectl get pv"
    alias kpvc="kubectl get pvc"
    alias kr="kubectl run"
    alias kcn='kubectl config set-context --current --namespace '


    alias dl="sudo docker ps -l -q"
    alias dps="sudo docker ps"
    alias di="sudo docker images"
    alias dip="sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}'"
    alias dkd="sudo docker run -d -P"
    alias dki="sudo docker run -i -t -P"
    alias dex="sudo docker exec -i -t"
    alias drmf='sudo docker stop $(sudo docker ps -a -q) && sudo docker rm $(sudo docker ps -a -q)'

alias h="history"
    alias h1="history 10"
    alias h2="history 20"
    alias h3="history 30"
    alias hgrep='history | grep'

alias pscpu="ps auxf | sort -nr -k 3"
    alias pscpu10="ps auxf | sort -nr -k 3 | head -10"
alias paux='ps aux | grep'
alias cpuinfo="lscpu"
alias gpumeminfo="grep -i --color memory /var/log/Xorg.0.log"
alias wget="wget -c"

alias usage='du -ch | grep total'
alias totalusage='df -hl --total | grep total'
alias partusage='df -hlT --exclude-type=tmpfs --exclude-type=devtmpfs'
alias most='du -hsx * | sort -rh | head -10'
alias rs='rsync -avxHAXWS --numeric-ids --info=progress2'
alias diff='colordiff'
alias path='echo -e ${PATH//:/\\n}'
alias ta='tmux a'
alias tn='tmux new -s'

source <(kubectl completion bash)
complete -F __start_kubectl k

export do="--dry-run=client -o yaml"
export fg="–force --grace-period=0"

=======================MY VIMRC===========================

syntax enable
set tabstop=2       " number of visual spaces per TAB
set softtabstop=2   " number of spaces in tab when editing
set expandtab       " tabs = spaces

set number		" show line numbers
set cursorline		" highlight the current line
set showcmd		" show command in bottom bar
filetype indent on	" load filetype specific indent files in ~/.vim/indent/*.vim
set wildmenu
set lazyredraw
set showmatch		" show matches of {} [] ()

set incsearch		" search as characters are entered
set hlsearch		" highlight matches
set ignorecase		" ignore upper/lower case when searching
let mapleader = "," 	" leader is comma
" turn off search highlight, mapping to ,<space>
nnoremap <leader><space> :nohlsearch<CR>

set backspace=indent,eol,start   " allows backspace to delete words
1 Like

Couple of my tips

export now=‘–grace-period=0 --force’
export do=‘–dry-run=client -o yaml’
Run nginx deployment with 2 replicas: kubectl run my-nginx --image=nginx --replicas=2 --port=80
Run nginx pod and expose it: kubectl run my-nginx --restart=Never --image=nginx --port=80 --expose
Run nginx deployment and expose it: kubectl run my-nginx --image=nginx --port=80 --expose

cm with values faa=popo,faa2=p0p0:
kubectl create configmap config --from-literal=faa=lala --from-literal=faa2=p0p0

cm from a envv file:
kubectl create cm configmap3 --from-env-file=config.env

set ai
set ts=2
set et
set cursorcolumn

here’s my shortcuts
alias kn=‘kubectl config set-context --current --namespace’
export do=‘–dry-run=client -o yaml’
export now=‘–force --grace-period=0’
alias kgd=‘k get deploy -o wide’
alias kgp=‘k get pods -o wide’
alias kgn=‘k get nodes -o wide’
alias kgs=‘k get svc -o wide’
alias kge=‘k get events - sort-by=’.metadata.creationTimestamp’ |tail -8’
export nks=‘-n kube-system’
alias ked=‘k edit deploy’
alias kep=‘k edit pods’
alias ken=‘k edit nodes’
alias kes=‘k edit svc’
alias kdd=‘k describe deploy’
alias kdp=‘k describe pods’
alias kdn=‘k describe nodes’
alias kds=‘k describe svc’
alias krf=‘k replace --force -f’
alias kcf=‘k create -f’

a couple more -

alias kn=‘kubectl config set-context --current --namespace’

alias kgd=‘k get deploy -o wide’

alias kgp=‘k get pods -o wide’

alias kgpw=‘k get pods -o wide --watch’

alias kgpa=‘k get pods -o wide -A’

alias kgn=‘k get nodes -o wide’

alias kgs=‘k get svc -o wide’

alias kge=‘k get events - sort-by=’.metadata.creationTimestamp’ | tail -8’

alias ked=‘k edit deploy’

alias kep=‘k edit pods’

alias ken=‘k edit nodes’

alias kes=‘k edit svc’

alias kd=‘k describe’

alias kdd=‘k describe deploy’

alias kdp=‘k describe pods’

alias kdn=‘k describe nodes’

alias kds=‘k describe svc’

alias krf=‘k replace --force -f’

alias kcf=‘k create -f’

export nks=‘-n kube-system’

export do=‘–dry-run=client -o yaml’

export now=‘–force --grace-period=0’

removing spaces -
alias kn=‘kubectl config set-context --current --namespace’
alias kgd=‘k get deploy -o wide’
alias kgp=‘k get pods -o wide’
alias kgpw=‘k get pods -o wide --watch’
alias kgpa=‘k get pods -o wide -A’
alias kgn=‘k get nodes -o wide’
alias kgs=‘k get svc -o wide’
alias kge=‘k get events - sort-by=’.metadata.creationTimestamp’ | tail -8’
alias ked=‘k edit deploy’
alias kep=‘k edit pods’
alias ken=‘k edit nodes’
alias kes=‘k edit svc’
alias kc=‘k create’
alias kcd=‘k create deploy’
alias kd=‘k describe’
alias kdel=‘k delete’
alias ke=‘k edit’
alias kdd=‘k describe deploy’
alias kdp=‘k describe pods’
alias kdn=‘k describe nodes’
alias kds=‘k describe svc’
alias krf=‘k replace --force -f’
alias kcf=‘k create -f’
export nks=‘-n kube-system’
export do=‘–dry-run=client -o yaml’
export now=‘–force --grace-period=0’