For enquiries call:

Phone

+1-469-442-0620

April flash sale-mobile

HomeBlogDevOpsKubernetes Cheat Sheet: Essential Commands And Examples

Kubernetes Cheat Sheet: Essential Commands And Examples

Published
08th Sep, 2023
Views
view count loader
Read it in
18 Mins
In this article
    Kubernetes Cheat Sheet: Essential Commands And Examples

    In this article, we'll discuss the importance of having a Kubernetes cheat sheet available so you can quickly get up-to-speed with this powerful tool before diving into more advanced topics like deployments and automation. 

    What is Kubernetes?

    Kubernetes is an open-source system for automating containerized applications' deployment, scaling, and management. It organizes containers into logical units so applications can be easily managed and discovered. Developers without experience managing large-scale systems can easily use Kubernetes to deploy and manage their apps. 

    What Does Kubernetes Cheat Sheet Mean?

    A Kubernetes Cheat Sheet is a document that helps developers to understand the Kubernetes concepts. The Kubernetes cheat sheet provides an overview of the significant aspects of the Kubernetes platform, including its architecture and components, and use cases. 

    Kubernetes Cheat Sheet with Examples (2023)

    Kubernetes is increasingly becoming the de facto standard for deploying applications in cloud environments. But Kubernetes can be confusing at first glance—and even more so if you're trying to implement it for the first time. That's why we've put together this Kubernetes cheat sheet with examples of how you might use Kubernetes to manage your own containerized applications.

    Kubernetes Terminology Cheat Sheet

    In this section, you'll find the most common Kubernetes terminology cheat sheet terms and how it relates to the Kubernetes platform. 

    Cluster

    A single-node cluster is a 'development environment,' typically used for developing, testing, and troubleshooting applications. A multi-node cluster is used when running production workloads. In this case, the nodes must be configured with sufficient resources to handle the expected workloads without impacting performance or availability. 

    Node (Master)

    The master node is responsible for managing resources within your cluster. It also runs the API server, which handles requests from clients like kubectl and calls into other parts of your system, like scheduling containers in pods and monitoring their health. The control plane runs on this node and several other components, such as the scheduler and controller manager.  

    Node (Worker)

    A node (worker) is a machine in your cluster that runs containerized applications. These machines are not required to host any control plane components or manage the cluster state because they are only responsible for running pods on their local disk space. 

    Pods

    Pods are the smallest deployable units in Kubernetes. Pods are made up of one or more containers where you run your application. A pod can be scaled up or down to meet your needs. You can also run multiple containers in the same pod, making it easy to scale out your application if running on a microservice architecture. Pods are assigned an IP address accessible from outside the cluster through NAT (Network Address Translation) or port mapping. 

    Labels

    Labels can be used to organize and select objects in Kubernetes and have an essential role in most Kubernetes features. You can assign labels as key-value pairs to objects such as pods, services, deployments, and replication controllers. 

    Master

    The master is the highest-level object in the API hierarchy. It's a single instance of the Kube-API server, which manages all API objects and their interactions. It runs an API server that provides access to all of the other components of the system. The master also runs a scheduler, which allocates resources among the nodes in the system. The master accepts client requests and translates them into instructions for the nodes. 

    Service

    A service is a logical group of pods with a shared purpose and identity, such as web or database servers. Service provides an abstraction for a set of pods that are grouped. Services are often used to manage access to pods and provide load balancing, which allows pods from different services to be exposed externally.  

    Kubernetes Objects Cheat Sheet

    Kubernetes objects are the building blocks of Kubernetes. They are the minor units that you can create and manipulate to build your application or service. The Kubernetes objects cheat sheet includes objects like Pods and serives, Replication Controllers, and Schedulers. Each object has a specific purpose and is used to manage resources in your cluster. In this section, we'll explore some of the most common Kubernetes objects: 

    Namespaces

    The aim of the namespace obect is to play the role of a seperator of resources stored up in the cluster. For the purpose of administering easier organisation, a single cluster may contain various namespaces. New clusters comprise multiple namespaces for system purposes. Users can create endless namespaces. For instance, you could create one for the development and the other for testing.

    CommandsDescription
    kubectl create namespace <namespace_name>To create a namespace with the given name
    kubectl get namespaceTo list the current namespace(s) in a cluster.
    kubectl describe namespace <namespace_name>To display the detailed state of a namespace
    kubectl delete namespace <namespace_name>To Delete a namespace
    kubectl edit namespace <namespace_name>To modify the definition of a namespace
    kubectl top namespace <namespace_name>To display resource (CPU/Memory/Storage) usage for a namespace

    Note that the keyword "namespace" can be replaced with its shorthand notation “ns” in the above commands. 

    Configuration Files (Manifest files)

    Kubernetes manifests are written in YAML format and describe how your cluster should look at any given time. Other objects within Kubernetes reference manifest files to determine which configuration files they need to load at runtime.

    CommandsDescription
    kubectl apply –f <manifest_file.yaml>To apply a configuration to an object by filename or standard input. It overrides the existing configuration.
    kubectl apply –f <manifest_file.yaml>To create object(s) using given manifest file
    kubectl apply –f ./dirTo create object(s) using all the manifest files in the given directory
    kubectl apply –f <url>To create object(s) using URL
    kubectl delete –f <manifest_file.yaml>To delete existing object(s) created using given manifest file

    Cluster Management & Contexts

    Clusters consist of multiple nodes that work together to provide services like storage or compute power to users who request them through their pods (which run on top of those services). Each node has its IP address, which allows users to connect directly with individual nodes; however, all connections into or out. 

    CommandDescription
    kubectl cluster-infoTo display endpoint information about the master and services in the cluster
    kubectl versionTo display the current K8s version running in the client and server
    kubectl config viewTo get the configuration of the cluster
    kubectl api-resourcesTo list the available API resources
    kubectl api-versionsTo list the available API resources
    kubectl get all –all-namespacesTo list everything in all the namespaces

    Kubernetes Architecture Cheat Sheet

    In this section, we'll explore some of the most common components of the Kubernetes architecture cheat sheet : 

    1. 3 Principles

    A) It's secure:

    Each node runs in a security context that limits its access to the Docker Engine. The Kubernetes API server runs in an even more limited context, so it can't be used to escalate privileges and compromise the cluster. 

    B) It's easy to use:

    Only the resources you actually need are exposed through the interface that Kubernetes offers. The mechanics won't be a problem for you. Kubernetes makes sure the state you provide actually exists. 

    C) It's highly portable:

    Kubernetes is designed to run on any cloud or bare metal infrastructure. It can also run on top of virtual machines such as VMware ESXi or OpenStack Nova if you need those capabilities for compatibility with existing applications or processes. 

    2. Fundamental Concepts of Kubernetes Architecture

    The following are some of the best fundamental concepts of Kubernetes Architecture

    1. Service: Service is classified as a static IP address which is assigned to every single pod. The best part about this Kubernetes object is that the communications with other pods stays intact despite replacing the IP with a new IP and a pod with new pod. 
    2. Volumes: Data is irreplaceable, and the loss of data leads to huge expense. To avoid this, one can simply use volumes to easily safeguard data from DB or monitoring agents.
    3. Kubelet: The container agent is responsible for maintaining pod sets; it connects to the API server and runs on each node in a cluster.
    4. ETCD: A metadata service that stores configuration data in a highly-available key value store; this data can be used by both Kubernetes and applications running on Kubernetes.

    Monitors resource performance and usage within each pod; this information is stored in etc. and in an HAProxy instance called Heapster, which exports metrics via HTTP endpoints that can then be consumed by monitoring tools like Prometheus or Grafana. 

    Want to become a skilled and expert Kubernetes developer? Check out the best Kubernetes courses to get started. 

    Kubernetes Components Cheat Sheet

    Kubernetes consists of several components in the Kubernetes components cheat sheet : 

    A) Control Plane Components

    The control plane refers to the management components you need to run Kubernetes. These include: 

    1. Kube-proxy: This component is a network proxy that controls how container traffic is routed through your cluster. It also forwards traffic between your VMs and hosts and between the different pods in your cluster. 
    2. CNI: Container Network Interface (CNI) provides an abstraction layer between Kubernetes and the various plugins that can be used for container networking, including Flannel, Weave Net, Calico, and more. 

    B) Node Components

    Nodes are the physical machines that make up a Kubernetes cluster. Nodes comprise three main components: Docker host, network plugin and OS image or kernel version. Docker host enables containers to run on Linux; network plugin describes how containers will communicate with each other. 

    C) Addons

    Addons are the components that provide additional functionality to the core Kubernetes API. They are not tightly coupled with Kubernetes and can be deployed independently. Addons can be installed on any node in a cluster, and they provide a plugin-like API to interact with them.

    Kubernetes Commands Cheat Sheet

    This section will cover some of the fundamental Kubernetes commands cheat sheet. 

    Imperative commands

    Imperative commands are instructions a user gives to the Kubernetes cluster to perform tasks. For example, you can use imperative commands to create or delete a pod or to start or stop a container. 

    Pods and Container Introspection Commands

    Pods are the basic unit of scheduling in Kubernetes, which is the process of spreading your application across multiple machines. A pod consists of one or more containers that share network namespaces, storage volumes, and other configuration details. The containers inside a pod are isolated from each other concerning resource usage (CPU/memory), but they can communicate through localhost. 

    Debugging Commands

    You can use debugging commands in Kubernetes to get information about various aspects of your cluster's behavior. For example, you can view the logs for an individual container or pod; list all running pods; check whether a particular service is healthy; find out details about a separate node, and so forth. 

    The commands to view the logs are listed below:

    CommandDescription
    kubectl logs <pod_name>To display the logs for a Pod with the given name
    kubectl logs --since=1h <pod_name>To display the logs of last 1 hour for the pod with the given name
    kubectl logs --tail-10 <pod_name>To display the most recent 10 lines of logs
    kubectl logs -c <container_name> <pod_name>To display the logs for a container in a pod with the given names
    kubectl logs <pod_name> pod.logTo save the logs into a file named as pod.log

    Cluster Introspection Commands

    The Kubernetes cluster introspection commands, kubectl, and Kube-API server provide a set of tools for inspecting the state of a Kubernetes cluster. You can use these commands to query for information about objects in your cluster and monitor their condition and that of your running applications.  

    Quick Commands

    Quick commands are single-line commands that return values from the Kubernetes API server. These are useful for getting quick access to information about your resources without having to write an entire script or shell command. 

     Our Online DevOps Courses will help you by equipping you with the most in-demand skills in the market. 

    Changing Resource Attributes

    There are two types of commands for changing resource attributes. They are: 

    1. Taints: They ensure that pods are not placed on inappropriate nodes. 
    2. Labels: They are used to identify pods. 

    You can refer to the below commands for changing resource attributes:

    CommandDescription
    kubectl taint <node_name><taint_name>To update the taints on one or more nodes.
    kubectl label pod <pod_name>To add or update the label of a pod

    For Cluster Introspection

    The cluster introspection tool is used to get information about the current state of a Kubernetes cluster. This information includes the nodes' number, status, and storage type.

    CommandDescription
    kubectl versionTo get the information related to the version.
    kubectl cluster-infoTo get the information related to the cluster
    kubectl config g viewTo get the configuration details
    kubectl describe node <node_name>To get the information about a node

    Interacting with Deployments and Services

    A deployment is a collection of pods that share an identical configuration. You can use a deployment to scale up or down your application by adding or removing pods from the cluster. You create a new deployment version whenever you change your application's configuration. 

    A service is used to expose one or more Pods as a single endpoint to clients. You can use services to load balance traffic across multiple Pods in your cluster and provide external access to pods that don't have public IP addresses. Services are also used for DNS resolution and name discovery.

    CommandDescription
    kubectl logs deploy/my-deploymentTo dump Pod logs for a Deployment (single-container case)
    kubectl logs deploy/my-deployment -c my-containerTo dump Pod logs for a Deployment (multi-container case)
    kubectl port-forward svc/my-service 5000To listen on local port 5000 and forward to port 5000 on Service backend
    kubectl port-forward svc/my-service 5000:my-service-portTo listen on local port 5000 and forward to Service target port with name <my-service-port>
    kubectl port-forward deploy/my-deployment 5000:6000To listen on local port 5000 and forward to port 6000 on a Pod created by <my-deployment>
    kubectl exec deploy/my-deployment – lsTo run command in first Pod and first container in Deployment (single- or multi-container cases)

    Copy Files and Directories to and From Containers

    The kubectl cp command can copy files and directories from one location to another. This command can be used to copy files and directories both outside of a container and inside a container. 

    CommandDescription
    kubectl cp /tmp/foo_dir my-pod:/tmp/bar_dirTo copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod in the current namespace
    kubectl cp /tmp/foo my-pod:/tmp/bar -c my-containerTo copy /tmp/foo local file to /tmp/bar in a remote pod in a specific container
    kubectl cp /tmp/foo my-namespace/my-pod:/tmp/barTo copy /tmp/foo local file to /tmp/bar in a remote pod in namespace my-namespace
    kubectl cp my-namespace/my-pod:/tmp/foo /tmp/barTo copy /tmp/foo from a remote pod to /tmp/bar locally

    Kubernetes Troubleshooting Cheat Sheet

    This section will cover some of the most commonly used Kubernetes troubleshooting cheat sheet commands. There are many more commands, but these are the ones you'll likely use most often. 

    kubectl get pods --all-namespaces

    This will give you a list of all the pods in your cluster. You can also use –all to get all pods, including non-running ones. 

    kubectl describes pod <pod name>

    This will give you more detailed information about your pod, including its status, IP address, and other details. 

    kubectl logs <pod name> --previous

    This will show the logs from your pod until the last time it was restarted or killed. This command is helpful to see what happened immediately before your pod stopped working correctly. It's also beneficial if you want to see what happened just before a new Kubernetes was deployed to your cluster (as this can cause pods to stop working).

    Download a Printable PDF of the Kubernetes Cheat Sheet

    If you're new to Kubernetes, you'll be happy to know that it's one of the most accessible tools to master. But if you're still feeling a little bit shaky on your feet or want a handy reference guide to keep on hand, this quick-reference Kubernetes Cheat Sheet pdf is for you. 

    Kubernetes Cheat Sheet: Tips and Tricks

    Here are some of the Kubernetes cheat sheet tips and tricks that you should keep in mind while using Kubernetes software: 

    Use Helm to manage your Kubernetes apps

    Helm is a package manager for Kubernetes that allows you to install applications into your cluster easily. It also makes it simple to upgrade or roll back those applications if needed. 

    Use a StatefulSet to deploy stateful applications

    StatefulSets are a great way to ensure that specific containers running in your pod are always up and running, even if some of their dependent services fail or are temporarily unavailable due to maintenance work on those other services. 

    Use Persistent Volumes to ensure data replication between pods

    The Persistent Volumes tool ensures that all data produced by one pod can be mirrored on another. This means that no matter where you are in the world, any changes made by one or both pods will always be mirrored in the other - even if they are in different locations! 

    Want to learn more about the tips and tricks of Kubernetes? Visit our Docker Kubernetes Course to get more information and become an expert. 

    How Helpful is the Kubernetes Cheat Sheet?

    The Kubernetes concepts cheat sheet is laid out to quickly make it easy to find what you're looking for. It includes categories such as "Getting Started" and "Kubectl Commands". There are also several other places where you can find additional information about the project. 

    Do You Want a Career in Kubernetes?

    We believe that anyone with the passion and dedication to learn can become a professional in their chosen field. We are here for you every step of the way. Our courses are designed to be accessible and engaging for students of all backgrounds and skill levels. No matter what your experience level is today, we will help you reach your goals and achieve success in your future career as a Kubernetes developer—all from the comfort and convenience of your own home or office.

    Conclusion

    Kubernetes is a great technology, and it's only getting better. If you want to learn how to use Kubernetes, KnowledgeHut is the place to go. We offer online boot camps with 24/7 support, so you can get the education you need no matter where you are. 

    Kubernetes Cheat Sheet FAQs

    1Can you have a cheat sheet on Kubernetes?

    Yes, Kubernetes does provide Kubernetes imperative commands cheat sheet for their developers to make them use the software more efficiently and faster. 

    2Can you bring a cheat sheet to the Kubernetes exam?

    CKA candidates may take the exam like an open book test. Bookmark the main points in the Kubernetes command line cheat sheet and use the CKA bookmarks. Many Kubernetes bookmarks are available that will make life easier for you when doing your exam! 

    3Is Kubernetes important for DevOps?

    Yes, Kubernetes is essential for DevOps. Kubernetes is an open-source container orchestration system that makes it easy to deploy, manage, and scale containerized applications. It automates containerized applications' deployment, scaling, and management so that application developers can focus on their code. 

    4What is the fastest way to learn Kubernetes?

    The fastest way to learn Kubernetes is by using a reliable source. Upgrade Knowledgehut is the best way to learn Kubernetes because it provides you with the most current, up-to-date information about Kubernetes.

    Profile

    Mayank Modi

    Blog Author

    Mayank Modi is a Red Hat Certified Architect with expertise in DevOps and Hybrid Cloud solutions. With a passion for technology and a keen interest in Linux/Unix systems, CISCO, and Network Security, Mayank has established himself as a skilled professional in the industry. As a DevOps and Corporate trainer, he has been instrumental in providing training and guidance to individuals and organizations. With over eight years of experience, Mayank is dedicated to achieving success both personally and professionally, making significant contributions to the field of technology.

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming DevOps Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon