DEV Community

Cover image for Knative functions
Ashok Nagaraj
Ashok Nagaraj

Posted on

5

Knative functions

Knative functions are used to create, build, and deploy stateless, event-driven functions as Knative Services by using the func CLI. Knative functions can run any container on any Kubernetes cluster, whether the container is built around a serverless function or other application code. Knative functions can handle network routing, event triggers and autoscaling.

Advantages of Knative functions and typical use cases are:

  • They are stateless and event-driven, which means they can respond quickly and efficiently to changes in data or user requests.
    • They are autoscaled by Knative based on demand, which means they can scale up or down as needed, even to zero when idle.
    • They support traffic management and progressive rollout, which means they can route requests to different versions of an application based on rules or percentages.
    • They are developer-friendly software, which means they provide a simple programming model that abstracts away complex Kubernetes concepts and tasks.
    • They offer flexibility and control, which means they allow developers to customize their functions with environment variables, annotations, secrets, config maps etc..

To create a serverless function with func CLI and Knative, you need to follow these steps:

  • Install Knative Serving and Eventing components on your Kubernetes cluster.
  • Install func CLI on your local machine.
  • Create a new project directory and initialize a function using func create command. You can specify the language, runtime, template and name of your function. For example:
  mkdir hello
  cd hello
  func create --runtime python --template http --name hello
Enter fullscreen mode Exit fullscreen mode
  • Write your function code in the generated file (e.g., hello.py). You can use any dependencies or libraries you need by adding them to the requirements.txt file.
  • Build your function into a container image using func build command. You need to provide a registry where your image will be pushed. For example:
  func build -r <registry>/<username>/hello
Enter fullscreen mode Exit fullscreen mode
  • Deploy your function to your cluster using func deploy command. You need to provide the same registry as before and a namespace where your function will run. For example:
  func deploy -r <registry>/<username>/hello -n default
Enter fullscreen mode Exit fullscreen mode
  • Invoke your function using func invoke command or by sending an HTTP request to its URL. For example:
  func invoke hello
  curl http://hello.default.example.com
Enter fullscreen mode Exit fullscreen mode

Related: Cloud events with python
To create a Python CloudEvents function using func CLI, you need to follow these steps:

  • Create a new project directory and initialize a function using func create command with --template events option. This will generate a project that responds to a CloudEvent over HTTP. For example:
  mkdir hello-events
  cd hello-events
  func create --runtime python --template events --name hello-events
Enter fullscreen mode Exit fullscreen mode
  • Write your function code in the generated file (e.g., hello-events.py). You can access the CloudEvent data and context as parameters of your function. For example:
  def main(event, context):
    print(f"Hello, {event['name']}!")
    print(f"Context: {context}")
Enter fullscreen mode Exit fullscreen mode
  • Build and deploy your function to your cluster using func build and func deploy commands as before. For example:
  func build -r <registry>/<username>/hello-events
  func deploy -r <registry>/<username>/hello-events -n default
Enter fullscreen mode Exit fullscreen mode
  • Invoke your function by sending a CloudEvent to its URL using curl or another tool. For example:
  curl -v "http://hello-events.default.example.com" \
    -X POST \
    -H "Ce-Id: say-hello" \
    -H "Ce-Specversion: 1.0" \
    -H "Ce-Type: greeting" \
    -H "Ce-Source: not-sendoff" \
    -H "Content-Type: application/json" \
    -d '{"name":"Knative"}'
Enter fullscreen mode Exit fullscreen mode

References

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more