Single docker image thats being deployed as 2 different services thru kubernetes and helm. Change context path of app

July 27, 2019, at 4:20 PM

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 ?

ex: Looks like below

apiVersion: apps/v1
kind: Deployment
  name: {{ include "fullname" . }}-bg
  {{- include "labels" . }}
  replicas: {{ .replicaCount }}
    matchLabels: {{ include "name" . }}-bg {{ .Release.Name }}
    type: Recreate 
    rollingUpdate: null 
      labels: {{ include "name" . }}-bg {{ .Release.Name }}
        - name: {{ .Chart.Name }}
          image: "{{ .image.repository }}:{{ .image.tag }}"
          imagePullPolicy: {{ .image.pullPolicy }}
            - name: SERVICE_NAME
              value: "ServiceB"
            - name:
              value: "true"
            - configMapRef:
                name: {{ include "commmonBaseName" . }}-configmap
Answer 1

There are few approaches into setting up the context path of an app.

  • 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 .war file you put in the webapp directory. Or, if it is a Spring Boot 2.X app, you could set it up with the Spring Boot property server.servlet.context-path, which can be also passed via an environment variable, specifically SERVER_SERVLET_CONTEXT_PATH. So, to give an example, in the container in your deployment pod spec:
    value: "/ServiceB"

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.

  • Using Ingress objects: If you have an Ingress controller running and properly configured, you can create an Ingress 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 /) but configure the context path from the Ingress. An example is the following, assuming you use Nginx Ingress Controller
apiVersion: extensions/v1beta1
kind: Ingress
  name: service-a-ingress
  annotations: /$1
    - host:
          - path: /ServiceA/(.*)
              serviceName: service-a
              servicePort: service-a-port

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

See this page to lear more about ingresses.

  • You can use an HTTP router software such as skipper to handle all this HTTP traffic configuration in-cluster.

  • If you use a service mesh solution such as Istio, they give you many ways to manage the traffic inside the mesh.

Rent Charter Buses Company
How do generate pdf file from word template using Apache PDFBox in Java?

How do generate pdf file from word template using Apache PDFBox in Java?

I have a word template where I put placeholders and I want replace those placeholders with data coming from databaseI don't want to use iText library due to the licensing problem

Reading a java program

Reading a java program

I am trying to read a java program

Best place to validate constructor input

Best place to validate constructor input

I'm not sure what the best practice for the location of validating input for my constructor would be, I could put logic outside of the constructor or accept the input and do logic to check inside the constructor