Hello World - Spark Java Framework

A simple web app written in Java using Spark Java Framework that you can use for testing. This guide describes the steps required to to create the helloworld-java sample app and deploy it to your cluster.

Prerequisites

You will need:

Develop

The sample app reads a TARGET environment variable, and prints Hello ${TARGET}!. If TARGET is not specified, World is used as the default value. You can also download a working copy of the sample, by running the following commands:

  1. git clone -b "release-0.21" https://github.com/knative/docs knative-docs
  2. cd knative-docs/docs/serving/samples/hello-world/helloworld-java
  1. Run the application locally:

    1. ./mvnw package && java -jar target/helloworld-0.0.1-SNAPSHOT-jar-with-dependencies.jar

    Go to http://localhost:8080/ to see your Hello World! message.

  2. In your project directory, create a file named Dockerfile and copy the code block below into it. For detailed instructions on dockerizing a Spark Java app, see Spark with Docker. For additional information on multi-stage docker builds for Java see Creating Smaller Java Image using Docker Multi-stage Build. Navigate to your project directory and copy the following code into a new file named Dockerfile:

    1. FROM maven:3.5-jdk-8-alpine as builder
    2. # Copy local code to the container image.
    3. WORKDIR /app
    4. COPY pom.xml .
    5. COPY src ./src
    6. RUN mvn package -DskipTests
    7. FROM openjdk:8-jre-alpine
    8. # Copy the jar to the production image from the builder stage.
    9. COPY --from=builder /app/target/helloworld-0.0.1-SNAPSHOT-jar-with-dependencies.jar helloworld.jar
    10. ENV PORT 8080
    11. EXPOSE 8080
    12. # Run the web service on container startup.
    13. CMD ["java","-jar","helloworld.jar"]
  3. To build the sample code into a container, and push using Docker Hub, enter the following commands and replace {username} with your Docker Hub username:

    1. # Build the container on your local machine
    2. docker build -t {username}/helloworld-java .
    3. # Push the container to docker registry
    4. docker push {username}/helloworld-java

Deploy

  1. After the build has completed and the container is pushed to Docker Hub, you can deploy the app into your cluster. Choose one of the following methods:

    Use kn to deploy the service, make sure to replace {username} with your Docker Hub username:

    1. kn service create helloworld-java --image=docker.io/{username}/helloworld-java --env TARGET="SparkJava Sample v1"

    This will wait until your service is deployed and ready, and ultimately it will print the URL through which you can access the service.

    1. Create a new file, service.yaml and copy the following service definition into the file. Make sure to replace {username} with your Docker Hub username.

      1. apiVersion: serving.knative.dev/v1
      2. kind: Service
      3. metadata:
      4. name: helloworld-java
      5. namespace: default
      6. spec:
      7. template:
      8. spec:
      9. containers:
      10. - image: docker.io/{username}/helloworld-java
      11. env:
      12. - name: TARGET
      13. value: "SparkJava Sample v1"
    2. Ensure that the container image value in service.yaml matches the container you built in the previous step. Apply the configuration using kubectl:

    1. kubectl apply --filename service.yaml

    After your service is created, Knative will perform the following steps:

    • Create a new immutable revision for this version of the app.
    • Network programming to create a route, ingress, service, and load balance for your app.
    • Automatically scale your pods up and down (including to zero active pods).

Verify

  1. Run one of the followings commands to find the domain URL for your service.

    1. kn service describe helloworld-java -o url

    Example:

    1. http://helloworld-java.default.1.2.3.4.xip.io
    1. kubectl get ksvc helloworld-java --output=custom-columns=NAME:.metadata.name,URL:.status.url

    Example:

    1. NAME URL
    2. helloworld-java http://helloworld-java.default.1.2.3.4.xip.io
  2. Now you can make a request to your app and see the result. Replace the URL below with the URL returned in the previous command.

    Example:

    1. curl http://helloworld-java.default.1.2.3.4.xip.io
    2. Hello SparkJava Sample v1!
    3. # Even easier with kn:
    4. curl $(kn service describe helloworld-java -o url)

    Note: Add -v option to get more detail if the curl command failed.

Delete

To remove the sample app from your cluster, delete the service record.

  1. kn service delete helloworld-java
  1. kubectl delete --filename service.yaml