Basic kubectl commands

The following commands should help you get started with Kubernetes and kubectl.

Get information about your cluster

List the resources available in your cluster. More details

kubectl get nodes
kubectl get deployments
kubectl get pods
kubectl get all

Run a container

It’s like docker run with a little bit different syntax. More details

kubectl run <name> --image=<image> --port=8080

Get logs from a container

This will output the logs from the container into your terminal. More details

kubectl logs <pod_name>

Execute command in container

This executes the env command in the container and displays the result. More details

kubectl exec <pod_name> env

Execute interactive shell in the container

This will execute bash in the container and you’ll be able to type commands interactively from your terminal. More details

kubectl exec -it <pod_name> bash

Run Kubernetes API proxy

This will run a local HTTP service, which will proxy request to the cluster. Using this service you can access the Kubernetes API, so you can control the cluster using HTTP requests. More details

kubectl proxy

Apply changes from a file

Describe what needs to be done in a .yaml file and let Kubernetes do the rest. More details

kubectl apply -f <file.yaml>

Delete everything from your cluster

If you no longer need the resources in your cluster you can drop them with this command. More details

kubectl delete all --all

Forward a local port to a container in the cluster

Either you expose the container’s port and then you can access it from your local machine or you can use this command to temporarily forward traffic from your local machine to the container. More details

kubectl port-forward <pod_name> <local_port>:<container_port>

Show version of kubectl

kubectl version --short

Config file location

~/.kube/config # Linux/MacOS
%userprofile%\.kube\config # Windows
Dávid Molnár's Picture

About Dávid Molnár

Dávid is a skilled Full-Stack Developer.

Munich, Germany https://mdavid626.com

Comments