将访问控制源添加到 Kubernetes

Cec*_*uez 5 kubernetes

由于 Android Cordova 应用程序中出现以下错误,我尝试允许访问控制源:

http://localhost:8080/#/: Line 0 : Access to XMLHttpRequest at 'https://api.v2.domain.com/api/v1/users/me/favorites?lat=42.5467&lng=-83.2113&radius=10.0&limit=5&search=' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Run Code Online (Sandbox Code Playgroud)

我试图找出在 Kubernetes 中添加它的位置 - 我假设它在服务或部署中的某个位置。

两者都是:

apiVersion: v1
kind: Service
metadata:
  name: domain-server
  annotations:
    dns.alpha.kubernetes.io/external: "api.v2.domain.com"
    service.beta.kubernetes.io/aws-load-balancer-ssl-cert: arn:aws:acm:us-east-2:152660121739:certificate/8efe41c4-9a53-4cf6-b056-5279df82bc5e
    service.beta.kubernetes.io/aws-load-balancer-backend-protocol: http
spec:
  type: LoadBalancer
  selector:
    app: domain-server
  ports:
    - port: 443
      targetPort: 8080
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: domain-server
spec:
  replicas: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 3
  revisionHistoryLimit: 10
  template:
    metadata:
      labels:
        app: domain-server
    spec:
      containers:
        - name: domain-server
          image: "152660121739.dkr.ecr.us-east-2.amazonaws.com/domain-server"
          imagePullPolicy: Always
          resources:
            limits:
              memory: "1200Mi"
            requests:
              memory: "900Mi"
              cpu: "200m"
          ports:
            - name: http
              containerPort: 8080
     ...
Run Code Online (Sandbox Code Playgroud)

这是放置标题的正确位置吗?如果是这样,如何将 CORS 添加到 Kubernetes 中?我注意到一些建议,例如 Nginx ingresses,但我使用的应用程序不使用 Nginx。

Aki*_*zer 1

这个问题与 Kubernetes 无关。浏览器强制执行 CORS,请在此处查看参考:https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS。您还可以使用curlpostman并查看内容而不会出现CORS错误。

通常 nginx 服务器可以解决这个问题,kubernetes-nginx 也没有什么不同。它基本上使用反向代理来控制服务。检查此参考以开始通过入口修复 CORS 错误: https: //kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#enable-cors