Validate resilience with fault injection

Uses: Kong Mesh
TL;DR

Create a MeshFaultInjection that targets the destination proxies and matches the caller by its SPIFFE ID, then check the effect:

  1. Use abort to return an HTTP status code, such as 503, and simulate errors.
  2. Use delay to add a fixed delay and simulate latency.
  3. Use responseBandwidth to slow the response body and simulate a saturated dependency.

Prerequisites

This page is part of the Kong Mesh scenarios: the Kong Air journey series.

Complete the previous page, Manage external services with MeshExternalService before completing this page.

Fault injection tests how callers behave when a destination fails or slows down. In this guide, the check-in-api sidecar fails the requests from flight-control on purpose, so you can see how flight-control handles each failure.

To learn more about what this guide does, read the following reference pages:

Record the baseline

Check that flight-control can call check-in-api before you inject a fault, so you can tell an injected fault apart from a permission denial or a broken deployment:

kubectl exec -n kong-air-production deploy/flight-control -- \
  wget -q -T 5 -O- http://check-in-api.kong-air-production.svc.cluster.local:8080/

The command returns immediately and prints the name of the Pod that responded.

Return errors

  1. Create a MeshFaultInjection that makes the check-in-api sidecar return 503 to flight-control, without forwarding the request to the application:

    kubectl apply -f - <<'EOF'
    apiVersion: kuma.io/v1alpha1
    kind: MeshFaultInjection
    metadata:
      name: test-check-in-api-abort
      namespace: kong-air-production
      labels:
        kuma.io/mesh: kong-air-mesh
    spec:
      targetRef:
        kind: Dataplane
        labels:
          app: check-in-api
      rules:
        - matches:
            - spiffeID:
                type: Exact
                value: spiffe://internal.kongair.com/ns/kong-air-production/sa/flight-control
          default:
            http:
              - abort:
                  httpStatus: 503
                  percentage: 100
    EOF
  2. Call check-in-api from flight-control, and print the response status:

    kubectl exec -n kong-air-production deploy/flight-control -- \
      wget -S -q -T 5 -O /dev/null http://check-in-api.kong-air-production.svc.cluster.local:8080/

    The check-in-api sidecar returns 503 before the request reaches the application, so wget reports a failure:

      HTTP/1.1 503 Service Unavailable
    wget: server returned error: HTTP/1.1 503 Service Unavailable

    It can take a few seconds for the policy to apply, if you still get a 200 response, wait and try again.

  3. Delete the abort policy:

    kubectl delete meshfaultinjection test-check-in-api-abort -n kong-air-production

Add latency

  1. Create a MeshFaultInjection that makes the check-in-api sidecar wait 5 seconds before it forwards each request from flight-control:

    kubectl apply -f - <<'EOF'
    apiVersion: kuma.io/v1alpha1
    kind: MeshFaultInjection
    metadata:
      name: test-check-in-api-latency
      namespace: kong-air-production
      labels:
        kuma.io/mesh: kong-air-mesh
    spec:
      targetRef:
        kind: Dataplane
        labels:
          app: check-in-api
      rules:
        - matches:
            - spiffeID:
                type: Exact
                value: spiffe://internal.kongair.com/ns/kong-air-production/sa/flight-control
          default:
            http:
              - delay:
                  value: 5s
                  percentage: 100
    EOF
  2. Call check-in-api from flight-control, with a wget timeout longer than the delay:

    kubectl exec -n kong-air-production deploy/flight-control -- \
      wget -q -T 15 -O- http://check-in-api.kong-air-production.svc.cluster.local:8080/

    After about 5 seconds, the flight-control sidecar ends the request:

    wget: server returned error: HTTP/1.1 504 Gateway Timeout

    The flight-control-timeout policy from Target workloads and services gives flight-control a 5-second request timeout, so the caller stops waiting before the delayed response arrives.

  3. Delete the delay policy:

    kubectl delete meshfaultinjection test-check-in-api-latency -n kong-air-production

Slow down responses

  1. Create a 3 KiB file in the check-in-api container, which serves it on /manifest.bin. The Pod name that check-in-api returns on / is too short to show a bandwidth limit, and a larger file would take longer than the 5-second request timeout of flight-control:

    kubectl exec -n kong-air-production deploy/check-in-api -c check-in-api -- \
      sh -c 'head -c 3072 /dev/urandom > /tmp/manifest.bin'
  2. Create a MeshFaultInjection that limits each check-in-api response to flight-control to 1 kbps. Unlike a delay, it forwards the request right away and slows the response body on its way back:

    kubectl apply -f - <<'EOF'
    apiVersion: kuma.io/v1alpha1
    kind: MeshFaultInjection
    metadata:
      name: test-check-in-api-throttle
      namespace: kong-air-production
      labels:
        kuma.io/mesh: kong-air-mesh
    spec:
      targetRef:
        kind: Dataplane
        labels:
          app: check-in-api
      rules:
        - matches:
            - spiffeID:
                type: Exact
                value: spiffe://internal.kongair.com/ns/kong-air-production/sa/flight-control
          default:
            http:
              - responseBandwidth:
                  limit: "1kbps"
                  percentage: 100
    EOF
  3. Download the file from flight-control, and print the time before and after the download:

    kubectl exec -n kong-air-production deploy/flight-control -- sh -c \
      'date +%s; wget -q -T 60 -O /dev/null http://check-in-api.kong-air-production.svc.cluster.local:8080/manifest.bin; date +%s'

    The two timestamps are about 3 seconds apart, because the check-in-api sidecar sends the 3 KiB body at 1 KiB per second:

    1792750000
    1792750003

Validate

  1. Delete the throttle policy:

    kubectl delete meshfaultinjection test-check-in-api-throttle -n kong-air-production
  2. Call check-in-api from flight-control:

    kubectl exec -n kong-air-production deploy/flight-control -- \
      wget -q -T 5 -O- http://check-in-api.kong-air-production.svc.cluster.local:8080/

    The command prints the Pod name immediately, as in the baseline, so no fault remains.

FAQs

Check the following:

  • The caller can already reach the destination. If a MeshTrafficPermission denies the caller, it gets 403 Forbidden, and you never see the injected fault.
  • The matches entry matches the caller’s SPIFFE ID. The trust domain comes from the MeshIdentity that issues the caller’s certificate.
  • The mesh treats the destination port as HTTP. On a plain TCP port, the proxy adds no fault filter. For how a port’s protocol is set, see Ports.
  • The new configuration reached the proxy. The control plane pushes policy changes over xDS within about 10 seconds, without a restart.

Yes, but the results are hard to read. When several policies select the same destination, the proxy adds their faults as separate filters and runs them in sequence. An abort that fires stops the request, so a delay from another policy never runs. With one policy at a time, you can attribute each result to the policy that you applied.

Remove the matches entry from the rule. A rule without a caller match applies to every caller of check-in-api.

The blast radius is the set of requests that the fault affects. Three controls limit it, from broadest to narrowest:

  1. targetRef picks the destination proxies. Select a non-production workload, such as one with an env: staging label.
  2. matches picks the callers by spiffeID. Use type: Exact to fault the requests from one workload identity.
  3. percentage picks how often the fault fires. Start at 1 and raise it as your confidence grows.

matches can’t select HTTP headers, methods, or paths, so a fault applies to every request from a matching identity to the targeted workload. For how each selector works, see Policy targeting.

Target the mesh-scoped zone egress, and match the MeshExternalService by its Server Name Indication (SNI). The other destinations that share the zone egress aren’t affected:

apiVersion: kuma.io/v1alpha1
kind: MeshFaultInjection
metadata:
  name: zone-egress-fault-injection
  namespace: kong-mesh-system
  labels:
    kuma.io/mesh: kong-air-mesh
spec:
  targetRef:
    kind: Dataplane
    labels:
      kuma.io/listener-zoneegress: enabled
  rules:
    - matches:
        - sni:
            type: Exact
            value: sni.extsvc.kong-air-mesh.zone1.kong-mesh-system.aeropay-api.80
      default:
        http:
          - abort:
              httpStatus: 503
              percentage: 50

For how to find the SNI, see Manage external services with MeshExternalService.

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!