将流量重定向到 Kubernetes 服务中的 Tomcat 上下文路径

Redirecting traffic to Tomcat context path in Kubernetes service

在我通过像这样更改 server.xml 文件来设置 Tomcat 服务器的上下文路径的场景中:

<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">

    <Context path="${catalinaContextPath}" docBase="${catalina.home}/atlassian-jira" reloadable="false" useHttpOnly="true">
        <Resource name="UserTransaction" auth="Container" type="javax.transaction.UserTransaction"
            factory="org.objectweb.jotm.UserTransactionFactory" jotm.timeout="60"/>
        <Manager pathname=""/>
        <JarScanner scanManifest="false"/>
        <Valve className="org.apache.catalina.valves.StuckThreadDetectionValve" threshold="120" />
    </Context>

</Host>

如果 catalinaContextPath 设置为 /my/new/context 服务器将在 Pod 中启动 URL: localhost:8080/my/new/context。如何更改服务,以便它将到达服务端口 80 的所有流量发送到容器路径 <pod_ip>:8080/my/new/context

这是我当前的服务:

apiVersion: v1
kind: Service
metadata:
namespace: jira
name: jira
spec:
selector:
    app: jira
    component: jira
ports:
- protocol: TCP
  name: serverport
  port: 80
  targetPort: 8080

我的用例是,我正在部署这个 this JIRA docker image in a Pod and I set the context path using the environment variable CATALINA_CONTEXT_PATH as specified in this documentation当我尝试访问它时,结果为 404。我认为这是因为流量被重定向到 <pod_ip>:8080<pod_ip>:8080 上没有任何内容 运行,因为 tomcat 已于 <pod_ip>:8080/my/new/context

启动

编辑: 这是我正在使用的ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  namespace: {{ .Values.global.app }}
  name: jira-ingress
  annotations: 
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: myhost
    http:
      paths:
      - path: /dev/jira(/|$)(.*)
        backend:
          serviceName: jira
          servicePort: 80
      - path: /prod/jira(/|$)(.*)
        backend:
          serviceName: jira
          servicePort: 80

每当我访问 myhost/dev/jira 时,我都需要它来转到我的 JIRA 实例。

这对于服务是不可能的...我强烈建议使用 Ingress 路径。

看这里:

由于您的应用程序 "real" root 是 /my/new/context,您可以重写匹配 /dev/jira URI 使用 Nginx's AppRoot:

If the Application Root is exposed in a different path and needs to be redirected, set the annotation nginx.ingress.kubernetes.io/app-root to redirect requests for /.

如果您使用此方法,则无需使用 rewrite-target 的捕获组。

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  namespace: {{ .Values.global.app }}
  name: jira-ingress
  annotations: 
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/app-root: /my/new/context
spec:
  rules:
  - host: myhost
    http:
      paths:
      - path: /dev/jira(/|$)(.*)
        backend:
          serviceName: jira
          servicePort: 80
      - path: /prod/jira(/|$)(.*)
        backend:
          serviceName: jira
          servicePort: 80