English

Kubernetes useful commands

I condensed below a cheat sheet of Kubernetes useful commands. I will keep updating this list as I come across other operations.

List all Kubernetes pods

kubectl get pods

List the pods in a different namespace (other than default)

kubectl get pods -n other_namespace

Describes a pod its settings including environment variables

kubectl describe pod my-pod

Connects to a pod’s command line

kubectl exec -it my-pod sh

Port forwards the port of a pod to localhost

kubectl port-forward my-pod 1234:1234

Tail to the logs of a pod

kubectl logs -f my-pod

Restarts a pod by removing it so the pod is recreated

kubectl delete pod my-pod

Forces the pod restart by removing it

kubectl delete pod my-pod --grace-period=0 --force

Delete all pods with status Completed

kubectl delete pod --field-selector=status.phase==Succeeded

Apply pod deployment changes

kubectl apply -f path/to/deployment.yml

Display the current context

kubectl config current-context

Change the context to a different context

kubectl config use-context other_context

Change the default namespace for subsequent commands:

kubectl config set-context --current --namespace=my-namespace

Shows all Kubernetes nodes

kubectl get node --show-labels

Gets information about a service

kubectl get service myservice -o yaml

Gets secrets such as username and password for a service

kubectl get secrets myservice -o yaml

Run a job right now from a scheduled cronjob

kubectl create job --from=cronjob/my-cron my-cron-quick-run

MacOS: add Kubernetes runnables to OS PATH

chmod +x ./kubectl<br>sudo mv ./kubectl /usr/local/bin/kubectl
bgasparotto

Recent Posts

Python function decorator

This guide will show you how to create a Python function decorator with a few…

2 years ago

Got permission denied while trying to connect to the Docker daemon socket

This guide will show you how to fix the error Got permission denied while trying…

2 years ago

Python virtual environment on Intellij IDEA

This guide will show you how to create a Python virtual environment on Intellij IDEA…

2 years ago

Find and kill processes on Linux and Mac by port number

This tutorial will quickly show you how to to find and kill processes on Linux,…

2 years ago

Python: Relocation R_X86_64_PC32 against symbol can not be used when making a shared object Error

This guide shows a possible solution for Python error Relocation R_X86_64_PC32 against symbol can not…

2 years ago

Create aliases for commands on Unix

This tutorial will show how to create aliases for commands on Unix based systems, so…

4 years ago