<p>I am building a local testing environment using <code>Kubernetes Minikube</code> cluster. &quot;Some&quot; of the backend APIs and the database were deployed inside the cluster and each of these APIs has its dedicated URL created using <code>ingress</code>. In addition to that, I've deployed &quot;all&quot; the backend APIS in <code>AWS ECS</code>, each of these APIs has a <code>Route53</code> record, and the frontend is connected to these APIs in the &quot;.env&quot; file. What I want to achieve is, when I run <code>yarn start</code> on my frontend(React) outside of the cluster, the frontend should first check if the service is presented in the local <code>Minikube</code> cluster, if it can't find the service in the cluster, it will connect to the one in <code>AWS ECS</code>. Is there a way to achieve this?</p>
<p>For better illustration, this is my frontend <code>.env</code> file</p>
<pre><code>SCHEDULE_API_URL = http://schedule.learning.com
DASHBOARD_API_URL = http://dashboard.learning.com
</code></pre>
<p>backend deployment yaml</p>
<pre><code>kind: Deployment
apiVersion: apps/v1
metadata:
  name: schedule
  labels:
    name: schedule
spec:
  replicas: 1
  selector:
    matchLabels:
      app: schedule
  template:
    metadata:
      labels:
        app: schedule
    spec:
      containers:
      - name: schedule
        image: &lt;image_name&gt;
        ports:
        - containerPort: 8080
        imagePullPolicy: Never
      restartPolicy: Always
---
kind: Service
apiVersion: v1
metadata:
  name: schedule-svc
  labels:
    app: schedule
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
 name: schedule-ingress
 namespace: default
 annotations:
   kubernetes.io/ingress.class: &quot;nginx&quot;
   nginx.ingress.kubernetes.io/rewrite-target: /
   nginx.ingress.kubernetes.io/proxy-read-timeout: &quot;12h&quot;
   nginx.ingress.kubernetes.io/ssl-redirect: &quot;false&quot;
spec:
 rules:
   - host: schedule.learning.com
     http:
       paths:
         - path: /
           pathType: Prefix
           backend:
             service:
               name: schedule-svc
               port:
                 number: 80

</code></pre>
<p>Backend APIs that have been deployed in AWS ECS</p>
<pre><code>SCHEDULE API  --&gt; Route 53 Record: http://schedule.learning.com
DASHBOARD API  --&gt; Route 53 Record: http://dashboard.learning.com
</code></pre>
<p>Based on the above example, when I run the frontend locally and outside of the cluster, it will connect to the <code>Schedule API</code>
<a href="http://schedule.learning.com" rel="nofollow noreferrer">http://schedule.learning.com</a> in the local cluster, but connect to the dashboard API <a href="https://dashboard.learning.com" rel="nofollow noreferrer">https://dashboard.learning.com</a> in ECS since it couldn't find it in the cluster.</p>
<p>Note:</p>
<ul>
<li>The frontend is deployed outside of the cluster</li>
<li>The API in the <code>local</code> cluster will have the same URL as the <code>ECS</code> one so that the frontend <code>env</code> doesn't have to be modified.</li>
<li>Even the  <code>Minikube</code> cluster is deployed locally, it is still a virtual machine</li>
</ul>

<p>I think more versatile solution would be if you would have <a href="https://kubernetes.io/docs/concepts/services-networking/ingress/" rel="nofollow noreferrer">Ingress</a> with two hosts solution. This way you wouldn't need any infrastructure changes (only a few adjustments in names).</p>
<p>In this solution, you would need to:</p>
<ul>
<li>Enable <code>Ingress</code> in <code>Minikube</code> using <code>$ minikube addons enable ingress</code></li>
<li>Create <code>LoadBalancer</code> to service on cloud environment</li>
<li>Have 2 deployments in Cloud environment and Minikube.</li>
<li>Add to <code>/etc/hosts</code> aliases for <code>$ minikube ip</code> address like</li>
</ul>
<pre><code>192.168.49.2 resource1.domain1.biz
192.168.49.2 resource2.domain2.biz
</code></pre>
<ul>
<li>Have 2 services, one which pointing to deployment in Minikube, second should be without <code>selector</code>.</li>
<li>Create <code>Endpoint</code> for second service pointing to <code>LoadBalancer</code> IP.</li>
</ul>
<p><strong>Configs</strong></p>
<p><em><strong>Ingress</strong></em></p>
<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress
spec:
  rules:
  - host: resource1.domain1.biz
    http:
      paths:
      - path: /first
        backend:
          serviceName: first-deployment
          servicePort: 8080
  - host: resource2.domain2.biz
    http:
      paths:
      - path: /second
        backend:
          serviceName: my-svc
          servicePort: 8080
</code></pre>
<p><em><strong>Endpoint</strong></em></p>
<pre><code>apiVersion: v1
kind: Endpoints
metadata:
  name: my-svc
subsets:
  - addresses:
      - ip: 34.91.XX.27 
    ports:
      - port: 80
</code></pre>
<p><strong>Tests</strong>*</p>
<pre><code>user@minikube-20:~$ curl -H &quot;HOST: resource1.domain1.biz&quot; http://192.168.49.2/first
Hello, world!
Version: 1.0.0
Hostname: first-deployment-5df6dc8d8b-mmx8c
user@minikube-20:~$ curl -H &quot;HOST: resource2.domain2.biz&quot; http://192.168.49.2/second
Hello, world!
Version: 1.0.0
Hostname: gke-deployment-85b75bf4f9-jgvhz
</code></pre>
<p>In this scenario you could also try to use <a href="https://kubernetes.io/docs/concepts/services-networking/service/#externalname" rel="nofollow noreferrer">ExternalName service</a></p>
<p>However, if you would like to achieve what you mention originally, which is creating <code>automatic failover</code> you would need to interference your configuration.</p>
<p>You could use:</p>
<ul>
<li><code>CoreDNS plugin</code> called <a href="https://github.com/coredns/alternate" rel="nofollow noreferrer">Alternate</a></li>
</ul>
<blockquote>
<p>Plugin Alternate is able to selectively forward the query to another upstream server, depending the error result provided by the initial resolver</p>
</blockquote>
<ul>
<li>Some kind of <code>LoadBalancer</code> or <code>proxy</code> with <code>health-check</code> and request retry like in <a href="https://docs.nginx.com/nginx/admin-guide/load-balancer/http-health-check/" rel="nofollow noreferrer">this example</a></li>
</ul>
