<p>I have an isomorphic JavaScript app that uses Vue's SSR plugin running on K8s. This app can either be rendered server-side by my Express server with Node, or it can be served straight to the client as with Nginx and rendered in the browser. It works pretty flawlessly either way.</p>

<p>Running it in Express with SSR is a much higher resource use however, and Express is more complicated and prone to fail if I misconfigure something. Serving it with Nginx to be rendered client side on the other hand is dead simple, and barely uses any resources in my cluster.</p>

<p>What I want to do is have a few replicas of a pod running my Express server that's performing the SSR, but if for some reason these pods go down, I want a fallback service on the ingress that will serve from a backup pod with just Nginx serving the client-renderable code.</p>

<p>Setting up the pods is easy enough, but how can I tell an ingress to serve from a different service then normal if the normal service is unreachable and/or responding too slowly to requests?</p>

<p>The easiest way to setup NGINX Ingress to meet your needs is by using the <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#default-backend" rel="nofollow noreferrer">default-backend</a> annotation. </p>

<blockquote>
  <p>This annotation is of the form 
  <code>nginx.ingress.kubernetes.io/default-backend: &lt;svc name&gt;</code>  to specify
  a custom default backend. This  <code>&lt;svc name&gt;</code>  is a reference to a
  service inside of the same namespace in which you are applying this
  annotation. This annotation overrides the global default backend.</p>
  
  <p>This service will be handle the response when the service in the
  Ingress rule does not have active endpoints. It will also handle the
  error responses if both this annotation and the  <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#custom-http-errors" rel="nofollow noreferrer">custom-http-errors
  annotation</a>  is set.</p>
</blockquote>

<p>Example: </p>

<pre class="lang-yaml prettyprint-override"><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-app-ingress
  namespace: default
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: "/"
    nginx.ingress.kubernetes.io/custom-http-errors: '404'
    nginx.ingress.kubernetes.io/default-backend: default-http-backend
spec:
  rules:
  - host: myapp.mydomain.com
    http:
      paths:
      - path: "/"
        backend:
          serviceName: custom-http-backend
          servicePort: 80
</code></pre>

<p>In this example NGINX is serving custom-http-backend as primary resource and if this service fails, it will redirect the end-user to default-http-backend. </p>

<p>You can find more details on this example <a href="https://koopakiller.com/kubernetes/nginx-ingress/ingress/2020/02/13/nginx-ingress-default-backend.html" rel="nofollow noreferrer">here</a>.</p>
