Going Serverless in Kubernetes using Kubeless
Serverless in Kubernetes using Kubeless
Well, how many of you have come across the term serverless at least once in your IT journey? Boom, I guess a lot of you might have heard about this term and most of them are also using these concepts to run their production environments. Do you also have your applications running in Kubernetes? If yes, Serverless and Kubernetes make a deadly combination. I have heard tales from a few of my peers saying that it really makes it harder to integrate Kubernetes with any cloud agnostic serverless component because it might make their migration to a different cloud harder. Hence, they focused on a natively used solution for Kubernetes and can be run on a Kubernetes cluster. There are many options out there in the market to run serverless workloads in Kubernetes. Ex Openfaas, knative, kubeless, etc. In this article I will walk you through kubeless by Bitnami. is a Kubernetes-native serverless framework that lets you deploy small bits of code without having to worry about the underlying infrastructure plumbing.
What is the entire story all about? (TLDR)
- Understand the concepts of kubeless.
- Writing a kubeless function using Golang.
- Using an HTTP trigger, to trigger the kubeless Golang function.
Prerequisites
- A Kubernetes Cluster ( Can be either On-Prem, AKS, EKS, GKE, Kind ).
- Helm, kubectl installed.
Story Resources
- GitHub Link: https://github.com/pavan-kumar-99/medium-manifests
- GitHub Branch: kubeless.
Install Kubeless
Kubeless gets shipped with multiple yaml files to support RBAC and Non-RBAC clusters. You can find the yaml files here. By default, Kubeless gets installed in the kubeless namespace.
$ kubectl create ns kubeless$ kubectl create -f https://github.com/kubeless/kubeless/releases/download/v1.0.8/kubeless-v1.0.8.yaml
Once the yaml files are installed, you should find the kubeless controller manager created in the kubeless namespace. You should also find CRD’s like functions, HTTP triggers, cronjob triggers created.
Let us also now install the kubeless binary here.
Once the kubeless binary is installed you can find the server config and also the Runtime configurations by
$ kubeless get-server-configINFO[0000] Current Server Config:INFO[0000] Supported Runtimes are: ballerina0.981.0, dotnetcore2.0, dotnetcore2.1, dotnetcore2.2, dotnetcore3.1, go1.13, go1.14, java1.8, java11, nodejs10, nodejs12, nodejs14, php7.2, php7.3, php7.4, python3.6, python3.7, python3.8, ruby2.3, ruby2.4, ruby2.5, ruby2.6, jvm1.8, nodejs_distroless8, nodejsCE8, vertx1.8
Alright, we are now set with all the installation work. Let us now get into action. Before we start triggering or first kubeless function let us understand the basic terminologies of Kubeless and the Go code that we would be using throughout this article.
Kubeless Functions
Functions are the main unit in Kubeless. Functions in Kubeless can be written in multiple languages like java, python, ruby, go, etc. Whenever a function is triggered via an HTTP call, cron triggers, etc it receives two arguments. Event and Context. An Event can be considered as an input to the regular functions. Whereas context can be considered as the attribute that contains 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
a) HTTP Trigger
b) CronJob Trigger
c) Kafka Trigger
d) NATS Trigger
In the scope of this article, we will use a Go Function with an HTTP trigger. Here is the sample Go code that will take an environment variable key as an input and will print its corresponding value. In a Real-time scenario, you might have many more complex use cases. We will limit this article for beginners to understand the concepts and then cover many other complex use cases in my later articles.
Creating the first Kubeless Function
Kubeless functions can be created using 2 ways. Using the Kubeless binary and the other one is via the Yaml files. Let us cover both methods in the article.
$ git clone https://github.com/pavan-kumar-99/medium-manifests.git \
-b kubeless $ cd medium-manifests/env-go $ kubeless function deploy env-get-go --runtime go1.13 --handler env-check.Foo --from-file env-check.goINFO[0000] Deploying function...
INFO[0000] Function env-get-go submitted for deployment
INFO[0000] Check the deployment status executing 'kubeless function ls env-get-go'
Yaas, your first function is now deployed. You can check the functions created by using the command
$ kubeless function ls $ kubectl get all -l function=env-get-go
Alright, your function is now running. What to do next? Let us invoke the function using the HTTP Trigger. You would need an Ingress controller to make your function available to the public. You can use any ingress controllers. We will use the Nginx Ingress controller in the scope of this article. Let us now install the Ingress controller using Helm.
$ 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 env-get-go-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 env-get-go-http-trigger --function-name env-get-go --path env ##Get the IP of the Ingress resource$ ip=$(kubectl get ing env-get-go-http-trigger -o jsonpath='{.status.loadBalancer.ingress[0].ip}')##Get the hostname of the Ingress resource$ host=$(kubectl get ing env-get-go-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
Awesome !! Found that Interesting? Feel free to comment in the comments section. We have seen how kubeless commands can be used. How about having the manifests and storing them in any version control system ( May be Git? ). Yes, kubeless provides that option to generate the YAML manifests.
I have also uploaded the manifests to my GitHub Repo.
$ git clone https://github.com/pavan-kumar-99/medium-manifests.git \
-b kubeless$ cd medium-manifests/
Cleanup
$ kubeless function delete env-get-go $ kubeless trigger http delete env-get-go-http-trigger
I hope this covers the concepts and terminologies required for someone to get started with serverless in Kubernetes using kubeless. Please feel free to comment on your serverless ideas and your best experiences with serverless on Kubernetes.
More Shortly………..