* Update golang images in code samples * fix sampleconsistency test |
||
---|---|---|
.. | ||
.mvn/wrapper | ||
src/main/java/com/example/helloworld | ||
.gitignore | ||
Dockerfile | ||
README.md | ||
helloworld.zip | ||
mvnw | ||
mvnw.cmd | ||
pom.xml | ||
service.yaml |
README.md
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:
- A Kubernetes cluster with Knative installed and DNS configured. See Install Knative Serving.
- Docker installed and running on your local machine, and a Docker Hub account configured.
- Java SE 8 or later JDK.
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:
git clone https://github.com/knative/docs.git knative-docs
cd knative-docs/code-samples/serving/hello-world/helloworld-java
-
Run the application locally:
mvn wrapper:wrapper ./mvnw package && java -jar target/helloworld-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Go to
http://localhost:8080/
to see yourHello World!
message. -
In your project directory, create a file named
Dockerfile
and copy the following code block 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 namedDockerfile
:FROM maven:3.5-jdk-8-alpine as builder # Copy local code to the container image. WORKDIR /app COPY pom.xml . COPY src ./src RUN mvn package -DskipTests FROM openjdk:8-jre-alpine # Copy the jar to the production image from the builder stage. COPY --from=builder /app/target/helloworld-0.0.1-SNAPSHOT-jar-with-dependencies.jar helloworld.jar ENV PORT 8080 EXPOSE 8080 # Run the web service on container startup. CMD ["java","-jar","helloworld.jar"]
-
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:# Build and push the container on your local machine. docker buildx build --platform linux/arm64,linux/amd64 -t "{username}/helloworld-java" --push .
Deploy
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:
kn
- Use
kn
to deploy the service, make sure to replace{username}
with your Docker Hub username:
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.
kubectl
- 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.
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: helloworld-java
namespace: default
spec:
template:
spec:
containers:
- image: docker.io/{username}/helloworld-java
env:
- name: TARGET
value: "SparkJava Sample v1"
- Ensure that the container image value in
service.yaml
matches the container you built in the previous step. Apply the configuration usingkubectl
:
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
- Run one of the followings commands to find the domain URL for your service.
kn
kn service describe helloworld-java -o url
Example:
http://helloworld-java.default.1.2.3.4.xip.io
kubectl
kubectl get ksvc helloworld-java --output=custom-columns=NAME:.metadata.name,URL:.status.url
Example:
NAME URL
helloworld-java http://helloworld-java.default.1.2.3.4.xip.io
- Now you can make a request to your app and see the result. Replace the following URL with the URL returned in the previous command.
Example:
curl http://helloworld-java.default.1.2.3.4.sslip.io
Hello SparkJava Sample v1!
# Even easier with kn:
curl $(kn service describe helloworld-java -o url)
Note: Add
-v
option to get more detail if thecurl
command failed.
Delete
To remove the sample app from your cluster, delete the service record:
kn
kn service delete helloworld-java
kubectl
kubectl delete --filename service.yaml