Skip to content

Commit

Permalink
doc(quickstarts): improved hello-world example
Browse files Browse the repository at this point in the history
Removed declared exception in ResourceMojo.extractEnricherConfig that is never thrown #2780 Signed-off-by: wayne kirimi <[email protected]>
---
chore(example): Added /hello endpoint that prints a message Signed-off-by: wayne kirimi <[email protected]>
---
chore(example): Removed declarations of thrown exceptions Signed-off-by: wayne kirimi <[email protected]>
---
chore(example): Updated image name Signed-off-by: wayne kirimi <[email protected]>
---
chore(example): Updated quickstart readme Signed-off-by: wayne kirimi <[email protected]>
---
chore(example): Updated quickstart with requested changes Signed-off-by: wayne kirimi <[email protected]>
---
chore(example): Updated project Readme Signed-off-by: wayne kirimi <[email protected]>
---
chore(example): Refactored a child class. Signed-off-by: wayne kirimi <[email protected]>
---
Merge branch 'master' into improve-example-2788
---
fix/quickstart-example: Fixed requested changes. Signed-off-by:wayne kirimi <[email protected]>
---
Update RootHandler.java
---
Readme changes for Hello-World quickstart
---
Readme change
---
review: fix hello-world quickstart
  • Loading branch information
waynemorphic authored Apr 25, 2024
1 parent 78a7989 commit ea0085f
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 101 deletions.
29 changes: 14 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ $ git clone [email protected]:eclipse/jkube.git
# 2. Move to Hello World Quickstart folder
$ cd jkube/quickstarts/maven/hello-world

# 3. Build Project and run JKube goals
# 3. Configure your local environment to re-use the Docker daemon inside the Minikube instance.

~ jkube/quickstarts/maven/hello-world : $ eval $(minikube -p minikube docker-env)

# 4. Build Project and run JKube goals
$ mvn clean install \
k8s:build `# Build Docker Image` \
k8s:resource `# Generate Kubernetes Manifests` \
Expand All @@ -150,26 +154,21 @@ $ mvn clean install \
```shell script
# Using Kubectl
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
helloworld-7c4665f464-xwskj 0/1 Completed 2 27s
$ kubectl logs jkube-sample-helloworld-7c4665f464-xwskj
Hello World!
# Using JKube
$ mvn k8s:log
[INFO] k8s: [NEW] helloworld-7c4665f464-xwskj status: Running
[INFO] k8s: [NEW] Tailing log of pod: helloworld-587dfff745-2kdpq
[INFO] k8s: [NEW] Press Ctrl-C to stop tailing the log
[INFO] k8s: [NEW]
[INFO] k8s: Hello World!
[INFO] k8s: [NEW] helloworld-7c4665f464-xwskj status: Running
NAME READY STATUS RESTARTS AGE
helloworld-664bf5fdff-2bmrt 1/1 Running 0 9s
$ kubectl get svc
helloworld NodePort 10.110.92.145 <none> 8080:32353/TCP 58m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7h
$ curl `minikube ip`:32353/hello
Hello World
```

#### Troubleshooting

If you experience problems using minikube that pod's status shows 'ImagePullBackOff' and not 'Completed' you must share the minikube's docker daemon environment with your shell with:
If you experience problems using minikube that pod's status shows 'ImagePullBackOff' and not 'Running' you must share the minikube's docker daemon environment with your shell with:

```shell script
$ eval $(minikube docker-env)
$ eval $(minikube -p minikube docker-env)
```

You can remove this from your shell again with:
Expand Down
125 changes: 43 additions & 82 deletions quickstarts/maven/hello-world/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,55 @@
name: "Maven :: Hello World"
description: |
Demo project for getting started with Eclipse JKube.
It just prints "Hello World" on command line and exits.
It starts a simple HTTP server on port 8080 that replies with "Hello World" to the /hello endpoint.
---
# JKube Hello World Sample

This is a demo project for getting started with Eclipse JKube. It just prints "Hello World" on command
line and exits. We would be using Eclipse JKube for building a docker image and deploying to Kubernetes
in single command.
This is a demo project for getting started with Eclipse JKube.
It starts a simple HTTP server on port 8080.
Performing a request to `/hello` endpoint will reply with "Hello World".
We will be using Eclipse JKube for building a docker image and deploying to Kubernetes with a single command.

1. Make sure you've minikube up and running.
2. Run the following command to run helloworld sample:
```
~/work/repos/jkube/quickstarts/maven/hello-world : $ mvn clean install k8s:build k8s:resource k8s:apply
[INFO] Scanning for projects...
[INFO]
[INFO] ---------< org.eclipse.jkube.samples:jkube-sample-helloworld >----------
[INFO] Building jkube-sample-helloworld 0.1.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ jkube-sample-helloworld ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ jkube-sample-helloworld ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/rohaan/work/repos/jkube/quickstarts/maven/hello-world/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ jkube-sample-helloworld ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /home/rohaan/work/repos/jkube/quickstarts/maven/hello-world/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ jkube-sample-helloworld ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/rohaan/work/repos/jkube/quickstarts/maven/hello-world/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:testCompile (default-testCompile) @ jkube-sample-helloworld ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /home/rohaan/work/repos/jkube/quickstarts/maven/hello-world/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ jkube-sample-helloworld ---
[INFO] Surefire report directory: /home/rohaan/work/repos/jkube/quickstarts/maven/hello-world/target/surefire-reports
## Prerequisites

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.eclipse.jkube.sample.helloworld.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.044 sec
- Java 8+
- Maven
- Minikube
- JKube

Results :
## Deploying Demo app to Kubernetes with JKube

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
1. Make sure you've [Minikube](https://minikube.sigs.k8s.io/docs/start/) up and running.
```shell
minikube start
```

[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ jkube-sample-helloworld ---
[INFO] Building jar: /home/rohaan/work/repos/jkube/quickstarts/maven/hello-world/target/jkube-sample-helloworld-0.1.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ jkube-sample-helloworld ---
[INFO] Installing /home/rohaan/work/repos/jkube/quickstarts/maven/hello-world/target/jkube-sample-helloworld-0.1.1-SNAPSHOT.jar to /home/rohaan/.m2/repository/org/eclipse/jkube/samples/jkube-sample-helloworld/0.1.1-SNAPSHOT/jkube-sample-helloworld-0.1.1-SNAPSHOT.jar
[INFO] Installing /home/rohaan/work/repos/jkube/quickstarts/maven/hello-world/pom.xml to /home/rohaan/.m2/repository/org/eclipse/jkube/samples/jkube-sample-helloworld/0.1.1-SNAPSHOT/jkube-sample-helloworld-0.1.1-SNAPSHOT.pom
[INFO]
[INFO] --- kubernetes-maven-plugin:0.1.1-SNAPSHOT:build (default-cli) @ jkube-sample-helloworld ---
[INFO] k8s: Building Docker image in Kubernetes mode
[INFO] k8s: [helloworld-java:0.1.1-SNAPSHOT] "hello-world": Created docker-build.tar in 34 milliseconds
[INFO] k8s: [helloworld-java:0.1.1-SNAPSHOT] "hello-world": Built image sha256:9baee
[INFO]
[INFO] --- kubernetes-maven-plugin:0.1.1-SNAPSHOT:resource (default-cli) @ jkube-sample-helloworld ---
[INFO] k8s: jkube-controller: Adding a default Deployment
[INFO] k8s: jkube-revision-history: Adding revision history limit to 2
[INFO] k8s: validating /home/rohaan/work/repos/jkube/quickstarts/maven/hello-world/target/classes/META-INF/jkube/kubernetes/jkube-sample-helloworld-deployment.yml resource
[INFO]
[INFO] --- kubernetes-maven-plugin:0.1.1-SNAPSHOT:apply (default-cli) @ jkube-sample-helloworld ---
[INFO] k8s: Using Kubernetes at https://192.168.39.149:8443/ in namespace default with manifest /home/rohaan/work/repos/jkube/quickstarts/maven/hello-world/target/classes/META-INF/jkube/kubernetes.yml
[INFO] k8s: Using namespace: default
[INFO] k8s: Creating a Deployment from kubernetes.yml namespace default name jkube-sample-helloworld
[INFO] k8s: Created Deployment: target/jkube/applyJson/default/deployment-jkube-sample-helloworld.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.407 s
[INFO] Finished at: 2020-02-10T21:44:54+05:30
[INFO] ------------------------------------------------------------------------
```
2. Configure your local environment to re-use the Docker daemon inside the Minikube instance.
```shell
$ eval $(minikube -p minikube docker-env)
```

3. Check logs of Created Pod:
```
~/work/repos/jkube/quickstarts/maven/hello-world : $ kubectl get pods
NAME READY STATUS RESTARTS AGE
jkube-sample-helloworld-7c4665f464-xwskj 0/1 Completed 2 27s
~/work/repos/jkube/quickstarts/maven/hello-world : $ kubectl logs jkube-sample-helloworld-7c4665f464-xwskj
Hello World!
```
3. Run the following command to run and deploy hello-world demo app to Kubernetes
```shell
$ mvn clean install k8s:build k8s:resource k8s:apply
```

4. Check logs of the created Pod
```
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
helloworld-664bf5fdff-2bmrt 1/1 Running 0 9s
```

5. Log the running Kubernetes services
```shell
$ kubectl get svc
helloworld NodePort 10.110.92.145 <none> 8080:32353/TCP 58m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7h
```

6. Call the `/hello` endpoint
```shell
$ curl `minikube ip`:32353/hello
Hello World
```
6 changes: 5 additions & 1 deletion quickstarts/maven/hello-world/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@

<description>
Demo project for getting started with Eclipse JKube.
It just prints "Hello World" on command line and exits.
It starts a simple HTTP server on port 8080 that replies with "Hello World" to the /hello endpoint.
</description>

<properties>
<jkube.version>${project.version}</jkube.version>
<jkube.enricher.jkube-service.type>NodePort</jkube.enricher.jkube-service.type>
</properties>

<build>
Expand Down Expand Up @@ -77,6 +78,9 @@
<build>
<from>openjdk:latest</from>
<cmd>java -jar maven/${project.artifactId}-${project.version}.jar</cmd>
<ports>
<port>8080</port>
</ports>
</build>
</image>
</images>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,31 @@
*/
package org.eclipse.jkube.sample.helloworld;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.logging.Logger;
import java.util.logging.Level;

import com.sun.net.httpserver.HttpServer;

/**
* Hello world!
* @author: Wayne Kirimi
*/
public class App {
public static void main(String[] args) {
System.out.println("Hello World!");

private static final Logger log = Logger.getLogger(App.class.getSimpleName());
private static final int PORT = 8080;

public static void main(String[] args) {

try {
HttpServer server = HttpServer.create(new InetSocketAddress(PORT), 0);
server.createContext("/hello", new RootHandler());
server.setExecutor(null);
server.start();
log.info("Server started on port: " + PORT);
} catch (IOException e) {
log.severe("Error occured when starting server: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.sample.helloworld;

import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Logger;
import java.util.logging.Level;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

/**
* @author: Wayne Kirimi
*/
public class RootHandler implements HttpHandler {

private static final Logger log = Logger.getLogger(App.class.getSimpleName());

@Override
public void handle(HttpExchange exchange) {
try (OutputStream outputStream = exchange.getResponseBody()) {
log.info("GET /hello");
String response = "Hello World";
exchange.sendResponseHeaders(200, response.length());
exchange.getResponseHeaders().set("Content-Type", "text/plain");
outputStream.write(response.getBytes());
log.info("Response written successfully: " + response);
} catch (IOException e) {
log.severe("Server failed to respond: " + e.getMessage());
}
}
}

0 comments on commit ea0085f

Please sign in to comment.