
Serverless computing is a cloud application development model that allows developers to build and run code without managing the underlying infrastructure. It allows developers to focus solely on code and application development while server management tasks are effectively handled by applying tools such as OpenFaas.
OpenFaaS is an open-source framework that allows developers to write code in the form of functions and deploy it as Docker containers in a Kubernetes cluster. It offers a web interface and a command line interface (faas-cli) to deploy, manage and monitor functions. OpenFaas leverages Kubernetes capabilities to manage containerized functions and handle tasks such as scaling, load balancing, and high availability.
This article explains how you can run self-hosted serverless functions with OpenFaas in a Vultr Kubernetes Engine (VKE) cluster.
Before you begin:
On the management server:
Using SSH, access the server as a non-root user with sudo privileges
Install Kubectl to access the cluster
Install the Helm Package Manager
Set up a Domain A recordpointing to the Ingress Controller Load Balancer IP Address
This article uses
openfaas.example.com. Replace it with your actual domain name.
To install OpenFaas on your Kubernetes Cluster. Create a new namespace, and use the helm package manager to install OpenFaas in the cluster as described in the steps below.
Create a new namespaces.yml file to define the OpenFaas namespaces
Add the following contents to the file
Save and close the file
Apply the namespaces to your cluster
Verify that the namespaces are active and available
Output:
Using helm, add the OpenFaaS repository to your local repositories
Update your Helm repositories
Create a values.yaml file to customize the OpenFaas helm chart parameters
Add the following contents to the file. Replace openfass.example.com with your actual domain
Install OpenFaaS to the openfaas-kube namespace
When the deployment is successful, your output should look like the one below:
View the OpenFaaS administrator account password
Your password should display in a plain format like the one below
Verify the OpenFaaS deployment status
Output:
Verify that all OpenFaas deployments are ready and available
Using a web browser of your choice such as Microsoft Edge, visit your OpenFaas domain name to access the web interface
When prompted, enter the following administrator details:
USERNAME: admin PASSWORD: Generated-Password-You-Copied-Earlier
When logged in, the OpenFaas dashboard should display
To secure and serve incoming OpenFaas requests using HTTPS, generate a Let's Encrypt SSL certificate using CertManager as described below.
Create a new Cluster Issuer file openfaas-issuer.yaml
Add the following contents to the file
Save and close the file
Apply the issuer resource
Rename and back up the values.yaml file
Recreate the file
Add the following contents to the file. Replace openfaas.example.com with your actual domain
Save and close the file
Using helm, apply the configuration changes to your cluster
Verify the SSL certificate status
Output:
Verify that the certificate status is Ready. If False, describe the certificate to view the issuance process.
Output:
In a new web browser window, verify that you can securely access your OpenFaas web interface using HTTPS
faas-cli is a command-line tool that provides a convenient way to interact with the OpenFaaS application in your cluster. It allows you to create, deploy, manage, and invoke serverless functions in your terminal session instead of the web interface. Install the CLI tool to create and manage functions as described below.
Export the OPENFAAS_URL environment variable with your OpenFaas HTTPS URL
To permanently save the environment variable. Edit the .bash_profile file in your user home directory, and add the export directive at the end of the file. Then, run the source /home/example_user/.bash_profile command to load the environment variable.
Install the faas-cli tool
Output:
Log in to OpenFaaS using the faas-cli login command. Replace vusipdumobCT with the actual administrator password you generated earlier
Output:
In this section, use the faas-cli tool to create a new Node.js function, and deploy it to your DockerHub repository and Kubernetes cluster as described below.
Verify that Docker is available on your server
Output:
If the command fails returns an error, add your user account to the Docker group
Activate the new Docker group membership
Log in to your DockerHub account
When prompted, enter your DockerHub credentials to build and push new images
Create a new function directory
Switch to the function directory
Populate the directory with the Node.js template function
The above command creates a new function-js.yml configuration file, a function-js directory with the handler.js and package.json files. When successful, your output should look like the one below:
Edit the handler.js file
Change the done placeholder value to your desired status code such as <h1>Hello Good Morning!</h1>
The above function returns a Hello Good Morning! response each time the function runs
Edit the function-js.yml file
Change the gateway directive to match your OpenFaas domain URL and include your DockerHub before the function-js:latest declaration to set up a new repository
Save and close the file
Using the faas up command, build the Docker image, push it to Docker Hub, and deploy it to your Kubernetes cluster
When successful, your output should look like the one below:
In your web browser, load your new function using the OpenFaas URL
When successful, your Hello, Good Morning status code should display on the page
Or, use the faas invoke command to verify the deployed function
Output:
Press Ctrl + D to close the output
To list all deployed functions, run the faas list command
Output:
To view information about your function, run the faas describe command
Output:
To delete a function, use the faas remove command
In this article, you installed OpenFaaS on a Vultr Kubernetes Engine (VKE) cluster and created serverless functions using the faas-cli tool. You can use the tool to create and deploy new functions using different programming languages to your Kubernetes cluster for access through the OpenFaas web interface. For more information, visit the OpenFaaS documentation.
0 Comments
Be the first to comment and share your perspective with the community.