# Kubectl Cheat Sheet 📎

> Kubectl Cheat Sheet

Source: https://extim.su/blog/kubectl-cheat-sheet-📎/

<h2><i class="nf nf-dev-kubernetes"></i> Today, I want to share a comprehensive command reference for everyday Kubernetes use - cleaner, deeper, and more practical than the standard cheat sheets</h2>
<h1>Kubectl Cheat Sheet</h1>
<hr>
<h2>🔧 Context &amp; Configuration</h2>
<table>
<thead>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>kubectl config get-contexts</code></td>
<td>List all contexts</td>
</tr>
<tr>
<td><code>kubectl config use-context &lt;context&gt;</code></td>
<td>Switch to a different context</td>
</tr>
<tr>
<td><code>kubectl config current-context</code></td>
<td>Show the active context</td>
</tr>
<tr>
<td><code>kubectl config view --minify</code></td>
<td>View config of current context</td>
</tr>
<tr>
<td><code>kubectl config set-context</code></td>
<td>Create or modify a context</td>
</tr>
<tr>
<td><code>kubectl config set-cluster</code></td>
<td>Set a cluster config</td>
</tr>
<tr>
<td><code>kubectl config set-credentials</code></td>
<td>Configure user credentials</td>
</tr>
<tr>
<td><code>kubectl config unset users.&lt;user&gt;</code></td>
<td>Remove a user from config</td>
</tr>
</tbody>
</table>
<hr>
<h2>📦 Pods</h2>
<table>
<thead>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>kubectl get pods</code></td>
<td>List all pods in current namespace</td>
</tr>
<tr>
<td><code>kubectl get pods -A</code></td>
<td>List pods across all namespaces</td>
</tr>
<tr>
<td><code>kubectl get pod &lt;name&gt; -o wide</code></td>
<td>Show pod details including IP and node</td>
</tr>
<tr>
<td><code>kubectl describe pod &lt;name&gt;</code></td>
<td>Detailed pod information</td>
</tr>
<tr>
<td><code>kubectl logs &lt;pod&gt;</code></td>
<td>Logs from main container</td>
</tr>
<tr>
<td><code>kubectl logs &lt;pod&gt; -c &lt;container&gt;</code></td>
<td>Logs from a specific container</td>
</tr>
<tr>
<td><code>kubectl exec -it &lt;pod&gt; -- /bin/sh</code></td>
<td>Open shell session in pod</td>
</tr>
<tr>
<td><code>kubectl delete pod &lt;pod&gt;</code></td>
<td>Delete a pod (useful for restart)</td>
</tr>
</tbody>
</table>
<hr>
<h2>🚀 Deployments</h2>
<table>
<thead>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>kubectl get deploy</code></td>
<td>List deployments</td>
</tr>
<tr>
<td><code>kubectl describe deploy &lt;name&gt;</code></td>
<td>View deployment details</td>
</tr>
<tr>
<td><code>kubectl scale deploy &lt;name&gt; --replicas=3</code></td>
<td>Scale deployment</td>
</tr>
<tr>
<td><code>kubectl rollout status deploy &lt;name&gt;</code></td>
<td>Track rollout progress</td>
</tr>
<tr>
<td><code>kubectl rollout history deploy &lt;name&gt;</code></td>
<td>Show rollout history</td>
</tr>
<tr>
<td><code>kubectl rollout undo deploy &lt;name&gt;</code></td>
<td>Roll back to previous revision</td>
</tr>
<tr>
<td><code>kubectl edit deploy &lt;name&gt;</code></td>
<td>Edit deployment in-place</td>
</tr>
<tr>
<td><code>kubectl delete deploy &lt;name&gt;</code></td>
<td>Delete a deployment</td>
</tr>
</tbody>
</table>
<hr>
<h2>📋 Services</h2>
<table>
<thead>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>kubectl get svc</code></td>
<td>List services</td>
</tr>
<tr>
<td><code>kubectl describe svc &lt;name&gt;</code></td>
<td>Detailed service info</td>
</tr>
<tr>
<td><code>kubectl expose pod nginx --port=80 --type=ClusterIP</code></td>
<td>Expose pod as service</td>
</tr>
<tr>
<td><code>kubectl port-forward svc/&lt;svc&gt; 8080:80</code></td>
<td>Forward local port to service</td>
</tr>
<tr>
<td><code>kubectl get endpoints</code></td>
<td>Show service endpoints</td>
</tr>
</tbody>
</table>
<hr>
<h2>🌐 Ingress</h2>
<table>
<thead>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>kubectl get ingress</code></td>
<td>List ingress resources</td>
</tr>
<tr>
<td><code>kubectl describe ingress &lt;name&gt;</code></td>
<td>Details of an ingress resource</td>
</tr>
</tbody>
</table>
<hr>
<h2>📦 ConfigMaps &amp; Secrets</h2>
<table>
<thead>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>kubectl get configmap</code></td>
<td>List ConfigMaps</td>
</tr>
<tr>
<td><code>kubectl create configmap &lt;name&gt; --from-literal=key=value</code></td>
<td>Create from literal</td>
</tr>
<tr>
<td><code>kubectl create configmap &lt;name&gt; --from-file=file.txt</code></td>
<td>Create from file</td>
</tr>
<tr>
<td><code>kubectl get secret</code></td>
<td>List Secrets</td>
</tr>
<tr>
<td><code>kubectl create secret generic &lt;name&gt; --from-literal=password=secret</code></td>
<td>Create basic secret</td>
</tr>
<tr>
<td><code>kubectl get secret &lt;name&gt; -o yaml</code></td>
<td>View base64-encoded secret</td>
</tr>
<tr>
<td><code>kubectl get secret &lt;name&gt; -o jsonpath=&quot;{.data.key}&quot; \| base64 -d</code></td>
<td>Decode a secret</td>
</tr>
</tbody>
</table>
<hr>
<h2>🔐 RBAC &amp; ServiceAccounts</h2>
<table>
<thead>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>kubectl create sa &lt;name&gt;</code></td>
<td>Create a ServiceAccount</td>
</tr>
<tr>
<td><code>kubectl get sa</code></td>
<td>List ServiceAccounts</td>
</tr>
<tr>
<td><code>kubectl get clusterrolebinding -A</code></td>
<td>List all ClusterRoleBindings</td>
</tr>
<tr>
<td><code>kubectl describe clusterrolebinding &lt;name&gt;</code></td>
<td>Show details of binding</td>
</tr>
<tr>
<td><code>kubectl auth can-i get pods --as=system:serviceaccount:ns:sa</code></td>
<td>Check access for a ServiceAccount</td>
</tr>
</tbody>
</table>
<hr>
<h2>📂 Namespaces</h2>
<table>
<thead>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>kubectl get ns</code></td>
<td>List namespaces</td>
</tr>
<tr>
<td><code>kubectl create ns &lt;name&gt;</code></td>
<td>Create a namespace</td>
</tr>
<tr>
<td><code>kubectl delete ns &lt;name&gt;</code></td>
<td>Delete a namespace</td>
</tr>
<tr>
<td><code>kubectl config set-context --current --namespace=&lt;ns&gt;</code></td>
<td>Set default namespace for current context</td>
</tr>
</tbody>
</table>
<hr>
<h2>🛠 Jobs &amp; CronJobs</h2>
<table>
<thead>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>kubectl create job hello --image=busybox -- echo Hello</code></td>
<td>Run one-time job</td>
</tr>
<tr>
<td><code>kubectl get jobs</code></td>
<td>List jobs</td>
</tr>
<tr>
<td><code>kubectl delete job &lt;name&gt;</code></td>
<td>Delete a job</td>
</tr>
<tr>
<td><code>kubectl create cronjob hello --image=busybox --schedule=&quot;*/5 * * * *&quot; -- echo Hi</code></td>
<td>Schedule recurring job</td>
</tr>
<tr>
<td><code>kubectl get cronjob</code></td>
<td>List CronJobs</td>
</tr>
</tbody>
</table>
<hr>
<h2>📦 Storage (PVs &amp; PVCs)</h2>
<table>
<thead>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>kubectl get pv</code></td>
<td>List PersistentVolumes</td>
</tr>
<tr>
<td><code>kubectl get pvc</code></td>
<td>List PersistentVolumeClaims</td>
</tr>
<tr>
<td><code>kubectl describe pvc &lt;name&gt;</code></td>
<td>PVC details</td>
</tr>
<tr>
<td><code>kubectl delete pvc &lt;name&gt;</code></td>
<td>Delete a PVC</td>
</tr>
</tbody>
</table>
<hr>
<h2>🔍 Debugging &amp; Troubleshooting</h2>
<table>
<thead>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>kubectl get events --sort-by=.metadata.creationTimestamp</code></td>
<td>Show recent events</td>
</tr>
<tr>
<td><code>kubectl logs &lt;pod&gt; --previous</code></td>
<td>Logs from a crashed pod</td>
</tr>
<tr>
<td><code>kubectl exec -it &lt;pod&gt; -- /bin/sh</code></td>
<td>Open shell in pod</td>
</tr>
<tr>
<td><code>kubectl debug -it &lt;pod&gt; --image=busybox --target=&lt;container&gt;</code></td>
<td>Ephemeral debug container</td>
</tr>
<tr>
<td><code>kubectl top pod</code></td>
<td>Show pod CPU/memory usage</td>
</tr>
<tr>
<td><code>kubectl get pods -o wide</code></td>
<td>Show node assignments and IPs</td>
</tr>
</tbody>
</table>
<hr>
<h2>📤 YAML &amp; Apply</h2>
<table>
<thead>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>kubectl apply -f &lt;file&gt;.yaml</code></td>
<td>Apply a YAML manifest</td>
</tr>
<tr>
<td><code>kubectl delete -f &lt;file&gt;.yaml</code></td>
<td>Delete resource defined in YAML</td>
</tr>
<tr>
<td><code>kubectl apply -f &lt;file&gt;.yaml --dry-run=client -o yaml</code></td>
<td>Preview resource definition</td>
</tr>
<tr>
<td><code>kubectl explain &lt;resource&gt;</code></td>
<td>Show schema for a resource</td>
</tr>
</tbody>
</table>
<hr>
<h2>📊 Output Formatting</h2>
<table>
<thead>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>-o wide</code></td>
<td>Show more details (e.g. IPs, nodes)</td>
</tr>
<tr>
<td><code>-o yaml</code></td>
<td>Output full YAML</td>
</tr>
<tr>
<td><code>-o jsonpath=&quot;{.items[*].metadata.name}&quot;</code></td>
<td>Query JSON paths</td>
</tr>
<tr>
<td><code>--field-selector status.phase=Running</code></td>
<td>Filter by field</td>
</tr>
<tr>
<td><code>-l app=nginx</code></td>
<td>Filter by label</td>
</tr>
<tr>
<td><code>--sort-by=.metadata.name</code></td>
<td>Sort output</td>
</tr>
</tbody>
</table>
<hr>
<h2>🔁 Port Forwarding &amp; Proxies</h2>
<table>
<thead>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>kubectl port-forward pod/&lt;pod&gt; 8080:80</code></td>
<td>Port forward to pod</td>
</tr>
<tr>
<td><code>kubectl port-forward svc/&lt;svc&gt; 9090:80</code></td>
<td>Port forward to service</td>
</tr>
<tr>
<td><code>kubectl proxy</code></td>
<td>Run API proxy at localhost:8001</td>
</tr>
</tbody>
</table>
<hr>
<h2>🧼 Cleanup &amp; Deletion</h2>
<table>
<thead>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>kubectl delete all --all</code></td>
<td>Delete all resources in namespace</td>
</tr>
<tr>
<td><code>kubectl delete pod,svc -l app=nginx</code></td>
<td>Delete resources by label</td>
</tr>
<tr>
<td><code>kubectl delete pvc --all</code></td>
<td>Remove all PVCs</td>
</tr>
</tbody>
</table>
<hr>
<h2>🧪 Common Shortcuts</h2>
<table>
<thead>
<tr>
<th>Task</th>
<th>Command</th>
</tr>
</thead>
<tbody>
<tr>
<td>Restart a pod</td>
<td><code>kubectl delete pod &lt;pod&gt;</code> (Deployment auto-recreates)</td>
</tr>
<tr>
<td>Watch pod status</td>
<td><code>watch kubectl get pods</code></td>
</tr>
<tr>
<td>Quick deploy NGINX</td>
<td><code>kubectl create deploy nginx --image=nginx</code></td>
</tr>
<tr>
<td>Expose NGINX</td>
<td><code>kubectl expose deploy nginx --port=80 --type=LoadBalancer</code></td>
</tr>
</tbody>
</table>

