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

Scope of all

AI-generated

all is a convenient category, not every resource type in the namespace. Query the specific type when completeness matters.

Describing resource

kubectl describe [TYPE] [NAME]

Deleting resource

kubectl delete [TYPE] [NAME]

Formatting output

Usage: kubectl [command] [TYPE] [NAME] -o \<output-format>

  • -o json output JSON formatted API object
  • -o name output only name of the resource
  • -o wide output in plaintext format with additional information
  • -o yaml output YAML formatted object

Examples:

kubectl create namespace test-123 --dry-run=client -o json

kubectl create namespace test-123 --dry-run=client -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: kubectx needs 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 <local-port>:<pod-port>

Imperative Commands (Single command to create resources)

Task Command
Create Pod kubectl run nginx --image=nginx
Create Pod Manifest YAML (-o yaml), don't create (--dry-run) kubectl run nginx --image=nginx --dry-run=client -o yaml
Create Deployment kubectl create deployment nginx --image=nginx --replicas=4
Create Deployment Manifest YAML (-o yaml), don't create (--dry-run) kubectl create deployment nginx --image=nginx --replicas=4 --dry-run=client -o yaml
Generate a YAML file out of YAML manifests [kubectl-command] -o yaml > manifest.yaml
Creating a Service - 1 kubectl expose pod redis --port=6379 --name redis-service --dry-run=client -o yaml
Creating a Service - 2 (this will not use pods label as selector, instead assume selector as app=redis) kubectl create service clusterip redis --tcp=6379:6379 --dry-run=client -o yaml
Creating NodePort Service to expose pod nginx's port 80 on port 30080 on the nodes kubectl expose pod nginx --port=80 --name nginx-service --type=NodePort --dry-run=client -o yaml
NodePort 2 kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yaml