Serverless applications using Knative services

To deploy a serverless application using OpenShift Serverless, you must create a Knative service. Knative services are Kubernetes services, defined by a route and a configuration, and contained in a YAML file.

Example Knative service YAML
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: helloworld-go (1)
  namespace: default (2)
spec:
  template:
    spec:
      containers:
        - image: gcr.io/knative-samples/helloworld-go (3)
          env:
            - name: TARGET (4)
              value: "Go Sample v1"
1 The name of the application.
2 The namespace the application will use.
3 The image of the application.
4 The environment variable printed out by the sample application.

You can create a serverless application by using one of the following methods:

  • Create a Knative service from the OpenShift Container Platform web console.

  • Create a Knative service using the kn CLI.

  • Create and apply a YAML file.

Creating serverless applications using the OpenShift Container Platform web console

You can create a serverless application using either the Developer or Administrator perspective in the OpenShift Container Platform web console.

Creating serverless applications using the Administrator perspective

Prerequisites

To create serverless applications using the Administrator perspective, ensure that you have completed the following steps.

  • The OpenShift Serverless Operator and Knative Serving are installed.

  • You have logged in to the web console and are in the Administrator perspective.

Procedure
  1. Navigate to the ServerlessServices page.

    Services page
  2. Click Create Service.

  3. Manually enter YAML or JSON definitions, or by dragging and dropping a file into the editor.

    Text editor
  4. Click Create.

Creating serverless applications using the Developer perspective

For more information about creating applications using the Developer perspective in OpenShift Container Platform, see the documentation on Creating applications using the Developer perspective.

Creating serverless applications using the kn CLI

Prerequisites
  • You have installed the OpenShift Serverless Operator and Knative Serving.

  • You have installed kn CLI.

Procedure
  1. Create the Knative service by entering the following command:

    $ kn service create <SERVICE-NAME> --image <IMAGE> --env <KEY=VALUE>
    Example
    $ kn service create hello --image gcr.io/knative-samples/helloworld-go --env TARGET=Knative
    
    Creating service 'hello' in namespace 'default':
    
      0.271s The Route is still working to reflect the latest desired specification.
      0.580s Configuration "hello" is waiting for a Revision to become ready.
      3.857s ...
      3.861s Ingress has not yet been reconciled.
      4.270s Ready to serve.
    
    Service 'hello' created with latest revision 'hello-bxshg-1' and URL:
    http://hello-default.apps-crc.testing

Creating serverless applications using YAML

To create a serverless application, you can create a YAML file and apply it using oc apply.

You can create a YAML file by copying the following example:

Example Knative service YAML
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: helloworld-go
  namespace: default
spec:
  template:
    spec:
      containers:
        - image: gcr.io/knative-samples/helloworld-go
          env:
            - name: TARGET
              value: "Go Sample v1"

In this example, the YAML file is named hello-service.yaml.

Procedure
  1. Navigate to the directory where the hello-service.yaml file is contained.

  2. Deploy the application by applying the YAML file.

    $ oc apply --filename hello-service.yaml

After the service has been created and the application has been deployed, Knative will create a new immutable revision for this version of the application.

Knative will also perform network programming to create a route, ingress, service, and load balancer for your application, and will automatically scale your pods up and down based on traffic, including inactive Pods.