{"id":1955,"date":"2024-06-11T06:49:27","date_gmt":"2024-06-11T06:49:27","guid":{"rendered":"https:\/\/www.nicktailor.com\/?p=1955"},"modified":"2025-06-11T07:13:17","modified_gmt":"2025-06-11T07:13:17","slug":"1955","status":"publish","type":"post","link":"https:\/\/nicktailor.com\/tech-blog\/1955\/","title":{"rendered":"Cheat Sheet for DevOps Engineers"},"content":{"rendered":"\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"UTF-8\"\/>\n    <title>kubectl Cheat Sheet &#8211; In-Depth Guide<\/title>\n    <style>\n        body {\n            font-family: Arial, sans-serif;\n            line-height: 1.6;\n            padding: 20px;\n            background-color: #f9f9f9;\n            color: #333;\n        }\n        h1, h2, h3 {\n            color: #222;\n        }\n        pre {\n            background: #f4f4f4;\n            padding: 12px;\n            border-left: 4px solid #ccc;\n            overflow-x: auto;\n        }\n        code {\n            background: #eee;\n            padding: 2px 4px;\n            border-radius: 4px;\n            font-family: Consolas, monospace;\n        }\n        hr {\n            border: none;\n            border-top: 2px solid #ddd;\n            margin: 40px 0;\n        }\n    <\/style>\n<\/head>\n<body>\n\n<p>Managing Kubernetes clusters efficiently requires fluency with <code>kubectl<\/code>, the command-line interface for interacting with the Kubernetes API. Whether you&#8217;re deploying applications, viewing logs, or debugging infrastructure, this tool is your gateway to smooth cluster operations.<\/p>\n\n<p>This in-depth cheat sheet will give you a comprehensive reference of how to use <code>kubectl<\/code> effectively in real-world operations, including advanced flags, filtering tricks, rolling updates, patching, output formatting, and resource exploration.<\/p>\n\n<hr \/>\n\n<h2>Shell Autocompletion<\/h2>\n<p>Boost productivity with shell autocompletion for <code>kubectl<\/code>:<\/p>\n\n<h3>Bash<\/h3>\n<pre><code>source &lt;(kubectl completion bash)\necho \"source &lt;(kubectl completion bash)\" &gt;&gt; ~\/.bashrc<\/code><\/pre>\n\n<h3>ZSH<\/h3>\n<pre><code>source &lt;(kubectl completion zsh)\necho '[[ $commands[kubectl] ]] &amp;&amp; source &lt;(kubectl completion zsh)' &gt;&gt; ~\/.zshrc<\/code><\/pre>\n\n<h3>Aliases<\/h3>\n<pre><code>alias k=kubectl\ncomplete -o default -F __start_kubectl k<\/code><\/pre>\n\n<hr \/>\n\n<h2>Working with Contexts &amp; Clusters<\/h2>\n<pre><code>kubectl config view                        # Show merged config\nkubectl config get-contexts               # List contexts\nkubectl config current-context            # Show current context\nkubectl config use-context my-context     # Switch to context<\/code><\/pre>\n\n<p>Set default namespace for future commands:<\/p>\n<pre><code>kubectl config set-context --current --namespace=my-namespace<\/code><\/pre>\n\n<hr \/>\n\n<h2>Deployments &amp; YAML Management<\/h2>\n<pre><code>kubectl apply -f .\/manifest.yaml          # Apply resource(s)\nkubectl apply -f .\/dir\/                   # Apply all YAMLs in directory\nkubectl create deployment nginx --image=nginx\nkubectl explain pod                       # Show pod schema<\/code><\/pre>\n\n<p>Apply resources from multiple sources:<\/p>\n<pre><code>kubectl apply -f .\/one.yaml -f .\/two.yaml\nkubectl apply -f https:\/\/example.com\/config.yaml<\/code><\/pre>\n\n<hr \/>\n\n<h2>Viewing &amp; Finding Resources<\/h2>\n<pre><code>kubectl get pods                          # List all pods\nkubectl get pods -o wide                  # Detailed pod listing\nkubectl get services                      # List services\nkubectl describe pod my-pod               # Detailed pod info<\/code><\/pre>\n\n<p>Filter &amp; sort:<\/p>\n<pre><code>kubectl get pods --field-selector=status.phase=Running\nkubectl get pods --sort-by='.status.containerStatuses[0].restartCount'<\/code><\/pre>\n\n<p>Find pod labels:<\/p>\n<pre><code>kubectl get pods --show-labels<\/code><\/pre>\n\n<hr \/>\n\n<h2>Updating &amp; Rolling Deployments<\/h2>\n<pre><code>kubectl set image deployment\/web nginx=nginx:1.19       # Set new image\nkubectl rollout status deployment\/web                   # Watch rollout\nkubectl rollout history deployment\/web                  # Show revisions\nkubectl rollout undo deployment\/web                     # Undo last change\nkubectl rollout undo deployment\/web --to-revision=2     # Revert to specific<\/code><\/pre>\n\n<hr \/>\n\n<h2>Editing, Scaling, Deleting<\/h2>\n<pre><code>kubectl edit deployment\/web                    # Live edit YAML\nkubectl scale deployment\/web --replicas=5      # Scale up\/down\nkubectl delete pod my-pod                      # Delete by name\nkubectl delete -f pod.yaml                     # Delete from file<\/code><\/pre>\n\n<hr \/>\n\n<h2>Logs, Execs &amp; Debugging<\/h2>\n<pre><code>kubectl logs my-pod                            # View logs\nkubectl logs my-pod -c container-name          # Logs from container\nkubectl logs my-pod --previous                 # Logs from crashed\nkubectl exec my-pod -- ls \/                    # Run command\nkubectl exec -it my-pod -- bash                # Shell access<\/code><\/pre>\n\n<hr \/>\n\n<h2>Working with Services<\/h2>\n<pre><code>kubectl expose pod nginx --port=80 --target-port=8080   # Create service\nkubectl port-forward svc\/my-service 8080:80             # Forward to local<\/code><\/pre>\n\n<hr \/>\n\n<h2>Resource Metrics<\/h2>\n<pre><code>kubectl top pod                              # Pod metrics\nkubectl top pod --sort-by=cpu                # Sort by CPU\nkubectl top node                             # Node metrics<\/code><\/pre>\n\n<hr \/>\n\n<h2>Patching Resources<\/h2>\n<p>Strategic merge patch:<\/p>\n<pre><code>kubectl patch deployment my-deploy -p '{\"spec\":{\"replicas\":4}}'<\/code><\/pre>\n\n<p>JSON patch with array targeting:<\/p>\n<pre><code>kubectl patch pod my-pod --type='json' -p='[\n  {\"op\": \"replace\", \"path\": \"\/spec\/containers\/0\/image\", \"value\":\"nginx:1.21\"}\n]'<\/code><\/pre>\n\n<hr \/>\n\n<h2>Cluster &amp; Node Management<\/h2>\n<pre><code>kubectl cordon my-node                        # Prevent new pods\nkubectl drain my-node                         # Evict pods for maintenance\nkubectl uncordon my-node                      # Resume scheduling\nkubectl get nodes                             # List all nodes\nkubectl describe node my-node                 # Node details<\/code><\/pre>\n\n<hr \/>\n\n<h2>Output Formatting<\/h2>\n<pre><code>kubectl get pods -o json\nkubectl get pods -o yaml\nkubectl get pods -o custom-columns=\"NAME:.metadata.name,IMAGE:.spec.containers[*].image\"<\/code><\/pre>\n\n<hr \/>\n\n<h2>Exploring API Resources<\/h2>\n<pre><code>kubectl api-resources                         # List all resources\nkubectl api-resources --namespaced=false      # Non-namespaced\nkubectl api-resources -o wide                 # Extended info<\/code><\/pre>\n\n<hr \/>\n\n<h2>Logging &amp; Verbosity<\/h2>\n<pre><code>kubectl get pods -v=6                         # Debug output\nkubectl get deployment -v=9                   # Full API trace<\/code><\/pre>\n\n<hr \/>\n\n<h2>Deployment Template Example<\/h2>\n<pre><code>apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: nginx-deployment\n  namespace: my-namespace\nspec:\n  replicas: 3\n  selector:\n    matchLabels:\n      app: nginx\n  template:\n    metadata:\n      labels:\n        app: nginx\n    spec:\n      containers:\n      - name: nginx\n        image: nginx:latest\n        ports:\n        - containerPort: 80<\/code><\/pre>\n\n<hr \/>\n\n<\/body>\n<\/html>\n","protected":false},"excerpt":{"rendered":"<p>kubectl Cheat Sheet &#8211; In-Depth Guide Managing Kubernetes clusters efficiently requires fluency with kubectl, the command-line interface for interacting with the Kubernetes API. Whether you&#8217;re deploying applications, viewing logs, or debugging infrastructure, this tool is your gateway to smooth cluster operations. This in-depth cheat sheet will give you a comprehensive reference of how to use kubectl effectively in real-world operations,<a href=\"https:\/\/nicktailor.com\/tech-blog\/1955\/\" class=\"read-more\">Read More &#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[145],"tags":[],"class_list":["post-1955","post","type-post","status-publish","format-standard","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/1955","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/comments?post=1955"}],"version-history":[{"count":3,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/1955\/revisions"}],"predecessor-version":[{"id":1960,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/1955\/revisions\/1960"}],"wp:attachment":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/media?parent=1955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/categories?post=1955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/tags?post=1955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}