Kubernetes Cheat Sheet (2023) for Freshers and Experienced

Topics Covered

Overview

Kubernetes has evolved as the de facto container orchestration standard, providing efficient containerized application administration and scaling. We've developed a detailed Kubernetes cheat sheet to help you learn faster and run your organization more efficiently. This blog provides an abstract of the cheat sheet, which covers key terms, fundamental commands, and critical ideas that any Kubernetes user should be familiar with.

kubernetes-cheat-sheet

Kubernetes Cheatsheet

Kubernetes Terminologies

TermDefinition
ClusterA group of nodes that collectively manage and run containerized applications in Kubernetes.
NodeA worker machine in the cluster responsible for running containers.
PodsThe smallest deployable units in Kubernetes, containing one or more containers sharing the same network and storage.
Kube SchedulerResponsible for scheduling Pods on worker nodes based on resource availability and constraints.
API ServerServes as the Kubernetes control plane's front end, allowing users and other components to interact with the cluster.
KubeletManages and runs containers on individual nodes, ensuring they maintain the desired state defined in the cluster's control plane.
Kube ProxyHandles network routing and load balancing for Services, enabling communication between Pods.
EtcdA distributed key-value store that stores cluster data, providing a source of truth for the Kubernetes control plane.
Controller ManagerManages various controllers, like ReplicaSets and Deployments, ensuring the desired state of resources in the cluster.
Container RuntimeThe software responsible for running containers, such as Docker or Containers.

Kubernetes Objects

In simple terms, a Kubernetes object is like a recipe that tells Kubernetes how to create and manage a specific component, like a container or a service, within the cluster. It helps Kubernetes understand what you want and takes care of the rest, ensuring your applications run smoothly.

ObjectDescription
PodThe smallest deployable unit in Kubernetes, encapsulating one or more containers and associated resources.
Service Abstracts access to applications running in Pods and provides a stable endpoint for intra-cluster or external communication.
VolumeProvides persistent storage for Pods, allowing data to survive Pod restarts and ensuring data durability.
NamespaceSegregates resources within a cluster, allowing teams or applications to operate independently without interference.

High-Level Objects (Controllers):

ObjectDescription
DeploymentManages replica Pods, enabling rolling updates and rollbacks while ensuring application availability and fault tolerance.
Replication ControllerEnsures a specified number of replica Pods are running and replaces failed Pods.
ReplicaSetAn improved version of Replication Controllers, maintaining a stable set of replica Pods with enhanced selector options.
StatefulSetEnsures ordered, unique Pods for stateful applications, such as databases, enabling stable network identities.
Job Manages Pods for batch processing, running until successful completion, suitable for one-off tasks or cron-like jobs.

This Kubernetes cheatsheet serves as a handy reference, providing essential information about objects and concepts to help you efficiently deploy and scale containerized applications.

Kubernetes Commands

Kubernetes commands, commonly used with the kubectl CLI (Kubernetes Control Tool), are essential for interacting with the Kubernetes cluster. Here are some of the most commonly used Kubernetes commands:

  1. kubectl create: Create a resource from a file or stdin. Example: kubectl create deployment nginx --image=nginx

  2. kubectl apply: Apply changes to resources using a configuration file. Example: kubectl apply -f deployment.yaml

  3. kubectl get: Retrieve resources of a specific type. Example: kubectl get pods or `kubectl get services

  4. kubectl describe: Show detailed information about a resource. Example: kubectl describe pod my-pod

  5. kubectl delete: Delete resources by filenames, stdin, resources, and names. Example: kubectl delete pod my-pod or kubectl delete -f deployment.yaml

  6. kubectl edit: Edit resources in the default editor. Example: kubectl edit deployment nginx

  7. kubectl logs: Print logs from a container in a Pod. Example: kubectl logs my-pod

  8. kubectl exec: Execute a command on a container in a Pod. Example: kubectl exec -it my-pod -- /bin/bash

  9. kubectl port-forward: Forward one or more local ports to a Pod. Example: kubectl port-forward my-pod 8080:80

  10. kubectl rollout: Manage rollouts of Deployments. Example: kubectl rollout status deployment/nginx

  11. kubectl scale: Scale the number of replicas of a resource. Example: kubectl scale deployment nginx --replicas=3

  12. kubectl expose: Expose a resource as a new Service. Example: kubectl expose deployment nginx --port=80 --target-port=8080

  13. kubectl apply -f: Apply changes to resources using configuration files. Example: kubectl apply -f deployment.yaml

  14. kubectl get nodes: List all nodes in the cluster. Example: kubectl get nodes

  15. kubectl get pods: List all Pods in the cluster. Example: kubectl get pods

  16. kubectl get services: List all Services and their assigned IPs. Example: `kubectl get services

  17. kubectl get deployments: List all Deployments in the cluster. Example: `kubectl get deployments

  18. kubectl get namespaces: List all namespaces in the cluster. Example: kubectl get namespaces

Kubectl Context: Managing Configurations

1. View Current Context

2. List Available Contexts

3. Switch Context

4. Set Default Namespace for a Context

5. Create a New Context

6. Delete a Context

7. Set Cluster Credentials for a Context

8. Change Cluster Server for a Context

9. Change Context for a Specific Command (temporary)

10. Switch to a Different Configuration File

By default, kubectl uses the configuration file located at ~/.kube/config. You can specify a different configuration file using the --kubeconfig flag.

11. Reset Context

If you want to revert to the default context (default configuration), you can use the following command:

These are just a few of the many commands available in Kubernetes. The kubectl CLI offers a wide range of functionalities for managing resources, inspecting the cluster, and performing various administrative tasks. For more such commands you can refer official Kubernetes cheatsheet as well.

Changing Resource Attributes in Kubernetes

In Kubernetes, you can change resource attributes using the kubectl edit command or by updating the resource's YAML file and applying the changes with kubectl apply.

Here's how you can change resource attributes using both methods:

Method 1: Using kubectl edit

  1. Get the resource you want to modify. For example, to edit a Deployment named "my-deployment," use the following command:
  1. Open the resource's YAML definition in the default editor (usually set by the KUBE_EDITOR environment variable or defaulting to vi).
  1. Modify the desired attributes in the YAML file and save the changes. The editor will automatically update the resource.

  2. Kubernetes will detect the changes and automatically apply them to the resource.

Method 2: Using kubectl apply with an updated YAML file

  1. Get the resource's YAML definition using kubectl get with the -o yaml flag. For example, to get the YAML of a Deployment named "my-deployment":
  1. Open the deployment. yaml file in a text editor and modify the desired attributes.

  2. Save the changes in the YAML file.

  3. Apply the updated YAML to the resource using kubectl apply:

Cluster Introspection command

Cluster introspection commands in Kubernetes allow you to inspect the current state of the cluster, retrieve information about its components, and gather insights into the health and status of various resources. These commands are invaluable for troubleshooting, monitoring, and understanding the overall health of the cluster. Here are some essential cluster introspection commands using kubectl:

  1. kubectl get nodes: List all the nodes in the cluster, along with their status and other details.
  1. kubectl describe node : Display detailed information about a specific node, including capacity, allocatable resources, conditions, and more.
  1. kubectl get pods --all-namespaces: List all Pods across all namespaces in the cluster.
  1. kubectl describe pod -n : View detailed information about a specific Pod, including events, labels, and annotations.
  1. kubectl get services --all-namespaces: List all Services across all namespaces in the cluster.
  1. kubectl describe service -n : Get detailed information about a specific Service, including endpoints and selectors.
  1. kubectl get deployments --all-namespaces: List all Deployments across all namespaces in the cluster.
  1. kubectl describe deployment -n : View detailed information about a specific Deployment, including replicas, strategy, and events.
  1. kubectl get configmaps --all-namespaces: List all ConfigMaps across all namespaces in the cluster.
  1. kubectl describe configmap -n : Display detailed information about a specific ConfigMap, including data and associated resources.

These cluster introspection Kubernetes commands help you gain visibility into the state of various components, resources, and objects within the Kubernetes cluster.

Deployment and Service Commands in Kubernetes

In Kubernetes, Deployments and Services are essential objects for managing and exposing applications. Here are some commonly used Kubernetes commands for working with Deployments and Services using kubectl:

Deployment Commands:

  1. Create a Deployment:

Example: kubectl create deployment nginx-deployment --image=nginx:latest

  1. Scale a Deployment:

Example: kubectl scale deployment nginx-deployment --replicas=3

  1. Update a Deployment:

Example: kubectl set image deployment/nginx-deployment nginx=nginx:1.19.0

  1. Rollout Status of a Deployment:

Example: kubectl rollout status deployment/nginx-deployment

  1. Undo a Deployment Update (Rollback):

Example: kubectl rollout undo deployment/nginx-deployment

  1. Pause and Resume a Deployment Rollout:

Example:

  1. Describe a Deployment:

Example: kubectl describe deployment nginx-deployment

  1. Delete a Deployment:

Example: kubectl delete deployment nginx-deployment

Service Commands:

  1. Create a Service:

Example: kubectl create service clusterip my-service --tcp=80:8080

Note: <service-type> can be clusterip, nodeport, loadbalancer, or externalname.

  1. Expose a Deployment as a Service:

Example: kubectl expose deployment nginx-deployment --type=ClusterIP --port=80 --target-port=8080

  1. Get Services:
  1. Describe a Service:

Example: kubectl describe service my-service

  1. Delete a Service:

Example: kubectl delete service my-service

These Kubernetes commands will allow you to create, manage, and interact with Deployments and Services in Kubernetes. They are fundamental for deploying and exposing applications in a Kubernetes cluster effectively.

Commands to Copy Files and Directories

In Kubernetes, you can use the kubectl cp command to copy files and directories between a container and your local machine or between containers in the cluster. Here are the basic commands for copying files and directories:

  1. Copy from a local machine to a container:

Example: kubectl cp myfile.txt my-namespace/my-pod:/tmp/myfile.txt

  1. Copy from a container to a local machine:

Example: kubectl cp my-namespace/my-pod:/tmp/myfile.txt ~/Downloads/myfile.txt

  1. Copy between containers in the same pod:

Example: kubectl cp my-namespace/my-pod:/tmp/sourcefile.txt my-namespace/my-pod:/tmp/destinationfile.txt

Note: Make sure to replace <local-file-path> with the path of the file or directory on your local machine, <namespace> with the namespace of the pod, <pod-name> with the name of the pod, <container-path> with the path inside the container, and <source-container-path> and <destination-container-path> with the paths inside the respective containers.

Kubernetes Networking

Kubernetes networking refers to the mechanisms and components that enable communication between Pods, Services, and external users within a Kubernetes cluster. Here in this Kubernetes cheat sheet, we will see some Kubernetes networking commands.

Here are some essential Kubernetes networking commands using kubectl:

  1. Get Nodes: List all nodes in the cluster along with their status and IP addresses.
  1. Get Pods: List all Pods in a specific namespace or across all namespaces.
  1. Get Services: List all Services in a specific namespace or across all namespaces.
  1. Describe Pod: View detailed information about a specific Pod, including its IP address and related events.
  1. Describe Service: Display detailed information about a specific Service, including its IP and port information.
  1. Expose Deployment: Expose a Deployment as a Service to make it accessible within the cluster.
  1. Port Forwarding: Forward local ports to a Pod for direct access.
  1. Apply Networking Configurations: Apply networking configurations defined in a YAML file.
  1. Get Ingress: List all Ingress resources in a specific namespace or across all namespaces.
  1. Describe Ingress: View detailed information about a specific Ingress resource.

These Kubernetes commands allow you to manage and monitor various networking resources in your Kubernetes cluster, ensuring seamless communication and accessibility for your containerized applications.

Logs in Kubernetes

In Kubernetes, you can access and view container logs using the kubectl logs command or by using logging solutions integrated into your cluster. Here's how you can access container logs in Kubernetes:

Using kubectl logs command:

  1. View Pod Logs:

Example: kubectl logs my-pod -n my-namespace

  1. View Container Logs in Multi-Container Pods:

Example: kubectl logs my-pod -c my-container -n my-namespace

  1. View Previous Container Logs:

Example: kubectl logs my-pod --previous -n my-namespace

  1. Follow Logs (Live Stream):

Example: kubectl logs -f my-pod -n my-namespace

FAQs

Q: What are the essential Kubernetes networking objects for seamless communication within the cluster?

A: The essential Kubernetes networking objects are Pods, Services, and Ingress. Pods have unique IP addresses, Services provide stable endpoints for communication, and Ingress manages external access to Services.

Q: How can I scale my application in Kubernetes to meet changing demands?

A: You can scale your application in Kubernetes using the kubectl scale command or by setting up Horizontal Pod Autoscalers (HPA) to automatically adjust the number of replicas based on CPU utilization or custom metrics.

Q: How can I access and view container logs in Kubernetes for monitoring and debugging purposes?

A: You can access and view container logs using the kubectl logs command followed by the Pod name and container name. For example, kubectl logs my-pod -c my-container. This will display the logs of the specified container in the given Pod.

Conclusion

  • Quick and comprehensive reference for `Kubernetes commands and terminologies.
  • Crucial understanding of Kubernetes objects for efficient container orchestration.
  • Flexibility to manage resource attributes with kubectl edit or kubectl apply.
  • Empowering cluster introspection commands for monitoring and troubleshooting.
  • Streamlined networking tasks for efficient communication.