0
0
Kubernetesdevops~7 mins

A/B testing with Ingress in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
A/B testing helps you compare two versions of an app by sending some users to one version and others to a different version. Using Kubernetes Ingress, you can control traffic routing to different app versions easily without changing your app code.
When you want to test a new feature on a small group of users before full release
When you want to compare performance between two app versions running in parallel
When you want to gradually shift traffic from an old version to a new version
When you want to route users based on specific rules like cookies or headers
When you want to run experiments to improve user experience without downtime
Config File - ingress-ab-testing.yaml
ingress-ab-testing.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    nginx.ingress.kubernetes.io/canary: "true"
    nginx.ingress.kubernetes.io/canary-weight: "20"
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: stable-service
            port:
              number: 80
      - path: /
        pathType: Prefix
        backend:
          service:
            name: canary-service
            port:
              number: 80

This Ingress routes traffic for example.com. The stable-service handles most traffic. The canary-service is marked as a canary with 20% traffic using annotations. This means 20% of users go to the canary version for testing, while 80% use the stable version.

Commands
This command creates or updates the Ingress resource that controls traffic routing for A/B testing between stable and canary services.
Terminal
kubectl apply -f ingress-ab-testing.yaml
Expected OutputExpected
ingress.networking.k8s.io/example-ingress created
This command checks that the Ingress resource is created and shows its current status and rules.
Terminal
kubectl get ingress example-ingress
Expected OutputExpected
NAME CLASS HOSTS ADDRESS PORTS AGE example-ingress <none> example.com 192.168.99.100 80 10s
This command shows detailed information about the Ingress, including annotations and backend services used for routing.
Terminal
kubectl describe ingress example-ingress
Expected OutputExpected
Name: example-ingress Namespace: default Address: 192.168.99.100 Default backend: <none> Rules: Host Path Backends ---- ---- -------- example.com / stable-service:80 (10.0.0.1:80) / canary-service:80 (10.0.0.2:80) Annotations: nginx.ingress.kubernetes.io/canary: true nginx.ingress.kubernetes.io/canary-weight: 20 Events: <none>
This command verifies that the canary version pods are running and ready to receive traffic.
Terminal
kubectl get pods -l app=canary-service
Expected OutputExpected
NAME READY STATUS RESTARTS AGE canary-service-5d8f7f7d7b-abc12 1/1 Running 0 5m
Key Concept

If you remember nothing else from this pattern, remember: Kubernetes Ingress can split traffic between stable and canary services using annotations to enable safe A/B testing.

Common Mistakes
Not adding the canary annotations to the Ingress resource
Without annotations, all traffic goes to the stable service, so no A/B testing happens.
Always add 'nginx.ingress.kubernetes.io/canary: "true"' and 'nginx.ingress.kubernetes.io/canary-weight' annotations to enable traffic splitting.
Using the same path for both stable and canary without specifying canary annotations
Ingress will route all traffic to the first matching backend, ignoring the canary service.
Use canary annotations and weights to tell Ingress how to split traffic between services.
Not verifying that canary pods are running before applying Ingress
Traffic sent to canary service will fail if pods are not ready, causing errors for users.
Check canary pods are running and ready with 'kubectl get pods -l app=canary-service' before routing traffic.
Summary
Create an Ingress resource with canary annotations to split traffic between stable and canary services.
Apply the Ingress and verify it is created and configured correctly.
Check that canary pods are running to safely receive test traffic.
Use annotations to control the percentage of traffic sent to the canary version.