iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Examples

Six worked Kubernetes manifests covering Deployment, Service, Ingress, ConfigMap, Secret, CronJob.

Kubernetes — examples

EXAMPLE
# ===== 1. Deployment + Service =====
apiVersion: apps/v1
kind: Deployment
metadata: { name: shop, labels: { app: shop } }
spec:
  replicas: 3
  selector: { matchLabels: { app: shop } }
  template:
    metadata: { labels: { app: shop } }
    spec:
      containers:
        - name: shop
          image: ghcr.io/me/shop:v1
          ports: [{ containerPort: 3000 }]
          resources:
            requests: { cpu: '100m', memory: '128Mi' }
            limits: { cpu: '500m', memory: '512Mi' }
          readinessProbe:
            httpGet: { path: /healthz, port: 3000 }
            initialDelaySeconds: 5
          livenessProbe:
            httpGet: { path: /healthz, port: 3000 }
            initialDelaySeconds: 30
---
apiVersion: v1
kind: Service
metadata: { name: shop }
spec:
  selector: { app: shop }
  ports: [{ port: 80, targetPort: 3000 }]

# ===== 2. Ingress =====
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: shop
  annotations:
    cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
  ingressClassName: nginx
  tls:
    - hosts: [shop.example.com]
      secretName: shop-tls
  rules:
    - host: shop.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service: { name: shop, port: { number: 80 } }

# ===== 3. ConfigMap + env =====
apiVersion: v1
kind: ConfigMap
metadata: { name: shop-config }
data:
  LOG_LEVEL: info
  REGION: ap-southeast-2

# In deployment:
spec:
  template:
    spec:
      containers:
        - name: shop
          envFrom:
            - configMapRef: { name: shop-config }

# ===== 4. Secret =====
apiVersion: v1
kind: Secret
metadata: { name: shop-secret }
type: Opaque
stringData:
  DATABASE_URL: postgres://...
  API_KEY: sk_live_...

# Use:
env:
  - name: DATABASE_URL
    valueFrom: { secretKeyRef: { name: shop-secret, key: DATABASE_URL } }

# ===== 5. CronJob =====
apiVersion: batch/v1
kind: CronJob
metadata: { name: cleanup }
spec:
  schedule: '0 4 * * *'      # 04:00 UTC daily
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          containers:
            - name: cleanup
              image: ghcr.io/me/cleanup:v1
              command: [./cleanup.sh]

# ===== 6. HorizontalPodAutoscaler =====
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata: { name: shop }
spec:
  scaleTargetRef: { apiVersion: apps/v1, kind: Deployment, name: shop }
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target: { type: Utilization, averageUtilization: 75 }

# ===== Apply =====
kubectl apply -f deploy.yaml -f svc.yaml -f ingress.yaml -f cm.yaml -f secret.yaml -f cron.yaml -f hpa.yaml

# Or apply a folder:
kubectl apply -f ./k8s/

# ===== Patterns =====
# - probes (readiness + liveness) on every container
# - resources (requests + limits) on every container
# - one Service per Deployment; one Ingress per public app
# - SealedSecrets / SOPS for committed secrets (never plain YAML in git)
# - HPA + cluster autoscaler for elastic scaling

# ===== Pitfalls =====
# - Missing requests -> the scheduler cannot pack pods
# - Missing limits -> noisy neighbour can starve others
# - Liveness probe failing fast -> CrashLoopBackOff for nothing
# - Secret with stringData committed unencrypted

Why it matters

Six K8s manifests cover the daily 80%: Deployment, Service, Ingress, ConfigMap, Secret, CronJob, HPA. Probes + resources on every container, secrets sealed, Ingress with TLS via cert-manager. These shapes ship most workloads.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# Deploy nginx with a service
kubectl create deployment web --image=nginx
kubectl expose deployment web --port=80
Try it Yourself »

Discussion

Loading…