如何在不使用 nginx 的情况下通过 ingress 启用 CORS?

2024-03-04

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

这是我的 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,如下所示:

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

我期望响应包含 Access-Control-Allow-*


现在CORS https://en.wikipedia.org/wiki/Cross-origin_resource_sharingGCP L7 负载均衡器不支持机制,因此入口-GCE https://github.com/kubernetes/ingress-gce入口控制器确实包含适当的注解 https://github.com/kubernetes/ingress-gce/blob/master/pkg/annotations/ingress.go要完成此功能,请找到here https://stackoverflow.com/questions/48190429/how-to-enable-cors-in-gke-with-ingress相关的堆栈线程。

如果您考虑将原生 GCP Ingress 类替换为Nginx 入口控制器 https://kubernetes.github.io/ingress-nginx/为了启用跨域 https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#enable-cors请求,那么您可能必须在原始 Ingress 资源定义中至少包含两个注释:

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

我找到了一个很棒的指导方针 https://cloud.google.com/community/tutorials/nginx-ingress-gke通过 GCP 社区教程解释了 GKE 中的 Nginx Ingress Controller 实现过程。

市场上还有其他 L7 代理框架可以利用 CORS 请求,例如Traefik https://docs.traefik.io/v2.0/middlewares/headers/, Skipper https://github.com/zalando/skipper/blob/master/docs/kubernetes/ingress-controller.md, etc.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在不使用 nginx 的情况下通过 ingress 启用 CORS? 的相关文章

随机推荐