kubectl
Syntax
Browse / Kubernetes (K8s) Essentials Cheatsheet
Kubernetes (K8s) Essentials Cheatsheet
A practical and concise Kubernetes (K8s) cheatsheet designed for DevOps engineers and SREs, covering essential commands, concepts, and best practices for managing containerized applications.
Kubernetes Core Concepts & Operations
KUBERNETES BASICS
|
Example: |
Get Resources |
List pods: |
Describe Resources |
Detailed info for a pod: |
Context & Config Switching |
List contexts: Tip: Use |
Namespace Operations |
List namespaces: Specify namespace with |
Current Context/Namespace |
Show current context: |
Pro Tip / Gotcha |
Gotcha: Always check your current context and namespace before running commands that modify resources, especially in production environments. A simple |
PODS & DEPLOYMENTS
Creating/Applying Resources |
Creates or updates resources based on YAML definition. Example: |
Listing Pods/Deployments |
Use |
Deleting Resources |
Caution: |
Pod vs. Deployment YAML |
Pod: Basic unit, ephemeral. Best Practice: Always use Deployments for stateless apps for easy scaling and rolling updates. |
Scaling Deployments |
Example: |
Rolling Updates |
Update image: Example: |
Pro Tip / Gotcha |
Pro Tip: Use |
SERVICES & NETWORKING
Service Types: ClusterIP |
Default type, exposes service on an internal IP. Only reachable from within the cluster. Use for: Internal services, backend components. |
Service Types: NodePort |
Exposes service on a static port on each Node’s IP. Accessible from outside the cluster via Use for: Exposing services on test clusters or when a LoadBalancer isn’t available. |
Service Types: LoadBalancer |
Exposes service externally using a cloud provider’s load balancer. Get an external IP. Use for: Production-grade external access to web applications. |
Ingress Basics |
Manages external access to services within a cluster, typically HTTP/S. Requires an Ingress Controller (e.g., Nginx, Traefik). Defines routing rules based on host/path. |
DNS Resolution |
Services are discoverable via DNS: Example: |
Port Forwarding |
Example: Access local: |
Pro Tip / Gotcha |
Gotcha: NodePort exposes your service on all nodes, potentially including those you don’t expect traffic on. For production, LoadBalancer or Ingress is almost always preferred for better security and routing. |
K8s Configuration, Storage, & Security
CONFIGMAPS & SECRETS
Creating ConfigMaps (Literal) |
Example: |
Creating ConfigMaps (File) |
Example: |
Creating Secrets (Literal) |
Example: |
Creating Secrets (File) |
Example: |
Mounting into Pods |
As Env Vars: |
Differences & Security |
ConfigMaps: Store non-confidential data (plain text). Security Tip: Encrypt Secrets at rest using KMS or tools like |
Pro Tip / Gotcha |
Gotcha: |
VOLUMES & STORAGE
EmptyDir |
A temporary, empty directory created when a Pod is assigned to a node. Deleted when the Pod is removed from the node. Use for: Scratch space, caching, temporary file storage. |
hostPath |
Mounts a file or directory from the host node’s filesystem into a Pod. Caution: Not recommended for most uses due to security and scheduling issues. Ties Pod to a specific node. |
PersistentVolumeClaim (PVC) |
A request for storage by a user. Consumes PV resources. Namespace-scoped. YAML: |
PersistentVolume (PV) |
A piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned. Cluster-scoped. YAML: |
StorageClass Usage |
Defines “classes” of storage. Allows dynamic provisioning of PVs when a PVC requests a specific StorageClass. Example: |
Deleting PVC/PV |
Delete PVC: If |
Pro Tip / Gotcha |
Pro Tip: For stateful applications, always use PVCs, which abstract the underlying storage. This makes your applications portable and less coupled to specific infrastructure. |
MONITORING & DEBUGGING
Viewing Logs ( |
Get logs: |
Executing Commands ( |
Run command: Use for: Quick debugging, file inspection inside a running container. |
Describing Resources ( |
Provides a detailed status, events, and configuration. Essential for understanding why a pod isn’t starting or behaving as expected. |
Events |
Check events: Events show what happened to a resource (e.g., Pod scheduled, Container pulled, Failed). Look for |
Liveness/Readiness Probes |
Liveness Probe: Checks if app is running. If fails, K8s restarts container. Best Practice: Always define these for production apps. |
Resource Usage ( |
Requires Metrics Server deployment in cluster.
Use for: Quickly checking CPU/Memory usage of pods and nodes. |
Pro Tip / Gotcha |
Gotcha: If your |
RBAC & SECURITY
ServiceAccounts |
Provides an identity for processes that run in a Pod. Pods typically run under a default ServiceAccount in their namespace. Can be explicitly assigned via |
Roles & ClusterRoles |
Role: Grants permissions within a specific namespace. Defines |
RoleBindings & ClusterRoleBindings |
RoleBinding: Binds a Role (or ClusterRole) to a ServiceAccount, User, or Group within a namespace. |
Viewing RBAC |
Test user permissions: Example: |
Network Policies (Ingress) |
Controls inbound traffic to Pods. Default: All pods are non-isolated and accept all traffic. Once a NetworkPolicy selects a Pod, it becomes isolated and rejects traffic not explicitly allowed. |
Network Policies (Egress) |
Controls outbound traffic from Pods. Default: All pods allow all outbound traffic. Once a NetworkPolicy selects a Pod, its egress traffic is restricted to only what’s explicitly allowed. |
Pro Tip / Gotcha |
Pro Tip: Implement |