Kubernetes-Cheatsheet

👋 Hello Kubernetes

# Check kubectl version
kubectl version --client

# Check cluster nodes
kubectl get nodes

# Create a namespace
kubectl create namespace dev

💡 Tip: Always set the namespace using kubectl config set-context --current --namespace=dev to avoid deploying to default namespace.

📦 Pods

# List all pods
kubectl get pods

# Describe a pod
kubectl describe pod my-pod

# Delete a pod
kubectl delete pod my-pod

💡 Tip: Pods are the smallest deployable unit in Kubernetes. They may contain one or more containers.

🚀 Deployments

# Create a deployment
kubectl create deployment nginx-deploy --image=nginx:latest

# Scale deployment
kubectl scale deployment nginx-deploy --replicas=3

# Update image
kubectl set image deployment/nginx-deploy nginx=nginx:1.25

💡 Tip: Deployments manage pods and ensure desired state. Use kubectl rollout status deployment/nginx-deploy to track progress.

🌐 Services

# Expose deployment as ClusterIP (default)
kubectl expose deployment nginx-deploy --port=80

# Expose as NodePort
kubectl expose deployment nginx-deploy --type=NodePort --port=80

# List services
kubectl get svc

💡 Tip: Services abstract access to pods. Common types: ClusterIP, NodePort, LoadBalancer.

🗂 ConfigMaps & Secrets

# Create ConfigMap from literal
kubectl create configmap app-config --from-literal=ENV=production

# Create Secret from literal
kubectl create secret generic db-pass --from-literal=password=12345

# Use ConfigMap/Secret in pod
# reference in YAML: envFrom: configMapRef / secretRef

💡 Tip: Store sensitive data in Secrets, non-sensitive configs in ConfigMaps.

💾 Persistent Volumes (PV & PVC)

# Create PersistentVolumeClaim
kubectl apply -f pvc.yaml

# Mount PVC to pod
# in pod YAML: volumes: - name: data pvc: claimName: my-pvc
# and container: volumeMounts: - mountPath: /data name: data

💡 Tip: PVC decouples storage from pods; allows persistent data even if pod restarts.

📛 Namespaces

# List namespaces
kubectl get namespaces

# Switch namespace
kubectl config set-context --current --namespace=dev

# Delete namespace
kubectl delete namespace dev

💡 Tip: Use namespaces to isolate environments like dev, staging, and production.

📜 kubectl Commands

# Get resources
kubectl get all
kubectl get pods,svc,deployments

# Describe resource
kubectl describe deployment nginx-deploy

# Logs
kubectl logs my-pod

# Exec into container
kubectl exec -it my-pod -- /bin/bash

💡 Tip: Combine kubectl get -o wide for more info, or kubectl get -o yaml for full YAML output.

📄 YAML Example (Deployment)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deploy
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.25
        ports:
        - containerPort: 80

💡 Tip: Use kubectl apply -f deployment.yaml to create/update resources declaratively.

⛵ Helm (Package Manager)

# Add Helm repo
helm repo add bitnami https://charts.bitnami.com/bitnami

# Update repo
helm repo update

# Install chart
helm install my-mysql bitnami/mysql

# List releases
helm list

💡 Tip: Helm simplifies deploying complex applications with templated manifests.

💡 Tips & Best Practices

- Use namespaces for environment isolation
- Always label resources (app, env, team)
- Prefer declarative YAML over imperative commands
- Monitor pods and deployments with readiness/liveness probes
- Use Helm for repeatable deployments
- Clean up unused resources regularly
- Secure secrets using Kubernetes Secrets

Post a Comment