K8s部署BentoML加载Swagger文档遇CORS错误,求解决方法
解决K8s部署BentoML服务的CORS跨域问题
针对你从Backstage应用访问Swagger docs.json时出现的CORS错误,可通过修改K8s的BentoDeployment配置,借助BentoML的环境变量配置项来开启并设置CORS规则:
- 修改环境变量配置
在现有的BENTOML_CONFIG_OPTIONS中添加CORS相关参数,核心配置项包括:
api_server.cors.enabled=true:开启CORS支持api_server.cors.allow_origins=http://localhost:8081:允许你的Backstage应用所在的源地址(生产环境建议指定具体域名,避免用通配符*)- 可选补充:
api_server.cors.allow_methods=GET,POST,OPTIONS:指定允许的HTTP请求方法 - 可选补充:
api_server.cors.allow_headers=*:允许所有请求头
- 完整配置示例
更新后的BentoDeployment配置如下:
apiVersion: serving.yatai.ai/v2alpha1 kind: BentoDeployment metadata: name: isleroyale spec: autoscaling: maxReplicas: 10 minReplicas: 2 bento: isleroyale ingress: enabled: true resources: limits: cpu: 4000m memory: 4Gi requests: cpu: 2000m memory: 3Gi envs: - name: BENTOML_CONFIG_OPTIONS value: >- tracing.exporter_type=otlp api_server.cors.enabled=true api_server.cors.allow_origins=http://localhost:8081 api_server.cors.allow_methods=GET,POST,OPTIONS
- 生效方式
应用修改后的配置后,重启K8s中的BentoML服务实例,新的CORS规则即可生效,此时Backstage应用就能正常访问docs.json资源。
内容的提问来源于stack exchange,提问作者Yogen Rai
相关产品推荐
相关产品推荐

