启用 CORS Kubernetes 入口问题

Enable CORS Kubernetes ingress issues

我有一个 Laravel 后端 API 和一个 Angular 前端。我用 Kubernetes ⎈Minikube.

上部署它们
NAME                                      READY   STATUS    RESTARTS      AGE
pod/backend-deployment-bd4f98697-c2scp    1/1     Running   1 (22m ago)   23m
pod/frontend-deployment-8bc989f89-cxj67   1/1     Running   0             23m
pod/mysql-0                               1/1     Running   0             23m

NAME                       TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
service/backend-service    NodePort    10.108.40.53    <none>        8000:30670/TCP   23m
service/frontend-service   NodePort    10.105.57.226   <none>        4200:32353/TCP   23m
service/kubernetes         ClusterIP   10.96.0.1       <none>        443/TCP          25m
service/mysql              ClusterIP   None            <none>        3306/TCP         23m

NAME                                  READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/backend-deployment    1/1     1            1           23m
deployment.apps/frontend-deployment   1/1     1            1           23m

NAME                                            DESIRED   CURRENT   READY   AGE
replicaset.apps/backend-deployment-bd4f98697    1         1         1       23m
replicaset.apps/frontend-deployment-8bc989f89   1         1         1       23m

NAME                     READY   AGE
statefulset.apps/mysql   1/1     23m

我可以用minikube service SERVICE-NAME访问前台服务和后台服务。


我还有一个 Ingress 前端。

NAME               CLASS    HOSTS                          ADDRESS     PORTS   AGE
frontend-ingress   <none>   kubiapp-frontend-group35.com   localhost   80      27m

我可以通过 curl 访问 Ingresscurl http://kubiapp-frontend-group35.com。 ✅
但是,当我在浏览器上检查 URL 时,出现了一些 CORS 错误。


frontend-ingress:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/enable-cors: "true"
    nginx.ingress.kubernetes.io/cors-allow-origin: "*"
    nginx.ingress.kubernetes.io/cors-allow-methods: "PUT, GET, POST, OPTIONS, DELETE"
    nginx.ingress.kubernetes.io/cors-allow-headers: "DNT,X-CustomHeader,X-LANG,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-Api-Key,X-Device-Id,Access-Control-Allow-Origin"
  name: frontend-ingress
  labels:
    name: frontend-ingress
spec:
  rules:
    - host: kubiapp-frontend-group35.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: frontend-service
                port:
                  number: 4200

我不太了解Ingress注解,它是否为前端启用了CORS策略?我不应该为后端做吗?我必须创建后端入口吗?

编辑:

错误信息是:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://backend-service:8000/api/health. (Reason: CORS request did not succeed). Status code: (null).


长话短说:

我可以访问 Ingress URL,但是我有 CORS 错误。如何用Kubernetes

解决

我犯的错误是对前端入口使用了 Kubernetes 注释。 我所要做的就是创建一个后端入口,它使用了注释:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/enable-cors: "true"
    nginx.ingress.kubernetes.io/cors-allow-origin: "*"
    nginx.ingress.kubernetes.io/cors-allow-methods: "PUT, GET, POST, OPTIONS, DELETE"
    nginx.ingress.kubernetes.io/cors-allow-headers: "DNT,X-CustomHeader,X-LANG,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-Api-Key,X-Device-Id,Access-Control-Allow-Origin"
  name: backend-ingress
  labels:
    name: backend-ingress
spec:
  rules:
    - host: kubiapp-backend-group35.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: backend-service
                port:
                  number: 8000

前端应用程序现在可以与 http://kubiapp-backend-group35.com 正确通信。