Tomcat

Build a bin-label servlet and run it on OpenShift with JBoss Web Server 6.2, the Red Hat Tomcat 10 image, an S2I build, and HTTP probes.

Updated CompTech Team

Open Liberty answered which bin a SKU sits in. This lab prints the label a picker sticks on that bin. The server is Tomcat 10, from JBoss Web Server 6.2 (OpenJDK 21). That image is the Tomcat build made for OpenShift: it runs as a non-root user, and its S2I scripts compile a Maven WAR and copy it into webapps.

The label is plain text. GET /labels/GPU-NODE returns one line. Health is two servlets you add, because Tomcat does not publish MicroProfile Health the way EAP and Liberty do.

Prerequisites

  • JDK 21 and Maven
  • OpenShift 4.x and oc, with a pull secret that can read registry.redhat.io

JBoss Web Server 6.2.5 is the release the 6.2 image stream was updated to. If your catalog has a newer 6.2 tag, use that tag in the import below.

Step 1: The WAR

mkdir labels && cd labels
mkdir -p src/main/java/com/comptech/labels

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.comptech</groupId>
  <artifactId>labels</artifactId>
  <version>1.0.0</version>
  <packaging>war</packaging>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.finalName>ROOT</project.build.finalName>
  </properties>
  <dependencies>
    <dependency>
      <groupId>jakarta.servlet</groupId>
      <artifactId>jakarta.servlet-api</artifactId>
      <version>6.0.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
</project>

ROOT.war deploys at the context root. Tomcat 10 speaks Jakarta Servlet 6, so the imports are jakarta.servlet, not javax.servlet.

src/main/java/com/comptech/labels/LabelServlet.java:

package com.comptech.labels;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/labels/*")
public class LabelServlet extends HttpServlet {
    private static final Map<String, String> NAMES = Map.of(
        "SRV-RACK", "Rack server", "GPU-NODE", "GPU node", "TOR-SW", "Top-of-rack switch");

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String path = req.getPathInfo();
        String sku = path == null ? "" : path.substring(1);
        String name = NAMES.get(sku);
        if (name == null) {
            resp.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        String warehouse = System.getenv().getOrDefault("APP_WAREHOUSE", "lab");
        resp.setContentType("text/plain");
        resp.getWriter().print(sku + " " + name + " " + warehouse + "\n");
    }
}

Servlet classes are public, one per file. LiveServlet.java:

package com.comptech.labels;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/health/live")
public class LiveServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/plain");
        resp.getWriter().print("UP\n");
    }
}

ReadyServlet.java:

package com.comptech.labels;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/health/ready")
public class ReadyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String warehouse = System.getenv("APP_WAREHOUSE");
        if (warehouse == null || warehouse.isBlank()) {
            resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
            return;
        }
        resp.setContentType("text/plain");
        resp.getWriter().print("UP\n");
    }
}

Liveness is the process. Readiness is the warehouse code. A missing APP_WAREHOUSE takes the pod out of the Service and does not restart it. The same split is in the Spring Boot lab.

mvn -B package
ls target/ROOT.war

Step 2: ImageStream and S2I

JWS is an S2I builder. You do not write a Containerfile for this lab. The assemble script runs Maven and copies every WAR under target/ into Tomcat's webapps. ARTIFACT_DIR overrides that directory. The default is target.

oc new-project labels
oc import-image jws62-openjdk21 \
  --from=registry.redhat.io/jboss-webserver-6/jws62-openjdk21-openshift-rhel8:6.2.5 \
  --confirm
oc get imagestream jws62-openjdk21
oc new-build --binary --strategy=source --name=labels \
  --image-stream=jws62-openjdk21:6.2.5
oc start-build labels --from-dir=. --follow

--strategy=source is S2I. The build's output is the ImageStream tag labels:latest in this project. If import cannot find tag 6.2.5, open the image in the Red Hat catalog and import the 6.2 tag your entitlement lists. An administrator who imported the JWS templates into the openshift project already has jboss-webserver62-openjdk21-tomcat10-openshift-ubi8.

Add target/ to .dockerignore only if you switch this build to a docker strategy. An S2I binary build uploads the directory as given, and the builder compiles inside the pod, so a local target/ is wasted upload. Delete it before oc start-build, or rely on Maven producing a fresh ROOT.war in the build pod. The assemble script's own Maven run is the one that matters.

Step 3: Deployment and probes

deploy/labels.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: labels
  annotations:
    image.openshift.io/triggers: >-
      [{"from":{"kind":"ImageStreamTag","name":"labels:latest"},"fieldPath":"spec.template.spec.containers[?(@.name==\"labels\")].image"}]
spec:
  replicas: 2
  selector:
    matchLabels: { app: labels }
  template:
    metadata:
      labels: { app: labels }
    spec:
      containers:
        - name: labels
          image: image-registry.openshift-image-registry.svc:5000/labels/labels:latest
          ports: [{ containerPort: 8080 }]
          env:
            - name: APP_WAREHOUSE
              valueFrom: { configMapKeyRef: { name: labels-config, key: warehouse } }
          startupProbe:
            httpGet: { path: /health/live, port: 8080 }
            failureThreshold: 30
            periodSeconds: 2
          readinessProbe:
            httpGet: { path: /health/ready, port: 8080 }
            periodSeconds: 10
          livenessProbe:
            httpGet: { path: /health/live, port: 8080 }
            periodSeconds: 10
          resources:
            requests: { cpu: 100m, memory: 256Mi }
            limits: { memory: 512Mi }
---
apiVersion: v1
kind: Service
metadata: { name: labels }
spec:
  selector: { app: labels }
  ports: [{ port: 80, targetPort: 8080 }]
---
apiVersion: route.openshift.io/v1
kind: Route
metadata: { name: labels }
spec:
  to: { kind: Service, name: labels }
  tls: { termination: edge, insecureEdgeTerminationPolicy: Redirect }

Tomcat on this image listens on 8080. The image-change annotation is the same one the EAP lab uses. A DeploymentConfig would put that trigger under spec.triggers instead of an annotation. This series applies Deployments.

oc create configmap labels-config --from-literal=warehouse=dhk-1
oc apply -f deploy/labels.yaml
oc rollout status deployment/labels
curl -sk "https://$(oc get route labels -o jsonpath='{.spec.host}')/labels/GPU-NODE"
# GPU-NODE GPU node dhk-1
curl -sk "https://$(oc get route labels -o jsonpath='{.spec.host}')/health/ready"

Remove the warehouse. The pod stays Running and leaves the Service:

oc set data configmap/labels-config warehouse=
oc rollout restart deployment/labels
oc get pods
oc set data configmap/labels-config warehouse=dhk-1
oc rollout restart deployment/labels

The servlet reads the variable at request time, but a ConfigMap env value is fixed when the pod starts, so the restart is required. After a code change, oc start-build labels --from-dir=. --follow and the trigger rolls the Deployment.

Troubleshooting

Symptom Likely cause Fix
Import of tag 6.2.5 fails That tag is not on your mirror Import the 6.2 tag the catalog shows for jws62-openjdk21-openshift-rhel8
Build cannot find assemble The image is not the S2I builder Use the OpenShift JWS image, not a plain Tomcat image
404 on /labels/GPU-NODE WAR context is the artifact name finalName is ROOT; oc logs shows which context deployed
Ready is 503 APP_WAREHOUSE is empty oc exec deploy/labels -- printenv APP_WAREHOUSE
javax.servlet compile error Servlet 5 packages, on a Tomcat 10 server Imports are jakarta.servlet

Clean up

oc delete project labels

What's next

JBoss EAP and Open Liberty are the other two servers. Container builds covers the S2I strategy this ImageStream was built with, and the registry the tag was pushed to.