Skip to content

Instantly share code, notes, and snippets.

@nordineb
Last active December 12, 2020 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nordineb/b96b78ef74bfb939854750a957721c18 to your computer and use it in GitHub Desktop.
Save nordineb/b96b78ef74bfb939854750a957721c18 to your computer and use it in GitHub Desktop.
Kubectl tips

Can be added to ~/.bashrc

alias k='kubectl'
alias kgp='kubectl get pods'
alias kgpa='kubectl get pods --all-namespaces'
alias kgs='kubectl get svc'
alias kgsa='kubectl get svc --all-namespace
export dry='--dry-run=client -o yaml'
k run nginx --image=nginx --restart=Never $dry

source <(kubectl completion zsh) echo 'alias k=kubectl' >>/.zshrc echo 'complete -F __start_kubectl k' >>/.zshrc

switch context between clusters (because kubectx is not available)

kubectl config get-contexts kubectl config set-context kind-kind

switch context between namespaces (because kubens is not available)

kubectl config view --minify | grep namespace kubectl config set-context --current --namespace=default

vim magic

can be added to ~/.vimrc

set number 
set ignorecase
set expandtab 
set ts=2 sw=2 sts=2
  • Fast save/exit: Instead of :wq, use ZZ

  • Return to bash without exiting wim: :w and CTL-Z to pause vim and job %1 to reenter vim

  • Delete to end of line: Position cursor and hit D

  • Delete entire line: DD

  • Force write a file that needs root permission: :w !sudo tee %

  • Indentation ** Put the cursor anywhere in the first line. ** Press V then jj to visually select the three lines. ** Press > to indent or < to unindent. ** Press . to repeat the indent, or u to undo if you have shifted too far.

  • Copy and paste lines ** :20 and V, select lines ** Copy using y eller Cut using d ** p Also delete lines using dd and undo using u

(or ) then typing your search pattern. Press Esc to cancel or press Enter to perform the search. Then press n to search forwards for the next occurrence, or N to search backwards. Type ggn to jump to the first match, or GN to jump to the last.

  • search ** /something and ENTER or ESC ** n og N to continue search forwrd /backward

write on a file that needs root permission

echo "foo" | sudo tee -a file

Editing objects: kubectl edit For labels prefer k label deployment/myapp app=myapp --overwrite For images prefer k set image nginx --image=nginx:1.7.9

show all api resources

k api-resources

List all available resources

kubectl  api-versions```

# List available fields
```k explain pod.spec.securityContext --recursive```

# Get help for a particular field
```k explain pod.spec.securityContext.runAsNonRoot```

restart all pods in a namespace
```kubectl -n {NAMESPACE} rollout restart deploy```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment