Kubernetes Cheatsheet
10xdev.blog/cheatsheets
# 1. kubectl Basics
# Get cluster information
kubectl cluster-info

# Get all resources in the current namespace
kubectl get all

# Get all pods in a specific namespace
kubectl get pods -n kube-system

# Apply a configuration file
kubectl apply -f my-app.yaml

# Delete resources defined in a file
kubectl delete -f my-app.yaml

# Get detailed information about a resource
kubectl describe pod my-pod-name

# View logs for a pod
kubectl logs my-pod-name

# Execute a command in a pod
kubectl exec -it my-pod-name -- /bin/sh
# 2. Pod Manifest
# A Pod is the smallest deployable unit in Kubernetes,
# representing a single instance of a running process.
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  labels:
    app: my-app
spec:
  containers:
  - name: my-container
    image: nginx:latest
    ports:
    - containerPort: 80
# 3. Deployment Manifest
# A Deployment manages a set of replica Pods, handling updates and rollbacks.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
spec:
  replicas: 3 # Desired number of Pods
  selector:
    matchLabels:
      app: my-app # Must match the Pod template's labels
  template: # Pod template
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: my-custom-app:1.0.0
        ports:
        - containerPort: 8080
# 4. Service Manifest
# A Service exposes an application running on a set of Pods as a network service.
apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  # type: ClusterIP (default) - Exposes the service on an internal IP in the cluster.
  # type: NodePort - Exposes the service on each Node's IP at a static port.
  # type: LoadBalancer - Exposes the service externally using a cloud provider's load balancer.
  type: LoadBalancer
  selector:
    app: my-app # Selects Pods with this label
  ports:
    - protocol: TCP
      port: 80 # Port to expose the service on
      targetPort: 8080 # Port the container is listening on
# 5. ConfigMap & Secret
# ConfigMap: For non-sensitive configuration data.
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-app-config
data:
  API_URL: "https://api.example.com"
  FEATURE_FLAG: "true"

---
# Secret: For sensitive data like passwords or API keys.
# Data is base64 encoded.
apiVersion: v1
kind: Secret
metadata:
  name: my-app-secret
type: Opaque
data:
  DB_PASSWORD: "cGFzc3dvcmQxMjM=" # echo -n 'password123' | base64
# 6. Using ConfigMaps & Secrets in a Pod
apiVersion: v1
kind: Pod
metadata:
  name: pod-with-config
spec:
  containers:
    - name: my-container
      image: my-app
      env:
        # Using a ConfigMap value as an environment variable
        - name: API_URL
          valueFrom:
            configMapKeyRef:
              name: my-app-config
              key: API_URL
        # Using a Secret value as an environment variable
        - name: DATABASE_PASSWORD
          valueFrom:
            secretKeyRef:
              name: my-app-secret
              key: DB_PASSWORD
# 7. Persistent Storage
# PersistentVolumeClaim (PVC): A request for storage by a user.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce # Can be mounted as read-write by a single Node
  resources:
    requests:
      storage: 1Gi # Request 1 Gibibyte of storage

---
# Using the PVC in a Pod
apiVersion: v1
kind: Pod
metadata:
  name: pod-with-storage
spec:
  containers:
    - name: my-container
      image: my-app
      volumeMounts:
        - name: my-storage
          mountPath: /data # Mount the volume at /data inside the container
  volumes:
    - name: my-storage
      persistentVolumeClaim:
        claimName: my-pvc
master* 0 0