Open In App

Serverless Kubernetes With Kubeless : Event-Driven Microservices

Last Updated : 13 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The concept is the same whether it is referred to as Serverless, Event-driven computing, or Functions As A Service (FaaS) dynamically assigns resources to run distinct functions, or microservices, that are triggered by events. Application developers can concentrate on the application rather than the underlying infrastructure and all of its maintenance aspects thanks to serverless computing platforms.

Although serverless platforms are offered by most cloud providers, you may create your own with just two materials. One is the container orchestration system Kubernetes, which has established itself as a common foundation for developing resilient, componentized systems. The second is any of several systems that Kubernetes uses to create serverless application patterns.

What is Kubeless?

On top of it is an open-source serverless computing technology called Kubeless. Code can be deployed using Kubeless without requiring infrastructure management. Kubeless performs auto-scaling, routing, monitoring, and troubleshooting using Kubernetes resources. It is necessary to develop and implement routines that can be accessed through three distinct trigger methods.

  • pub-sub triggered
  • HTTP triggered
  • schedule triggered

HTTP triggered, exposed with its services and scheduling function, translates to a task; Pubsub triggered is managed using Kafka cluster, an integrated part of the Kubeless installation package. At the moment, Netcore, Ruby, NodeJS, and Python are supported.

Kubernetes Components

For this to be implemented you’ll need the:

  • A Kubernetes cluster (kind or minikube will work in a pinch).
  • Cluster admin access to your cluster (Kubeless installs CRDs and creates ClusterRoles).
  • kubectl installed and configured to communicate with your cluster.

Install Kubeless in your Kubernetes cluster

To get started with Kubeless, you need a running Kubernetes cluster. You can use a local cluster like Minikube or a cloud-based solution such as Google Kubernetes Engine (GKE) or Amazon EKS.

Installing Kubeless

Kubeless contains two pieces: a controller that’s running on your Kubernetes cluster, and a CLI that runs on your development machine.

To install Kubeless on your Kubernetes cluster, you can use the following commands:

kubectl create ns kubeless
kubectl create -f https://github.com/kubeless/kubeless/releases/download/v1.0.8/kubeless-v1.0.8.yaml

Installing Kubeless

The kubeless controller manager should be created in the kubeless namespace once the yaml files are installed. Additionally, CRDs such as functions, HTTP triggers, and cronjob triggers must to be built. You can check the status of the deployment by running the command below:

kubectl get pod -n kubeless

Screenshot-2024-01-18-172034

Deploy your first Kubeless function

Kubeless function

Kubeless’s primary building block is a function. Kubeless allows functions to be created in a variety of languages, including go, python, ruby, and java. A function always receives two arguments when it is called via an HTTP call, cron trigger, etc. Situation and Background. One may think of an event as an input to the standard functions. On the other hand, context is the attribute that holds the metadata.

Triggers

Triggers are the piece of code that will automatically respond ( or invoke a function ) to events like an HTTP call, life-cycle events, or on a schedule. The triggers that are currently available in kubeless are

  • HTTP Trigger
  • CronJob Trigger
  • Kafka Trigger
  • NATS Trigger

We’re now ready to create a function. We’ll keep things easy by writing a function that says hello and echos back the data it gets.

Open your favorite IDE, create a file named hello.py and paste the below code:

Triggers

Regardless of the language or event source, all functions in Kubeless have the same structure. Generally speaking, each function

  1. receives an object event as the initial input. All of the event source’s information is contained in this option. The content of the function request should be included in the key ‘data’ specifically.
  2. obtains a second object context containing general function information.
  3. gives back a string or object that can be utilized to reply to the caller.
  • Create the function with the kubeless CLI:

Create Function

Deploying function

Let’s take a closer look at the command:

  1. hello: This is the name of the function we want to deploy.
  2. –runtime python3.4: This is the runtime we want to use to run our function. Run kubeless ‘get-server-config’ to see all the available options.
  3. –from-file hello.py: This is the file containing the function code. This can be a file or a zip file of up to 1MB of size.
  4. –handler function.hello: This specifies the file and the exposed function that will be used when receiving requests.

Yes, your first function is now deployed. You can check the functions created by using the command

kubeless function ls

Kubeless function ls

Once the function is ready, you can call it by running:

kubeless function call hello --data 'Hey'

Kubeless function Call

Now that your function has started, good to go. Next steps, what should I do? Now let’s use the HTTP Trigger to call the function. For your function to be accessible to the public, you would require an Ingress controller. Any ingress controller will work. For the sake of this essay, we’ll be using the Nginx Ingress controller. Now let’s use Helm to install the Ingress controller.

helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
helm install ingress-nginx ingress-nginx/ingress-nginx
kubectl get pods -l app.kubernetes.io/name=ingress-nginx

You should now have an Ingress controller running in your Kubernetes cluster.

Let us now create an HTTP trigger using the kubeless command. If you observe my command precisely, I create an HTTP trigger event with the name hello-http-trigger and at the path env. This means that we will be able to invoke the function by sending an HTTP request to the endpoint http://<ingress-ip>/env.

##Create a HTTP trigger
kubeless trigger http create hello-http-trigger --function-name hello --path env

##Get the IP of the Ingress resource
ip=$(kubectl get ing hello-http-trigger -o jsonpath='{.status.loadBalancer.ingress[0].ip}')

##Get the hostname of the Ingress resource
host=$(kubectl get ing hello-http-trigger -o jsonpath='{.spec.rules[0].host}')

##Invoke the function by triggering a HTTP request
curl --data 'HOSTNAME' --header "Host: $host" --header "Content-Type:application/json" $ip/env;echo

Monitoring and Logging

  • Utilize Kubernetes tools and additional monitoring solutions to monitor the performance and logs of your serverless functions.

Cleanup

You can delete the function using the command below:

kubeless function delete hello
kubeless function ls

Kubeless Best Practices

  1. Assign Each Function a Minimal Role: Consider the idea of least privilege when granting roles and permissions to the serverless functionalities. The smallest set of permissions needed for any function to carry out its specified duties should be granted. This lessens the area that could be attacked and lessens the effect of any security flaws.
  2. Keep an eye on the information flow: It is essential to track and observe the information flow within the serverless application in order to spot any unusual activity or possible security breaches. By tracking and analyzing the information flow, logging and monitoring solutions—such as third-party tools or the built-in monitoring features of Kubernetes—can be implemented to enable the proactive discovery and mitigation of security vulnerabilities.
  3. Incorporate Tests for Production, CI/CD, and Service Configuration: For production settings, continuous integration and deployment (CI/CD), and service configuration, a strong testing approach is needed. Include automated tests at every development lifecycle stage to verify the security and functionality of your Kubeless functions.
  4. Dependencies for Secure Applications: Make sure the dependencies your serverless functions use are current and safe. To find and fix any security vulnerabilities, update the dependencies on a regular basis and run vulnerability checks. To provide an additional degree of protection, think about utilizing technologies for scanning container images.

Conclusion

In conclusion, serverless Kubernetes with Kubeless offers a powerful and flexible platform for building event-driven microservices. It simplifies the process of deploying, scaling, and managing serverless functions by leveraging the capabilities of Kubernetes.

This approach enables the creation of scalable, responsive, and efficient microservices that can seamlessly integrate with other Kubernetes services and resources. With the ability to trigger functions based on various events, such as HTTP requests, cron jobs, or custom events, Kubeless empowers developers to build applications that are highly responsive to real-time data streams, webhooks, and IoT device messages.

Serverless Kubernetes With Kubeless: Event-Driven Microservices – FAQ’s

What is serverless architecture in Kubernetes?

Serverless Kubernetes is a deployment framework for container management in the cloud in which you get the benefits of serverless architecture with the fast, reliable performance of Kubernetes.

What is the use of Serverless in Microservices architecture?

While serverless microservices carry the general advantages of serverless architecture, such as less overhead and improved cost efficiency, their primary benefit is the ease with which you can combine serverless functions and other managed services

What is the maximum memory size for serverless?

By default, your functions have 128 MB of memory allocated. You can increase that value up to 10 GB.

What is the function of kubeless?

Kubeless allows deploying code without having to worry about infrastructure. Kubeless uses kubernetes resources for auto-scaling, routing, monitoring and troubleshooting.

What is the difference between Kubeless and Knative?

Kubeless builds an image out of code and starts it on Kubernetes. Knative does the same, but uses a more modular approach, enabling different components to plug into and adapt to different deployment scenarios. Knative and Kubeless are both categorized as serverless and task processing tools.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads