Knative Tutorial

In this chapter we will use Knative + Camel-k to deploy a simple route that will download a file from s3 and send the file content as response to the Knative serving call.

Enterprise Integration Pattern

Poll Enrich

Camel comes with flavor pollEnrich as a choice of content enricher in the DSL. The other one is enrich pollEnrich uses a Polling Consumer to obtain the additional data. It is usually used for Event Message messaging, for instance to read a file or download a FTP file.

Demo Overview

The demo has an Apache Camel route configured as knative serving endpoint, that gets activated once it receives an event i.e. s3 object (xml file) name. Upon activation it starts a Apache Camel route that applies Poll Enricher EIP to download the file from and send the content as the response to the Knative serving service call

The following diagram shows the pictorial overview of the demo:

Poll Enricher

Deployment

You can skip this step and move to deploy cartoon message downloader if you have already completed the previous section

Make s3 data bucket

  • minikube

  • minishift

S3_URL=$(minikube service -n knativetutorial minio-server --url)

S3_URL=$(minishift service -n knativetutorial minio-server --url)

For this demos we will be using a s3 bucket called data, run the following command to create the bucket in s3:

aws --endpoint-url $S3_URL s3 mb s3://data

Verify the created bucket by listing the s3 storage, running the following command:

aws --endpoint-url $S3_URL s3 ls s3://data

For more information on how to use aws cli with minio server is available here.

You can also create the bucket via the minio web console.

Deploy Cartoon Messages Downloader

Navigate to camel-k demo sources folder:

cd $TUTORIAL_HOME/advanced/camel-k/quickstart

kamel run \
 --property 's3EndpointUrl=http://minio-server' \
 --property 'minioAccessKey=demoaccesskey' \
 --property 'minioSecretKey=demosecretkey' \
 src/main/java/CartoonMessagesDownloader.java

You can check the deployed integration context:

kamel get

Running the above command will return something like:

NAME                    PHASE                   CONTEXT
cartoon-messages-downloader  Building Context        ctx-bhvn6lcba0587gdq5jbg

Watching Logs

Since a Cron job source is used in this section of the tutorial, it would emit events every minute. We can watch the logs of the service to see the messages delivered.

The logs could be watched using the command:

  • kubectl

  • oc

kubectl logs -n knativetutorial -f <pod-name> -c user-container
oc logs -n knativetutorial -f <pod-name> -c user-container
  • Using stern with the command stern -n knativetutorial cartoon-messages-downloader, to filter the logs further add -c user-container to the stern command.

stern -n knativetutorial -c user-container cartoon-messages-downloader

Test the Route

Upload test data

Navigate to the quickstart folder:

cd $TUTORIAL_HOME/advanced/camel-k/quickstart

  • minikube

  • minishift

S3_URL=$(minikube service -n knativetutorial minio-server --url)

S3_URL=$(minishift service -n knativetutorial minio-server --url)

Upload XML file called message1.xml to s3 bucket called data,

aws --endpoint-url $S3_URL s3 cp data/message1.xml  s3://data

Upload a text file called hello.txt to s3 bucket called data,

aws --endpoint-url $S3_URL s3 cp data/hello.txt  s3://data

Verify if the XML and text files were uploaded to the s3 bucket:

aws --endpoint-url $S3_URL s3 ls s3://data

Invoke service

Invoke the service by passing a header fileName with value message1.xml, this fileName header value will be used by Camel route to download the file from s3 and return it as the response.

  • kubectl

  • oc

IP_ADDRESS="$(minikube ip):$(kubectl get svc istio-ingressgateway --namespace istio-system --output 'jsonpath={.spec.ports[?(@.port==80)].nodePort}')"

IP_ADDRESS="$(minishift ip):$(oc get svc istio-ingressgateway --namespace istio-system --output 'jsonpath={.spec.ports[?(@.port==80)].nodePort}')"

We need to know the knative service cartoon-messages-downloader domain name that needs to be passed as Host header, we will query and and set that value in an environment variable called SVC_URL:

  • kubectl

  • oc

SVC_URL=`kubectl -n knativetutorial get ksvc cartoon-messages-downloader -ojsonpath='{.status.domain}'`

SVC_URL=`oc  -n knativetutorial get ksvc cartoon-messages-downloader -ojsonpath='{.status.domain}'`

curl -v -H "Host:${SVC_URL}" -H 'fileName: message1.xml' $IP_ADDRESS

The event simulation above will be removed once the CamelSource event source is available with Knative Eventing.

On success which is usually HTTP 200, with response having the content of the downloaded file in this case it will be:

<?xml version="1.0" encoding="UTF-8"?>
<cartoon title="TomAndJerry">
  <productionHouse>MGM</productionHouse>
  <country>US</country>
  <genre>comedy</genre>
</cartoon>

When the cartoon-messages-downloader has been scaled to zero; when giving a new request it will try to scale up, it will not be able to serve the request until the pod is up, during such time you might get HTTP 502 Bad Gateway errors, give some more time for the pod to come up before issuing the request again.

Right now camel-k does not add Liveliness and Readiness Probes which can help avoid this issue. There is a open issue which is fixed and will be part of 0.3.1 release.

Cleanup

kamel delete cartoon-messages-downloader -n knativetutorial