meer-nasser-ali / serverless-sentiment

This tutorial demonstrates how to work with OpenShift Serverless

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Deploy a Serverless Application that detects sentiment of text on Red Hat OpenShift

Introduction

This tutorial aims to demonstrate the serverless functionality on Red Hat OpenShift. In this tutorial you will deploy an application that is made of frontend and backend. The frontend is an angular application that consists of a simple form where the user submits a sentence which is then processed in the backend to later view the output of the sentiment. The backend is a python application based on Flask that uses TextBlob library to detect the sentiment in addition to Cloudant to save and fetch results.
The following Architecture diagram gives an overview of the project's components. image

Prerequisites

For this tutorial you will need:

Estimated Time

It will take you around 30 minutes to complete this tutorial.

Steps

  • Fork the GitHub repo
  • Create Cloudant Datavase on IBM Cloud
  • Login from the CLI
  • Install OpenShift Serverless Operator

Fork the GitHub repo

  • First thing you need to do is fork the GitHub repository so you can make your own changes later.

Create Cloudant Database on IBM Cloud

  • In in this tutorial we will be using Cloudant to save the JSON objects in the database. Create the service on IBM Cloud and name it 'cloudant-sentiment'. cloudant service
  • Once created, go to the newly provisioned service and create credintials from 'Service Credintials' tab, make sure the role is 'Manager'. You will be using these credintials in your code at a later step. credintials
  • Next, go to Dashboard under Manage tab and click 'Launch Dashboard'.
    launch dashboard
  • Then create the Database as shown in the image. Name it 'sample', select Non-parttioned, and click Create. createdb
  • The sample database opens automatically. Leave the database empty for now. At a later step, you will create the documents through the backend.

Install OpenShift Serverless

  • From the web console, you can install the OpenShift Serverless Operator using the OperatorHub in your OpenShift dashboard. Use Update Channel version 4.5 serverless operator installed
  • Check if Knative Serving was installed successfully.
oc get knativeserving.operator.knative.dev/knative-serving -n knative-serving --template='{{range .status.conditions}}{{printf "%s=%s\n" .type .status}}{{end}}'

image

Login from the CLI

  • Go to the web console and click on your username at the top right then 'Copy Login Command', then display the token and copy the oc login command in your terminal.
    login

Create Project

  • From the CLI, create a project and name it 'sentiment-project' as shown in the following command.
oc new-project sentiment-project
  • Make sure that you are in the correct project using the following command.
oc project sentiment-project

Add Environment Variables to your Backend Application

  • In this step, you will be using secrets to pass your Cloudant service credintials to the applications. Use the following command and make sure to replace the fields with your actual credintials.
oc create secret generic secret --from-literal=apikey=<YOUR-CLOUDANT-API-KEY-HERE> -n sentiment-project
  • Add the URL of your Cloudant instance as a configmap using the following command, make sure to replace the value of the url.
oc create configmap my-config --from-literal=url=<YOUR-CLOUDANT-URL-HERE> -n sentiment-project

Create Backend Application

  • In this step, you will be creating the backend application through the service.yaml file that's in the backend directory in the github repo. Use the following command.
oc apply -f https://raw.githubusercontent.com/nerdingitout/serverless-sentiment/main/backend/service.yaml


The yaml file contains the following information. Make sure that the namespace matches the name you created.

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: sentiment 
  namespace: sentiment-project 
spec:
  template:
    spec:
      containers:
        - image: s4saif/senti:v7
  • Once created, you can find the newly deployed application on the topology as shown below. Keep in mind that it is a serverless application so the pods will be terminated if you aren't accessing it which means the circle around the pod will be colored in white. If you try to access the application externally, you will notice new pods have been created, which will change the color to dark blue. You might notice that the application is inaccessible, but don't worry much, we will be using the it with the frontend application. topology backend
  • Make sure to copy the route of the backend, because you will be using it to make an API call from your frontend application. To view the route of the backend from the CLI, write the following command.
oc get route.serving.knative.dev

route

Edit your Frontend application

  • In your forked repo, you will need to replace the URL in the typescript code. Go to frontend-app.component.ts in the/frontend/src/app/frontend-app/ directory. Add the URL of the backend that you copied earlier to (line 22) the following section in the onSubmit() function.
  onSubmit(){
    this.apiSentiment='';
    // Simple POST request with a JSON body and response type <any>, replace backend url in the api post request
    this.http.post<any>('<ADD-BACKEND-URL-HERE>'+'/api/post_sentiment', { text: this.Sentence.value }).subscribe(data => {
      this.apiSentimentNum = data.sentiment;
      this.apiText = data.text;
    .
    .
    .
    })
  }
  • In your forked repo, make sure to edit the buildconfig.yaml file, by replacing the value of uri with the URL of your github repo as shown below.
spec:
  output:
    to:
      kind: ImageStreamTag
      name: angular:latest
  runPolicy: Serial
  source:
    git:
      ref: main
      uri: <YOUR-GITHUB-REPO-URL-HERE>
    type: Git

Create your frontend application

  • To create the frontend application, in this tutorial, use the oc apply command for the files in the frontend/yamls/ directory. You can clone your repo and use the following command for your local directory.
oc apply -f yamls/

Or you can use the following commands with your URL (make sure to replace the username).

oc apply -f https://raw.githubusercontent.com/<YOUR-USERNAME>/serverless-sentiment/main/frontend/yamls/buildconfig.yaml
oc apply -f https://raw.githubusercontent.com/<YOUR-USERNAME>/serverless-sentiment/main/frontend/yamls/dc.yaml
oc apply -f https://raw.githubusercontent.com/<YOUR-USERNAME>/serverless-sentiment/main/frontend/yamls/is.yaml
oc apply -f https://raw.githubusercontent.com/<YOUR-USERNAME>/serverless-sentiment/main/frontend/yamls/service.yaml
  • Expose your frontend application to access it externally
oc expose <pod-name>
  • Get the route of your frontend application
oc get routes

Test Your application and View logs

  • Open the frontend application from the external route and submit messages in the form. image
  • Use oc get pods command to see the pods from the serverless application get created and destroyed. Run it multiple times and notice changes how the application scales up and down everytime you submit your sentences through the frontend application.
oc get pods
  • You can also view
oc get all -n sentiment-project

View the Database

image

Summary

In this tutorial, you performed several tasks to build an entire appliction that makes use of the serverless functionality. On Red Hat OpenShift, you can build serverless applications through the Serverless Operator that is based on the Knative project. In this tutorial, you used one of the main components of Knative, which is Knative Serving. Knative Serving autoscales your application on demand and scales it down to zero when it's not used, and through this tutorial you were able to learn how it works.

About

This tutorial demonstrates how to work with OpenShift Serverless

License:Apache License 2.0


Languages

Language:TypeScript 61.0%Language:JavaScript 13.2%Language:HTML 11.6%Language:Dockerfile 7.0%Language:Python 6.3%Language:CSS 0.9%