How to enable CORS with ingress without using nginx?

8/1/2019

I'm trying to setup RESTful API application with Kubernetes. I have a barebones setup with a cluster, static IP address, app deployed with exposed service of type NodePort, and an ingress configured with a managed certificate for SSL. I need to enable CORS and I am not yet using nginx. Is it possible, or do I need to install nginx instead of the default gce class?

Here is my ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: artsdata-ingress
  annotations:
    kubernetes.io/ingress.global-static-ip-name: "artsdasta-static-ip"
    networking.gke.io/managed-certificates: artsdata-certificate
    ingress.kubernetes.io/enable-cors: "true"
spec:
  backend:
    serviceName: artsdata-kg
    servicePort: 80

To check I am using curl as follows:

curl -H "Access-Control-Request-Method: GET" -H "Origin: http://localhost" --head http://db.artsdata.ca

I am expecting the response to include Access-Control-Allow-*

-- Gregory Saumier-Finch
kubernetes-ingress

1 Answer

8/1/2019

Currently CORS mechanism is not supported in GCP L7 load balancer, therefore ingress-gce ingress controller does contain appropriate annotation to accomplish this functionality, find here related Stack thread.

If you consider replacing native GCP Ingress class by Nginx Ingress Controller in order to enable Cross-origin requests then you might have to include at least two annotations in the origin Ingress resource definition:

kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/enable-cors: "true"

I've found a great guideline through GCP community tutorials that explains Nginx Ingress Controller implementation procedure in GKE.

There are also the other L7 proxy frameworks available on the market that can leverage CORS requests like Traefik, Skipper, etc.

-- mk_sta
Source: StackOverflow