Skip to content

Instantly share code, notes, and snippets.

@nasirhafeez
Last active May 29, 2022 11:02
Show Gist options
  • Save nasirhafeez/3644d76d00c73dc5dfadb90cfe1e8b56 to your computer and use it in GitHub Desktop.
Save nasirhafeez/3644d76d00c73dc5dfadb90cfe1e8b56 to your computer and use it in GitHub Desktop.
Kubernetes Commands

Kubernetes Commands

List Pods

kubectl get pod
kubectl describe pod <pod_name>
kubectl get po -o wide

Delete Pods

Get deployments

kubectl get deployments --all-namespaces

Delete deployment

kubectl delete -n <namespace> deployment <deployment>

Access Pod

sudo kubectl exec -it <pod_name> -- /bin/sh
sudo kubectl exec -it <pod_name> -c <container_name> -- /bin/sh     // in case of multiple containers in a pod

Logging

kubectl logs <pod_name> <container_name>

Explain

kubectl explain <resource type like pod, rs>
kubectl explain pod
kubectl explain pod.metadata

Get YAML/JSON from pod

kubectl get po myfirstpod -o yaml
kubectl get po myfirstpod -o json

Create POD using yaml

kubectl create -f myfirstpod.yaml

Create POD using command line

kubectl run <pod_name> --image aamirpinger/helloworld --port 80 --restart=Never

Port Forwarding

kubectl port-forward <pod_name> 8080:80

Labels

kubectl label pod myfirstpod123 app=helloworld [--overwrite]
kubectl get pod --show-labels
kubectl get pod -L run,env,type

Filtering based on labels

kubectl get pod -l type=frontend
kubectl get pod -l type=!backend

Namespaces

kubectl get ns
kubectl get po -n <ns name>
kubectl get po --all-namespaces
kubectl delete ns <ns name>
kubectl delete pod --all

Edit Resoure

kubectl edit <resource type> <resource name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment