Ultimate Kubectl Cheat Sheet: Master Kubernetes Commands

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.

Kubectl Cheat Sheet

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:

  1. Install kubectl on Ubuntu:
   sudo apt-get update && sudo apt-get install -y kubectl
  1. Install kubectl on macOS:
   brew install kubectl
  1. 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

  1. 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.

  1. 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.

  1. List all services:
   kubectl get svc

This command lists all services running in your cluster, showing their types, cluster IPs, and more.

  1. 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.

  1. 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

  1. 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.

  1. 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.

  1. Create a new namespace:
   kubectl create namespace <namespace>

Creates a new namespace in your cluster. Replace <namespace> with your desired namespace name.

  1. 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

  1. 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.

  1. 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

  1. Delete a resource:
   kubectl delete <resource_type> <resource_name>

Deletes a specified resource from your cluster.

  1. Delete a pod:
   kubectl delete pod <pod_name>

Deletes a specific pod. This command is useful for removing malfunctioning or unwanted pods.

  1. Delete a deployment:
   kubectl delete deployment <deployment_name>

Deletes a deployment and all associated pods.

  1. Delete a namespace:
   kubectl delete namespace <namespace>

Deletes an entire namespace and all resources within it.

Advanced kubectl Commands

Logs and Monitoring

  1. View logs for a pod:
   kubectl logs <pod_name>

Retrieves the logs of a specific pod, helping you debug and monitor applications.

  1. 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.

  1. 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

  1. 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.

  1. 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

  1. 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.

  1. 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

  1. Set the current context:
   kubectl config use-context <context_name>

Switches to a specified context, allowing you to manage multiple clusters easily.

  1. List all contexts:
   kubectl config get-contexts

Lists all available contexts in your kubeconfig file.

  1. Switch context:
   kubectl config use-context <context_name>

Switches to the specified context, making it the current one.

  1. 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

  1. 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.

  1. Run a pod:
   kubectl run <pod_name> --image=<image>

Creates and runs a new pod with the specified name and image.

  1. 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:

CategoryCommandDescription
Installation and Configurationcurl -LO "https://.../kubectl"Download kubectl
chmod +x ./kubectlMake kubectl executable
sudo mv ./kubectl /usr/local/bin/kubectlMove kubectl to a directory in PATH
kubectl config viewView current configuration
kubectl config use-context <context-name>Set current context
kubectl config get-contextsList all contexts
Cluster Informationkubectl cluster-infoDisplay cluster information
kubectl get nodesList all nodes
kubectl get namespacesList all namespaces
Pod Managementkubectl get podsList 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/bashExecute a command in a pod
kubectl cp <pod-name>:/path/to/file /local/pathCopy files from pod to local
Deployment Managementkubectl get deploymentsList 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 Managementkubectl get svcList 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 Managementkubectl get namespacesList 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 ConfigMapskubectl create secret generic <secret-name> --from-file=<path-to-file>Create a secret from a file
kubectl get secretsList 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 configmapsList all ConfigMaps
kubectl describe configmap <configmap-name>Describe a ConfigMap
Other Useful Commandskubectl proxyStart 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
kubectl cheat sheet

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


Share this article with tech community
WhatsApp Group Join Now
Telegram Group Join Now

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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