If you’re working with Kubernetes, “kubectl” is your go-to command-line tool for interacting with your cluster. This kubectl cheat sheet provides a quick reference guide to help you master the most commonly used kubectl commands. Whether you’re a beginner or an experienced Kubernetes user, this cheat sheet will make managing your cluster more efficient and straightforward.
Table of Contents
What is kubectl?
Kubectl is the command-line interface (CLI) for Kubernetes, a powerful system for managing containerized applications across a cluster of machines. Kubectl enables you to run commands against Kubernetes clusters to deploy applications, inspect and manage cluster resources, and view logs.
Installing and Configuring kubectl
Before diving into the commands, ensure that kubectl is installed and configured correctly. You can install kubectl by following the official Kubernetes installation guide. Here are some common installation commands:
- Install kubectl on Ubuntu:
sudo apt-get update && sudo apt-get install -y kubectl
- Install kubectl on macOS:
brew install kubectl
- Configure kubectl to communicate with your Kubernetes cluster:
kubectl config set-cluster my-cluster --server=https://my-cluster-api-server
kubectl config set-context my-context --cluster=my-cluster --user=my-user
kubectl config use-context my-context
Kubectl Cheat Sheet
Common kubectl Commands
Get Commands
- List all nodes in the cluster:
kubectl get nodes
This command retrieves a list of all nodes in your Kubernetes cluster, providing their status, roles, and other details.
- List all pods in a namespace:
kubectl get pods -n <namespace>
Use this command to list all pods within a specific namespace. Replace <namespace>
with the name of your namespace.
- List all services:
kubectl get svc
This command lists all services running in your cluster, showing their types, cluster IPs, and more.
- Describe a resource in detail:
kubectl describe <resource_type> <resource_name>
Provides detailed information about a specific resource. Replace <resource_type>
and <resource_name>
with the type and name of the resource, respectively.
- List all namespaces:
kubectl get namespaces
Lists all namespaces within your cluster, giving an overview of the isolation boundaries in your environment.
Create and Apply Commands
- Create resources from a file:
kubectl create -f <file.yaml>
Creates Kubernetes resources defined in a YAML file. Replace <file.yaml>
with your YAML file’s path.
- Apply changes to resources from a file:
kubectl apply -f <file.yaml>
Updates resources in your cluster based on the configuration in a YAML file.
- Create a new namespace:
kubectl create namespace <namespace>
Creates a new namespace in your cluster. Replace <namespace>
with your desired namespace name.
- Create a secret:
kubectl create secret generic <secret_name> --from-literal=<key>=<value>
Creates a new secret with the specified key-value pairs.
Update and Edit Commands
- Edit a resource configuration directly:
kubectl edit <resource_type> <resource_name>
Opens the configuration of a resource in your default text editor for immediate editing.
- Patch a resource:
kubectl patch <resource_type> <resource_name> -p '{"spec":{"field":"value"}}'
Applies a partial update to a resource using a JSON patch.
Delete Commands
- Delete a resource:
kubectl delete <resource_type> <resource_name>
Deletes a specified resource from your cluster.
- Delete a pod:
kubectl delete pod <pod_name>
Deletes a specific pod. This command is useful for removing malfunctioning or unwanted pods.
- Delete a deployment:
kubectl delete deployment <deployment_name>
Deletes a deployment and all associated pods.
- Delete a namespace:
kubectl delete namespace <namespace>
Deletes an entire namespace and all resources within it.
Advanced kubectl Commands
Logs and Monitoring
- View logs for a pod:
kubectl logs <pod_name>
Retrieves the logs of a specific pod, helping you debug and monitor applications.
- Stream logs for a pod:
kubectl logs -f <pod_name>
Streams real-time logs from a pod. The -f
option keeps the connection open to stream logs.
- Tail logs for a pod:
kubectl logs --tail=<number_of_lines> <pod_name>
Retrieves the last few lines of logs from a pod. Replace <number_of_lines>
with the desired number of lines.
Exec and Port Forward
- Execute a command in a pod:
kubectl exec -it <pod_name> -- <command>
Runs a command inside a running pod. The -it
flags enable interactive mode and allocate a terminal.
- Port forward a port to your local machine:
kubectl port-forward <pod_name> <local_port>:<remote_port>
Forwards a port from a pod to your local machine, making it accessible as if it were running locally.
Deployment Management
- Kubectl restart a pod:
kubectl delete pod <pod_name>
Deletes a pod, causing the deployment or replication controller to create a new instance. This is a manual method to restart a pod.
- Restart a deployment:
kubectl rollout restart deployment <deployment_name>
Restarts all pods in a deployment. This is a more controlled way to restart pods managed by a deployment.
Configuration and Context Management
- Set the current context:
kubectl config use-context <context_name>
Switches to a specified context, allowing you to manage multiple clusters easily.
- List all contexts:
kubectl config get-contexts
Lists all available contexts in your kubeconfig file.
- Switch context:
kubectl config use-context <context_name>
Switches to the specified context, making it the current one.
- Set a default namespace:
kubectl config set-context --current --namespace=<namespace>
Sets a default namespace for the current context, reducing the need to specify it in each command.
File and Resource Management
- Copy a file from a pod to your local machine:
kubectl cp <pod_name>:<path_to_file> <local_path>
Copies a file from a pod to your local filesystem.
- Run a pod:
kubectl run <pod_name> --image=<image>
Creates and runs a new pod with the specified name and image.
- Debug a pod:
kubectl debug <pod_name>
Debugs an existing pod, providing tools and configurations to diagnose issues.
Useful Tips
- Autocompletion: Enable autocompletion for kubectl commands to save time:
source <(kubectl completion bash)
- Aliases: Create aliases for frequently used commands to simplify your workflow. For example:
alias k='kubectl'
alias kgp='kubectl get pods'
Kubectl Cheet Sheet in Tabular form
Sure, here’s the data in a tabular format for easy reference and design:
Category | Command | Description |
---|---|---|
Installation and Configuration | curl -LO "https://.../kubectl" | Download kubectl |
chmod +x ./kubectl | Make kubectl executable | |
sudo mv ./kubectl /usr/local/bin/kubectl | Move kubectl to a directory in PATH | |
kubectl config view | View current configuration | |
kubectl config use-context <context-name> | Set current context | |
kubectl config get-contexts | List all contexts | |
Cluster Information | kubectl cluster-info | Display cluster information |
kubectl get nodes | List all nodes | |
kubectl get namespaces | List all namespaces | |
Pod Management | kubectl get pods | List all pods |
kubectl get pods -n <namespace> | List pods in a namespace | |
kubectl describe pod <pod-name> | Describe a pod | |
kubectl logs <pod-name> | Get logs from a pod | |
kubectl logs <pod-name> -c <container-name> | Get logs from a specific container in a pod | |
kubectl exec -it <pod-name> -- /bin/bash | Execute a command in a pod | |
kubectl cp <pod-name>:/path/to/file /local/path | Copy files from pod to local | |
Deployment Management | kubectl get deployments | List all deployments |
kubectl describe deployment <deployment-name> | Describe a deployment | |
kubectl scale deployment <deployment-name> --replicas=<number> | Scale a deployment to a specified number of replicas | |
kubectl rollout status deployment/<deployment-name> | Check the rollout status of a deployment | |
kubectl rollout restart deployment/<deployment-name> | Restart a deployment | |
Service Management | kubectl get svc | List all services |
kubectl describe svc <service-name> | Describe a service | |
kubectl expose pod <pod-name> --type=NodePort --name=<service-name> | Expose a pod as a service | |
Namespace Management | kubectl get namespaces | List all namespaces |
kubectl create namespace <namespace-name> | Create a new namespace | |
kubectl delete namespace <namespace-name> | Delete a namespace | |
kubectl config set-context --current --namespace=<namespace-name> | Set the default namespace | |
Secrets and ConfigMaps | kubectl create secret generic <secret-name> --from-file=<path-to-file> | Create a secret from a file |
kubectl get secrets | List all secrets | |
kubectl describe secret <secret-name> | Describe a secret | |
kubectl create configmap <configmap-name> --from-file=<path-to-file> | Create a ConfigMap from a file | |
kubectl get configmaps | List all ConfigMaps | |
kubectl describe configmap <configmap-name> | Describe a ConfigMap | |
Other Useful Commands | kubectl proxy | Start a proxy to the Kubernetes API server |
kubectl apply -f <file.yaml> | Apply a configuration from a file | |
kubectl delete -f <file.yaml> | Delete resources defined in a file | |
source <(kubectl completion bash) | Enable bash completion for kubectl |
Conclusion
Mastering kubectl commands can significantly enhance your productivity and efficiency in managing Kubernetes clusters. Keep this kubectl cheat sheet handy for quick reference and you’ll be navigating your Kubernetes environment like a pro in no time. For more in-depth information, always refer to the official Kubernetes documentation.
This kubectl cheat sheet is designed to help you quickly reference and master essential Kubernetes commands. Whether you’re deploying applications, managing resources, or troubleshooting issues, these commands will streamline your workflow and enhance your productivity. Keep this guide handy as you navigate your Kubernetes environment, and you’ll be managing your cluster like a pro in no time. For more detailed information, always refer to the official Kubernetes documentation. Happy coding !
If you are preparing for Java interviews then checkout here