Skip to main content

Kubernetes Most Used Commands

· 5 min read
Guille Vigil

Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It is widely used by many organizations to manage their containerized applications and services. Kubernetes provides a rich set of commands that allow developers and operators to interact with the Kubernetes cluster and manage their applications and services. In this blog post, we will explore some of the most used Kubernetes commands that can help you streamline your Kubernetes deployments and efficiently manage your applications.

Esential Commands for Developers

Get Information

Kubernetes provides a set of commands to get information about the resources running in the cluster. You can use these commands to get information about pods, services, deployments, and other resources.

kubectl version # Get the Kubernetes version
kubectl cluster-info # Display cluster info
kubectl config view --minify --output 'jsonpath={..user}' # Get the current user
tip

Try with context and cluster to get more information about the current context and cluster.

Gather Resources

Kubernetes allows you to gather resources from the cluster. You can use these commands to get resources such as pods, services, deployments, and other resources.

kubectl get pods # List all pods
kubectl get services # List all services
kubectl get deployments # List all deployments
kubectl get nodes # List all nodes
kubectl get namespaces # List all namespaces
tip

Try with other resources such as configmaps, secrets, ingresses, replicasets, statefulsets, daemonsets, persistentvolumeclaims, hpa, jobs, crd, etc.

tip

Use kubectl get all to get all resources in the cluster.

Use Flags to Filter

Kubernetes allows you to use flags to filter the resources. You can use these flags to filter the resources based on labels, fields, and other criteria.

kubectl get pods -w -o wide -A --show-labels # Watch all pods with wide output and labels
tip

Use those flags wisely!

-w to watch for changes, -o wide to get more information, -A to get all resources, --show-labels to show labels.

Debugging

Kubernetes provides a set of commands to debug the resources running in the cluster. You can use these commands to debug pods, services, deployments, and other resources.

kubectl describe pod <pod-name> # Describe a pod
kubectl logs <pod-name> # Get logs from a pod
tip

If you would like to follow the logs, use -f flag.

Imperative Commands

Kubernetes allows you to run imperative commands to manage resources. You can use these commands to create, delete, and update resources.

kubectl create deployment <name> --image=<image> # Create a deployment
kubectl expose deployment <name> --port=<port> --target-port=<target-port> --type=NodePort # Expose a deployment
kubectl scale deployment <name> --replicas=<replicas> # Scale a deployment
kubectl create secret generic <name> --from-literal=<key>=<value
tip

Use --dry-run=client and -o yaml flags to generate the resource manifest.

Troubleshooting

Kubernetes provides a set of commands to troubleshoot the resources running in the cluster. You can use these commands to troubleshoot pods, services, deployments, and other resources.

kubectl run <name> --image=<image> --restart=Never --rm -it -- sh # Run a pod for troubleshooting
kubectl exec -it <pod-name> -- sh # Execute a command in a pod
tip

You are now inside the pod, use the shell to run commands and troubleshoot.

Scale & Update

Kubernetes allows you to scale and update resources. You can use these commands to scale and update deployments, replicasets, and other resources.

kubectl scale deployment <name> --replicas=<replicas> # Scale a deployment
kubectl set image deployment/<name> <container-name>=<new-image> # Update a deployment
kubectl rollout history deployment/<name> # Get rollout history
kubectl rollout undo deployment/<name> # Undo a rollout
kubectl rollout status deployment/<name> # Get rollout status
kubectl rollout restart deployment/<name> # Restart a rollout

Modify Resources

Kubernetes allows you to modify resources. You can use these commands to modify resources such as pods, services, deployments, and other resources.

kubectl edit pod <pod-name> # Edit a pod
kubectl apply -f <file> # Apply a configuration file
kubectl replace -f <file> # Replace a configuration file
kubectl delete -f <file> # Delete a configuration file
kubectl patch <resource> <name> -p <patch> # Patch a resource

Monitoring

Kubernetes provides a set of commands to monitor the resources running in the cluster. You can use these commands to monitor pods, services, deployments, and other resources.

kubectl top nodes # Show the top nodes
kubectl top pods # Show the top pods
warning

You need to have the metrics-server installed in your cluster to use these commands. Follow the official documentation to install the metrics-server.

Connect to Apps

Kubernetes allows you to connect to applications running in the cluster. You can use these commands to connect to pods, services, deployments, and other resources.

kubectl port-forward <pod-name> <local-port>:<remote-port> # Forward a port
kubectl port-forward service/<service-name> <local-port>:<remote-port> # Forward a port for a service
tip

Open in your browser http://localhost:<local-port> to play with your application.

Other Commands

Useful commands to manage your Kubernetes cluster and resources.

kubectl get secret admin-secret -o jsonpath="{.data.password}" | base64 --decode # Get a secret
kubectl api-resources # List all resources
kubectl explain <resource> # Explain a resource

Tools

A great list of tools to manage your Kubernetes cluster and resources.

kubens # Switch between namespaces
kubectx # Switch between contexts
k9s # Terminal-based UI to interact with your Kubernetes cluster
stern # Multi pod and container log tailing