Kubectl Commands
Inderjeet Singh
SRE @Genpact | ?? AWS Cloud & DevOps Engineer | ?? Kubernetes | ?? Terraform | ???? Python Enthusiast | ?? Genpact Green Belt | ?? YouTuber & Content Creator | ?? Building Scalable Solutions
Kubectl Commands - Quick Reference for dealing with K8s cluster
Creating objects
Kubernetes manifests can be defined in YAML or JSON. The file extension .yaml, .yml, and .json can be used.
kubectl apply -f ./my-manifest.yaml # create resource(s)
kubectl apply -f ./my1.yaml -f ./my2.yaml # create from multiple files
kubectl apply -f ./dir # create resource(s) in all manifest files in dir
kubectl create deployment nginx --image=nginx # start a single instance of nginx
# create a Job which prints "Hello World"
kubectl create job hello --image=busybox:1.28 -- echo "Hello World"
# create a CronJob that prints "Hello World" every minute
kubectl create cronjob hello --image=busybox:1.28 --schedule="*/1 " -- echo "Hello World"
# get the documentation for pod manifests
kubectl explain pods
Viewing and finding resources
# Get commands with basic output
kubectl get services # List all services in the namespace
kubectl get pods -A # List all pods in all namespaces
kubectl get pods -o wide # List all pods with more details
kubectl get deployment my-dep # List a particular deployment
kubectl get pods # List all pods in the namespace
kubectl get pod my-pod -o yaml # Get a pod's YAML
kubectl get pods -n namespace # Get pods in specific namespace
# Describe commands
领英推荐
kubectl describe nodes my-node
kubectl describe pods my-pod
# List Services Sorted by Name
kubectl get services --sort-by=.metadata.name
# List PersistentVolumes
kubectl get pv
# Get all running pods in the all namespaces
kubectl get pods -A | egrep -i "Completed | Running"
#Get Failed pods in all namespaces
kubectl get pods -A | egrep -iv "Completed | Running"
Interacting with running Pods
kubectl logs my-pod # dump pod logs (stdout)
kubectl logs -l name=myLabel # dump pod logs, with label
kubectl logs -f my-pod # stream pod logs (stdout)
kubectl logs -f my-pod -c my-container # stream pod specified container logs
Interacting with Deployments and Services
kubectl logs deploy/my-deployment # dump Pod logs for a Deployment
kubectl logs deploy/my-deployment -c my-container # dump Pod logs for a Deployment (multi-container case)
kubectl exec -it <pod_name> -c <container_name> — /bin/bash # login into container
Please share with others and like if this this is helpful!
#kubernetes #K8s #kubectl #devops #SRE