Skip to content

Latest commit

 

History

History
269 lines (177 loc) · 4.02 KB

KUBECTL-CHEAT-SHEET.md

File metadata and controls

269 lines (177 loc) · 4.02 KB

Kubectl cheat sheet

Everybody can need a little help...

Auto complete

Bash

Add autocomplete permanently to your bash shell

echo "source <(kubectl completion bash)" >> ~/.bashrc

Zsh

Add autocomplete permanently to your zsh shell

echo "[[ $commands[kubectl] ]] && source <(kubectl completion zsh)" >> ~/.zshrc

Shortcut

Add this to your bashrc to have a shortcut to kubectl on k

alias k=kubectl
complete -F __start_kubectl k

Change kubectl config

export KUBECONFIG=~/ops/my_cluster/kubeconfig

Commands

Basics

List resources in default namespace

k get all

List resources in all namespaces

k get all --all-namespaces

List resources in a specific namespace

k get all -n kube-system

List specific resource

k get [RESOURCE] # Example: k get pods

List a particular resource

k get [RESOURCE] [NAME] # Example: k get pods backend
k get [RESOURCE]/[NAME] # Example: k get pods/backend

Every time, you can choose between using [RESOURCE] [NAME] of [RESOURCE]/[NAME]

[RESOURCE] [NAME] is specially usefull to provide auto completion on specific resource (eg: type k get pods and press tab to see available pods)

[RESOURCE]/[NAME] is specially usefull after listing because outputs will already be of form [RESOURCE]/[NAME] (for example, after k get all you would like to read logs of your crashed pod, you would type k logs and paste the pod name)

Describe a particular resource (provide a lot of useful infos)

k describe [RESOURCE] [NAME] # Example: k describe nodes worker-1

Get logs of a pod

k logs my-pod

Get logs of specific container in pod (multi container case)

k logs my-pod -c my-container

Execute command in pod

k exec my-pod -- ls -la

Execute command in specific container in pod (multi container case)

k exec my-pod -c my-container -- ls -la

Open interactive shell in pod

k exec my-pod -it -- sh

Get events

k get events

Proxy

k proxy

Change default namespace used (default=default)'

k config set-context --current --namespace=monitoring

Nodes

Check nodes IP

k get nodes -o wide

Labels

Label resource

k label pods my-pod app=backend

Unlabel resource

k label pods my-pod app-

Filter with labels

k get pods -l app=backend

Filter resources which HAVE the label

k get pods -l app

Port forwarding

Access service locally

k port-forward service/traefik-dashboard-service :8080

Access pod locally

k port-forward pods/redis-master-765d459796-258hz :6379

Yaml files

Deploy kubernetes object stored in yaml file

k apply -f filename

Deploy all kubernetes objects stored in directory

k apply -f directory

Redeploy a changed kubernetes object that has been already deployed

k apply -f filename

Delete a kubernetes object

k delete -f filename

Compare cluster state with file state

k diff -f filename

Output formating

Additional informations

k get nodes -o wide

Format output as yaml

k get service kubernetes -o yaml

Format output as json

k get service kubernetes -o json

Format output

# Here, we only retrieve node's internal ip
k get nodes -o jsonpath='{ $.items[*].status.addresses[?(@.type=="InternalIP")].address }'

Metrics

If you have metrics-server installed on your cluster (example in 02_monitoring/), you can retrieve pods / nodes metrics

All nodes metrics

k top nodes

Specific node metrics

k top nodes my-node

All pods metrics

k top pods --containers

Specific pod metrics

k top pods my-pod --containers

Contributors

GitHub Logo   LeChatErrant - creator and maintainer