Kubectl Commands
Kubectl Cheat-Sheet for Kubernetes Management
Kubectl is the command-line interface for interacting with Kubernetes clusters. This cheat sheet provides essential commands for managing your Kubernetes environment efficiently. It covers various aspects, from configuring your connection to managing complex deployments.
Kubectl Contexts and Configuration
Manage your Kubernetes cluster connections and configurations effectively.
Command | Description |
---|---|
kubectl config view |
Show the current kubeconfig file content. |
kubectl config get-contexts |
List all available contexts defined in your kubeconfig file. |
kubectl config current-context |
Display the name of the currently active context. |
kubectl config use-context <context-name> |
Switch the current context to a different one. |
kubectl config set-context <context-name> --namespace=<namespace> |
Set the default namespace for a specific context. |
kubectl config set-context <context-name> --cluster=<cluster-name> |
Associate a specific cluster with a context. |
kubectl config set-context <context-name> --user=<user-name> |
Assign a user credential to a context. |
kubectl config set-context <context-name> --namespace=<namespace> --cluster=<cluster-name> --user=<user-name> |
Configure all properties for a context at once. |
Kubernetes Cluster Management
Gain insights into your Kubernetes cluster's health and resources.
Command | Description |
---|---|
kubectl cluster-info |
Display connection information for the Kubernetes master and services. |
kubectl get nodes |
List all nodes (worker machines) in the cluster. |
kubectl get pods |
List all pods running across all namespaces in the cluster. |
kubectl get services |
List all services exposed within the cluster. |
kubectl get deployments |
List all deployment resources managing application replicas. |
kubectl get namespaces |
List all namespaces, which provide a mechanism for isolating groups of resources. |
kubectl get events |
List cluster events, useful for troubleshooting. |
Kubectl Resource Management
Apply, manage, and inspect Kubernetes resources using declarative configuration files.
Command | Description |
---|---|
kubectl apply -f <file.yaml> |
Apply a configuration change from a YAML or JSON file. |
kubectl delete -f <file.yaml> |
Delete resources defined in a configuration file. |
kubectl get <resource-type> |
List all resources of a specified type (e.g., pods, services, deployments). |
kubectl describe <resource-type> <resource-name> |
Show detailed information about a specific resource. |
kubectl edit <resource-type> <resource-name> |
Edit the configuration of a live resource in your default editor. |
kubectl exec -it <pod-name> -- <command> |
Execute a command inside a running pod. |
Kubectl Pod Management
Commands for creating, managing, and inspecting individual pods.
Command | Description |
---|---|
kubectl run <pod-name> --image=<image-name> |
Create a new pod with a specified container image. |
kubectl delete pod <pod-name> |
Delete a specific pod. |
kubectl get pod <pod-name> |
Retrieve detailed information about a specific pod. |
kubectl describe pod <pod-name> |
Get a comprehensive description of a pod's state and events. |
kubectl logs <pod-name> |
Fetch the logs from a pod's container. |
kubectl exec -it <pod-name> -- /bin/bash |
Open an interactive bash shell inside a pod. |
kubectl cp <pod-name>:<source-path> <destination-path> |
Copy files between a pod and your local filesystem. |
kubectl top node |
Display resource usage (CPU/Memory) for all nodes. |
kubectl top pod |
Display resource usage for all pods in the current namespace. |
kubectl top pod <pod-name> |
Display resource usage for a specific pod. |
Kubectl Service Management
Commands for exposing and managing network services within Kubernetes.
Command | Description |
---|---|
kubectl expose pod <pod-name> --port=<service-port> --target-port=<container-port> |
Create a new service to expose a pod. |
kubectl delete service <service-name> |
Delete a specific service. |
kubectl get service <service-name> |
Retrieve details about a specific service. |
kubectl describe service <service-name> |
Get a detailed description of a service's configuration and endpoints. |
Kubectl Deployment Management
Manage the lifecycle and scaling of your application deployments.
Command | Description |
---|---|
kubectl create deployment <deployment-name> --image=<image-name> |
Create a new deployment with a specified container image. |
kubectl delete deployment <deployment-name> |
Delete a specific deployment and its associated pods. |
kubectl get deployment <deployment-name> |
Retrieve details about a specific deployment. |
kubectl describe deployment <deployment-name> |
Get a detailed description of a deployment's status and configuration. |
kubectl scale deployment <deployment-name> --replicas=<number> |
Adjust the number of replicas for a deployment. |
kubectl rollout status deployment/<deployment-name> |
Monitor the progress of a deployment rollout. |
kubectl rollout history deployment/<deployment-name> |
View the revision history of a deployment. |
kubectl rollout undo deployment/<deployment-name> |
Roll back a deployment to its previous revision. |
kubectl rollout undo deployment/<deployment-name> --to-revision=<revision-number> |
Roll back a deployment to a specific revision number. |
Kubectl Namespace Management
Organize and isolate resources within your Kubernetes cluster using namespaces.
Command | Description |
---|---|
kubectl create namespace <namespace-name> |
Create a new namespace. |
kubectl delete namespace <namespace-name> |
Delete a specific namespace and all its resources. |
kubectl get namespace <namespace-name> |
Retrieve details about a specific namespace. |
kubectl describe namespace <namespace-name> |
Get a detailed description of a namespace's status and resource quotas. |