This is what asdf d I want to copy
- Kubernets Cheatsheet
- Kubernets Commands (Kubectl commands Reference)
- Kubernets reference resources
In Bash, we can do autocompletion of kubectl commands
For faster command Execution s example:
Kubectl exec -it ngin
Hit tab you will automatically promt
Kubectl exec -it nginx-f89759699-8rzf8
we need to add lines in the ~/.bashrc
source <(kubectl completion bash)
echo "source <(kubectl completion bash)"
alias k=kubectl
complete -F __start_kubectl k
After Adding just reload the profile by
source ~/.bashrc
kubectx is a utility to manage and switch between kubectl contexts.
USAGE:
kubectx : list the contexts
kubectx <NAME> : switch to context <NAME>
kubectx - : switch to the previous context
kubectx -c, --current : show the current context name
kubectx <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
kubectx <NEW_NAME>=. : rename current-context to <NEW_NAME>
kubectx -d <NAME> : delete context <NAME> ('.' for current-context)
(this command won't delete the user/cluster entry
that is used by the context)
kubectx -u, --unset : unset the current context
Usage
$ kubectx
oregon
minikube
$ kubectx oregon
Switched to context "oregon".
kubenx is a utility to manage and switch between kubectl namespaces.
USAGE:
kubens : list the namespaces
kubens <NAME> : change the active namespace
kubens - : switch to the previous namespace
kubens -c, --current : show the current namespace
usage
$ kubens kube-system
Context "test" set.
Active namespace is "kube-system".
$ kubens -
Context "test" set.
Active namespace is "default".
kubens also supports Tab completion on bash/zsh/fish shells.
sudo git clone https://github.com/ahmetb/kubectx /opt/kubectx
sudo cp /opt/kubectx/kubectx /usr/local/bin/kubectx
sudo cp /opt/kubectx/kubens /usr/local/bin/kubens
Its Very difficult to know Which Cluster you are in the Kubernets.
There is a script By which, you can able to promt in Bash in which cluster You are in , and which namespace you are in Namespace.
Download the script and paste in the location.
curl -o https://github.com/jonmosco/kube-ps1/blob/master/kube-ps1.sh ~/kube-ps1.sh
Copy below Commands in the ~/.bashrc
source ~/kube-ps1.sh>>~/.bashrc
PS1='[\u@\h \W $(kube_ps1)]\$ '>>~/.bashrc
After Adding just reload the profile by
source ~/.bashrc
For Autocompletion of kubens and kubectl
git clone https://github.com/ahmetb/kubectx.git ~/.kubectx
COMPDIR=$(pkg-config --variable=completionsdir bash-completion)
ln -sf ~/.kubectx/completion/kubens.bash $COMPDIR/kubens
ln -sf ~/.kubectx/completion/kubectx.bash $COMPDIR/kubectx
cat << EOF >> ~/.bashrc
#kubectx and kubens
export PATH=~/.kubectx:\$PATH
EOF
Your will be promted with Bash Command promt like below
reference:
Some times you may need to execute Command, Inside the container to check services, telnet, nslookup
kubectl run multitool --image=praqma/network-multitool -n default
Add below command in the ~/.bashrc
alias kip="kubectl -n default exec -it multitool --">>~/.bashrc
After Adding just reload the profile by
source ~/.bashrc
Then you can execute following Commands inside the pod. By the Below commands.
kip nslookup kubernetes
Kubernetes namespace is in struck
namespace=<deleting_namespace>
kubectl get namespace ${namespace} -o json \
| tr -d "\n" | sed "s/\"finalizers\": \[[^]]\+\]/\"finalizers\": []/" \
| kubectl replace --raw /api/v1/namespaces/stucked-namespace/finalize -f -
kubectl delete namespace ${namespace} --grace-period=15
or
kubectl get namespace ${namespace} -o json > ${namespace}.json
kubectl replace --raw "/api/v1/namespaces/${namespace}/finalize" -f ./${namespace}.json
K8s ingress not deleting
Get the ing
ingress=$(kubectl get ingress -o custom-columns=":metadata.name")
kubectl patch ingress ${ingress} -p '{"metadata":{"finalizers":[]}}' --type=merge
Get the pods name running in the Node with the labels
Lable="intent=apps"
# get the pods Running
kubectl get nodes -l $Lable | tail -n +2 |awk '{print $1}' | xargs -I {} kubectl describe node {} | awk '/Non-terminated Pods:/, /Allocated resources:/'
# Get the node Events
kubectl get nodes -l $Lable | tail -n +2 | awk '{print $1}' | xargs -I {} kubectl describe node {} | awk '/Events:/, /Name:/'
Delete the jobs with pod failing
errorMessage=image
kubectl delete jobs $(kubectl get po | grep -i $errorMessage | awk {'print $1'} | rev| cut -d'-' -f2- | rev)
limit is for Enforcing, Reqiests is for Allocating resourcse Cgroups(Allocate the Resources), Namespaces(Segregate the resources)
if CPU more --> throtled wait for some time to process requests if Memory more --> OOM killed pods will restart
Get the total CPU and Memory of pods, Helpfull during the HPA
awk '{sum += $3} END {print sum}' <(kubectl top po | tail -n +2 )'
HPA add limits to this
( total resource / deseried pods ) * ( 100 / Desired HPA metrics )