<h1>Problem</h1>
<p>I am getting a 503 response when my frontend container tries to contact an endpoint of my backend container while running them both in Kubernetes. When running the containers on my machine locally, all seems fine.</p>
<h1>Details</h1>
<p>My Spring backend has an endpoint &quot;/hello&quot;, which returns &quot;Hello World!&quot;. I have configured the CORS as flexible as possible in hope of ruling this out. Relevant code:</p>
<pre><code>@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping(&quot;/**&quot;)
                .allowedMethods(&quot;*&quot;);
    }

    @Bean
    public CommonsRequestLoggingFilter logFilter() {
        var filter = new CommonsRequestLoggingFilter();
        filter.setIncludeQueryString(true);
        filter.setIncludePayload(true);
        filter.setMaxPayloadLength(10_000);
        filter.setIncludeHeaders(false);
        filter.setBeforeMessagePrefix(&quot;Request: &quot;);
        return filter;
    }
}
</code></pre>
<p>My Kubernetes config is as follows:</p>
<h3>backend.yaml</h3>
<pre><code># Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: knote-backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: knote
  template:
    metadata:
      labels:
        app: knote
    spec:
      containers:
        - name: backend
          image: knote-backend
          ports:
            - containerPort: 8080
          imagePullPolicy: Never
          resources:
            limits:
              cpu: &quot;2&quot;
              memory: &quot;512Mi&quot;
---
# Service
apiVersion: v1
kind: Service
metadata:
  name: knote-backend
spec:
  selector:
    app: knote
    tier: backend
  ports:
    - port: 8080
      targetPort: 8080
</code></pre>
<h2>frontend.yaml</h2>
<pre><code># Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: knote-frontend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: knote
      tier: frontend
  template:
    metadata:
      labels:
        app: knote
        tier: frontend
    spec:
      containers:
        - name: frontend
          image: &quot;knote-frontend&quot;
          ports:
            - containerPort: 3000
          imagePullPolicy: Never
          resources:
            limits:
              cpu: &quot;2&quot;
              memory: &quot;512Mi&quot;
---
# Service
apiVersion: v1
kind: Service
metadata:
  name: knote-frontend
spec:
  selector:
    app: knote
    tier: frontend
  ports:
    - port: 3000
      targetPort: 3000
</code></pre>
<h3>ingress.yaml</h3>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: knote-ingress
  annotations:
    nginx.ingress.kubernetes.io/enable-cors: &quot;true&quot;
    nginx.ingress.kubernetes.io/cors-allow-origin: &quot;http://192.168.49.2&quot;
  labels:
    name: knote-ingress
spec:
  rules:
    - http:
        paths:
          - pathType: Prefix
            path: &quot;/api&quot;
            backend:
              service:
                name: knote-backend
                port:
                  number: 8080
          - pathType: Prefix
            path: &quot;/&quot;
            backend:
              service:
                name: knote-frontend
                port:
                  number: 3000
</code></pre>
<p>Output of &quot;kubectl describe ingress knote-ingress&quot;:</p>
<pre><code>Name:             knote-ingress
Labels:           name=knote-ingress
Namespace:        default
Address:          192.168.49.2
Ingress Class:    nginx
Default backend:  &lt;default&gt;
Rules:
  Host        Path  Backends
  ----        ----  --------
  *           
              /api   knote-backend:8080 (&lt;none&gt;)
              /      knote-frontend:3000 (10.244.0.64:3000)
Annotations:  nginx.ingress.kubernetes.io/cors-allow-origin: http://192.168.49.2
              nginx.ingress.kubernetes.io/enable-cors: true
Events:
  Type    Reason  Age                  From                      Message
  ----    ------  ----                 ----                      -------
  Normal  Sync    31m (x3 over 4h32m)  nginx-ingress-controller  Scheduled for sync
</code></pre>
<h1>Tried</h1>
<p>So far, I have tried</p>
<ul>
<li>Checking whether the containers work locally: they do</li>
<li>Adding/removing the CORS annotations in ingress.yaml: no effect</li>
<li>Adding allowedOrigins of &quot;knote-frontend&quot; and &quot;192.168.49.2&quot;: no effect</li>
<li>curl within knote-backend to localhost which works, but curl within knote-frontend fails</li>
</ul>
<p>EDIT:
This is the request header when trying to reach the '/hello' endpoint:</p>
<pre><code>GET /api/hello HTTP/1.1
Host: 192.168.49.2
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/114.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://192.168.49.2/
DNT: 1
Connection: keep-alive
Sec-GPC: 1
Pragma: no-cache
Cache-Control: no-cache
</code></pre>
<p>It tries to reach /api/hello, which the ingress should redirect to knote-backend:8080, which is where the Spring backend should listen and reply with &quot;Hello World!&quot;. The response header is a typical:</p>
<pre><code>HTTP/1.1 503 Service Temporarily Unavailable
Date: Sun, 25 Jun 2023 12:46:20 GMT
Content-Type: text/html
Content-Length: 190
Connection: keep-alive
</code></pre>

DaPurr
<p>Maybe remove <code>tier: backend</code> from the backend's Service.  or add <code>tier: backend</code> to your backend's Deployment.  Your service is looking for two selectors.  But only one of them is found.</p>
