I’m using a local cluster that is Kind to test an application that has to run inside a Pod.The pourpose is to see if the application running inside a Pod is able to authenticate to the API server with a service account that I have already created. I created the application using Java leaveraging the kubernetes.io library as follow:
package com.example;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.util.Config;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
public class KubernetesController {
private static final Logger LOGGER = Logger.getLogger(KubernetesController.class.getName());
private ApiClient client;
public KubernetesController() {
try {
String token = new String(Files.readAllBytes(Paths.get("/var/run/secrets/kubernetes.io/serviceaccount/token")));
ApiClient client = Config.fromToken("https://kubernetes.default.svc", token, false);
Configuration.setDefaultApiClient(client);
} catch (IOException e) {
throw new RuntimeException("Failed to initialize Kubernetes client", e);
}
}
public void start() throws Exception {
try {
LOGGER.info("Connected to cluster: " + client.getBasePath());
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error", e);
throw e;
}
}
}
this is the pom.xml file:
<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>
<groupId>com.example</groupId>
<artifactId>my-maven-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>my-maven-project</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<version>14.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.example.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
And this is the Dockerfile that I have created and than pushed to my repository:
FROM openjdk:11-jre-slim
COPY target/my-maven-project-0.0.1-SNAPSHOT.jar/ app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]
I created a Service-account with some roles to be used inside the cluster, and than I created a deployment to deploy the docker image that I have created:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-maven-project
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: my-maven-project
template:
metadata:
labels:
app: my-maven-project
spec:
serviceAccountName: costum-controller
containers:
- name: my-maven-project
image: sasa001/salvo:my-maven-project
imagePullPolicy: Always
ports:
- containerPort: 8080
When I apply the deployment to the cluster, the pod goes to “CrashLoopBackOff” and displaying the logs these are the problems:
Exception in thread "main" java.lang.NoClassDefFoundError: io/kubernetes/client/util/Config
at com.example.KubernetesController.<init>(KubernetesController.java:20)
at com.example.App.main(App.java:7)
Caused by: java.lang.ClassNotFoundException: io.kubernetes.client.util.Config
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
I don’t understand why it isn’t able to find the class Config of that library because VisualStudio is not giving me any problem on that class and also I’m able to build the project with maven.
yeppa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.