回答问题

我正在尝试在运行 traefik 的 kubernetes 中启用 CORS。因此,在响应标头中,我现在可以将 CORS 来源添加到每个主机。我怎样才能做到这一点?下面是 k8 ingress.yml 文件。

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  namespace: test
  labels:
    app: test-cors-app
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
  - host: company.host
    http:
      paths:
      - path: /
        backend:
          serviceName: test-service
          servicePort: 80

更新我有 traefik 1.7

Answers

您可以在入口中使用此注释:traefik.ingress.kubernetes.io/custom-response-headersingress.kubernetes.io/custom-response-headers都可以与 traefik 一起使用。

将您的 ingress.yml 文件修改为:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  namespace: test
  labels:
    app: test-cors-app
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.ingress.kubernetes.io/custom-response-headers: "Access-Control-Allow-Origin:*||Access-Control-Allow-Methods:GET,POST,OPTIONS||Access-Control-Allow-Headers:DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range||Access-Control-Expose-Headers:Content-Length,Content-Range"
spec:
  rules:
  - host: company.host
    http:
      paths:
      - path: /
        backend:
          serviceName: test-service
          servicePort: 80

**相应修改注解属性。它也适用于 traefik 较低版本。参见此处

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐