Skip to content

Commit

Permalink
KTLN-630 - Kotlin Application Deployment with Docker and Kubernetes (B…
Browse files Browse the repository at this point in the history
…aeldung#1121)

* feat: deploy kotlin app to k8s

* fix: no tests
  • Loading branch information
lucaCambi77 authored Oct 15, 2024
1 parent dfa9b38 commit 2207a91
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 0 deletions.
14 changes: 14 additions & 0 deletions kotlin-docker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/bin/

#ignore gradle
.gradle/


#ignore build and generated files
build/
node/
out/

#ignore installed node modules and package lock file
node_modules/
package-lock.json
1 change: 1 addition & 0 deletions kotlin-docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Relevant Articles
11 changes: 11 additions & 0 deletions kotlin-docker/dockerk8s/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Use an official OpenJDK runtime as a parent image
FROM openjdk:17-jdk-slim

# Set the working directory in the container
WORKDIR /app

# Copy the JAR file into the container
COPY ../target/kotlin-app.jar /app/kotlin-app.jar

# Run the application
CMD ["java", "-jar", "/app/kotlin-app.jar"]
19 changes: 19 additions & 0 deletions kotlin-docker/dockerk8s/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: kotlin-app-deployment
spec:
replicas: 1
selector:
matchLabels:
app: kotlin-app
template:
metadata:
labels:
app: kotlin-app
spec:
containers:
- name: kotlin-app-container
image: DOCKER_IMAGE
ports:
- containerPort: 8080
12 changes: 12 additions & 0 deletions kotlin-docker/dockerk8s/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: kotlin-app
spec:
type: NodePort
ports:
- port: 8080
targetPort: 8080
nodePort: 30000
selector:
app: kotlin-app
95 changes: 95 additions & 0 deletions kotlin-docker/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?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>
<artifactId>kotlin-docker</artifactId>
<name>kotlin-docker</name>
<packaging>jar</packaging>

<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-3</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../parent-boot-3</relativePath>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>

<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>docker-k8s</id>
<build>
<finalName>kotlin-app</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.baeldung.dockerk8s.ApplicationKt</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.baeldung.dockerk8s

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class Application

fun main(args: Array<String>) {
runApplication<Application>(*args)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.dockerk8s

import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/api")
class HelloController {
@GetMapping("/hello")
fun hello(): ResponseEntity<String> {
return ResponseEntity.ok("Hello From Docker and Kubernetes!")
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@
<module>kotlin-blockchain</module>
<module>kotlin-testing</module>
<module>kotlin-mockito</module>
<module>kotlin-docker</module>
<!-- <module>kotlin-tornadofx</module> --> <!-- not compatible with Java 9+ -->
<module>kotlin-kover</module>
<module>ktlint-custom</module>
Expand Down

0 comments on commit 2207a91

Please sign in to comment.