{"id":13449175,"url":"https://github.com/eon01/KubectlCheatSheet","last_synced_at":"2025-03-22T22:32:15.183Z","repository":{"id":55565500,"uuid":"316046578","full_name":"eon01/KubectlCheatSheet","owner":"eon01","description":"The Definitive Kubectl Sheetcheat. ⭐ Give it a star if you like it. Work (always) in progress !","archived":false,"fork":false,"pushed_at":"2023-12-24T08:49:28.000Z","size":719,"stargazers_count":187,"open_issues_count":0,"forks_count":77,"subscribers_count":8,"default_branch":"main","last_synced_at":"2024-09-05T13:13:38.794Z","etag":null,"topics":["cheatsheet","cheatsheets","kubectl","kubernetes","snippets"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eon01.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-11-25T20:27:13.000Z","updated_at":"2024-09-01T09:22:42.000Z","dependencies_parsed_at":"2023-12-24T09:33:42.230Z","dependency_job_id":"345ec8d8-2ff1-476b-acc5-4829037e1288","html_url":"https://github.com/eon01/KubectlCheatSheet","commit_stats":null,"previous_names":["eon01/kubectl-cheatsheet","eon01/kubectl-sheetcheat"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eon01%2FKubectlCheatSheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eon01%2FKubectlCheatSheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eon01%2FKubectlCheatSheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eon01%2FKubectlCheatSheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eon01","download_url":"https://codeload.github.com/eon01/KubectlCheatSheet/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221840606,"owners_count":16889827,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cheatsheet","cheatsheets","kubectl","kubernetes","snippets"],"created_at":"2024-07-31T06:00:32.728Z","updated_at":"2024-10-28T14:30:22.321Z","avatar_url":"https://github.com/eon01.png","language":null,"funding_links":[],"categories":["Others","kubernetes"],"sub_categories":[],"readme":"\n# 👌 Kubectl Cheatsheet\n\n🍴 Fork and contribute. Your contribution is welcome 🙌\n\n# Sponsors\n\n## 📚 Cloud Native Microservices With Kubernetes\n\nA 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?\u0026linkCode=ll1\u0026tag=eon01-20\u0026linkId=a78552e5f2c08434be802425be87a91a\u0026language=en_US\u0026ref_=as_li_ss_tl).\n\n## ❤️ Kaptain, Kubernetes Weekly Newsletter\n\nCurated 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).\n\n## 🛍️ Show off your love for all things TECH\n\nIf 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/)\n\n# TOC\n\n- [Pods](#pods)\n  * [List all pods in namespace `\u003cdefault\u003e`](#list-all-pods-in-namespace---default--)\n  * [View a pod in watch mode](#view-a-pod-in-watch-mode)\n  * [View all pods in watch mode](#view-all-pods-in-watch-mode)\n  * [List sorted pods](#list-sroted-pods)\n  * [List pods using a different output](#list-pods-using-a-different-output)\n  * [Formatting output](#formatting-output)\n  * [List all pods in a namespace](#list-all-pods-in-a-namespace)\n  * [List all pods in all namespaces](#list-all-pods-in-all-namespaces)\n  * [Create from an image](#create-from-an-image)\n  * [Run pod in an interactive shell mode](#run-pod-in-an-interactive-shell-mode)\n  * [Run a command after creating a pod](#run-a-command-after-creating-a-pod)\n  * [Executing a command in a running pod](#executing-a-command-in-a-running-pod)\n  * [Create a pod: dry run mode (without really creating it)](#create-a-pod--dry-run-mode--without-really-creating-it-)\n  * [Patch a pod](#patch-a-pod)\n  * [Create from a YAML file](#create-from-a-yaml-file)\n  * [Export YAML from the dry run mode](#export-yaml-from-the-dry-run-mode)\n  * [Create from STDIN](#create-from-stdin)\n  * [Create multiple resources from STDIN](#create-multiple-resources-from-stdin)\n  * [Create in a namespace](#create-in-a-namespace)\n  * [Create in a namespace from a file](#create-in-a-namespace-from-a-file)\n  * [Delete pods](#delete-pods)\n  * [Get pod logs](#get-pod-logs)\n  * [List all container id of init container of all pods](#list-all-container-id-of-init-container-of-all-pods)\n  * [Show metrics for a given pod](#show-metrics-for-a-given-pod)\n  * [Show metrics for a given pod and all its containers](#show-metrics-for-a-given-pod-and-all-its-containers)\n- [Deployments](#deployments)\n  * [Create a deployment](#create-a-deployment)\n  * [Create a deployment with a predefined replica number](#create-a-deployment-with-a-predefined-replica-number)\n  * [Create a deployment with a predefined replica number and opening a port](#create-a-deployment-with-a-predefined-replica-number-and-opening-a-port)\n  * [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)\n  * [Get a deployment](#get-a-deployment)\n  * [Watch a deployment](#watch-a-deployment)\n  * [List all deployments](#list-all-deployments)\n  * [Update the image](#update-the-image)\n  * [Scale a deployment](#scale-a-deployment)\n  * [Dry run and YAML output](#dry-run-and-yaml-output)\n  * [Create a deployment from a file](#create-a-deployment-from-a-file)\n  * [Edit a deployment](#edit-a-deployment)\n  * [Rollback deployment](#rollback-deployment)\n  * [Get rollout history](#get-rollout-history)\n  * [Roll back to a previous revision](#roll-back-to-a-previous-revision)\n  * [Execute deployment rollout operations](#execute-deployment-rollout-operations)\n- [Port Forwarding](#port-forwarding)\n  * [Choosing localhost port](#choosing-localhost-port)\n  * [Listening on the same port](#listening-on-the-same-port)\n  * [Listen on a random port locally](#listen-on-a-random-port-locally)\n  * [Listen on port on localhost + another IP](#listen-on-port-on-localhost---another-ip)\n  * [Listen on a forwarded port on all addresses](#listen-on-a-forwarded-port-on-all-addresses)\n- [Services](#services)\n  * [Create a service](#create-a-service)\n  * [Delete service(s)](#delete-service-s-)\n  * [Describe a service](#describe-a-service)\n- [Nodes](#nodes)\n  * [Get node](#get-node)\n  * [Get a specific node](#get-a-specific-node)\n  * [Show node metrics](#show-node-metrics)\n  * [Get external IPs of cluster nodes](#get-external-ips-of-cluster-nodes)\n  * [Describe commands with verbose output](#describe-commands-with-verbose-output)\n  * [Check which nodes are ready](#check-which-nodes-are-ready)\n  * [Mark a node as unschedulable](#mark-a-node-as-unschedulable)\n  * [Drain a node for maintenance](#drain-a-node-for-maintenance)\n  * [Mark a node as schedulable](#mark-a-node-as-schedulable)\n- [Namespaces](#namespaces)\n  * [List namespaces](#list-namespaces)\n  * [List or describe a namespace](#list-or-describe-a-namespace)\n  * [Create namespace](#create-namespace)\n  * [Delete namespace](#delete-namespace)\n- [Service accounts](#service-accounts)\n  * [List service accounts](#list-service-accounts)\n  * [Get a service account](#get-a-service-account)\n  * [Create a service account](#create-a-service-account)\n  * [Delete a service account](#delete-a-service-account)\n  * [Describe a service account](#describe-a-service-account)\n- [Events](#events)\n  * [List events](#list-events)\n  * [List sorted events](#list-sorted-events)\n  * [List formatted events](#list-formatted-events)\n- [Documentation](#documentation)\n  * [Get the documentation for pod manifests](#get-the-documentation-for-pod-manifests)\n  * [Get the documentation for service manifests](#get-the-documentation-for-service-manifests)\n- [Describing resources](#describing-resources)\n- [Editing resources](#editing-resources)\n  * [Edit a service](#edit-a-service)\n  * [Edit a service with your favorite text editor](#edit-a-service-with-your-favorite-text-editor)\n- [Deleting Resources](#deleting-resources)\n  * [Delete a resource using the type and name specified in `\u003cfile\u003e`](#delete-a-resource-using-the-type-and-name-specified-in---file--)\n  * [Delete pods and services with same names](#delete-pods-and-services-with-same-names)\n  * [Delete pods and services with a custom label](#delete-pods-and-services-with-a-custom-label)\n  * [Delete all pods and services in a namespace](#delete-all-pods-and-services-in-a-namespace)\n  * [Delete all resources in a namespace](#delete-all-resources-in-a-namespace)\n- [All get commands](#all-get-commands)\n- [Abbreviations / Short forms of resource types](#abbreviations---short-forms-of-resource-types)\n- [Verbose Kubectl](#verbose-kubectl)\n- [Cluster](#cluster)\n  * [Display addresses of the master and services](#display-addresses-of-the-master-and-services)\n  * [Dump cluster state to STDOUT](#dump-cluster-state-to-stdout)\n  * [Dump cluster state to a file](#dump-cluster-state-to-a-file)\n  * [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)\n  * [List all images running in a cluster](#list-all-images-running-in-a-cluster)\n- [Kubectl context](#kubectl-context)\n  * [Show merged kubeconfig settings](#show-merged-kubeconfig-settings)\n  * [Use multiple kubeconfig](#use-multiple-kubeconfig)\n    + [Get a list of users](#get-a-list-of-users)\n  * [Display the first user](#display-the-first-user)\n  * [Get the password for the \"admin\" user](#get-the-password-for-the--admin--user)\n    + [Display the current context](#display-the-current-context)\n    + [Display list of contexts](#display-list-of-contexts)\n    + [Set the default context to `\u003ccluster\u003e`](#set-the-default-context-to---cluster--)\n  * [Sets a user entry in kubeconfig](#sets-a-user-entry-in-kubeconfig)\n  * [Sets a user with a client key](#sets-a-user-with-a-client-key)\n  * [Sets a user with basic auth](#sets-a-user-with-basic-auth)\n  * [Sets a user with client certificate](#sets-a-user-with-client-certificate)\n  * [Set a context utilizing a specific config file](#set-a-context-utilizing-a-specific-config-file)\n  * [Set a context utilizing a specific username and namespace.](#set-a-context-utilizing-a-specific-username-and-namespace)\n- [Alias](#alias)\n  * [Create an alias on *nix](#create-an-alias-on--nix)\n  * [Create an alias on Windows](#create-an-alias-on-windows)\n- [Kubectl imperative (create) vs declarative (apply)](#kubectl-imperative--create--vs-declarative--apply-)\n  * [Create](#create)\n  * [Apply](#apply)\n\n\n\n\n\n## Pods\n\n### List all pods in namespace `\u003cdefault\u003e`\n\n```\nkubectl get pods\n```\n\nor \n\n```\nkubectl get pod\n```\n\nor\n\n```\nkubectl get po\n```\n\n### View a pod in watch mode\n\n```\nkubectl get pod \u003cpod\u003e --watch\n```\n\n### View all pods in watch mode\n\n```\nkubectl get pods -A --watch\n```\n\n### List sroted pods \n\n```\nkubectl get pods --sort-by='.status.containerStatuses[0].restartCount'\n```\n\n### List pods using a different output\n\n```\nkubectl get pods -o \u003cjson|yaml|wide|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=...\u003e\n```\n\nExamples: \n\n- JSON output\n\n```\nkubectl get pods -o json\n```\n\nor\n\n```\nkubectl get pods -ojson\n```\n\nor\n\n```\nkubectl get pods -o=json\n```\n\n- Wide output:\n\n```\nkubectl get pods -o wide\n```\n\n- Custom columns:\n\n```\nkubectl get pods -o custom-columns='DATA:spec.containers[*].image'\n```\n\nor \n\n```\nkubectl get pods -o custom-columns='DATA:spec.containers[*].volumeMounts'\n```\n\nor\n\n```\nkubectl get pods -o custom-columns='DATA:metadata.*'\n```\n\n\n### Formatting output\n\nTo 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))\n\nOutput format | Description\n--------------| -----------\n`-o=custom-columns=\u003cspec\u003e` | Print a table using a comma separated list of custom columns\n`-o=custom-columns-file=\u003cfilename\u003e` | Print a table using the custom columns template in the `\u003cfilename\u003e` file\n`-o=json`     | Output a JSON formatted API object\n`-o=jsonpath=\u003ctemplate\u003e` | Print the fields defined in a [jsonpath](/docs/reference/kubectl/jsonpath) expression\n`-o=jsonpath-file=\u003cfilename\u003e` | Print the fields defined by the [jsonpath](/docs/reference/kubectl/jsonpath) expression in the `\u003cfilename\u003e` file\n`-o=name`     | Print only the resource name and nothing else\n`-o=wide`     | Output in the plain-text format with any additional information, and for pods, the node name is included\n`-o=yaml`     | Output a YAML formatted API object\n\n### List all pods in a namespace\n\n```\nkubectl get pods -n \u003cnamespace\u003e\n```\n\nor \n\n```\nkubectl -n \u003cnamespace\u003e get pods \n```\n\nor \n\n```\nkubectl --namespace \u003cnamespace\u003e get pods \n```\n\n### List all pods in all namespaces\n\n```\nkubectl get pods --all-namespaces\n```\n\nor\n\n```\nkubectl get pods -A\n```\n\n### Create from an image\n\n```\nkubectl run \u003cpod\u003e --generator=run-pod/v1 --image=\u003cimage\u003e\n```\n\nIn the following cheatsheet, we will be using images such as nginx or busybox.\n\nExample:\n\n```\nkubectl run nginx --generator=run-pod/v1 --image=nginx\n```\n\n```\nkubectl run busybox --generator=run-pod/v1 --image=busybox\n```\n\n### Run pod in an interactive shell mode\n\n```\nkubectl run -i --tty nginx --image=nginx -- sh \n```\n\n### Run a command after creating a pod\n\n```\nkubectl run busybox --image=busybox -- sleep 100000\n```\n\n### Executing a command in a running pod\n\n```\nkubectl exec \u003cpod\u003e -- \u003ccommand\u003e\n```\n\nOr pass stdin to the container in TTY mode:\n\n```\nkubectl exec -it \u003cpod\u003e -- \u003ccommand\u003e\n```\n\nExample:\n\n```\nkubectl exec -it nginx -- ls -lrth /app/\n```\n\n### Create a pod: dry run mode (without really creating it)\n\n```\nkubectl run \u003cpod\u003e --generator=run-pod/v1 --image=nginx --dry-run \n```\n\n### Patch a pod\n\n```\nkubectl patch pod \u003cpod\u003e -p '\u003cpatch\u003e'\n```\n\nExample:\n\n```\nkubectl patch pod \u003cpod\u003e -p  '{\"spec\":{\"containers\":[{\"name\":\"kubernetes-serve-hostname\",\"image\":\"new image\"}]}}'\n```\n\nAnother example:\n\n```\nkubectl patch pod valid-pod --type='json' -p='[{\"op\": \"replace\", \"path\": \"/spec/containers/0/image\", \"value\":\"new image\"}]'\n```\n\n\n### Create from a YAML file\n\n```\nkubectl create -f pod.yaml\n```\n\n### Export YAML from the dry run mode\n\n```\nkubectl run nginx --generator=run-pod/v1 --image=nginx --dry-run -o yaml\n```\n\n### Create from STDIN\n\n```\ncat \u003c\u003cEOF | kubectl create -f -\napiVersion: v1\nkind: Pod\nmetadata:\n  name: nginx-pod\nspec:\n  containers:\n  - name: nginx\n    image: nginx:latest\nEOF\n```\n\n### Create multiple resources from STDIN\n\n```\ncat \u003c\u003cEOF | kubectl create -f -\napiVersion: v1\nkind: Pod\nmetadata:\n  name: nginx-pod\nspec:\n  containers:\n  - name: nginx\n    image: nginx:latest\n---\napiVersion: v1\nkind: Pod\nmetadata:\n  name: busybox\nspec:\n  containers:\n  - name: busybox\n    image: busybox\n    args:\n    - sleep\n    - \"100\"\n```\n\n### Create in a namespace\n\n```\nkubectl run nginx --generator=run-pod/v1 --image=nginx -n \u003cnamespace\u003e\n```\n\n### Create in a namespace from a file\n\n```\nkubectl create -f pod.yaml -n \u003cnamespace\u003e\n```\n\n### Delete pods\n\n```\nkubectl delete pod/\u003cpod\u003e\n```\n\nor \n\n```\nkubectl delete pod \u003cpod\u003e \n```\n\nIf you create the pod from a file, you can also use:\n\n```\nkubectl delete -f pod.yaml \n```\n\nTo force deletion:\n\n```\nkubectl delete pod \u003cpod\u003e --grace-period=0 --force\n```\n\n### Get pod logs\n\n```\nkubectl logs \u003cpod\u003e\n```\n\nor\n\nSometimes a pod contains more than 1 container. You need to filter the output to get logs for a specific container(s)\n\n```\nkubectl logs \u003cpod\u003e -c \u003ccontainer\u003e\n```\n\nTo follow the logs output (tail -f):\n\n```\nkubectl logs -f \u003cpod\u003e\n```\n\nIf you need to output the logs for all pods with a label\n\n```\nkubectl logs -l \u003clabel_name\u003e=\u003clabel_value\u003e\n```\n\nExample:\n\n```\nkubectl logs -l env=prod\n```\n\nYou can also view logs in a multi container case with labels:\n\n```\nkubectl logs -l \u003clabel_name\u003e=\u003clabel_value\u003e -c \u003ccontainer\u003e\n```\n\nOr view all cotainers logs with a given label:\n\n```\nkubectl logs -f -l \u003clabel_name\u003e=\u003clabel_value\u003e --all-containers\n```\n\n### List all container id of init container of all pods\n\n```\nkubectl get pods --all-namespaces -o jsonpath='{range .items[*].status.initContainerStatuses[*]}{.containerID}{\"\\n\"}{end}' | cut -d/ -f3\n```\n\n### Show metrics for a given pod\n\n```\nkubectl top pod \u003cpod\u003e\n```\n\n### Show metrics for a given pod and all its containers\n\n```\nkubectl top pod \u003cpod\u003e --containers               \n```\n\n## Deployments\n\n### Create a deployment\n\n```\nkubectl run \u003cdeployment\u003e --image=\u003cimage\u003e\n```\n\nor\n\n```\nkubectl create deployment \u003cdeployment\u003e --image=\u003cimage\u003e\n```\n\n### Create a deployment with a predefined replica number\n\n```\nkubectl run \u003cdeployment\u003e --image=\u003cimage\u003e --replicas=\u003cnumber\u003e\n```\n\n### Create a deployment with a predefined replica number and opening a port\n\n```\nkubectl run \u003cdeployment\u003e --image=\u003cimage\u003e --replicas=\u003creplicas\u003e --port=\u003cport\u003e\n```\n\nExample:\n\n```\nkubectl run nginx --image=nginx --replicas=2 --port=80\n```\n\n**Note**: The default generator for `kubectl run` is `--generator=deployment/apps.v1`.\n\n\n**Note**: `--generator=deployment/apps.v1` is deprecated and will be removed in future versions. Use kubectl run `--generator=run-pod/v1` or `kubectl create` instead.\n\n\n### Create a deployment with a predefined replica number, opening a port and exposing it\n\n```\nkubectl run nginx --image=nginx --replicas=2 --port=80 --expose\n```\n\n### Get a deployment\n\n```\nkubectl get deploy \u003cdeployment\u003e\n```\n\n### Watch a deployment\n\n```\nkubectl get deployment \u003cdeployment\u003e --watch\n```\n\nor \n\n```\nkubectl get deployment \u003cdeployment\u003e -w\n```\n\nOr using a shorter version:\n\n```\nkubectl get deploy \u003cdeployment\u003e -w\n```\n\nOr even the longer one:\n\n\n```\nkubectl get deployments.apps \u003cdeployment\u003e --watch\n```\n\n### List all deployments\n\nSame as listing pods, you have multiple options from namespace to output formatters:\n\n```\nkubectl get deploy -n \u003cnamespace\u003e\n\nkubectl get deploy --all-namespaces \nkubectl get deploy -A\n\nkubectl get deploy -oyaml\nkubectl get deploy -owide\n\n```\n\n### Update the image\n\n\nRolling update \"nginx\" containers of \"nginx\" deployment, updating the image:\n\n```\nkubectl set image deployment/nginx nginx=nginx:1.9.1\n```\n\nRolling update \"api\" containers of \"backend\" deployment, updating the image:\n\n```\nkubectl set image deployment/backend api=image:v2\n```\n\n### Scale a deployment\n\n```\nkubectl scale --replicas=5 deployment/\u003cdeployment\u003e\n```\n\n**Note**: You can use a shorter version: \n\n```\nkubectl scale --replicas=5 deploy/\u003cdeployment\u003e\n```\n\n### Dry run and YAML output\n\n```\nkubectl run nginx --image=nginx --replicas=2 --port=80 --dry-run -o yaml\n```\n\n### Create a deployment from a file\n\n```\nkubectl apply -f deployment.yaml\n```\n\n### Edit a deployment\n\n```\nkubectl edit deployment/\u003cdeployment\u003e\n```\n\n### Rollback deployment \n\nAfter editing your deployment, you had an error, a solution can be rolling back to the old deployment status:\n\n```\nkubectl rollout undo deployment \u003cdeployment\u003e\n```\n\n### Get rollout history\n\nYou can check the rollout history:\n\n```\nkubectl rollout history deployment \u003cdeployment\u003e\n```\n\n\n```\nkubectl rollout history deployment \u003cdeployment\u003e\n```\n\nExample:\n\n```\nkubectl rollout history deployment nginx\n```\n\ngives you:\n\n```\nREVISION  CHANGE-CAUSE\n2         kubectl set image deployment/nginx nginx=nginx:1.9.1 --record=true\n3         \u003cnone\u003e\n```\n\n### Roll back to a previous revision\n\nUsing the information from the rollout history, we can get back our deployment to a given revision:\n\n```\nkubectl rollout undo deployment \u003cdeployment\u003e --to-revision=\u003crevision\u003e\n```\n\nExample:\n\n```\nkubectl rollout undo deployment nginx --to-revision=2\n```\n\n### Execute deployment rollout operations\n\n```\nkubectl rollout status deployment \u003cdeployment\u003e\nkubectl rollout pause deployment \u003cdeployment\u003e\nkubectl rollout resume deployment \u003cdeployment\u003e\n```\n\n\n## Port Forwarding\n\n### Choosing localhost port\n```\nkubectl port-forward deployment \u003cdeployment\u003e  \u003clocahost-port\u003e:\u003cdeployment-port\u003e\nkubectl port-forward pod \u003cpod\u003e  \u003clocahost-port\u003e:\u003cpod-port\u003e\n```\n\nExample:\n\nForward to localhost 8090 from pod 6379:\n\n```\nkubectl port-forward redis 8090:6379\n```\n\n### Listening on the same port\n\n```\nkubectl port-forward pod \u003cpod\u003e \u003cport\u003e\n```\n\nExample: Listen on ports 8000 and 9000 on localhost, forwarded from the same ports in the pod (8000 and 9000)\n\n```\nkubectl port-forward pod nginx 8000 9000\n```\n\n### Listen on a random port locally\n\n```\nkubectl port-forward pod \u003cpod\u003e :\u003cpod-port\u003e\n```\n\nExample:\n\n```\nkubectl port-forward pod nginx :80\n```\n\n### Listen on port on localhost + another IP\n\n```\nkubectl port-forward --address localhost,\u003cIP.IP.IP.IP\u003e pod \u003cpod\u003e \u003clocahost-port\u003e:\u003cpod-port\u003e\n```\n\nExample:\n\n```\nkubectl port-forward --address localhost,10.10.10.1 pod redis 8090:6379\n```\n\n### Listen on a forwarded port on all addresses \n\n```\nkubectl port-forward --address 0.0.0.0 pod \u003cpod\u003e \u003chosts-port\u003e:\u003cpod-port\u003e\n```\n\n## Services\n\n### Create a service\n\n```\nkubectl create service \u003cclusterip|externalname|loadbalancer|nodeport\u003e \u003cservice\u003e [flags] [options]\u003e\n```\n\nExamples:\n\n```\nkubectl create service clusterip myclusterip --tcp=5678:8080\nkubectl create service  loadbalancer myloadbalancer --tcp=80\n```\n\nYou can use `svc` instead of `service`.\n\n### Delete service(s)\n\n```\nkubectl delete service myclusterip\nkubectl delete service myloadbalancer\n\nkubectl delete svc myclusterip\nkubectl delete svc myloadbalancer\n```\n\nor \n\n```\nkubectl delete service myclusterip myloadbalancer\n```\n\n### Describe a service\n\n```\nkubectl describe service \u003cservice\u003e\n```\n\n## Nodes\n\n### Get node\n\n```\nkubectl get nodes\n```\n\n### Get a specific node\n\n```\nkubectl get nodes \u003cnode\u003e\n```\n\n### Show node metrics\n\n```\nkubectl top node \u003cnode\u003e\n```\n\n### Get external IPs of cluster nodes\n\n```\nkubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type==\"ExternalIP\")].address}'\n```\n\n### Describe commands with verbose output\n\n```\nkubectl describe nodes \u003cnode\u003e\n```\n\n### Check which nodes are ready\n\n```\nJSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}' \u0026\u0026 kubectl get nodes -o jsonpath=\"$JSONPATH\" | grep \"Ready=True\"\n```\n\n### Mark a node as unschedulable\n\n```\nkubectl cordon \u003cnode\u003e\n```\n\n### Drain a node for maintenance\n\n```\nkubectl drain \u003cnode\u003e\n```\n\n### Mark a node as schedulable\n\n```\nkubectl uncordon \u003cnode\u003e\n```\n\n## Namespaces\n\n### List namespaces\n\n```\nkubectl get namespaces \n```\n\nor\n\n```\nkubectl get ns \n```\n\n### List or describe a namespace\n\n```\nkubectl get namespace \u003cnamespace\u003e\nkubectl describe namespace \u003cnamespace\u003e\n```\n\n### Create namespace\n\n```\nkubectl create namespace \u003cnamespace\u003e\n```\n\nor \n\n```\nkubectl create -f namespace.yaml\n```\n\nor \n\n```\ncat \u003c\u003cEOF | kubectl create -f -\napiVersion: v1\nkind: Namespace\nmetadata:\n  name: mynamespace\nEOF\n```\n\n### Delete namespace \n\n```\nkubectl delete namespace \u003cnamespace\u003e \n```\n\nor \n\n```\nkubectl delete -f namespace.yaml\n```\n\n## Service accounts\n\n### List service accounts\n\n```\nkubectl get serviceaccounts\n```\n\nor \n\n```\nkubectl get sa\n```\n\n### Get a service account\n\n```\nkubectl get serviceaccount \u003cserviceaccount\u003e\n```\n\nor\n\n```\nkubectl get serviceaccounts \u003cserviceaccount\u003e\n```\n\nor\n\n```\nkubectl get sa \u003cserviceaccount\u003e\n```\n\nor \n\n```\nkubectl get sa/\u003cserviceaccount\u003e\n```\n\n### Create a service account\n\n```\nkubectl create serviceaccount \u003cserviceaccount\u003e\n```\n\n### Delete a service account\n\n```\nkubectl delete serviceaccount \u003cserviceaccount\u003e \n```\n\nor \n\n```\nkubectl delete -f myserviceaccount.yaml\n```\n\n### Describe a service account\t\n\n```\nkubectl describe serviceaccount \u003cserviceaccount\u003e \n```\n\n## Events\n\n### List events\n\n```\nkubectl get events -A\n```\n\n### List sorted events\n\n```\nkubectl get events --sort-by=\u003cJSONPath\u003e\n```\n\nExample: Sorted by timestamp\n\n```\nkubectl get events --sort-by=.metadata.creationTimestamp\n```\n\n### List formatted events\n\n```\nkubectl get events -o \u003cjson|yaml|wide|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=...\u003e\n```\n\nExample:\n\n```\nkubectl get events -owide\n```\n\n## Documentation\n\n\n### Get the documentation for pod manifests\n\n```\nkubectl explain pod\n```\n\n### Get the documentation for service manifests\n\n```\nkubectl explain service\n```\n\n## Describing resources\n\n```\nkubectl describe \u003cresource\u003e \u003creosurce_name\u003e\n```\n\nExample:\n\n```\nkubectl describe pod busybox\n```\n\nor \n\n\n```\nkubectl describe nodes minikube \n```\n\nOther possible resources you can use with `describe`:\n\n```\napiservices.apiregistration.k8s.io\ncertificatesigningrequests.certificates.k8s.io\nclusterrolebindings.rbac.authorization.k8s.io\nclusterroles.rbac.authorization.k8s.io\ncomponentstatuses\nconfigmaps\ncontrollerrevisions.apps\ncronjobs.batch\ncsidrivers.storage.k8s.io\ncsinodes.storage.k8s.io\ncustomresourcedefinitions.apiextensions.k8s.io\ndaemonsets.apps\ndaemonsets.extensions\ndeployments.apps\ndeployments.extensions\nendpoints\nevents\nevents.events.k8s.io\nhorizontalpodautoscalers.autoscaling\ningresses.extensions\ningresses.networking.k8s.io\njobs.batch\nleases.coordination.k8s.io\nlimitranges\nmutatingwebhookconfigurations.admissionregistration.k8s.io\nnamespaces\nnetworkpolicies.extensions\nnetworkpolicies.networking.k8s.io\nnodes\npersistentvolumeclaims\npersistentvolumes\npoddisruptionbudgets.policy\npods\npodsecuritypolicies.extensions\npodsecuritypolicies.policy\npodtemplates\npriorityclasses.scheduling.k8s.io\nreplicasets.apps\nreplicasets.extensions\nreplicationcontrollers\nresourcequotas\nrolebindings.rbac.authorization.k8s.io\nroles.rbac.authorization.k8s.io\nruntimeclasses.node.k8s.io\nsecrets\nserviceaccounts\nservices\nstatefulsets.apps\nstorageclasses.storage.k8s.io\nvalidatingwebhookconfigurations.admissionregistration.k8s.io\nvolumeattachments.storage.k8s.io\n```\n\n## Editing resources\n\n### Edit a service\n\n```\nkubectl edit service \u003cservice\u003e                    \n```\n\n### Edit a service with your favorite text editor\n\n```\nKUBE_EDITOR=\"vim\" edit service \u003cservice\u003e  \n```\n\n**Note**: Change `service` by any editable resource type like pods.\n\n## Deleting Resources\n\n### Delete a resource using the type and name specified in `\u003cfile\u003e`\n\n```\nkubectl delete -f \u003cfile\u003e      \n```\n\n### Delete pods and services with same names \n\n```\nkubectl delete pod,service \u003cname1\u003e \u003cname2\u003e\n```\n\n### Delete pods and services with a custom label\n\n```\nkubectl delete pods,services -l \u003clabel-name\u003e=\u003clabel-value\u003e\n```\n\n### Delete all pods and services in a namespace\n\n```  \nkubectl -n \u003cnamespace\u003e delete pods,services --all              \n```\n\n### Delete all resources in a namespace\n\n```\nkubectl delte \u003cnamespace\u003e\n```\n\n## All get commands \n\n```\nkubectl get all\nkubectl get pods\nkubectl get replicasets\nkubectl get services\nkubectl get nodes\nkubectl get namespaces\nkubectl get configmaps\nkubectl get endpoints \n```\n\n## Abbreviations / Short forms of resource types\n\n|      Resource type       |   Abbreviations   |\n|--------------------------|-------------------|\n| componentstatuses        | cs                |\n| configmaps               | cm                |\n| daemonsets               | ds                |\n| deployments              | deploy            |\n| endpoints                | ep                |\n| event                    | ev                |\n| horizontalpodautoscalers | hpa               |\n| ingresses                | ing               |\n| limitranges              | limits            |\n| namespaces               | ns                |\n| nodes                    | no                |\n| persistentvolumeclaims   | pvc               |\n| persistentvolumes        | pv                |\n| pods                     | po                |\n| podsecuritypolicies      | psp               |\n| replicasets              | rs                |\n| replicationcontrollers   | rc                |\n| resourcequotas           | quota             |\n| serviceaccount           | sa                |\n| services                 | svc               |\n\n\n## Verbose Kubectl\n\n```\nkubectl run nginx  --image=nginx --v=5\n```\n\nVerbosity | Description\n--------------| -----------\n`--v=0` | Generally useful for this to *always* be visible to a cluster operator.\n`--v=1` | A reasonable default log level if you don't want verbosity.\n`--v=2` | Useful steady state information about the service and important log messages that may correlate to significant changes in the system. This is the recommended default log level for most systems.\n`--v=3` | Extended information about changes.\n`--v=4` | Debug level verbosity.\n`--v=6` | Display requested resources.\n`--v=7` | Display HTTP request headers.\n`--v=8` | Display HTTP request contents.\n`--v=9` | Display HTTP request contents without truncation of contents.\n\n\n(Table source: [K8s docs](https://kubernetes.io/docs))\n\n\n## Cluster\n\n### Display addresses of the master and services\n\n```\nkubectl cluster-info                        \n```\n\n### Dump cluster state to STDOUT\n\n```\nkubectl cluster-info dump           \n```\n\n### Dump cluster state to a file\n\n```\nkubectl cluster-info dump --output-directory=\u003c/file/path\u003e\n```\n\n### Compares the current cluster state against the state that the cluster would be in if the manifest was applied\n\n```\nkubectl diff -f ./my-manifest.yaml\n```\n\n### List all images running in a cluster\n\n```\nkubectl get pods -A -o=custom-columns='DATA:spec.containers[*].image'\n```\n\n## Kubectl context\n\n### Show merged kubeconfig settings\n\n```\nkubectl config view \n```\n\n### Use multiple kubeconfig \n\n```\nKUBECONFIG=~/.kube/config1:~/.kube/config2:~/.kube/config3\n```\n\n#### Get a list of users\n\n```\nkubectl config view -o jsonpath='{.users[*].name}'\n```\n\n### Display the first user\n\n```\nkubectl config view -o jsonpath='{.users[].name}'\n```\n\n### Get the password for the \"admin\" user\n\n```\nkubectl config view -o jsonpath='{.users[?(@.name == \"admin\")].user.password}'\n```\n\n#### Display the current context\n\n```\nkubectl config current-context\n```\n\n#### Display list of contexts \n\n```\nkubectl config get-contexts\n```\n\n#### Set the default context to `\u003ccluster\u003e`\n\n```\nkubectl config use-context \u003ccluster\u003e\n```\n\n### Sets a user entry in kubeconfig\n\n```\nkubectl config set-credentials \u003cusername\u003e [options]\n```\n\n### Sets a user with a client key\n\n```\nkubectl config set-credentials \u003cuser\u003e --client-key=~/.kube/admin.key\n```\n\n### Sets a user with basic auth\n\n```\nkubectl config set-credentials --username=\u003cusername\u003e --password=\u003cpassword\u003e\n```\n\n### Sets a user with client certificate\n\n```\nkubectl config set-credentials \u003cuser\u003e --client-certificate=\u003cpath/to/cert\u003e --embed-certs=true\n```\n\n### Set a context utilizing a specific config file\n\n```\nkubectl config --kubeconfig=\u003cconfig/path\u003e use-context \u003ccluster\u003e\n```\n\n### Set a context utilizing a specific username and namespace.\n\n```\nkubectl config set-context gce --user=cluster-admin --namespace=foo \\\n  \u0026\u0026 kubectl config use-context gce\n```\n\n##  Alias\n\n### Create an alias on *nix\n\n```\nalias k=kubectl\n```\n\n### Create an alias on Windows\n\n```\nSet-Alias -Name k -Value kubectl\n```\n\n## Kubectl imperative (create) vs declarative (apply)\n\n### Create\n\nYou tell your cluster what you want to create, replace or delete, not how you want you it to look like.\n\n```\nkubectl create -f \u003cfilename|url\u003e\n\nkubectl delete deployment \u003cdeployment-name\u003e\nkubectl delete deployment \u003cdeployment-filename\u003e\nkubectl delete deployment \u003cdeployment-url\u003e\n```\n\n### Apply\n\nYou tell your cluster how you want it to look like.\n\nThe creation, deletion and modification of objects is done via a single command. The declarative approach is a statement of the desired end result. \n\n\n```\nkubectl apply -f \u003cfilename|url\u003e\n\nkubectl delete -f \u003cdeployment-filename\u003e\nkubectl apply -f \u003cdeployment-filename\u003e\n```\n\nIf the deployment is deleted in `\u003cdeployment-filename\u003e`, it will also be deleted from the cluster.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feon01%2FKubectlCheatSheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feon01%2FKubectlCheatSheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feon01%2FKubectlCheatSheet/lists"}