Hello World - Scala using Akka HTTP
A microservice which demonstrates how to get set up and running with Knative Serving when using Scala and Akka HTTP. It will respond to a HTTP request with a text specified as an ENV
variable named MESSAGE
, defaulting to "Hello World!"
.
Follow the steps below to create the sample code and then deploy the app to your cluster. You can also download a working copy of the sample, by running the following commands:
git clone -b "{{< branch >}}" https://github.com/knative/docs knative-docs
cd knative-docs/docs/serving/samples/hello-world/helloworld-scala
Before you begin
- A Kubernetes cluster installation with Knative Serving up and running.
- Docker installed locally, and running, optionally a Docker Hub account configured or some other Docker Repository installed locally.
- Java JDK8 or later installed locally.
- Scala’s standard build tool sbt installed locally.
Configuring the Service descriptor
Importantly, in service.yaml change the image reference to match up with your designated repository, i.e. replace {username}
with your Dockerhub username in the example below.
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: helloworld-scala
namespace: default
spec:
template:
spec:
containers:
- image: docker.io/{username}/helloworld-scala
env:
- name: MESSAGE
value: "Scala & Akka on Knative says hello!"
- name: HOST
value: "localhost"
Publishing to Docker
In order to build the project and create and push the Docker image, run:
# Build the container on your local machine
docker build -t {username}/helloworld-scala .
# Push the container to docker registry
docker push {username}/helloworld-scala
Deploying to Knative Serving
Apply the Service yaml definition:
kubectl apply --filename helloworld-scala.yaml
Then find the service host:
kubectl get ksvc helloworld-scala \
--output=custom-columns=NAME:.metadata.name,URL:.status.url
# It will print something like this, the URL is what you're looking for.
# NAME URL
# helloworld-scala http://helloworld-scala.default.1.2.3.4.xip.io
Finally, to try your service, use the obtained URL:
curl -v http://helloworld-scala.default.1.2.3.4.xip.io
Cleanup
kubectl delete --filename helloworld-scala.yaml
kubetl delete --filename helloworld-scala.yaml
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.