You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求基于Ingress的多命名空间TLS路由配置方案建议

多命名空间微服务的Ingress TLS路由配置方案

核心实现思路

只申请production.application.com一张TLS证书,通过Ingress的路径规则,将不同路径的请求转发到对应命名空间的前端服务,全程保持TLS加密。

Ingress配置示例(以NGINX Ingress Controller为例)

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: multi-project-ingress
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "true" # 强制HTTP转HTTPS
    nginx.ingress.kubernetes.io/use-regex: "true" # 适配路径中的特殊字符
    nginx.ingress.kubernetes.io/proxy-set-header: "X-Forwarded-Prefix /project-1.com" # 可选:给后端传递前缀信息,方便前端处理
spec:
  tls:
  - hosts:
    - production.application.com
    secretName: production-tls-secret # 提前将证书存入这个Secret
  rules:
  - host: production.application.com
    http:
      paths:
      - path: /project-1.com(/|$)(.*)
        pathType: ImplementationSpecific
        backend:
          service:
            name: frontend-1
            namespace: project-1
            port:
              number: 443 # 前端服务的HTTPS端口,若前端用HTTP则改为80
      - path: /project-2.com(/|$)(.*)
        pathType: ImplementationSpecific
        backend:
          service:
            name: frontend-2
            namespace: project-2
            port:
              number: 443
      # 复制上述规则,依次添加project-3、project-4、project-5的路由

关键注意事项

  • 前端路径适配:前端静态资源和路由不能硬编码根路径,需配置基础路径。比如React项目在public/index.html添加<base href="/project-1.com/" />,Vue项目在vue.config.js设置publicPath: '/project-1.com/',避免资源加载失败。
  • Ingress Controller权限:确保Ingress Controller的ServiceAccount有跨命名空间访问Service的权限,主流NGINX Ingress Controller默认配置了足够的RBAC权限,若出现访问拒绝需检查角色绑定。
  • 证书管理:可以用CertManager自动申请和续期production.application.com的证书,避免手动维护证书的麻烦。

替代方案

如果路径路由的适配成本过高,可考虑以下方案:

  • 子域名路由(备选):若后续允许调整域名策略,可使用project-1.production.application.com这类子域名,部分CA支持单域名多SAN证书(一张证书包含多个子域名),无需通配符也能实现,每个子域名对应一个命名空间的前端服务。
  • Gateway API:如果集群支持Kubernetes Gateway API,用Gateway和HTTPRoute资源替代Ingress,实现更标准化的跨命名空间路由,配置逻辑和Ingress类似,但API设计更灵活。

内容的提问来源于stack exchange,提问作者sanjeeth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 04:31:12