OpenShift 4.3是否支持路径重写?原3.11注解迁移问题咨询
在OpenShift 4.3中实现路径重写的方案
没问题,OpenShift 4.3完全支持路径重写,只是不再使用旧的ingress.bluemix.net/rewrite-path注解,而是采用OpenShift Router(基于HAProxy)提供的原生注解来实现。针对你的需求——保持原有主机地址不变,分别为UI和API应用添加/aps-ui/和/aps-api/路径并完成重写,具体方案如下:
核心注解说明
OpenShift 4.x中用于路径重写的关键注解是:
haproxy.router.openshift.io/rewrite-target
这个注解用来指定转发到后端服务时的目标路径,配合Route的spec.path字段,就能实现路径的重写映射。
UI应用的Route配置示例
假设你的UI应用对应的Kubernetes Service名为aps-ui-service,服务端口为8080,原有主机地址为your-existing-host.com。创建Route时通过以下配置实现路径重写:
apiVersion: route.openshift.io/v1 kind: Route metadata: name: aps-ui-route annotations: # 将访问路径重写为应用根路径/ haproxy.router.openshift.io/rewrite-target: / spec: host: your-existing-host.com # 对外暴露的路径 path: /aps-ui to: kind: Service name: aps-ui-service port: targetPort: 8080
这个配置的效果是:当用户访问https://your-existing-host.com/aps-ui/或其子路径(如https://your-existing-host.com/aps-ui/dashboard)时,OpenShift Router会自动将路径重写为/或/dashboard,转发给后端的UI服务,同时保持主机地址不变。
API应用的Route配置示例
同理,针对API应用,只需调整path和对应的后端服务名称即可:
apiVersion: route.openshift.io/v1 kind: Route metadata: name: aps-api-route annotations: haproxy.router.openshift.io/rewrite-target: / spec: host: your-existing-host.com path: /aps-api to: kind: Service name: aps-api-service port: targetPort: 8080
额外注意事项
- 如果你的API应用本身的接口路径前缀是
/api,可以将rewrite-target的值改为/api,这样访问/aps-api/users会被重写为/api/users转发给后端服务。 - 配置完成后,可以通过
oc get route aps-ui-route aps-api-route命令检查Route的状态,确保HOST/PORT字段显示正确的主机地址。 - 测试时直接访问对应的路径,验证是否能正常加载应用内容,若出现404错误,检查
rewrite-target和后端服务的路径是否匹配。
内容的提问来源于stack exchange,提问作者Mansi
相关产品推荐
相关产品推荐

