kubernetes ingress ab test

example of how ingress can be used for ab tests

G request request inga ingress a request->inga 50% ingb ingress b request->ingb 50% svca service a inga->svca svcb service b ingb->svcb poda pod a svca->poda podb pod b svcb->podb

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-a
data:
  index.html: |
    <style>html,body{display:flex;align-items:center;justify-content:center;font-size:25vh}</style>
    A
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-b
data:
  index.html: |
    <style>html,body{display:flex;align-items:center;justify-content:center;font-size:25vh}</style>
    B
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-a
  labels:
    app: nginx-a
spec:
  replicas: 1
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  selector:
    matchLabels:
      app: nginx-a
  template:
    metadata:
      labels:
        app: nginx-a
      annotations:
        owner: [email protected]
        repository: http://hg.nginx.org/nginx/
    spec:
      volumes:
        - name: nginx-a
          configMap:
            name: nginx-a
      containers:
      - name: nginx-a
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        resources:
          limits:
            cpu: 500m
            memory: 500Mi
          requests:
            cpu: 50m
            memory: 100Mi
        readinessProbe:
          failureThreshold: 1
          periodSeconds: 5
          timeoutSeconds: 1
          successThreshold: 1
          tcpSocket:
            port: 80
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: /
            port: 80
            scheme: HTTP
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
        startupProbe:
          failureThreshold: 10
          httpGet:
            path: /
            port: 80
            scheme: HTTP
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
        volumeMounts:
          - name: nginx-a
            mountPath: /usr/share/nginx/html
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: poolDestination
                    operator: In
                    values:
                      - app
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-a
  labels:
    app: nginx-a
spec:
  type: ClusterIP
  selector:
    app: nginx-a
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-b
  labels:
    app: nginx-b
spec:
  replicas: 1
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  selector:
    matchLabels:
      app: nginx-b
  template:
    metadata:
      labels:
        app: nginx-b
      annotations:
        owner: [email protected]
        repository: http://hg.nginx.org/nginx/
    spec:
      volumes:
        - name: nginx-b
          configMap:
            name: nginx-b
      containers:
      - name: nginx-b
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        resources:
          limits:
            cpu: 500m
            memory: 500Mi
          requests:
            cpu: 50m
            memory: 100Mi
        readinessProbe:
          failureThreshold: 1
          periodSeconds: 5
          timeoutSeconds: 1
          successThreshold: 1
          tcpSocket:
            port: 80
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: /
            port: 80
            scheme: HTTP
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
        startupProbe:
          failureThreshold: 10
          httpGet:
            path: /
            port: 80
            scheme: HTTP
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
        volumeMounts:
          - name: nginx-b
            mountPath: /usr/share/nginx/html
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: poolDestination
                    operator: In
                    values:
                      - app
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-b
  labels:
    app: nginx-b
spec:
  type: ClusterIP
  selector:
    app: nginx-b
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-a
  labels:
    app: nginx-a
spec:
  ingressClassName: external
  rules:
  - host: nginx-ab.mac-blog.org.ua
    http:
      paths:
      - backend:
          service:
            name: nginx-a
            port:
              number: 80
        path: /
        pathType: ImplementationSpecific
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-b
  labels:
    app: nginx-b
  annotations:
    owner: [email protected]
    repository: http://hg.nginx.org/nginx/
    # POI: enables traffic split 50%
    nginx.ingress.kubernetes.io/canary: "true"
    nginx.ingress.kubernetes.io/canary-weight: "50"
spec:
  ingressClassName: external
  rules:
  # POI: DNS is same as in previous ingress
  - host: nginx-ab.mac-blog.org.ua
    http:
      paths:
      - backend:
          service:
            name: nginx-b
            port:
              number: 80
        path: /
        pathType: ImplementationSpecific

the key point here is that we are adding second ingress looking at same domain with following annotations:

nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-weight: "50"

we can test it like so:

curl -s --resolve nginx-ab.dev.rabota.ua:80:20.13.179.68 http://nginx-ab.dev.rabota.ua/ | grep -E "A|B"

original article