├── img └── header.png └── README.md /img/header.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eon01/KubectlCheatSheet/HEAD/img/header.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # 👌 Kubectl Cheatsheet 3 | 4 | 🍴 Fork and contribute. Your contribution is welcome 🙌 5 | 6 | # Sponsors 7 | 8 | ## 📚 Cloud Native Microservices With Kubernetes 9 | 10 | A Comprehensive Guide to Building, Scaling, Deploying, Observing, and Managing Highly-Available Microservices in Kubernetes. A comprehensive and example-rich guide suitable for learners of all levels. Get the book [on Amazon](https://www.amazon.com/Cloud-Native-Microservices-Kubernetes-Highly-Available-ebook/dp/B0C923BYSL?&linkCode=ll1&tag=eon01-20&linkId=a78552e5f2c08434be802425be87a91a&language=en_US&ref_=as_li_ss_tl). 11 | 12 | ## ❤️ Kaptain, Kubernetes Weekly Newsletter 13 | 14 | Curated Kubernetes news, tutorials, tools, research papers, jobs, and more - delivered to your inbox every week. [Subscribe and start receiving our free weekly newsletter](https://faun.dev/newsletter/kaptain). 15 | 16 | ## 🛍️ Show off your love for all things TECH 17 | 18 | If you're seeking a cozy sweatshirt to wear during your extended coding sessions or a mug to exhibit your passion for programming, [you can find it all on ByteVibe](https://bytevibe.co/) 19 | 20 | # TOC 21 | 22 | - [Pods](#pods) 23 | * [List all pods in namespace ``](#list-all-pods-in-namespace---default--) 24 | * [View a pod in watch mode](#view-a-pod-in-watch-mode) 25 | * [View all pods in watch mode](#view-all-pods-in-watch-mode) 26 | * [List sorted pods](#list-sroted-pods) 27 | * [List pods using a different output](#list-pods-using-a-different-output) 28 | * [Formatting output](#formatting-output) 29 | * [List all pods in a namespace](#list-all-pods-in-a-namespace) 30 | * [List all pods in all namespaces](#list-all-pods-in-all-namespaces) 31 | * [Create from an image](#create-from-an-image) 32 | * [Run pod in an interactive shell mode](#run-pod-in-an-interactive-shell-mode) 33 | * [Run a command after creating a pod](#run-a-command-after-creating-a-pod) 34 | * [Executing a command in a running pod](#executing-a-command-in-a-running-pod) 35 | * [Create a pod: dry run mode (without really creating it)](#create-a-pod--dry-run-mode--without-really-creating-it-) 36 | * [Patch a pod](#patch-a-pod) 37 | * [Create from a YAML file](#create-from-a-yaml-file) 38 | * [Export YAML from the dry run mode](#export-yaml-from-the-dry-run-mode) 39 | * [Create from STDIN](#create-from-stdin) 40 | * [Create multiple resources from STDIN](#create-multiple-resources-from-stdin) 41 | * [Create in a namespace](#create-in-a-namespace) 42 | * [Create in a namespace from a file](#create-in-a-namespace-from-a-file) 43 | * [Delete pods](#delete-pods) 44 | * [Get pod logs](#get-pod-logs) 45 | * [List all container id of init container of all pods](#list-all-container-id-of-init-container-of-all-pods) 46 | * [Show metrics for a given pod](#show-metrics-for-a-given-pod) 47 | * [Show metrics for a given pod and all its containers](#show-metrics-for-a-given-pod-and-all-its-containers) 48 | - [Deployments](#deployments) 49 | * [Create a deployment](#create-a-deployment) 50 | * [Create a deployment with a predefined replica number](#create-a-deployment-with-a-predefined-replica-number) 51 | * [Create a deployment with a predefined replica number and opening a port](#create-a-deployment-with-a-predefined-replica-number-and-opening-a-port) 52 | * [Create a deployment with a predefined replica number, opening a port and exposing it](#create-a-deployment-with-a-predefined-replica-number--opening-a-port-and-exposing-it) 53 | * [Get a deployment](#get-a-deployment) 54 | * [Watch a deployment](#watch-a-deployment) 55 | * [List all deployments](#list-all-deployments) 56 | * [Update the image](#update-the-image) 57 | * [Scale a deployment](#scale-a-deployment) 58 | * [Dry run and YAML output](#dry-run-and-yaml-output) 59 | * [Create a deployment from a file](#create-a-deployment-from-a-file) 60 | * [Edit a deployment](#edit-a-deployment) 61 | * [Rollback deployment](#rollback-deployment) 62 | * [Get rollout history](#get-rollout-history) 63 | * [Roll back to a previous revision](#roll-back-to-a-previous-revision) 64 | * [Execute deployment rollout operations](#execute-deployment-rollout-operations) 65 | - [Port Forwarding](#port-forwarding) 66 | * [Choosing localhost port](#choosing-localhost-port) 67 | * [Listening on the same port](#listening-on-the-same-port) 68 | * [Listen on a random port locally](#listen-on-a-random-port-locally) 69 | * [Listen on port on localhost + another IP](#listen-on-port-on-localhost---another-ip) 70 | * [Listen on a forwarded port on all addresses](#listen-on-a-forwarded-port-on-all-addresses) 71 | - [Services](#services) 72 | * [Create a service](#create-a-service) 73 | * [Delete service(s)](#delete-service-s-) 74 | * [Describe a service](#describe-a-service) 75 | - [Nodes](#nodes) 76 | * [Get node](#get-node) 77 | * [Get a specific node](#get-a-specific-node) 78 | * [Show node metrics](#show-node-metrics) 79 | * [Get external IPs of cluster nodes](#get-external-ips-of-cluster-nodes) 80 | * [Describe commands with verbose output](#describe-commands-with-verbose-output) 81 | * [Check which nodes are ready](#check-which-nodes-are-ready) 82 | * [Mark a node as unschedulable](#mark-a-node-as-unschedulable) 83 | * [Drain a node for maintenance](#drain-a-node-for-maintenance) 84 | * [Mark a node as schedulable](#mark-a-node-as-schedulable) 85 | - [Namespaces](#namespaces) 86 | * [List namespaces](#list-namespaces) 87 | * [List or describe a namespace](#list-or-describe-a-namespace) 88 | * [Create namespace](#create-namespace) 89 | * [Delete namespace](#delete-namespace) 90 | - [Service accounts](#service-accounts) 91 | * [List service accounts](#list-service-accounts) 92 | * [Get a service account](#get-a-service-account) 93 | * [Create a service account](#create-a-service-account) 94 | * [Delete a service account](#delete-a-service-account) 95 | * [Describe a service account](#describe-a-service-account) 96 | - [Events](#events) 97 | * [List events](#list-events) 98 | * [List sorted events](#list-sorted-events) 99 | * [List formatted events](#list-formatted-events) 100 | - [Documentation](#documentation) 101 | * [Get the documentation for pod manifests](#get-the-documentation-for-pod-manifests) 102 | * [Get the documentation for service manifests](#get-the-documentation-for-service-manifests) 103 | - [Describing resources](#describing-resources) 104 | - [Editing resources](#editing-resources) 105 | * [Edit a service](#edit-a-service) 106 | * [Edit a service with your favorite text editor](#edit-a-service-with-your-favorite-text-editor) 107 | - [Deleting Resources](#deleting-resources) 108 | * [Delete a resource using the type and name specified in ``](#delete-a-resource-using-the-type-and-name-specified-in---file--) 109 | * [Delete pods and services with same names](#delete-pods-and-services-with-same-names) 110 | * [Delete pods and services with a custom label](#delete-pods-and-services-with-a-custom-label) 111 | * [Delete all pods and services in a namespace](#delete-all-pods-and-services-in-a-namespace) 112 | * [Delete all resources in a namespace](#delete-all-resources-in-a-namespace) 113 | - [All get commands](#all-get-commands) 114 | - [Abbreviations / Short forms of resource types](#abbreviations---short-forms-of-resource-types) 115 | - [Verbose Kubectl](#verbose-kubectl) 116 | - [Cluster](#cluster) 117 | * [Display addresses of the master and services](#display-addresses-of-the-master-and-services) 118 | * [Dump cluster state to STDOUT](#dump-cluster-state-to-stdout) 119 | * [Dump cluster state to a file](#dump-cluster-state-to-a-file) 120 | * [Compares the current cluster state against the state that the cluster would be in if the manifest was applied](#compares-the-current-cluster-state-against-the-state-that-the-cluster-would-be-in-if-the-manifest-was-applied) 121 | * [List all images running in a cluster](#list-all-images-running-in-a-cluster) 122 | - [Kubectl context](#kubectl-context) 123 | * [Show merged kubeconfig settings](#show-merged-kubeconfig-settings) 124 | * [Use multiple kubeconfig](#use-multiple-kubeconfig) 125 | + [Get a list of users](#get-a-list-of-users) 126 | * [Display the first user](#display-the-first-user) 127 | * [Get the password for the "admin" user](#get-the-password-for-the--admin--user) 128 | + [Display the current context](#display-the-current-context) 129 | + [Display list of contexts](#display-list-of-contexts) 130 | + [Set the default context to ``](#set-the-default-context-to---cluster--) 131 | * [Sets a user entry in kubeconfig](#sets-a-user-entry-in-kubeconfig) 132 | * [Sets a user with a client key](#sets-a-user-with-a-client-key) 133 | * [Sets a user with basic auth](#sets-a-user-with-basic-auth) 134 | * [Sets a user with client certificate](#sets-a-user-with-client-certificate) 135 | * [Set a context utilizing a specific config file](#set-a-context-utilizing-a-specific-config-file) 136 | * [Set a context utilizing a specific username and namespace.](#set-a-context-utilizing-a-specific-username-and-namespace) 137 | - [Alias](#alias) 138 | * [Create an alias on *nix](#create-an-alias-on--nix) 139 | * [Create an alias on Windows](#create-an-alias-on-windows) 140 | - [Kubectl imperative (create) vs declarative (apply)](#kubectl-imperative--create--vs-declarative--apply-) 141 | * [Create](#create) 142 | * [Apply](#apply) 143 | 144 | 145 | 146 | 147 | 148 | ## Pods 149 | 150 | ### List all pods in namespace `` 151 | 152 | ``` 153 | kubectl get pods 154 | ``` 155 | 156 | or 157 | 158 | ``` 159 | kubectl get pod 160 | ``` 161 | 162 | or 163 | 164 | ``` 165 | kubectl get po 166 | ``` 167 | 168 | ### View a pod in watch mode 169 | 170 | ``` 171 | kubectl get pod --watch 172 | ``` 173 | 174 | ### View all pods in watch mode 175 | 176 | ``` 177 | kubectl get pods -A --watch 178 | ``` 179 | 180 | ### List sroted pods 181 | 182 | ``` 183 | kubectl get pods --sort-by='.status.containerStatuses[0].restartCount' 184 | ``` 185 | 186 | ### List pods using a different output 187 | 188 | ``` 189 | kubectl get pods -o 190 | ``` 191 | 192 | Examples: 193 | 194 | - JSON output 195 | 196 | ``` 197 | kubectl get pods -o json 198 | ``` 199 | 200 | or 201 | 202 | ``` 203 | kubectl get pods -ojson 204 | ``` 205 | 206 | or 207 | 208 | ``` 209 | kubectl get pods -o=json 210 | ``` 211 | 212 | - Wide output: 213 | 214 | ``` 215 | kubectl get pods -o wide 216 | ``` 217 | 218 | - Custom columns: 219 | 220 | ``` 221 | kubectl get pods -o custom-columns='DATA:spec.containers[*].image' 222 | ``` 223 | 224 | or 225 | 226 | ``` 227 | kubectl get pods -o custom-columns='DATA:spec.containers[*].volumeMounts' 228 | ``` 229 | 230 | or 231 | 232 | ``` 233 | kubectl get pods -o custom-columns='DATA:metadata.*' 234 | ``` 235 | 236 | 237 | ### Formatting output 238 | 239 | To output details to your terminal window in a specific format, add the `-o` (or `--output`) flag to a supported `kubectl` command (source: [K8s docs](https://kubernetes.io/docs)) 240 | 241 | Output format | Description 242 | --------------| ----------- 243 | `-o=custom-columns=` | Print a table using a comma separated list of custom columns 244 | `-o=custom-columns-file=` | Print a table using the custom columns template in the `` file 245 | `-o=json` | Output a JSON formatted API object 246 | `-o=jsonpath=