Create an OpenShift Serverless function
A Function as a Service (FaaS) object can be used to process data on an as-needed basis, with the function shutting down after a period of time if not used. Using a service in this way enables to you limit costs, since you only pay for the CPU cycles when you actually need them.
In the world of Kubernetes, Knative is the technology that is most commonly used to implement the FaaS pattern. Red Hat OpenShift implements the FaaS pattern using Knative via the Red Hat OpenShift Serverless operator.
You can use a Function-as-a-Service (FaaS) object to process data as needed, with the function shutting down after a period of time if unused. If you try to access the function after it's shut down, there is a slight delay as it restores. Using a service in this way enables you to limit costs because you only pay for the CPU cycles when you actually need them.
In the world of Kubernetes, Knative is the most commonly used technology implementing the FaaS pattern. Red Hat OpenShift implements the FaaS pattern using Knative via the Red Hat OpenShift Serverless operator.
What you’ll be doing
This activity consists of two pieces: A back-end function and a front-end web application. You will implement both pieces. The back end is created from the command line, while the front end is created via the OpenShift web-based user interface (i.e., the “dashboard”). Here are the steps:
- Start the back-end serverless function.
- Start the front-end Photobooth application.
- Open the Photobooth application.
- Get the serverless URL.
- Paste the URL into Photobooth.
- Take a picture and wait for results.
- Take another picture.
- Wait, then take third picture.
How long will this activity take?
You should budget about 30 minutes to complete this sandbox activity.
What will you learn?
When you have finished this activity, you will be able to create an OpenShift Serverless function from either the command line or the dashboard.
You will need the following in order to complete this activity:
- A Developer Sandbox for Red Hat OpenShift account.
occommand-line interface installed on your computer.
- A web browser to access your Developer Sandbox dashboard.
You don’t need to know any programming languages to complete this task. If you want to view the code, the back-end is written in Java; the front-end is Blazor running in WebAssembly.
If you get stuck, if something isn’t working, or if you simply have questions, you can contact us via email at email@example.com.
Preparing for step 1: Log in to your sandbox at the command line
If you’re unsure how to do this, you can find instructions here: Access your Developer Sandbox for Red Hat OpenShift from the command line
In step 1, we’ll create the back-end serverless function from the command line. The only tool needed is the
oc command-line interface. We do not need to clone or download the source code GitHub repository; we can reference the YAML file directly from our command line.
Here are the contents of the
service.yaml file that we’ll be using—notice the
labels and the
apiVersion: serving.knative.dev/v1 kind: Service metadata: name: overlayimage labels: sandbox: serverless sandbox-serverless: overlay spec: template: spec: containers: - image: quay.io/rhdevelopers/imageoverlay:latest
labels allow us to attach multiple key/value pairs to our objects. In this example, I’m creating unique values that will make it easier to remove all of the objects created.
containers entry specifies the Linux image that will be run. In order for a container to be used as a serverless function, no special coding is necessary. The OpenShift Operator, based on Knative serving, does all the special work. The OpenShift Operator allows you to implement an existing web API application, written in the programming language of your choice, as a function.
Step 1: Start the back-end serverless function
Run the following command:
oc apply -f https://raw.githubusercontent.com/redhat-developer-demos/image-overlay/master/service.yaml
In the next step, we’ll implement the front-end application, Photobooth, by importing an existing Linux image via the OpenShift dashboard (the web interface).
Step 2: Start the front-end Photobooth application
From the dashboard, select the +Add option (Figure 1).
Next, select the option to add by using an existing container image (Figure 2).
Enter this value for the image (Figure 3):
Near the bottom of the form, under Advanced options, select Port 8080 as the port to be used (marked 1 in Figure 4), and select Labels (2).
Enter the following two values for Labels, as shown in Figure 5:
Select Resource type. Enter Deployment (marked as 1 in Figure 6) as the Resource type, then Create (2).
Step 3: Open the Photobooth application
Open the Photobooth application in your default web browser by clicking on the external route arrow in the OpenShift dashboard (Figure 7).
Your image will appear, as shown in Figure 8.
Before you can successfully use the Photobooth app, you need to tell it the correct URL for the service. The URL is the path to the service using the
Note: The route is case-sensitive.
Step 4. Get the serverless URL
At the command line, run the following command and copy the route:
oc get ksvc
Here’s an example of the output:
NAME URL LATESTCREATED LATESTREADY READY REASON overlayimage https://overlayimage-rhn-engineering-foobar-dev.apps.sandbox.x8i5.p1.openshiftapps.com overlayimage-00001 overlayimage-00001 True
Step 5: Paste the URL into Photobooth
Paste the URL with the appended
/overlayImage route into the Photobooth web page and select Apply (Figure 9).
Step 6. Take a picture and wait for the results
Now, click Take Picture and wait for the results to appear. It might take 10 to 15 seconds as the serverless function is scaled from zero to one. Figure 10 shows an example of the output. Note the image you see will be of your face, not mine.
Step 7: Take another picture
Immediately take another picture. Notice that this time, the results are almost instantaneous. This is because the serverless function is awake and serving requests. This will continue until one minute of inactivity, after which the serverless function will scale to zero again.
Step 8: Wait, then take a third picture
After waiting for more than 60 seconds, take another picture.
Again, there is a delay until the serverless function scales to one. This cycle of scaling up and down is the essence of a serverless function; it uses CPU cycles only as needed.
- When considering a serverless function, you must decide if the slight delay to scale from zero to one is appropriate for the application.
- The back-end application, image-overlay, is written in Java.
- The front-end application, Photobooth, is written in C# using the Blazor WASM framework.
- The image overlay is a 640-480, mostly transparent, PNG file.
- The text (“Serverless!”) is passed from the front-end application.
Go beyond: Move it, remove it, improve it
Now that you know how to create an OpenShift Serverless function from the command line and the dashboard, here are some other ideas to try.
You can download all of the YAML files associated with this application and use them to move the app to another OpenShift instance by using the Export Application button in the upper-right corner of the OpenShift dashboard.
You can remove parts of all of this activity by using one of the following commands:
- To remove only the
oc delete all -l sandbox-serverless=overlay
- To remove only the Photobooth application:
oc delete all -l sandbox-serverless=photobooth
- To remove all of the objects associated with this activity:
oc delete all -l sandbox=serverless
Some ideas to improve or alter this activity:
- Write your own back-end function in a different language.
- Clone the back-end code and use a different overlay image. Rebuild the image and use it instead of
- Write the front end in another language. Use a server-based web engine where it reads the URL from an environment variable so it doesn’t need to be entered on the screen.