Kubectl — Kubernetes CLI
Installation
Install the kubectl binary to the user home-directory
version=$(curl -L -s https://dl.k8s.io/release/stable.txt)
binary_url=https://dl.k8s.io/release/$version/bin/linux/amd64/kubectl
curl -L $binary_url -o ~/bin/kubectl && chmod +x ~/bin/kubectl
# example for loading tab-completion
source <(kubectl completion bash)Find a script kubectl-install1 for Linux on Github
Plugin Manager
Install Krew plugin manager2 manual …or use krew-install3
wget https://github.com/kubernetes-sigs/krew/releases/download/v0.4.5/krew-linux_amd64.tar.gz
tar -xvf krew-linux_amd64.tar.gz
cp krew-linux_amd64 ~/bin/krew
krew install krew
# add following to your shell environment
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"Useful plugins4…
Configuration
Custom configuration file:
export KUBECONFIG=$(realpath kubeconfig.yml)
# check your current context
kubectl config view
# check resource access
kubectl auth can-i get podskubeconfig file loading order…
- --kubeconfigcommand option
- $KUBECONFIGenvironment variable
- ~/.kube/configin the user home-directory
Configuration may include clusters, users & contexts8
# list available contexts
kubectl config get-contexts
# set current context in kubeconfig
kubectl config get-contexts $nameCommands
kubectl run
# start an interactive container…
kubectl run $pod_name --image=$image --restart=Never --stdin --tty
kubectl delete pods $pod_name
# specific shell
kubectl run $pod_name --image=$image --restart=Never -it --rm -- /bin/bash
# specific application (Perl in this example)
kubectl run $pod_name --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'kubectl run9 common options:
- --image=— which container image to use
- -itaka- --stdin&- --tty— spawn an interactive shell- …allocate a terminal for each container
- …keep stdinopen (even if empty)
 
- Clean up…
- --restart=Never— pod should never be restarted
- --rm— automatically remove pod after exit
 
kubectl exec
# execute a command in a specified pod
kubectl exec $pod_name -- curl -s https://10.20.30.40
# specify a container
kubectl exec $pod_name -c $container_name #...
# start an interactive shell
kubectl exec -it my-pod -- /bin/bashUse cases…
- …debugging & diagnostics …for example check logs, inspect files
- …modify configuration files & environment variables
- …run test scripts …validate application behavior
# copy file to & from containers
kubectl cp /local/path  $pod_name:/path/in/container
kubectl cp $pod_name:/path/in/container /local/pathkubectl logs
# show logs entires from the last rotation
kubectl logs $pod_name
# show logs for a specific container
kubectl logs $pod_name -c $container_nameContainerized application log to stdout/stderr…
- …automatically rotated daily …every time the logs reaches 10MB
- …logs deleted when the pod is deleted
kubectl apply
Imperative commands — Fail if a resource exists already
# run an instance of the nginx container by creating a deployment object
kubectl create deployment nginx --image nginx
# create the objects defined in a configuration file
kubectl create -f deployment.yaml
# update the objects defined in a configuration file
# note: dropping all changes to the object missing from the configuration file
kubectl replace -f deployment.yamlDeclarative commands — Create new or modify existing objects
- …create, update, and delete operations are automatically detected per-object
- …retains changes, even if the changes are not in the object configuration file
# apply changes
kubectl apply -f nginx/
# see what changes are going to be made
kubectl diff -f nginx/
# use option -R for recursive directory decentEcosystem
k9s TUI for Kuberntes10
- Configuration…
- k9s infoto check config paths
- …for terminal colors with custom skin11
 
- Keyboard short cuts…
- ctrl-q or :q(Vi style) to exit
- ctrl-d delete a resource
- ctrl-k kill a resource (no confirmation!)
 
- ctrl-q or 
Footnotes
- kubectlInstall Script, GitHub
 https://github.com/vpenso/kubernetes-playground/blob/master/bin/kubectl-install↩︎
- Krew Project 
 https://krew.sigs.k8s.io
 https://github.com/kubernetes-sigs/krew↩︎
- krew-installScript, Github
 https://github.com/vpenso/kubernetes-playground/blob/master/bin/krew-install↩︎
- List of - kubectlplugins, Krew Documentation
 https://krew.sigs.k8s.io/plugins↩︎
- kubectx+- kubens: Power tools for kubectl, GitHub
 https://github.com/ahmetb/kubectx↩︎
- kubectl treePlugin, GitHub
 https://github.com/ahmetb/kubectl-tree↩︎
- Stern - kubectlPlugin, Github
 https://github.com/stern/stern↩︎
- Configure Access to Multiple Clusters, Kubernetes Documentation 
 https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/↩︎
- kubectl run, Documentation
 https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#run↩︎
- K9s Kubernetes CLI 
 https://k9scli.io
 https://github.com/derailed/k9s
 https://k9scli.io/topics/commands↩︎
- K9s Skins, GitHub 
 https://thelinuxnotes.com/k9s-skins-preview
 https://github.com/derailed/k9s/tree/master/skins↩︎