Skip to content

Instantly share code, notes, and snippets.

@mohashari
Created March 14, 2026 15:12
Show Gist options
  • Select an option

  • Save mohashari/e192933c9b52b133b98523e2c93fdafb to your computer and use it in GitHub Desktop.

Select an option

Save mohashari/e192933c9b52b133b98523e2c93fdafb to your computer and use it in GitHub Desktop.
Code snippets — Kubernetes For Backend Engineers
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-api
template:
metadata:
labels:
app: my-api
spec:
containers:
- name: api
image: myapp:1.0.0
resources:
requests:
memory: "128Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
readinessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
apiVersion: v1
kind: Service
metadata:
name: api-service
spec:
selector:
app: my-api
ports:
- port: 80
targetPort: 8080
type: ClusterIP
apiVersion: v1
kind: ConfigMap
metadata:
name: api-config
data:
LOG_LEVEL: "info"
MAX_CONNECTIONS: "100"
---
apiVersion: v1
kind: Secret
metadata:
name: api-secrets
type: Opaque
data:
# base64 encoded values
DB_PASSWORD: cGFzc3dvcmQxMjM=
env:
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: api-config
key: LOG_LEVEL
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: api-secrets
key: DB_PASSWORD
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1 # Can have 1 extra pod during update
maxUnavailable: 0 # Never have fewer than desired count
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: api-deployment
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
# See what's running
kubectl get pods -n my-namespace
kubectl get deployments
# Debug a pod
kubectl describe pod <pod-name>
kubectl logs <pod-name> --tail=100 -f
# Execute into a container
kubectl exec -it <pod-name> -- /bin/sh
# Apply a manifest
kubectl apply -f deployment.yaml
# Rollback a deployment
kubectl rollout undo deployment/api-deployment
# Check rollout status
kubectl rollout status deployment/api-deployment
You declare desired state → Kubernetes reconciles → Actual state matches desired state
apiVersion: v1
kind: Pod
metadata:
name: my-api
spec:
containers:
- name: api
image: myapp:1.0.0
ports:
- containerPort: 8080
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment