Skip to content

Deploy and manage containerized applications on Knative's serverless infrastructure easily, cheaply and scale massively.

Notifications You must be signed in to change notification settings

serverless-components/knative

Repository files navigation

serverless framework knative component

Deploy and manage containerized applications on serverless Knative infrastructure easily, cheaply and scale massively. This component supports two ways of building container images out of your source code:

  • The Kubernetes mode will use Kaniko for building container image from your source code and user Docker Hub as a registry for handing over the container image to Knative. The credentials for a Docker Hub account need to be added to the configuration. This mode requires a Kubernetes cluster on which you are allowed to run Pods in privileged mode.

  • The OpenShift mode builds the container image with OpenShift's S2I mechanism and used the OpenShift internal registry for image hand-over, which does not require any extra security setup. This mode works only on OpenShift with OpenShift Serverless installed for running Knative services.

The mode is autodetected from the connected cluster. OpenShift mode is used by default when you have configured a connection URL to an OpenShift cluster; otherwise, Kubernetes mode is used.

Click the link above to collaborate on our Roadmap for the Serverless Framework's integration with Knative, Red Hat's OpenShift and more.

Prerequisites

Depending on which mode you are using, there are different prerequisites beside the access to a given Kubernetes or OpenShift cluster.

Kubernetes Mode

Knative should be installed on your Kubernetes cluster.

Furthermore, you need to create a ServiceAccount which is allowed to perform the following actions:

  • Create / Remove Namespaces
  • Create / Remove ConfigMaps
  • Create / Remove Pods
  • Create / Remove PersistentVolumeClaims
  • Create / Remove Knative Services

The ServiceAccount token will be used to manage the deployments and removals of Knative Service and needs to be configured.

Also, you must be able to run Pods in privileged mode. The privileged mode is a requirement for Kaniko for being able to create Docker images.

Docker Hub account

For the Kubernetes mode, a Docker Hub account is required. Currently, we're only supporting Docker Hub as a container registry for the Kubernetes mode, so you need a docker.com account. The credentials need to be added to the configuration.

Support for more container registries is on our Roadmap.

OpenShift Mode

For the OpenShift mode, you must have OpenShift Serverless installed on the cluster. OpenShift Serverless can be easily installed with the help of the Operator Catalog. Otherwise, there are no requirements except of course that you have access to the OpenShift cluster. Authentication is done via a token that you can obtain with oc whoami --show-token while being connected to the cluster.

Configuration

The credentials needed for connection to the cluster and possibly to Docker Hub are stored in .env with the following keys and meaning for the various modes:

Variable Kubernetes Mode OpenShift Mode
KUBERNETES_ENDPOINT The API Endpoint URL to the Kubernetes cluster, without port Same as for Kubernetes but for connecting to an OpenShift Cluster
KUBERNETES_PORT Port of the API endpoint Port of the API endpoint
KUBERNETES_SERVICE_ACCOUNT_TOKEN Token of the Service Account under which the deployment should be done The OpenShift access token which can be obtained with oc whoami --show-token
KUBERNETES_SKIP_TLS_VERIFY Set this to true if you want to skip TLS server certificate verification. Set this to true if you want to skip TLS server certificate verification.
DOCKER_USERNAME User name for connecting to Docker Hub Not used
DOCKER_PASSWORD User password for connecting to Docker Hub Not used

Quickstarts

Four examples can be used as quickstart or blueprint for your projects. All are simple REST application that returns a friendly "Hello World !" message.

  • hello-express is a simple Express application which returns "Hello Express !" when called
  • hello-go is an HTTP listener, written in Go, that returns "Hello Go !"
  • hello-quarkus is an example of a Quarkus Web app written in Java that returns "Hello Quarkus !"
  • hello-cgi demonstrates how a good old CGI bin can be deployed with the Serverless Framework for Knative. It will print "Hello CGI !" when accessed.

As seen in the examples, all you need is to provide a Dockerfile that will package your application into a container image. The application has to listen to port 8080 for now.

To run any of the examples perform the following steps:

  1. Install the latest Serverless Framework via npm install --global serverless.
  2. Copy the code of one of the example directories, e.g. cp -r example/quarkus /tmp/hello-quarkus.
  3. Change in this director (e.g. cd /tmp/hello).
  4. Run npm install to install all the dependencies.
  5. Update the org property in the serverless.yml file with the name of your serverless.com account.
  6. Update the .env file with your Kubernetes or OpenShift credentials and, if needed, your Docker secrets. See above for the possible configuration values.
  7. Run serverless deploy. If you want some output, use the options --verbose --debug.
  8. Visit the service URL to see your application running on your Kubernetes cluster via Knative or OpenShift Serverless.
  9. Optional: Download kn for inspecting the Knative services.
  10. Make some changes to your sample application and run serverless deploy again.
  11. Check with the URL that you change has been picked up.
  12. Run serverless remove to remove everything from your cluster.