Kubernetes: tomcat web context listener not called during deployment

37 Views Asked by At

Docker image:

   FROM tomcat:latest
   WORKDIR /usr/local/tomcat/webapps
   COPY my_webapp.war .

The webapp my_webapp.war contains a web listener:

@WebListener
public class MyContextListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {
        // do something, logging
    }
}

Build docker image:

docker build -t my_webapp:latest .

Start the container using docker, the web app is deployed and the MyContextListener is called (seen from logging).

  docker run -p 8091:8080 my_webapp:latest

But using k8s, the web app is deployed, but the MyContextListener is not called.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my_webapp
  labels:
    app: my_webapp
spec:
  selector:
    matchLabels:
      app: my_webapp
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: my_webapp
    spec:
      containers:
        - image: my_webapp:latest
          ports:
            - containerPort: 8080

Inspect the image (based on tomcat)

  docker inspect my_webapp:latest

Contains:

  "Cmd": [
            "catalina.sh",
            "run"
        ],

After overriding the image command by

    spec:
      containers:
        - image: my_webapp:latest
          ports:
            - containerPort: 8080
          command: ["/bin/sh", "-c", "sleep 100000"]

kubectl get pods

 my_webapp-846c7c99b5-s7kqt   1/1     Running   0          8h

Then shell into the container:

   kubectl exec -it my_webapp-846c7c99b5-s7kqt -- /bin/sh
   $ cd /usr/local/tomcat
   $ bin/catalina.sh run

The webapp is deployed and the web listener MyContextListener is called.

any difference between 1) k8s starting the container and running the command, and 2) manually run the command from a separated shell?

0

There are 0 best solutions below