Kubernetes Cheat Sheet (2023) for Freshers and Experienced
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 Cheatsheet
Kubernetes Terminologies
Term | Definition |
---|---|
Cluster | A group of nodes that collectively manage and run containerized applications in Kubernetes. |
Node | A worker machine in the cluster responsible for running containers. |
Pods | The smallest deployable units in Kubernetes, containing one or more containers sharing the same network and storage. |
Kube Scheduler | Responsible for scheduling Pods on worker nodes based on resource availability and constraints. |
API Server | Serves as the Kubernetes control plane's front end, allowing users and other components to interact with the cluster. |
Kubelet | Manages and runs containers on individual nodes, ensuring they maintain the desired state defined in the cluster's control plane. |
Kube Proxy | Handles network routing and load balancing for Services, enabling communication between Pods. |
Etcd | A distributed key-value store that stores cluster data, providing a source of truth for the Kubernetes control plane. |
Controller Manager | Manages various controllers, like ReplicaSets and Deployments, ensuring the desired state of resources in the cluster. |
Container Runtime | The 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.
Object | Description |
---|---|
Pod | The 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. |
Volume | Provides persistent storage for Pods, allowing data to survive Pod restarts and ensuring data durability. |
Namespace | Segregates resources within a cluster, allowing teams or applications to operate independently without interference. |
High-Level Objects (Controllers):
Object | Description |
---|---|
Deployment | Manages replica Pods, enabling rolling updates and rollbacks while ensuring application availability and fault tolerance. |
Replication Controller | Ensures a specified number of replica Pods are running and replaces failed Pods. |
ReplicaSet | An improved version of Replication Controllers, maintaining a stable set of replica Pods with enhanced selector options. |
StatefulSet | Ensures 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:
-
kubectl create: Create a resource from a file or stdin. Example: kubectl create deployment nginx --image=nginx
-
kubectl apply: Apply changes to resources using a configuration file. Example: kubectl apply -f deployment.yaml
-
kubectl get: Retrieve resources of a specific type. Example: kubectl get pods or `kubectl get services
-
kubectl describe: Show detailed information about a resource. Example: kubectl describe pod my-pod
-
kubectl delete: Delete resources by filenames, stdin, resources, and names. Example: kubectl delete pod my-pod or kubectl delete -f deployment.yaml
-
kubectl edit: Edit resources in the default editor. Example: kubectl edit deployment nginx
-
kubectl logs: Print logs from a container in a Pod. Example: kubectl logs my-pod
-
kubectl exec: Execute a command on a container in a Pod. Example: kubectl exec -it my-pod -- /bin/bash
-
kubectl port-forward: Forward one or more local ports to a Pod. Example: kubectl port-forward my-pod 8080:80
-
kubectl rollout: Manage rollouts of Deployments. Example: kubectl rollout status deployment/nginx
-
kubectl scale: Scale the number of replicas of a resource. Example: kubectl scale deployment nginx --replicas=3
-
kubectl expose: Expose a resource as a new Service. Example: kubectl expose deployment nginx --port=80 --target-port=8080
-
kubectl apply -f: Apply changes to resources using configuration files. Example: kubectl apply -f deployment.yaml
-
kubectl get nodes: List all nodes in the cluster. Example: kubectl get nodes
-
kubectl get pods: List all Pods in the cluster. Example: kubectl get pods
-
kubectl get services: List all Services and their assigned IPs. Example: `kubectl get services
-
kubectl get deployments: List all Deployments in the cluster. Example: `kubectl get deployments
-
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
- Get the resource you want to modify. For example, to edit a Deployment named "my-deployment," use the following command:
- Open the resource's YAML definition in the default editor (usually set by the KUBE_EDITOR environment variable or defaulting to vi).
-
Modify the desired attributes in the YAML file and save the changes. The editor will automatically update the resource.
-
Kubernetes will detect the changes and automatically apply them to the resource.
Method 2: Using kubectl apply with an updated YAML file
- 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":
-
Open the deployment. yaml file in a text editor and modify the desired attributes.
-
Save the changes in the YAML file.
-
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:
- kubectl get nodes: List all the nodes in the cluster, along with their status and other details.
- kubectl describe node
: Display detailed information about a specific node, including capacity, allocatable resources, conditions, and more.
- kubectl get pods --all-namespaces: List all Pods across all namespaces in the cluster.
- kubectl describe pod
-n : View detailed information about a specific Pod, including events, labels, and annotations.
- kubectl get services --all-namespaces: List all Services across all namespaces in the cluster.
- kubectl describe service
-n : Get detailed information about a specific Service, including endpoints and selectors.
- kubectl get deployments --all-namespaces: List all Deployments across all namespaces in the cluster.
- kubectl describe deployment
-n : View detailed information about a specific Deployment, including replicas, strategy, and events.
- kubectl get configmaps --all-namespaces: List all ConfigMaps across all namespaces in the cluster.
- 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:
- Create a Deployment:
Example: kubectl create deployment nginx-deployment --image=nginx:latest
- Scale a Deployment:
Example: kubectl scale deployment nginx-deployment --replicas=3
- Update a Deployment:
Example: kubectl set image deployment/nginx-deployment nginx=nginx:1.19.0
- Rollout Status of a Deployment:
Example: kubectl rollout status deployment/nginx-deployment
- Undo a Deployment Update (Rollback):
Example: kubectl rollout undo deployment/nginx-deployment
- Pause and Resume a Deployment Rollout:
Example:
- Describe a Deployment:
Example: kubectl describe deployment nginx-deployment
- Delete a Deployment:
Example: kubectl delete deployment nginx-deployment
Service Commands:
- Create a Service:
Example: kubectl create service clusterip my-service --tcp=80:8080
Note: <service-type> can be clusterip, nodeport, loadbalancer, or externalname.
- Expose a Deployment as a Service:
Example: kubectl expose deployment nginx-deployment --type=ClusterIP --port=80 --target-port=8080
- Get Services:
- Describe a Service:
Example: kubectl describe service my-service
- 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:
- Copy from a local machine to a container:
Example: kubectl cp myfile.txt my-namespace/my-pod:/tmp/myfile.txt
- Copy from a container to a local machine:
Example: kubectl cp my-namespace/my-pod:/tmp/myfile.txt ~/Downloads/myfile.txt
- 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:
- Get Nodes: List all nodes in the cluster along with their status and IP addresses.
- Get Pods: List all Pods in a specific namespace or across all namespaces.
- Get Services: List all Services in a specific namespace or across all namespaces.
- Describe Pod: View detailed information about a specific Pod, including its IP address and related events.
- Describe Service: Display detailed information about a specific Service, including its IP and port information.
- Expose Deployment: Expose a Deployment as a Service to make it accessible within the cluster.
- Port Forwarding: Forward local ports to a Pod for direct access.
- Apply Networking Configurations: Apply networking configurations defined in a YAML file.
- Get Ingress: List all Ingress resources in a specific namespace or across all namespaces.
- 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:
- View Pod Logs:
Example: kubectl logs my-pod -n my-namespace
- View Container Logs in Multi-Container Pods:
Example: kubectl logs my-pod -c my-container -n my-namespace
- View Previous Container Logs:
Example: kubectl logs my-pod --previous -n my-namespace
- 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.