Mastering Kubernetes: A Comprehensive Cheat Sheet for Cluster Management

Essential Commands and Tips for Navigating, Debugging, and Optimizing Your Kubernetes Workflow

Mastering Kubernetes: A Comprehensive Cheat Sheet for Cluster Management

Cluster Information

kubectl cluster-info

Displays detailed information about the Kubernetes cluster.

kubectl get nodes

Lists all the nodes available in the cluster.

kubectl describe node <node-name>

Provides detailed information about a specific node.

kubectl top node

Shows the resource usage (CPU/Memory) of nodes.

kubectl top pod

Displays the resource usage (CPU/Memory) of pods.

kubectl version

Shows the versions of the client and server.

Namespaces

kubectl get namespaces

Lists all namespaces in the cluster.

kubectl create namespace <name>

Creates a new namespace with the specified name.

kubectl delete namespace <name>

Deletes the specified namespace.

kubectl config set-context --current --namespace=<namespace>

Sets the default namespace for the current context.

Pods

kubectl get pods

Lists all pods in the current namespace.

kubectl get pods -A

Lists all pods across all namespaces.

kubectl describe pod <pod-name>

Provides detailed information about a specific pod.

kubectl logs <pod-name>

Displays logs from a specific pod.

kubectl logs <pod-name> -c <container-name>

Shows logs from a specific container within a pod.

kubectl exec -it <pod-name> -- sh

Executes a shell command inside a running pod interactively.

kubectl port-forward <pod-name> <local-port>:<pod-port>

Forwards a local port to a port on a pod for local access.

kubectl delete pod <pod-name>

Deletes the specified pod.

kubectl cp <file-path> <pod-name>:/<path>

Copies files from local to a specified path in the pod.

Deployments

kubectl get deployments

Lists all deployments in the current namespace.

kubectl describe deployment <name>

Provides detailed information about a specific deployment.

kubectl apply -f <file.yaml>

Applies configuration changes from a specified YAML file.

kubectl scale deployment <name> --replicas=<n>

Scales the deployment to the specified number of replicas.

kubectl rollout restart deployment <name>

Restarts the specified deployment.

kubectl delete deployment <name>

Deletes the specified deployment.

kubectl rollout undo deployment <name>

Reverts the last deployment rollout.

kubectl set image deployment/<name> <container-name>=<new-image>

Updates the container image for a specific deployment.

kubectl expose deployment <name> --port=<port>

Exposes a deployment as a service on the specified port.

Services

kubectl get services

Lists all services in the current namespace.

kubectl describe service <name>

Provides detailed information about a specific service.

kubectl expose pod <name> --type=NodePort --port=<port>

Exposes a pod as a NodePort service on the specified port.

kubectl delete service <name>

Deletes the specified service.

kubectl port-forward service/<service-name> <local-port>:<service-port>

Forwards a local port to a service port for local access.

ConfigMaps

kubectl create configmap <name> --from-literal=<key>=<value>

Creates a ConfigMap with the specified key-value pair.

kubectl get configmaps

Lists all ConfigMaps in the current namespace.

kubectl describe configmap <name>

Provides detailed information about a specific ConfigMap.

kubectl delete configmap <name>

Deletes the specified ConfigMap.

kubectl apply -f <configmap-file.yaml>

Applies a ConfigMap configuration from a YAML file.

Secrets

kubectl create secret generic <name> --from-literal=<key>=<value>

Creates a generic secret with the specified key-value pair.

kubectl get secrets

Lists all secrets in the current namespace.

kubectl describe secret <name>

Provides detailed information about a specific secret.

kubectl delete secret <name>

Deletes the specified secret.

kubectl apply -f <secret-file.yaml>

Applies a secret configuration from a YAML file.

Jobs

kubectl create job <name> --image=<image>

Creates a job with the specified image.

kubectl get jobs

Lists all jobs in the current namespace.

kubectl describe job <name>

Provides detailed information about a specific job.

kubectl delete job <name>

Deletes the specified job.

CronJobs

kubectl create cronjob <name> --image=<image> --schedule="* * * * *"

Creates a cron job with the specified image and schedule.

kubectl get cronjobs

Lists all cron jobs in the current namespace.

kubectl delete cronjob <name>

Deletes the specified cron job.

Persistent Volumes

kubectl get pv

Lists all Persistent Volumes (PVs) in the cluster.

kubectl get pvc

Lists all Persistent Volume Claims (PVCs) in the cluster.

kubectl describe pv <name>

Provides detailed information about a specific Persistent Volume.

kubectl describe pvc <name>

Provides detailed information about a specific Persistent Volume Claim.

kubectl delete pv <name>

Deletes the specified Persistent Volume.

kubectl delete pvc <name>

Deletes the specified Persistent Volume Claim.

Ingress

kubectl get ingress

Lists all ingress resources in the current namespace.

kubectl describe ingress <name>

Provides detailed information about a specific ingress resource.

kubectl apply -f <ingress-file.yaml>

Applies or updates an ingress resource from a YAML file.

kubectl delete ingress <name>

Deletes the specified ingress resource.

Debugging

kubectl get events

Lists all events in the current cluster.

kubectl describe <resource> <name>

Provides detailed information about any specified resource.

kubectl logs -f <pod-name>

Streams logs from a specified pod in real-time.

kubectl exec -it <pod-name> -- sh

Opens a shell inside a running pod interactively.

kubectl port-forward <pod-name> <local-port>:<pod-port>

Forwards a local port to a port on a pod for local access.

General Management

kubectl apply -f <file.yaml>

Applies configurations specified in a YAML file.

kubectl delete -f <file.yaml>

Deletes resources defined in a specified YAML file.

kubectl edit <resource> <name>

Opens a resource configuration for editing directly.

kubectl explain <resource>

Displays information about a specified resource type.

kubectl config view

Displays the current Kubernetes configuration settings.

References

  1. Kubernetes Official Documentation:
    https://kubernetes.io/docs/ The official Kubernetes documentation is the most reliable source for understanding Kubernetes concepts and commands.

  2. Kubectl Cheat Sheet:
    https://kubernetes.io/docs/reference/kubectl/cheatsheet/ A handy cheat sheet for kubectl commands provided by the Kubernetes team.

  3. Kubernetes GitHub Repository:
    https://github.com/kubernetes/kubernetes The official Kubernetes GitHub repository for source code and issue tracking.

  4. Kubernetes API Reference:
    https://kubernetes.io/docs/reference/kubernetes-api/ A detailed API reference for Kubernetes resources.

  5. Kubernetes Blog:
    https://kubernetes.io/blog/ The official Kubernetes blog for updates, tutorials, and best practices.