Deploy a Url Shortener on Kubernetes for Real-world Ops Practice

A hands-on guide to deploying a scalable URL shortener on Kubernetes.

JR

2 minute read

A hands-on guide to deploying a scalable URL shortener on Kubernetes, focusing on real-world operational patterns and observability.

Why This Matters

A public-facing URL shortener teaches cluster lifecycle management, ingress configuration, autoscaling, and monitoring—skills tested daily in production environments. It’s small enough to iterate quickly but complex enough to expose common failure modes.

Workflow: Build and Deploy

  1. Cluster Setup

    • Use a managed provider (EKS, GCP GKE, AKS) to minimize control-plane hassle.
    • Enable monitoring add-ons (e.g., CloudWatch, Stackdriver) for visibility.
  2. Application Structure

    • Write a minimal Go/Python service with an API to store and retrieve short URLs (e.g., Redis backend).
    • Containerize with Docker and push to a registry (ECR, GCR).
  3. Kubernetes Manifests

    • Deploy with Helm for environment-specific config overrides.
    • Example deployment snippet:
      apiVersion: apps/v1  
      kind: Deployment  
      metadata:  
        name: url-shortener  
      spec:  
        replicas: 3  
        selector:  
          matchLabels:  
            app: url-shortener  
        template:  
          metadata:  
            labels:  
              app: url-shortener  
          spec:  
            containers:  
            - name: server  
              image: my-registry/url-shortener:latest  
              ports:  
                - containerPort: 8080  
              resources:  
                limits:  
                  memory: "256Mi"  
                  cpu: "500m"  
      
  4. Ingress and DNS

    • Use cert-manager for TLS (Let’s Encrypt).
    • Configure ingress to route traffic to the service:
      apiVersion: networking.k8s.io/v1  
      kind: Ingress  
      metadata:  
        name: url-shortener-ingress  
        annotations:  
          certmanager.k8s.io/cluster-issuer: "letsencrypt-prod"  
      spec:  
        tls:  
        - hosts:  
            - short.example.com  
          secretName: short-tls  
        rules:  
        - host: short.example.com  
          http:  
            paths:  
            - path: /  
              pathType: Prefix  
              backend:  
                service:  
                  name: url-shortener-svc  
                  port:  
                    number: 80  
      
  5. Observability

    • Deploy Prometheus and Grafana for metrics.
    • Alert on HTTP error rates and Redis connection latency.

Policy Example: Autoscaling

Implement Horizontal Pod Autoscaler (HPA) to scale based on CPU usage:

apiVersion: autoscaling/v2  
kind: HorizontalPodAutoscaler  
metadata:  
  name: url-shortener-hpa  
spec:  
  scaleTargetRef:  
    apiVersion: apps/v1  
    kind: Deployment  
    name: url-shortener  
  minReplicas: 2  
  maxReplicas: 10  
  metrics:  
  - type: Resource  
    resource:  
      name: cpu  
      target:  
        type: Utilization  
        averageUtilization: 80  

Tooling

  • kubectl: For day-to-day cluster interaction.
  • Helm: For versioned, repeatable deployments.
  • Prometheus/Grafana: For metrics and dashboards.
  • cert-manager: For automated TLS certificate management.

Tradeoffs

  • Cost vs. Availability: Running 3 replicas across zones improves uptime but increases cost. Consider starting with 2 replicas in a single zone for prototyping.
  • Redis Persistence: For simplicity, skip persistent volumes initially, but note this risks data loss. Add PVs later for production parity.

Troubleshooting

  • Ingress 404 Errors: Verify DNS A record points to ingress controller IP.
  • HPA Not Scaling: Check HPA events (kubectl describe hpa) for metric retrieval issues.
  • Crashing Pods: Use kubectl describe pod to inspect startup errors (e.g., missing environment variables).

Next Steps

After stabilizing the URL shortener, contribute to open-source Kubernetes projects (e.g., fixing bugs in Helm charts or writing operator controllers) to deepen expertise. Real-world contributions demonstrate problem-solving beyond toy projects.

Source thread: Project for a portfolio?

comments powered by Disqus