Kubernetes

What is Kubernetes?

Kubernetes (K8s) is an open-source system for automating deployment, scaling, and management of containerized applications.

It groups containers that make up an application into logical units for easy management and discovery. Kubernetes builds upon 15 years of experience of running production workloads at Google, combined with best-of-breed ideas and practices from the community. [Ref]

 

Gather info

  • kubectl get pods List all pods in the namespace
  • kubectl get pods --all-namespaces List all pods in all namespaces
  • kubectl get pods -o wide List all pods in the current namespace, with more details
  • kubectl get pod my-pod -o yaml Get a pod's YAML
  • kubectl get pods --show-labels Show labels for all pods (or any other Kubernetes object that supports labelling)
  • kubectl get services List all services in the namespace
  • kubectl get deployment my-deployment List a particular deployment
  • kubectl get events --sort-by=.metadata.creationTimestamp List Events sorted by timestamp

Delete Resources

  • kubectl delete pod my-pod Delete particular pod
  • kubectl delete deployment my-deployment Delete particular deployment
  • kubectl delete service my-service Delete particulaer service
  • kubectl -n my-namespace delete pod,svc --all Delete all pods and services in namespace my-namespace
  • kubectl delete -f . Delete all resources for the existing manifests inside the folder (i.e current path)

PODs

  • kubectl logs my-pod Dump pod logs (stdout)
  • kubectl logs my-pod --previous Dump pod logs (stdout) for a previous instantiation of a container
  • kubectl logs my-pod -c my-container Dump pod container logs (stdout, multi-container case)
  • kubectl logs -f my-pod -c my-container Stream pod container logs (stdout, multi-container case)
  • kubectl logs -f -l name=myLabel --all-containers Stream all pods logs with label name=myLabel (stdout)
  • kubectl run -i --tty busybox --image=busybox -- sh Run pod as interactive shell
  • kubectl run nginx --image=nginx --dry-run=client -o yaml > pod.yaml Run pod nginx and write its spec into a file called pod.yaml
  • kubectl port-forward my-pod 5000:6000 Listen on port 5000 on the local machine and forward to port 6000 on my-pod
  • kubectl exec my-pod -- ls / Run command in existing pod (1 container case)
  • kubectl exec my-pod -c my-container -- ls / Run command in existing pod (multi-container case)
  • kubectl top pod POD_NAME --containers Show metrics for a given pod and its containers

Nodes

  • kubectl get nodes List all nodes
  • kubectl get nodes -o=wide List all nodes with extented info
  • kubectl get nodes --show-labels List all nodes including the labels
  • kubectl cordon my-node Mark my-node as unschedulable
  • kubectl drain my-node Drain my-node in preparation for maintenance
  • kubectl uncordon my-node Mark my-node as schedulable
  • kubectl top node my-node Show metrics for a given node

Deployments

  • kubectl create deployment nginx --image=nginx Start a single instance of nginx
  • kubectl get deployment my-deployment List a particular deployment
  • kubectl rollout restart deployment/frontend Rolling restart of the "frontend" deployment
  • kubectl rollout status -w deployment/frontend Watch rolling update status of "frontend" deployment until completion
  • kubectl rollout undo deployment/frontend --to-revision=2 Rollback to a specific revision
  • kubectl rollout undo deployment/frontend Rollback to the previous deployment
  • kubectl rollout history deployment/frontend Check the history of deployments including the revision
  • kubectl autoscale deployment foo --min=2 --max=10 Auto scale a deployment "foo"
  • kubectl port-forward deploy/my-deployment 5000:6000 Listen on local port 5000 and forward to port 6000 on a Pod created by <my-deployment>
  • kubectl exec deploy/my-deployment -- ls run command in first Pod and first container in Deployment (single- or multi-container cases)

CronJobs/Jobs

  • kubectl create cronjob hello --image=busybox --schedule="*/1 * * * *" -- echo "Hello World" create a CronJob that prints "Hello World" every minute
  • create a Job which prints "Hello World" kubectl create job hello --image=busybox -- echo "Hello World"
  • kubectl patch cronjobs my-cron -p '{"spec" : {"suspend" : false }}' unsuspend a specific cronjob
  • kubectl patch cronjobs pph-payments-listener-cron -p '{"spec" : {"suspend" : true }}' suspend a specific cronjob