diff --git a/README.md b/README.md index bc551eb956..c66246b211 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,11 @@ $ git clone git@github.com: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` \ @@ -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 8080:32353/TCP 58m +kubernetes ClusterIP 10.96.0.1 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: diff --git a/quickstarts/maven/hello-world/README.md b/quickstarts/maven/hello-world/README.md index 5663a088f3..0f0c3f59d7 100644 --- a/quickstarts/maven/hello-world/README.md +++ b/quickstarts/maven/hello-world/README.md @@ -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 8080:32353/TCP 58m + kubernetes ClusterIP 10.96.0.1 443/TCP 7h + ``` + +6. Call the `/hello` endpoint + ```shell + $ curl `minikube ip`:32353/hello + Hello World + ``` diff --git a/quickstarts/maven/hello-world/pom.xml b/quickstarts/maven/hello-world/pom.xml index 19fec4226c..90b2ca46ab 100644 --- a/quickstarts/maven/hello-world/pom.xml +++ b/quickstarts/maven/hello-world/pom.xml @@ -26,11 +26,12 @@ 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. ${project.version} + NodePort @@ -77,6 +78,9 @@ openjdk:latest java -jar maven/${project.artifactId}-${project.version}.jar + + 8080 + diff --git a/quickstarts/maven/hello-world/src/main/java/org/eclipse/jkube/sample/helloworld/App.java b/quickstarts/maven/hello-world/src/main/java/org/eclipse/jkube/sample/helloworld/App.java index 74b4e61692..11e2d15faa 100644 --- a/quickstarts/maven/hello-world/src/main/java/org/eclipse/jkube/sample/helloworld/App.java +++ b/quickstarts/maven/hello-world/src/main/java/org/eclipse/jkube/sample/helloworld/App.java @@ -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()); } + } } diff --git a/quickstarts/maven/hello-world/src/main/java/org/eclipse/jkube/sample/helloworld/RootHandler.java b/quickstarts/maven/hello-world/src/main/java/org/eclipse/jkube/sample/helloworld/RootHandler.java new file mode 100644 index 0000000000..b64d5d0a07 --- /dev/null +++ b/quickstarts/maven/hello-world/src/main/java/org/eclipse/jkube/sample/helloworld/RootHandler.java @@ -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()); + } + } +}