首页 > 解决方案 > K8S Ingress 是否提供任何后端目标路径以及端口?

问题描述

我有两个 Web 应用程序在同一个码头运行。

如果我只是点击 ip:port 那么它会带来 UI 应用程序和上下文路径,它会带来另一个 REST 应用程序。

以下是配置:

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" 
      "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="contextPath">/</Set>
    <Set name="war">./webapps/my-ui.war</Set>
</Configure>
<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" 
      "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="contextPath">/api</Set>
    <Set name="war">./webapps/my-rest-api.war</Set>
</Configure>

是否有任何选项可以在入口中提供目标路径?

标签: kuberneteskubernetes-ingress

解决方案


从这里的 Kubernetes 文档中,这是一个入口示例:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /testpath
        backend:
          serviceName: test
          servicePort: 80

您可以根据需要添加任意数量的规则,以将路径映射到正确的服务和端口,在您的情况下,您可以有这样的入口:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: app-ingress
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: ui-service
          servicePort: 80
  - http:
      paths:
      - path: /api
        backend:
          serviceName: rest-api-service
          servicePort: 80

推荐阅读