Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

🏠 Back to Blog

Lab 05: Traffic Management with NGINX Ingress

  • Without a traffic router, canary weight = pod ratio
  • With trafficRouting, weight is applied at the router, independent of replica count
  • NGINX: controller creates <stableIngress>-canary with nginx.ingress.kubernetes.io/canary-* annotations

Install NGINX ingress

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.11.2/deploy/static/provider/cloud/deploy.yaml
kubectl wait -n ingress-nginx --for=condition=ready pod \
  --selector=app.kubernetes.io/component=controller --timeout=120s

# local clusters: no external IP, use port-forward
kubectl port-forward -n ingress-nginx svc/ingress-nginx-controller 8080:80
  • Requests use -H "Host: ..." against localhost:8080, so no /etc/hosts edit needed

Weighted canary

kubectl create namespace traffic-demo
kubectl config set-context --current --namespace=traffic-demo
apiVersion: v1
kind: Service
metadata:
  name: traffic-demo-stable
spec:
  selector:
    app: traffic-demo
  ports:
  - name: http
    port: 80
    targetPort: http
---
apiVersion: v1
kind: Service
metadata:
  name: traffic-demo-canary
spec:
  selector:
    app: traffic-demo
  ports:
  - name: http
    port: 80
    targetPort: http
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: traffic-demo-ingress
spec:
  ingressClassName: nginx
  rules:
  - host: traffic-demo.local
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: traffic-demo-stable
            port:
              number: 80
---
apiVersion: argoproj.io/v1alpha1
kind: Rollout
metadata:
  name: traffic-demo
spec:
  replicas: 5
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      app: traffic-demo
  template:
    metadata:
      labels:
        app: traffic-demo
    spec:
      containers:
      - name: app
        image: argoproj/rollouts-demo:blue
        ports:
        - name: http
          containerPort: 8080
  strategy:
    canary:
      stableService: traffic-demo-stable
      canaryService: traffic-demo-canary
      trafficRouting:
        nginx:
          stableIngress: traffic-demo-ingress
      steps:
      - setWeight: 10
      - pause: {duration: 1m}
      - setWeight: 40
      - pause: {duration: 1m}
      - setWeight: 80
      - pause: {duration: 1m}
  • Both services use the same selector in the manifest; the controller adds the rollouts-pod-template-hash selector (stable -> stable RS, canary -> canary RS)
  • The stable ingress must point at stableService
kubectl apply -f traffic-demo.yaml
kubectl argo rollouts get rollout traffic-demo --watch
kubectl argo rollouts set image traffic-demo app=argoproj/rollouts-demo:yellow

kubectl get ingress
kubectl get ingress traffic-demo-ingress-canary -o yaml | grep canary
annotations:
  nginx.ingress.kubernetes.io/canary: "true"
  nginx.ingress.kubernetes.io/canary-weight: "10"
  • canary-weight follows the current setWeight
# 100 requests, count colors
for i in $(seq 100); do
  curl -s -H "Host: traffic-demo.local" http://localhost:8080/color; echo
done | sort | uniq -c
  • ~10 yellow / ~90 blue at the first step
  • Canary RS is still scaled to match the weight (1 pod at 10% of 5, rounded up) unless setCanaryScale overrides it

Header-based routing

  • Extra annotations added to the canary ingress with additionalIngressAnnotations
  • NGINX: header match overrides the weight (always -> canary, never -> stable)
  strategy:
    canary:
      stableService: header-stable
      canaryService: header-canary
      trafficRouting:
        nginx:
          stableIngress: header-ingress
          additionalIngressAnnotations:
            canary-by-header: X-Canary
            canary-by-header-value: always
      steps:
      - setWeight: 20
      - pause: {}
      - setWeight: 50
      - pause: {}
  • Same layout as above (2 services, an ingress for header.local, a Rollout), with header-* names
  • Annotation keys are prefixed with nginx.ingress.kubernetes.io/ by the controller
kubectl argo rollouts set image header-demo app=argoproj/rollouts-demo:green

# no header: follows weight
curl -s -H "Host: header.local" http://localhost:8080/color
# canary
curl -s -H "Host: header.local" -H "X-Canary: always" http://localhost:8080/color
# stable
curl -s -H "Host: header.local" -H "X-Canary: never" http://localhost:8080/color
  • Rollout stays paused at 20% (pause: {}), so the header can be tested before promote
  • Other routers (Istio, ALB) use setHeaderRoute / setMirrorRoute steps with managedRoutes instead; Istio also shifts via VirtualService weights

Traffic + analysis

  • Analysis steps can use ingress metrics, e.g. NGINX controller metrics scraped by Prometheus (lab 04)
apiVersion: argoproj.io/v1alpha1
kind: AnalysisTemplate
metadata:
  name: nginx-success-rate
spec:
  metrics:
  - name: success-rate
    interval: 30s
    count: 3
    failureLimit: 1
    successCondition: result[0] >= 0.95
    provider:
      prometheus:
        address: http://prometheus.monitoring.svc.cluster.local:9090
        query: |
          sum(rate(nginx_ingress_controller_requests{status=~"2..",exported_namespace="traffic-demo",ingress="traffic-demo-ingress-canary"}[2m]))
          /
          sum(rate(nginx_ingress_controller_requests{exported_namespace="traffic-demo",ingress="traffic-demo-ingress-canary"}[2m]))
      steps:
      - setWeight: 20
      - analysis:
          templates:
          - templateName: nginx-success-rate
      - setWeight: 50
      - pause: {duration: 1m}
  • Prometheus must scrape the ingress controller; label names (exported_namespace, ingress) depend on the controller version and scrape config, check in Prometheus first
  • Failed analysis -> abort -> canary ingress weight back to 0

Debug

kubectl get endpoints traffic-demo-stable traffic-demo-canary
kubectl get ingressclass
kubectl describe ingress traffic-demo-ingress-canary
kubectl logs -n ingress-nginx deploy/ingress-nginx-controller --tail=50
kubectl logs -n argo-rollouts deploy/argo-rollouts | grep -i ingress

# bypass ingress
kubectl port-forward svc/traffic-demo-canary 8082:80
  • Canary ingress not appearing: check stableIngress name, ingress class, and controller logs
  • Controller RBAC must allow ingress create/update (included in install.yaml)

Cleanup

kubectl delete namespace traffic-demo
kubectl delete -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.11.2/deploy/static/provider/cloud/deploy.yaml