<p>We have a single docker image thats being deployed as 2 different services thru kubernetes and helm with names like "ServiceA" and "ServiceB". At the point deploy happens need to set the context path of Tomcat to be something different like /ServiceA and /ServiceB. How can this be done ? is there anything that can be set directly on the yaml ?</p>

<p>ex: Looks like below</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "fullname" . }}-bg
  {{- include "labels" . }}

spec:
  replicas: {{ .replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "name" . }}-bg
      app.kubernetes.io/instance: {{ .Release.Name }}
  strategy:
    type: Recreate 
    rollingUpdate: null 
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "name" . }}-bg
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .image.repository }}:{{ .image.tag }}"
          imagePullPolicy: {{ .image.pullPolicy }}
          env:           
            - name: SERVICE_NAME
              value: "ServiceB"
            - name: background.jobs.enabled
              value: "true"
          envFrom:
            - configMapRef:
                name: {{ include "commmonBaseName" . }}-configmap
            -
</code></pre>

<p>There are few approaches into setting up the context path of an app.</p>

<ul>
<li>From the app itself: This depends on the language/framework/runtime your application uses. For example, if it's a traditional Java web application that runs on Tomcat, it would be served by default from the context path of the name of the  <code>.war</code> file you put in the <code>webapp</code> directory. Or, if it is a Spring Boot 2.X app, you could set it up with the Spring Boot property <code>server.servlet.context-path</code>, which can be also passed via an environment variable, specifically <code>SERVER_SERVLET_CONTEXT_PATH</code>. So, to give an example, in the container in your deployment pod spec:</li>
</ul>

<pre><code>env:           
  - name: SERVER_SERVLET_CONTEXT_PATH
    value: "/ServiceB"
</code></pre>

<p>However, this kind of app-specific settings are most of the times not needed in Kubernetes, since you can handle those concerns on the outer layers.</p>

<ul>
<li>Using Ingress objects: If you have an Ingress controller running and properly configured, you can create an <code>Ingress</code> that will manage path prefix stripping, and other HTTP Layer7 concerns. This means, you can leave your application itself as-is (like serving from root context <code>/</code>) but configure the context path from the Ingress. An example is the following, assuming you use <a href="https://kubernetes.github.io/ingress-nginx/" rel="nofollow noreferrer">Nginx Ingress Controller</a></li>
</ul>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: service-a-ingress
  annotations:
    ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - host: service-a.yourdomain.com
      http:
        paths:
          - path: /ServiceA/(.*)
            backend:
              serviceName: service-a
              servicePort: service-a-port
</code></pre>

<p>Note the capture group <code>(.*)</code> in the <code>path</code>, and <code>$1</code> in the rewrite target - it will rewrite the request paths like <code>/ServiceA/something</code> to <code>/something</code> before forwarding the packet to your backend.</p>

<p>See <a href="https://kubernetes.io/docs/concepts/services-networking/ingress/" rel="nofollow noreferrer">this page</a> to lear more about ingresses.</p>

<ul>
<li><p>You can use an HTTP router software such as <a href="https://github.com/zalando/skipper" rel="nofollow noreferrer">skipper</a> to handle all this HTTP traffic configuration in-cluster.</p></li>
<li><p>If you use a service mesh solution such as <a href="https://istio.io/" rel="nofollow noreferrer">Istio</a>, they give you <a href="https://istio.io/docs/concepts/traffic-management/" rel="nofollow noreferrer">many ways to manage the traffic</a> inside the mesh.</p></li>
</ul>
