Added examples of kn usage for the Scala sample app (#2895)

This commit is contained in:
dr.max 2021-03-17 12:50:54 -07:00 committed by GitHub
parent 0b4158d385
commit b090d1919a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 206 additions and 88 deletions

View File

@ -1,87 +0,0 @@
A microservice which demonstrates how to get set up and running with Knative
Serving when using [Scala](https://scala-lang.org/) and [Akka](https://akka.io/)
[HTTP](https://doc.akka.io/docs/akka-http/current/). 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:
```shell
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](../../../../install/README.md) with
Knative Serving up and running.
- [Docker](https://www.docker.com) installed locally, and running, optionally a
Docker Hub account configured or some other Docker Repository installed
locally.
## Configuring the Service descriptor
Importantly, in [service.yaml](./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.
```yaml
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: TARGET
value: "Scala Sample v1"
```
## Publishing to Docker
In order to build the project and create and push the Docker image, run:
```shell
# 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](./service.yaml):
```shell
kubectl apply --filename service.yaml
```
Then find the service host:
```shell
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:
```shell
curl -v http://helloworld-scala.default.1.2.3.4.xip.io
```
## Cleanup
```shell
kubectl delete --filename service.yaml
```

View File

@ -5,4 +5,209 @@ weight: 1
type: "docs"
---
{{% readfile file="README.md" %}}
A microservice which demonstrates how to get set up and running with Knative
Serving when using [Scala](https://scala-lang.org/) and [Akka](https://akka.io/)
[HTTP](https://doc.akka.io/docs/akka-http/current/). 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:
```shell
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](../../../../install/README.md) with
Knative Serving up and running.
- [Docker](https://www.docker.com) installed locally, and running, optionally a
Docker Hub account configured or some other Docker Repository installed
locally.
- [Java JDK8 or later](https://adoptopenjdk.net/installation.html) installed
locally.
- [Scala's](https://scala-lang.org/) standard build tool
[sbt](https://www.scala-sbt.org/) installed locally.
## Configuring the sbt build
If you want to use your Docker Hub repository, set the repository to
"docker.io/yourusername/yourreponame".
If you use Minikube, you first need to run:
```shell
eval $(minikube docker-env)
```
If want to use the Docker Repository inside Minikube, either set this to
"dev.local" or if you want to use another repository name, then you need to run
the following command after `docker:publishLocal`:
```shell
docker tag yourreponame/helloworld-scala:<version> dev.local/helloworld-scala:<version>
```
Otherwise Knative Serving won't be able to resolve this image from the Minikube
Docker Repository.
You specify the repository in [build.sbt](./build.sbt):
```scala
dockerRepository := Some("your_repository_name")
```
You can learn more about the build configuration syntax
[here](https://www.scala-sbt.org/1.x/docs/Basic-Def.html).
## Configuring the Service descriptor
Importantly, in [helloworld-scala.yaml](./helloworld-scala.yaml) **change the
image reference to match up with the repository**, name, and version specified
in the [build.sbt](./build.sbt) in the previous section.
```yaml
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: helloworld-scala
namespace: default
spec:
template:
spec:
containers:
- image: "your_repository_name/helloworld-scala:0.0.1"
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 either:
```shell
sbt docker:publishLocal
```
or
```shell
sbt docker:publish
```
Which of them to use is depending on whether you are publishing to a remote or a
local Docker Repository.
## Deploying to Knative Serving
{{< tabs name="helloworld_ruby" default="kn" >}}
{{% tab name="yaml" %}}
Apply the [Service yaml definition](./helloworld-scala.yaml):
```shell
kubectl apply --filename helloworld-scala.yaml
```
{{< /tab >}}
{{% tab name="kn" %}}
With `kn` you can deploy the service with
```shell
kn service create helloworld-scala --image=docker.io/{username}/helloworld-scala --env TARGET="Scala 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.
The output will look like:
```
Creating service 'helloworld-scala' in namespace 'default':
0.035s The Configuration is still working to reflect the latest desired specification.
0.139s The Route is still working to reflect the latest desired specification.
0.250s Configuration "helloworld-scala" is waiting for a Revision to become ready.
8.040s ...
8.136s Ingress has not yet been reconciled.
8.277s unsuccessfully observed a new generation
8.398s Ready to serve.
Service 'helloworld-scala' created to latest revision 'helloworld-scala-abcd-1' is available at URL:
http://helloworld-scala.default.1.2.3.4.xip.io
```
{{< /tab >}}
{{< /tabs >}}
{{< tabs name="service_url" default="kn" >}}
{{% tab name="kubectl" %}}
Then find the service host:
```shell
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:
```shell
curl -v http://helloworld-scala.default.1.2.3.4.xip.io
```
{{< /tab >}}
{{% tab name="kn" %}}
```shell
kn service describe helloworld-scala -o url
```
Example:
```shell
http://helloworld-scala.default.1.2.3.4.xip.io
```
Finally, to try your service, use the obtained URL:
```shell
curl -v http://helloworld-scala.default.1.2.3.4.xip.io
```
{{< /tab >}}
{{< /tabs >}}
## Cleanup
{{< tabs name="service_url" default="kn" >}}
{{% tab name="kubectl" %}}
```shell
kubectl delete --filename helloworld-scala.yaml
```
```
kubetl delete --filename helloworld-scala.yaml
```
{{< /tab >}}
{{% tab name="kn" %}}
```shell
kn service delete helloworld-scala
```
{{< /tab >}}
{{< /tabs >}}