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
- 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 therequirements.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
- 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
- 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
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
- 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}")
- Build and deploy your function to your cluster using
func build
andfunc deploy
commands as before. For example:
func build -r <registry>/<username>/hello-events
func deploy -r <registry>/<username>/hello-events -n default
- 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"}'
References
Top comments (0)