Spring Boot Thymeleaf Docker Image on AWS: Whitelabel Error Page 404

6/5/2021

I deployed Spring Boot Thymeleaf Application to AWS Kubernetes Cluster.

Locally it works fine:

http://localhost:8097

But when I deploy it to AWS Kubernetes Cluster, I see the following error:

Whitelabel Error Page 404

Here are some files of my application:

application.properties:

### server port
server.port=8097

WebController.java:

@Controller
public class WebController {

    @Autowired
    private CustomerDAO customerDAO;

    @GetMapping(path = "/")
    public String index() {
        return "external";
    }

}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.6</version>
		<relativePath />
	</parent>
	<groupId>skyglass</groupId>
	<artifactId>customer-management-keycloak</artifactId>
	<version>1.0.0</version>
	<name>customer-management-keycloak</name>
	<description>Customer Management Application</description>

	<properties>
		<java.version>11</java.version>	
	</properties>
	
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.keycloak.bom</groupId>
                <artifactId>keycloak-adapter-bom</artifactId>
                <version>13.0.1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>	

	<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.keycloak</groupId>
            <artifactId>keycloak-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
	

	</dependencies>

           <build>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <configuration>
                            <addResources>true</addResources>
                        </configuration>
                    </plugin>
					<!-- Docker Spotify Plugin -->
					<plugin>
						<groupId>com.spotify</groupId>
						<artifactId>dockerfile-maven-plugin</artifactId>
						<version>1.4.13</version>
						<executions>
							<execution>
								<id>default</id>
								<goals>
									<goal>build</goal>
								</goals>
							</execution>
						</executions>
						<configuration>
							<repository>skyglass/${project.name}</repository>
							<tag>${project.version}</tag>
							<skipDockerInfo>true</skipDockerInfo>
						</configuration>
					</plugin>	                    
                </plugins>
            </build>

</project>

Dockerfile:

FROM adoptopenjdk/openjdk11:alpine-jre
VOLUME /tmp
EXPOSE 8097
ADD target/*.jar app.jar
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

customermgmt.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: customermgmt
  labels:
    app: customermgmt
spec:
  replicas: 1
  selector:
    matchLabels:
      app: customermgmt
  template:
    metadata:
      labels:
        app: customermgmt
    spec:
      containers:
        - name: customermgmt
          image: skyglass/customer-management-keycloak:1.0.0
          imagePullPolicy: Always
          ports:
            - containerPort: 8097
              hostPort: 8097


---
apiVersion: v1
kind: Service
metadata:
  name: customermgmt
spec:
  ports:
    - protocol: TCP
      name: web
      port: 80
      targetPort: 8097
  selector:
    app: customermgmt

traefik-ingress.yaml:

apiVersion: "networking.k8s.io/v1beta1"
kind: "Ingress"
metadata:
  name: "traefik-customermgmt-ingress"
spec:
  ingressClassName: "traefik-lb"
  rules:
  - host: "keycloak.skycomposer.net"
    http:
      paths:
      - path: "/customermgmt"
        backend:
          serviceName: "customermgmt"
          servicePort: 80
-- Mykhailo Skliar
amazon-web-services
docker
kubernetes
spring-boot
thymeleaf

1 Answer

6/5/2021

The following configuration in application.properties fixed the issue:

server.servlet.context-path=/customermgmt

The path name should be equal to the ingress path

See the full code on my github:

https://github.com/skyglass-examples/customer-management-keycloak

-- Mykhailo Skliar
Source: StackOverflow