Skip to content

Commit f332df1

Browse files
committed
Use Gradle build tool
1 parent 9d77c26 commit f332df1

File tree

10 files changed

+168
-152
lines changed

10 files changed

+168
-152
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
HELP.md
22
.gradle
33
build/
4-
target/
54
!gradle/wrapper/gradle-wrapper.jar
65
!**/src/main/**/build/
76
!**/src/test/**/build/
@@ -36,3 +35,7 @@ out/
3635

3736
### VS Code ###
3837
.vscode/
38+
39+
# project- specific
40+
41+
.tempo-data/

Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM openjdk:17-slim as builder
2+
WORKDIR /usr/local/temp
3+
ARG JAR_FILE=build/libs/*.jar
4+
ADD ${JAR_FILE} application.jar
5+
RUN java -Djarmode=layertools -jar application.jar extract
6+
7+
FROM openjdk:17-slim
8+
ARG VERSION=latest
9+
LABEL version=${VERSION}
10+
LABEL maintainer="Ashutosh Sahoo"
11+
LABEL description="spring-boot-kubernetes-mysql"
12+
13+
WORKDIR /usr/local/app
14+
RUN useradd --user-group --system --create-home --no-log-init app
15+
USER app
16+
COPY --from=builder /usr/local/temp/dependencies/ ./
17+
COPY --from=builder /usr/local/temp/spring-boot-loader/ ./
18+
COPY --from=builder /usr/local/temp/snapshot-dependencies/ ./
19+
COPY --from=builder /usr/local/temp/application/ ./
20+
ENTRYPOINT ["java","org.springframework.boot.loader.JarLauncher"]

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# Spring Boot Kubernetes and MySQL
22

3-
Sample project to test and deploy spring boot application with mysql database in kubernetes using JKube maven plugin.
3+
Sample project to test and deploy spring boot application with mysql database in kubernetes.
44

55
## Prerequisite
66

77
- Docker with kubernetes enabled
88
- Kubernetes command-line tool(kubectl)
99
- JDK 17 LTS
10-
- Maven
10+
- Gradle
1111

1212
## Start application
1313

1414
- Create secrets and start mysql database
1515

1616
```sh
17-
kubectl create -f deployment/secrets.yaml
18-
kubectl create -f deployment/mysql-deployment.yaml
17+
kubectl apply -f deployment/secrets.yaml
18+
kubectl apply -f deployment/mysql-deployment.yaml
19+
1920
```
2021

2122
- Build application and deploy in kubernetes
2223

2324
```sh
24-
mvn clean package
25-
mvn k8s:build k8s:resource k8s:apply
25+
gradle clean dockerTag
26+
kubectl apply -f deployment/app-k8s.yaml
27+
2628
```
2729

2830
- Test application :
@@ -32,6 +34,7 @@ curl -X GET \
3234
http://localhost:31371/api/v1/pets \
3335
-H 'Accept: application/json' \
3436
-H 'Content-Type: application/json'
37+
3538
```
3639

3740
Response should be :
@@ -52,13 +55,14 @@ Response should be :
5255
### Delete deployment, service, secret and pvc
5356

5457
```sh
55-
mvn k8s:undeploy
58+
kubectl delete -f deployment/app-k8s.yaml
5659
kubectl delete -f deployment/mysql-deployment.yaml
5760
kubectl delete -f deployment/secrets.yaml
61+
5862
```
5963
### Reference
6064

6165
- [Spring Boot](https://spring.io/projects/spring-boot)
62-
- [Eclipse JKube](https://github.com/eclipse/jkube)
66+
- [Gradle Docker image build plugin](https://plugins.gradle.org/plugin/com.palantir.docker)
6367
- [OpenJDK Docker](https://hub.docker.com/_/openjdk)
6468
- [MySQL Docker](https://hub.docker.com/_/mysql)

build.gradle

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
plugins {
2+
id 'java'
3+
id 'org.springframework.boot' version '3.0.5'
4+
id 'io.spring.dependency-management' version '1.1.0'
5+
id 'idea'
6+
id 'com.palantir.docker' version '0.35.0'
7+
}
8+
9+
group 'com.ashu.practice'
10+
version '5.0.0'
11+
12+
sourceCompatibility = '17'
13+
14+
repositories {
15+
mavenCentral()
16+
}
17+
18+
configurations {
19+
compileOnly {
20+
extendsFrom annotationProcessor
21+
}
22+
}
23+
24+
dependencies {
25+
implementation 'org.springframework.boot:spring-boot-starter-actuator'
26+
implementation 'org.springframework.boot:spring-boot-starter-web'
27+
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
28+
runtimeOnly 'com.mysql:mysql-connector-j'
29+
compileOnly 'org.projectlombok:lombok'
30+
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
31+
annotationProcessor 'org.projectlombok:lombok'
32+
testImplementation 'org.springframework.boot:spring-boot-starter-test'
33+
testImplementation 'com.h2database:h2'
34+
}
35+
36+
tasks.named('test') {
37+
useJUnitPlatform()
38+
}
39+
40+
41+
jar {
42+
enabled = false
43+
}
44+
45+
docker {
46+
dependsOn bootJar
47+
name "ashutoshsahoo/${project.name}:latest"
48+
buildArgs([JAR_FILE: "${project.name}-${project.version}.jar", VERSION: "${project.version}"])
49+
files bootJar
50+
// tag 'latest', "${project.parent.version}"
51+
// noCache true
52+
}
53+
54+
55+
idea {
56+
module {
57+
downloadJavadoc = true
58+
downloadSources = true
59+
}
60+
}

deployment/Jenkinsfile

Lines changed: 0 additions & 39 deletions
This file was deleted.

deployment/app-k8s.yaml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
apiVersion: v1
3+
kind: List
4+
items:
5+
- apiVersion: v1
6+
kind: Service
7+
metadata:
8+
labels:
9+
app: spring-boot-kubernetes-mysql
10+
version: 5.0.0
11+
name: spring-boot-kubernetes-mysql
12+
spec:
13+
ports:
14+
- name: http
15+
port: 8080
16+
protocol: TCP
17+
targetPort: 8080
18+
nodePort: 31371
19+
type: NodePort
20+
selector:
21+
app: spring-boot-kubernetes-mysql
22+
- apiVersion: apps/v1
23+
kind: Deployment
24+
metadata:
25+
labels:
26+
app: spring-boot-kubernetes-mysql
27+
version: 5.0.0
28+
name: spring-boot-kubernetes-mysql
29+
spec:
30+
selector:
31+
matchLabels:
32+
app: spring-boot-kubernetes-mysql
33+
template:
34+
metadata:
35+
labels:
36+
app: spring-boot-kubernetes-mysql
37+
version: 5.0.0
38+
spec:
39+
containers:
40+
- name: spring-boot-kubernetes-mysql
41+
image: ashutoshsahoo/spring-boot-kubernetes-mysql:5.0.0
42+
imagePullPolicy: IfNotPresent
43+
resources:
44+
limits:
45+
memory: "1Gi"
46+
cpu: "1"
47+
requests:
48+
memory: "100Mi"
49+
cpu: "100m"
50+
ports:
51+
- containerPort: 8080
52+
name: http
53+
protocol: TCP
54+
env:
55+
- name: SPRING_PROFILES_ACTIVE
56+
value: k8s
57+
- name: SPRING_DATASOURCE_URL
58+
valueFrom:
59+
configMapKeyRef:
60+
name: mysql-config
61+
key: MYSQL_DATABASE_URL
62+
- name: SPRING_DATASOURCE_USERNAME
63+
valueFrom:
64+
secretKeyRef:
65+
name: mysql-secret
66+
key: MYSQL_USER
67+
- name: SPRING_DATASOURCE_PASSWORD
68+
valueFrom:
69+
secretKeyRef:
70+
name: mysql-secret
71+
key: MYSQL_PASSWORD

pom.xml

Lines changed: 0 additions & 77 deletions
This file was deleted.

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = 'spring-boot-kubernetes-mysql'

src/main/jkube/deployment.yaml

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/main/jkube/service.yaml

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)