Kubectl Cheat Sheet#
Creating Resource#
Description |
Command |
|---|---|
With YAML resource definition file |
kubectl create -f resource-def-file.yaml |
With command line details |
kubectl create [TYPE] [NAME] –[additional details=value] |
Getting resource#
kubectl get [TYPE] [NAME]
Getting “all” resources
kubectl get all
Describing resource#
kubectl describe [TYPE] [NAME]
Deleting resource#
kubectl delete [TYPE] [NAME]
Formatting output#
Usage: kubectl [command] [TYPE] [NAME] -o <output-format>
-o jsonoutput JSON formatted API object-o nameoutput only name of the resource-o wideoutput in plaintext format with additional information-o yamloutput YAML formatted object
Examples:
kubectl create namespace test-123 --dry-run -o json
kubectl create namespace test-123 --dry-run -o yaml
# Wide
kubectl get pods -o wide
AWS EKS Cluster#
Adding cluster to kubectl#
aws eks update-kubeconfig --region <region> --name <cluster-name>
Switching clusters#
kubectx <cluster-arn>
NOTE:
kubectxneeds to be installed first!
Resource Labels#
Adding label#
kubectl label <resource-type> <resource-name> <label-name>=<label-value>
Removing label / Unlabelling#
kubectl label <resource-type> <resource-name> <label-name>-
Port Forward#
kubectl port-forward mongo-75f59d57f4-4nd6q <external/local-port>:<pod-port>
Imperative Commands (Single command to create resources)#
Task |
Command |
|---|---|
Create Pod |
|
Create Pod Manifest YAML (-o yaml), don’t create (–dry-run) |
|
Create Deployment |
|
Create Deployment Manifest YAML (-o yaml), don’t create (–dry-run) |
|
Generate a YAML file out of YAML manifests |
|
Creating a Service - 1 |
|
Creating a Service - 2 (this will not use pods label as selector, instead assume selector as app=redis) |
|
Creating NodePort Service to expose pod nginx’s port 80 on port 30080 on the nodes |
|
NodePort 2 |
|