Are you diving into the world of Kubernetes and feeling overwhelmed by the numerous concepts and commands? Don’t worry, we’ve got you covered with a comprehensive Kubernetes cheat sheet that will help you navigate through some of the most commonly used elements. Whether you’re just starting or looking for a quick reference guide, this cheat sheet will make your Kubernetes journey smoother.

Pods

A pod is the smallest deployable unit in Kubernetes, representing a single instance of a process. Here’s how to work with pods:

  • Create a Pod: kubectl create pod <pod-name> --image=<image-name>
  • List Pods: kubectl get pods
  • Describe a Pod: kubectl describe pod <pod-name>
  • Delete a Pod: kubectl delete pod <pod-name>

Deployments

Deployments manage the lifecycle of pods, ensuring desired replicas are running. Here’s what you need to know:

  • Create a Deployment: kubectl create deployment <deployment-name> --image=<image-name>
  • List Deployments: kubectl get deployments
  • Scale a Deployment: kubectl scale deployment <deployment-name> --replicas=<desired-replicas>
  • Update a Deployment: kubectl set image deployment/<deployment-name> <container-name>=<new-image>
  • Rollback a Deployment: kubectl rollout undo deployment/<deployment-name>

Services

Services provide networking and load balancing to pods. Here’s how to manage them:

  • Create a Service: kubectl create service <service-type> <service-name> --tcp=<port>:<target-port>
  • List Services: kubectl get services
  • Expose a Deployment as a Service: kubectl expose deployment <deployment-name> --port=<port> --target-port=<target-port> --type=<service-type>

ConfigMaps and Secrets

ConfigMaps and Secrets manage configuration and sensitive information. Here’s how to use them:

  • Create a ConfigMap: kubectl create configmap <configmap-name> --from-file=<file-path>
  • Create a Secret: kubectl create secret generic <secret-name> --from-literal=<key>=<value>
  • List ConfigMaps: kubectl get configmaps
  • List Secrets: kubectl get secrets

Namespace

Namespaces provide isolation within a cluster. Here’s how to manage them:

  • Create a Namespace: kubectl create namespace <namespace-name>
  • List Namespaces: kubectl get namespaces
  • Switch Namespace: kubectl config set-context --current --namespace=<namespace-name>

Pod Logs and Port Forwarding

Accessing pod logs and forwarding ports are essential for troubleshooting:

  • View Pod Logs: kubectl logs <pod-name>
  • Stream Pod Logs: kubectl logs -f <pod-name>
  • Forward Local Port to Pod: kubectl port-forward <pod-name> <local-port>:<pod-port>

Exec into a Pod

Execute commands inside a pod to perform tasks:

  • Execute Command in Pod: kubectl exec -it <pod-name> -- <command>

Labels and Selectors

Labels and selectors enable grouping and filtering resources:

  • Add Labels to a Resource: kubectl label <resource-type> <resource-name> <key>=<value>
  • List Resources with Labels: kubectl get <resource-type> -l <key>=<value>

Pod Affinity and Anti-affinity

Control pod placement within your cluster:

  • Set pod affinity and anti-affinity using the affinity and affinity.podAntiAffinity fields in the pod’s spec.

Resource Limits and Requests

Define resource constraints for pods:

  • Set limits and requests in the pod’s resources section.

Pod Lifecycle

Execute commands before termination or after startup:

  • Use preStop and postStart hooks in the pod’s spec.

Kubernetes is a powerful orchestration platform, and this cheat sheet provides a foundational understanding of its key concepts and commands. Remember that this is just a starting point – delve into the official Kubernetes documentation for more in-depth insights and examples. Whether you’re managing pods, deployments, services, or namespaces, this cheat sheet will be your trusty companion as you navigate the Kubernetes landscape. Happy orchestrating!