Created
March 14, 2026 15:12
-
-
Save mohashari/e192933c9b52b133b98523e2c93fdafb to your computer and use it in GitHub Desktop.
Code snippets — Kubernetes For Backend Engineers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| apiVersion: v1 | |
| kind: Service | |
| metadata: | |
| name: api-service | |
| spec: | |
| selector: | |
| app: my-api | |
| ports: | |
| - port: 80 | |
| targetPort: 8080 | |
| type: ClusterIP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| env: | |
| - name: LOG_LEVEL | |
| valueFrom: | |
| configMapKeyRef: | |
| name: api-config | |
| key: LOG_LEVEL | |
| - name: DB_PASSWORD | |
| valueFrom: | |
| secretKeyRef: | |
| name: api-secrets | |
| key: DB_PASSWORD |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| spec: | |
| strategy: | |
| type: RollingUpdate | |
| rollingUpdate: | |
| maxSurge: 1 # Can have 1 extra pod during update | |
| maxUnavailable: 0 # Never have fewer than desired count |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| You declare desired state → Kubernetes reconciles → Actual state matches desired state |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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