Skip to content

Commit 2207a91

Browse files
authored
KTLN-630 - Kotlin Application Deployment with Docker and Kubernetes (#1121)
* feat: deploy kotlin app to k8s * fix: no tests
1 parent dfa9b38 commit 2207a91

File tree

9 files changed

+179
-0
lines changed

9 files changed

+179
-0
lines changed

kotlin-docker/.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/bin/
2+
3+
#ignore gradle
4+
.gradle/
5+
6+
7+
#ignore build and generated files
8+
build/
9+
node/
10+
out/
11+
12+
#ignore installed node modules and package lock file
13+
node_modules/
14+
package-lock.json

kotlin-docker/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## Relevant Articles

kotlin-docker/dockerk8s/Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Use an official OpenJDK runtime as a parent image
2+
FROM openjdk:17-jdk-slim
3+
4+
# Set the working directory in the container
5+
WORKDIR /app
6+
7+
# Copy the JAR file into the container
8+
COPY ../target/kotlin-app.jar /app/kotlin-app.jar
9+
10+
# Run the application
11+
CMD ["java", "-jar", "/app/kotlin-app.jar"]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: kotlin-app-deployment
5+
spec:
6+
replicas: 1
7+
selector:
8+
matchLabels:
9+
app: kotlin-app
10+
template:
11+
metadata:
12+
labels:
13+
app: kotlin-app
14+
spec:
15+
containers:
16+
- name: kotlin-app-container
17+
image: DOCKER_IMAGE
18+
ports:
19+
- containerPort: 8080

kotlin-docker/dockerk8s/service.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: kotlin-app
5+
spec:
6+
type: NodePort
7+
ports:
8+
- port: 8080
9+
targetPort: 8080
10+
nodePort: 30000
11+
selector:
12+
app: kotlin-app

kotlin-docker/pom.xml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<artifactId>kotlin-docker</artifactId>
6+
<name>kotlin-docker</name>
7+
<packaging>jar</packaging>
8+
9+
<parent>
10+
<groupId>com.baeldung</groupId>
11+
<artifactId>parent-boot-3</artifactId>
12+
<version>1.0.0-SNAPSHOT</version>
13+
<relativePath>../parent-boot-3</relativePath>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-web</artifactId>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>org.jetbrains.kotlin</groupId>
24+
<artifactId>kotlin-reflect</artifactId>
25+
<version>${kotlin.version}</version>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.jetbrains.kotlin</groupId>
30+
<artifactId>kotlin-stdlib</artifactId>
31+
<version>${kotlin.version}</version>
32+
</dependency>
33+
</dependencies>
34+
35+
<build>
36+
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
37+
<plugins>
38+
<plugin>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-maven-plugin</artifactId>
41+
</plugin>
42+
<plugin>
43+
<groupId>org.jetbrains.kotlin</groupId>
44+
<artifactId>kotlin-maven-plugin</artifactId>
45+
<version>${kotlin.version}</version>
46+
<configuration>
47+
<args>
48+
<arg>-Xjsr305=strict</arg>
49+
</args>
50+
<compilerPlugins>
51+
<plugin>spring</plugin>
52+
</compilerPlugins>
53+
</configuration>
54+
<dependencies>
55+
<dependency>
56+
<groupId>org.jetbrains.kotlin</groupId>
57+
<artifactId>kotlin-maven-allopen</artifactId>
58+
<version>${kotlin.version}</version>
59+
</dependency>
60+
</dependencies>
61+
</plugin>
62+
</plugins>
63+
</build>
64+
65+
<profiles>
66+
<profile>
67+
<id>docker-k8s</id>
68+
<build>
69+
<finalName>kotlin-app</finalName>
70+
<plugins>
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-shade-plugin</artifactId>
74+
<version>3.2.4</version>
75+
<executions>
76+
<execution>
77+
<phase>package</phase>
78+
<goals>
79+
<goal>shade</goal>
80+
</goals>
81+
<configuration>
82+
<transformers>
83+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
84+
<mainClass>com.baeldung.dockerk8s.ApplicationKt</mainClass>
85+
</transformer>
86+
</transformers>
87+
</configuration>
88+
</execution>
89+
</executions>
90+
</plugin>
91+
</plugins>
92+
</build>
93+
</profile>
94+
</profiles>
95+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.dockerk8s
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication
4+
import org.springframework.boot.runApplication
5+
6+
@SpringBootApplication
7+
class Application
8+
9+
fun main(args: Array<String>) {
10+
runApplication<Application>(*args)
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.dockerk8s
2+
3+
import org.springframework.http.ResponseEntity
4+
import org.springframework.web.bind.annotation.GetMapping
5+
import org.springframework.web.bind.annotation.RequestMapping
6+
import org.springframework.web.bind.annotation.RestController
7+
8+
@RestController
9+
@RequestMapping("/api")
10+
class HelloController {
11+
@GetMapping("/hello")
12+
fun hello(): ResponseEntity<String> {
13+
return ResponseEntity.ok("Hello From Docker and Kubernetes!")
14+
}
15+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@
533533
<module>kotlin-blockchain</module>
534534
<module>kotlin-testing</module>
535535
<module>kotlin-mockito</module>
536+
<module>kotlin-docker</module>
536537
<!-- <module>kotlin-tornadofx</module> --> <!-- not compatible with Java 9+ -->
537538
<module>kotlin-kover</module>
538539
<module>ktlint-custom</module>

0 commit comments

Comments
 (0)