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
- kubectl version: Display the Kubernetes client and server version.
- Example: kubectl version --short displays a concise version information.
- kubectl cluster-info: Display cluster information.
- Example: kubectl cluster-info provides details about the cluster.
- kubectl get [resource]: List one or more resources.
- Example: kubectl get pods lists all pods in the current namespace.
- kubectl describe [resource] [name]: Show detailed information about a resource.
- Example: kubectl describe pod [pod-name] provides detailed information about the specified pod.
- 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
- kubectl create -f [file]: Create a resource from a file.
- Example: kubectl create -f deployment.yaml creates resources defined in deployment.yaml.
- 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.
- kubectl delete [resource] [name]: Delete resources by names.
- Example: kubectl delete pod [pod-name] deletes the specified pod.
- 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.
- Creating a Pod
- Example: kubectl run nginx --image=nginx creates a pod with the nginx image.
- Creating a Deployment
- Example: kubectl create deployment nginx --image=nginx creates a deployment named nginx.
- Exposing a Deployment
- Example: kubectl expose deployment nginx --port=80 --target-port=80 exposes the nginx deployment on port 80.
- 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
- Cluster: A set of nodes that run containerized applications managed by Kubernetes.
- Node: A worker machine in Kubernetes, can be a VM or a physical machine.
- Pod: The smallest and simplest Kubernetes object. Represents a single instance of a running process.
- Service: An abstract way to expose an application running on a set of pods as a network service.
- Ingress: Manages external access to services, typically HTTP.
- ConfigMap: A way to store configuration data in key-value pairs.
- Secret: A way to store sensitive data, such as passwords and tokens.
- Volume: A directory containing data, accessible to containers in a pod.
- Persistent Volume (PV): A piece of storage in the cluster that has been provisioned by an administrator.
- 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.