Skip to content

Kubernetes useful commands

Kubernetes logo

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

Leave a Reply

Your email address will not be published. Required fields are marked *