如何在不使用nginx的情况下启用带有入口的CORS? [英] How to enable CORS with ingress without using nginx?

查看:71
本文介绍了如何在不使用nginx的情况下启用带有入口的CORS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Kubernetes设置RESTful API应用程序.我有一个带有群集的准系统设置,一个静态IP地址,使用NodePort类型的暴露服务部署的应用程序以及一个配置有SSL托管证书的入口.我需要启用CORS,但尚未使用nginx.是否有可能,还是我需要安装nginx而不是默认的gce类?

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?

这是我的ingress.yaml

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

要检查我是否正在按如下方式使用curl:

To check I am using curl as follows:

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

我希望响应中包含Access-Control-Allow-*

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

推荐答案

当前 CORS GCP L7负载平衡器不支持该机制,因此 ingress-gce 入口控制器确实包含适当的机制注释来完成此功能,找到此处相关的堆栈线程.

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.

如果您考虑通过 Nginx Ingress Controller 替换本地GCP Ingress类以启用跨源请求然后您可能必须在原始Ingress资源定义中至少包含两个注释:

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"

我通过GCP社区教程找到了很棒的指南解释了GKE中的Nginx Ingress Controller实现过程.

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

市场上还有其他可以利用CORS请求的L7代理框架,例如跳过器等.

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

这篇关于如何在不使用nginx的情况下启用带有入口的CORS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆