Skip to content

Commit

Permalink
refactor: Docker-file-simple quickstart
Browse files Browse the repository at this point in the history
- Removed unnecessary dependencies + tests to speed up build times (quickstarts CI job)
- Added minimum configuration to expose a service
- Made application runnable
- Static file is served from rest endpoint

Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa authored and rohanKanojia committed Jun 11, 2020
1 parent 07477a2 commit 02155e6
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 326 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#

FROM openjdk:latest
COPY maven/target/zero-config-dockerfile.jar /tmp/zero-config-dockerfile.jar
COPY maven/target/docker-file-simple.jar /deployments/docker-file-simple.jar
# Copying a file inside project root directory
COPY maven/static-dir-in-project-root/my-file.txt /tmp/my-file.txt
CMD ["java", "-jar", "/tmp/zero-config-dockerfile.jar"]
COPY maven/static-dir-in-project-root/my-file.txt /deployments/my-file.txt
CMD ["java", "-jar", "/deployments/docker-file-simple.jar"]
106 changes: 106 additions & 0 deletions quickstarts/maven/docker-file-simple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Eclipse JKube sample with simple Dockerfile mode

You can build your Docker images with just providing one `Dockerfile` in your project root directory and Eclipse JKube
will pick it up for docker build.
For simple `Dockerfile` mode, your project's current directory is provided as docker context directory for build.
If you want to copy some files from your current directory(`target/docker-file-simple.jar` in this case), you need
to prefix project directory contents with default assembly name(i.e `maven`). So your `Dockerfile` would look like this:
```
FROM openjdk:latest
COPY maven/target/docker-file-simple.jar /deployments/docker-file-simple.jar
COPY maven/static-dir-in-project-root/my-file.txt /deployments/my-file.txt
CMD ["java", "-jar", "/deployments/docker-file-simple.jar"]
```

In order to provide a valid Kubernetes service YAML manifest, the following properties are added to [pom.xml](pom.xml)
to make the Pod port accessible:
```xml
<jkube.enricher.jkube-service.port>8080</jkube.enricher.jkube-service.port>
<jkube.enricher.jkube-service.type>NodePort</jkube.enricher.jkube-service.type>
```

# Building Docker image
```shell script
$ mvn k8s:build
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< org.eclipse.jkube:docker-file-simple >----------------
[INFO] Building Eclipse JKube :: Quickstarts :: Maven :: Dockerfile :: Simple 1.0.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- kubernetes-maven-plugin:1.0.0-SNAPSHOT:build (default-cli) @ docker-file-simple ---
[INFO] k8s: Running in Kubernetes mode
[INFO] k8s: Building Docker image in Kubernetes mode
[INFO] k8s: [jkube/docker-file-simple:latest]: Created docker-build.tar in 261 milliseconds
[INFO] k8s: [jkube/docker-file-simple:latest]: Built image sha256:52d46
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.977 s
[INFO] Finished at: 2020-06-11T07:59:44+02:00
[INFO] ------------------------------------------------------------------------
$ docker images | grep docker-file-simple
jkube/docker-file-simple 1.0 c8df6da232b3 8 seconds ago 516MB
```

# Generating Kubernetes Manifests and Deploying to Kubernetes
```shell script
$ mvn k8s:resource k8s:apply
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< org.eclipse.jkube:docker-file-simple >----------------
[INFO] Building Eclipse JKube :: Quickstarts :: Maven :: Dockerfile :: Simple 1.0.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- kubernetes-maven-plugin:1.0.0-SNAPSHOT:resource (default-cli) @ docker-file-simple ---
[INFO] k8s: jkube-controller: Adding a default Deployment
[INFO] k8s: jkube-service: Adding a default service 'docker-file-simple' with ports [8080]
[INFO] k8s: jkube-healthcheck-spring-boot: Adding readiness probe on port 8080, path='/actuator/health', scheme='HTTP', with initial delay 10 seconds
[INFO] k8s: jkube-healthcheck-spring-boot: Adding liveness probe on port 8080, path='/actuator/health', scheme='HTTP', with initial delay 180 seconds
[INFO] k8s: jkube-revision-history: Adding revision history limit to 2
[INFO] k8s: validating /home/user/00-MN/projects/forks/jkube/quickstarts/maven/docker-file-simple/target/classes/META-INF/jkube/kubernetes/docker-file-simple-deployment.yml resource
[INFO] k8s: validating /home/user/00-MN/projects/forks/jkube/quickstarts/maven/docker-file-simple/target/classes/META-INF/jkube/kubernetes/docker-file-simple-service.yml resource
[INFO]
[INFO] --- kubernetes-maven-plugin:1.0.0-SNAPSHOT:apply (default-cli) @ docker-file-simple ---
[INFO] k8s: Using Kubernetes at https://172.17.0.3:8443/ in namespace default with manifest /home/user/00-MN/projects/forks/jkube/quickstarts/maven/docker-file-simple/target/classes/META-INF/jkube/kubernetes.yml
[INFO] k8s: Using namespace: default
[INFO] k8s: Updating a Service from kubernetes.yml
[INFO] k8s: Updated Service: target/jkube/applyJson/default/service-docker-file-simple-1.json
[INFO] k8s: Updating Deployment from kubernetes.yml
[INFO] k8s: Updated Deployment: target/jkube/applyJson/default/deployment-docker-file-simple-2.json
[INFO] k8s: HINT: Use the command `kubectl get pods -w` to watch your pods start up
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.688 s
[INFO] Finished at: 2020-06-11T08:01:21+02:00
[INFO] ------------------------------------------------------------------------
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
docker-file-simple-686cddc6b9-lvgtv 0/1 Running 0 84s
$ kubectl logs pod/zero-config-dockerfile-686cddc6b9-lvgtv
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.7.RELEASE)
2020-06-11 05:51:57.509 INFO 1 --- [ main] o.e.j.quickstart.maven.helloworld.App : Starting App v1.0.0-SNAPSHOT on docker-file-simple-9dbf5f98c-wmhh5 with PID 1 (/deployments/docker-file-simple.jar started by root in /)
2020-06-11 05:51:57.516 INFO 1 --- [ main] o.e.j.quickstart.maven.helloworld.App : No active profile set, falling back to default profiles: default
2020-06-11 05:51:59.793 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-06-11 05:51:59.807 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-06-11 05:51:59.808 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.34]
2020-06-11 05:51:59.926 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-06-11 05:51:59.926 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2241 ms
2020-06-11 05:52:00.931 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-06-11 05:52:01.296 INFO 1 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2020-06-11 05:52:01.407 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-06-11 05:52:01.411 INFO 1 --- [ main] o.e.j.quickstart.maven.helloworld.App : Started App in 4.751 seconds (JVM running for 5.453)
2020-06-11 05:52:13.373 INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-06-11 05:52:13.374 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-06-11 05:52:13.384 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 9 ms
$ curl $(minikube ip):$(kg svc docker-file-simple -n default -o jsonpath='{.spec.ports[].nodePort}')/static-file
This is a dummy file which should be copied inside Dockerfile
```
67 changes: 67 additions & 0 deletions quickstarts/maven/docker-file-simple/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019 Red Hat, Inc.
This program and the accompanying materials are made
available under the terms of the Eclipse Public License 2.0
which is available at:
https://www.eclipse.org/legal/epl-2.0/
SPDX-License-Identifier: EPL-2.0
Contributors:
Red Hat, Inc. - initial API and implementation
-->

<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 https://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.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<name>Eclipse JKube :: Quickstarts :: Maven :: Dockerfile :: Simple</name>
<groupId>org.eclipse.jkube</groupId>
<artifactId>docker-file-simple</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
<java.version>11</java.version>
<jkube.enricher.jkube-service.port>8080</jkube.enricher.jkube-service.port>
<jkube.enricher.jkube-service.type>NodePort</jkube.enricher.jkube-service.type>
<kubernetes-maven-plugin.version>${project.version}</kubernetes-maven-plugin.version>
</properties>

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

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.eclipse.jkube</groupId>
<artifactId>kubernetes-maven-plugin</artifactId>
<version>${kubernetes-maven-plugin.version}</version>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
*/
package org.eclipse.jkube.quickstart.maven.helloworld;

/**
* Hello world!
*/
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
public static void main(String[] args) {
System.out.println("Hello World!");
SpringApplication.run(App.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.quickstart.maven.helloworld;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

@RestController
public class HelloController {

@GetMapping("/")
public String index() {
return "Greetings from Spring Boot!!";
}

@GetMapping(path = "/static-file")
public String staticFile() throws IOException {
return Files.readString(Path.of("/","deployments", "my-file.txt"));
}
}
70 changes: 0 additions & 70 deletions quickstarts/maven/simple-dockerfile/README.md

This file was deleted.

Loading

0 comments on commit 02155e6

Please sign in to comment.