Bytes
Data Science

Kubernetes Cheat Sheet (Kubectl Cheat Sheet)

Last Updated: 1st August, 2024
icon

Arunav Goswami

Data Science Consultant at almaBetter

Explore our Kubernetes cheat sheet for essential commands and concepts. Learn kubectl commands, Kubernetes objects, and more to streamline orchestration tasks

Kubernetes, an open-source container orchestration platform, automates the deployment, scaling, and management of containerized applications. For developers and system administrators, having a cheat sheet for Kubernetes commands can significantly improve efficiency and understanding. This guide provides an essential Kubernetes commands cheat sheet, covering kubectl commands, Kubernetes objects, concepts, and more.

Introduction to Kubernetes

Kubernetes is designed to manage containerized applications in a clustered environment, providing mechanisms for deployment, maintenance, and scaling of applications. This cheat sheet will help you navigate through essential commands and concepts of Kubernetes, ensuring you have the necessary tools and knowledge at your fingertips.

Kubernetes Commands Cheat Sheet

Basic kubectl Commands Cheat Sheet

  1. kubectl version: Display the Kubernetes client and server version.
    • Example: kubectl version --short displays a concise version information.
  2. kubectl cluster-info: Display cluster information.
    • Example: kubectl cluster-info provides details about the cluster.
  3. kubectl get [resource]: List one or more resources.
    • Example: kubectl get pods lists all pods in the current namespace.
  4. kubectl describe [resource] [name]: Show detailed information about a resource.
    • Example: kubectl describe pod [pod-name] provides detailed information about the specified pod.
  5. kubectl logs [pod]: Print the logs for a container in a pod.
    • Example: kubectl logs [pod-name] prints logs from the specified pod.

Creating and Deleting Resources

  1. kubectl create -f [file]: Create a resource from a file.
    • Example: kubectl create -f deployment.yaml creates resources defined in deployment.yaml.
  2. kubectl apply -f [file]: Apply a configuration to a resource by file name or stdin.
    • Example: kubectl apply -f deployment.yaml applies changes from deployment.yaml.
  3. kubectl delete [resource] [name]: Delete resources by names.
    • Example: kubectl delete pod [pod-name] deletes the specified pod.
  4. kubectl delete -f [file]: Delete resources defined in a file.
    • Example: kubectl delete -f deployment.yaml deletes resources defined in deployment.yaml.

Kubernetes Objects Cheat Sheet

Pods

  • Pods are the smallest, most basic deployable objects in Kubernetes. A pod represents a single instance of a running process in your cluster.

Deployments

  • Deployments provide declarative updates to applications. They are used to manage stateless services.
    • Example: kubectl get deployments lists all deployments.

Services

  • Services are an abstract way to expose an application running on a set of pods as a network service.
    • Example: kubectl get services lists all services in the current namespace.

ConfigMaps

  • ConfigMaps are used to store configuration data in key-value pairs.
    • Example: kubectl get configmaps lists all ConfigMaps.

Secrets

  • Secrets are used to store sensitive data, such as passwords and tokens.
    • Example: kubectl get secrets lists all secrets.

Kubernetes Concepts Cheat Sheet

Namespaces

  • Namespaces are used to divide cluster resources between multiple users.
    • Example: kubectl get namespaces lists all namespaces in the cluster.

Labels and Selectors

  • Labels are key-value pairs attached to objects, used for organizing and selecting subsets of objects.
    • Example: kubectl get pods --selector app=myapp lists all pods with the label app=myapp.

Annotations

  • Annotations are key-value pairs attached to objects, used to store non-identifying information.
    • Example: kubectl annotate pod [pod-name] description='my pod' adds an annotation to a pod.

Taints and Tolerations

  • Taints are applied to nodes to prevent them from accepting any pods that do not tolerate the taints.
    • Example: kubectl taint nodes node1 key=value:NoSchedule applies a taint to a node.
  • Tolerations are applied to pods, allowing them to be scheduled on nodes with matching taints.
    • Example: kubectl get pods --selector key=value lists pods that tolerate specific taints.

Kubernetes Imperative Commands Cheat Sheet

Imperative commands are used to create and manage resources without using configuration files.

  1. Creating a Pod
    • Example: kubectl run nginx --image=nginx creates a pod with the nginx image.
  2. Creating a Deployment
    • Example: kubectl create deployment nginx --image=nginx creates a deployment named nginx.
  3. Exposing a Deployment
    • Example: kubectl expose deployment nginx --port=80 --target-port=80 exposes the nginx deployment on port 80.
  4. Scaling a Deployment
    • Example: kubectl scale deployment nginx --replicas=3 scales the nginx deployment to 3 replicas.

Working with Pods

  • kubectl get pods [--namespace [namespace]]: Lists all pods in the specified namespace.
  • kubectl describe pod [pod-name]: Describes a pod in detail.
  • kubectl logs [pod-name]: Retrieves logs for a pod.
  • kubectl exec [pod-name] -- [command]: Executes a command in a container in the pod.
  • kubectl delete pod [pod-name]: Deletes a specified pod.

Managing Deployments

  • kubectl get deployments [--namespace [namespace]]: Lists all deployments in the specified namespace.
  • kubectl describe deployment [deployment-name]: Describes a deployment in detail.
  • kubectl create deployment [deployment-name] --image=[image]: Creates a new deployment.
  • kubectl scale deployment [deployment-name] --replicas=[number]: Scales a deployment to the specified number of replicas.
  • kubectl rollout status deployment [deployment-name]: Checks the status of a deployment rollout.
  • kubectl rollout undo deployment [deployment-name]: Rolls back to the previous deployment.

Services and Networking

  • kubectl get services [--namespace [namespace]]: Lists all services in the specified namespace.
  • kubectl describe service [service-name]: Describes a service in detail.
  • kubectl expose deployment [deployment-name] --type=[type] --port=[port]: Exposes a deployment as a new service.

ConfigMaps and Secrets

  • kubectl create configmap [configmap-name] --from-literal=[key]=[value]: Creates a ConfigMap from a literal value.
  • kubectl get configmaps: Lists all ConfigMaps.
  • kubectl describe configmap [configmap-name]: Describes a ConfigMap in detail.
  • kubectl create secret generic [secret-name] --from-literal=[key]=[value]: Creates a Secret from a literal value.
  • kubectl get secrets: Lists all Secrets.
  • kubectl describe secret [secret-name]: Describes a Secret in detail.

Persistent Storage

  • kubectl get pv: Lists all Persistent Volumes (PVs).
  • kubectl describe pv [pv-name]: Describes a Persistent Volume in detail.
  • kubectl get pvc: Lists all Persistent Volume Claims (PVCs).
  • kubectl describe pvc [pvc-name]: Describes a Persistent Volume Claim in detail.

Namespace Management

  • kubectl get namespaces: Lists all namespaces.
  • kubectl create namespace [namespace]: Creates a new namespace.
  • kubectl delete namespace [namespace]: Deletes a namespace.

Monitoring and Debugging

  • kubectl top nodes: Displays resource (CPU/memory) usage of nodes.
  • kubectl top pods [--namespace [namespace]]: Displays resource (CPU/memory) usage of pods.
  • kubectl logs [pod-name]: Retrieves logs for a pod.
  • kubectl port-forward [pod-name] [local-port]:[pod-port]: Forwards one or more local ports to a pod.

Kubernetes Terms Cheat Sheet

Common Terms

  1. Cluster: A set of nodes that run containerized applications managed by Kubernetes.
  2. Node: A worker machine in Kubernetes, can be a VM or a physical machine.
  3. Pod: The smallest and simplest Kubernetes object. Represents a single instance of a running process.
  4. Service: An abstract way to expose an application running on a set of pods as a network service.
  5. Ingress: Manages external access to services, typically HTTP.
  6. ConfigMap: A way to store configuration data in key-value pairs.
  7. Secret: A way to store sensitive data, such as passwords and tokens.
  8. Volume: A directory containing data, accessible to containers in a pod.
  9. Persistent Volume (PV): A piece of storage in the cluster that has been provisioned by an administrator.
  10. Persistent Volume Claim (PVC): A request for storage by a user.

Conclusion

This Kubernetes cheat sheet serves as a quick reference guide for developers and system administrators. It covers essential kubectl commands, Kubernetes objects, core concepts, and imperative commands to help manage and deploy containerized applications efficiently. Keep this cheat sheet handy to streamline your Kubernetes operations and enhance your productivity.

AlmaBetter
Made with heartin Bengaluru, India
  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • 4th floor, 315 Work Avenue, Siddhivinayak Tower, 152, 1st Cross Rd., 1st Block, Koramangala, Bengaluru, Karnataka, 560034
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2024 AlmaBetter