K8s/ Angular CORS issue REST server K8s/ Angular CORS issue REST server kubernetes kubernetes

K8s/ Angular CORS issue REST server


When putting an app together in kubernetes, you would typicall try to make all services available on the same domain using Ingress definitions.

For example, if you have a backend service + deployment for a .NET app hosting APIs and a service + deployment hosting your frontend (e.g. nginx + build output of ng build --prod), it could look like:

apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata:   name: my-application spec:   rules:   - host: www.example.com     http:       paths:       - path: /api         backend:           serviceName: api-service           servicePort: 80       - path: /         backend:           serviceName: frontend           servicePort: 80

Such a setup would eliminate cross-origin issues since for the browser using the site, all URLs would be on the same origin.

For developing for such an environment, it makes sense to use Angular's proxy integration so ng serve would also set up forwarding the /api routes to an appropriate development endpoint.

Some ingress controllers also support setting CORS headers on endpoints made available over HTTP, so you would not set them in the API service but on the infrastructure / deployment configuration that defines your cluster-external HTTP endpoints and routing. See these annotations for the nginx ingress controller for example.