05. March 2022
K8S Cheatsheet
Little cheatsheet about k8s and useful aliases.
Cheatsheet
Here are some useful commands and aliases to manipulate k8s more easily
The basics
The basics aliases
alias k=kubectl
Switch contexts (mainly clusters)
alias kprod='kubectl config use-context production_cluster_name'
alias knprod='kubectl config use-context non_production_cluster_name'
alias kcontext='kubectl config use-context'
USAGE : ‘kcontext non_production_cluster_name’ # switch to non production cluster
Switch namespaces (dev / lqt / indus / …)
alias kns='kubectl config set-context --current --namespace'
USAGE :
kns my_namespace
# switch to your namespace
Useful commands
k <some_commands> -n atidgbl1
-n
is used to specify a namespace on which run the command.
List, describe resources
k get <resource_type>
USAGE : get is useful with
| grep <service_name>
example :
k get pod | grep grafana
k describe <resource_type> <resource_name>
Get logs
k logs <pod_name>
k logs -f <pod_name>
Follow the pod’s logs
k logs --since 15m -f <pod_name>
Follow the pod’s logs and display the last 15 minuts
Can be used with a lot of other options cf :
k logs --help
Manipulate resources
k rollout restart <resource_type> <resource_name>
It will restart each pod (one by one) under the given resource_name (must be a deployment, statefulset or daemonset).
k delete <resource_type> <resource_name>
It will delete the resource identified, only the pod will be recreated if managed by a Deployment, a StatefulSet or a DaemonSet.
k edit <resource_type> <resource_name>
It will edit the resource identified, the resource can be recreated if necessary
k scale <resource_type> <resource_name> --replicas=X
It will scale up / down the number of pods managed by the resources. <resource_type> can be one of the following : Deployment, ReplicaSet, StatefulSet (resources that manage pods)
Troubleshooting commands
k get
k describe
k logs
In addition of these 3 usefull commands you can interact directly with the pod itself.
k top pod <pod_name>
It will display the current use of resources of a pod (CPU & RAM)
k exec -it <pod_name> -- bash
Open a bash in the pod (thanks to the
-it
for interactive mode), if bash is installed on the pod (not in most ULTIM microservices)
k exec <pod_name> -- ls
Used to run a single command in a terminal.
Make your own aliases
create a file in your personal folder (aka ~ folder) and source it in your bashrc
vim ~/.aliases
put your aliases in it like
alias k='kubectl'
then
vim ~/.bashrc
source your alias file on top of this file like
source ~/.aliases
open a new bash (to reload the bashrc file again) and enjoy your aliases.