Introduction

This guide is a walkthrough of the Kubernetes application marketplace on Civo. If you are a #KUBE100 beta tester, you have access to the world's first managed k3s service, and can deploy clusters from your dashboard or the command line. To make setup easier, we have developed a one-click install marketplace for applications. By following this guide, you will deploy a cluster, install an application from the marketplace, and verify it is working as expected. You will then be able to extend this cluster with customisations of your choice.

Pre-requisites

As of the time of writing, access to the managed Kubernetes service is in Beta, for which you can apply here.

To administer your cluster from the command line, you will need to set up kubectl using the instructions for your operating system.

You will also need to have Docker set up, because our function will be managed as a Docker container. Make sure you also have a Docker Hub login, as that will allow you to post the container to the registry so that it can be deployed.

To deploy the function we are going to create, you will also need to set up faas-cli from OpenFaaS.

Once you're all set, we'll get deploying!

Setting Up Your Cluster

Let's set up a two-node cluster using the Civo Kubernetes dashboard. Simply log in to your account and click on the Kubernetes link on the left in the navigation pane. If you are setting up your first cluster, you will see the following screen:

Kubernetes Account

Create a cluster by clicking the button, which brings up the setup screen below:

Cluster creation

I'm going to use the following options:

  • Name: app-demo
  • How many nodes? 2
  • Select Size: Medium

Scrolling down, we can see the Marketplace with applications available for installation onto our cluster:

Civo Kubernetes Marketplace

We are going to set up OpenFaaS to demonstrate an easy proof of concept. A later post will aim to demonstrate the marketplace in speeding up real-world application workflows.

Click OpenFaaS to select it for installation.

Now that we have our options selected, hit "Create" and wait for the cluster to become ready. The page will show details of the cluster as it's built.

Accessing the OpenFaaS User Interface

Once your cluster is built, scroll up the page to get your cluster's DNS name. You can copy it to the clipboard by clicking the entry.

Kubernetes Cluster DNS Name

The installed application, OpenFaaS, will have configuration information which we can view by refreshing the page and clicking on its icon under "Installed applications":

OpenFaas Instructions

We just need the password for our cluster's administration panel. We can find it on the OpenFaas application information pane, under Gateway password, shown on the image above. The system will allow you to copy it to your clipboard with a simple click. Feel free to ignore the rest of the information under "Obtain access" in the OpenFaaS information pane for now.

You can go ahead and open a web browser and navigate to http://{your-civo-dns.k8s.civo.com}:31112. Use the username admin and the password you just copied from the dashboard panel.

You should see something along the lines of the following:

OpenFaasUI

OpenFaaS allows us to quickly deploy serverless functions using our cluster as the end-point for our queries. You can build your on function to perform a task, or use one of the available functions in the OpenFaas repository.

We are going to build a function on the command line and deploy it into our cluster.

Getting Started With Our Function

In your terminal, having set up the OpenFaas command-line tool and kubectl, go through the steps of getting terminal access to OpenFaaS on your cluster. These are listed in the OpenFaaS information pane on your cluster page, but also repeated below:

Copy the DNS Name from the cluster information page, and paste it between the quotes in the code below:

$ export DNS=""
$ export OPENFAAS_URL=http://$DNS:31112

We will need our kubeconfig, so download it from the cluster info page (look for "Kubeconfig: Click to download") and save it as civo-kubeconfig.

Now set your KUBECONFIG variable to point at your new cluster:

$ export KUBECONFIG=/path-to/your/civo-kubeconfig

Now we can use faas-cli to log in to our cluster. Save the cluster's password you obtained above as password.txt and run the following:

$ cat password.txt | faas-cli login --username admin --password-stdin
Calling the OpenFaaS server to validate the credentials...
WARNING! Communication is not secure, please consider using HTTPS. Letsencrypt.org offers free SSL/TLS certificates.
credentials saved for admin

Building our Function

OpenFaaS comes with function scaffolds for a number of languages that saves us from writing tedious configuration files. Let's get an updated list:

$ faas-cli template pull
Fetch templates from repository: https://github.com/openfaas/templates.git at master
Attempting to expand templates from https://github.com/openfaas/templates.git
Fetched 17 template(s) : [csharp csharp-armhf dockerfile dockerfile-armhf go go-armhf java12 java8 node node-arm64 node-armhf php7 python python-armhf python3 python3-armhf ruby] from https://github.com/openfaas/templates.git

Because it's simple, nice and effective, I'm going to use Ruby for my function example. You can pick your favourite language from the template list that was downloaded.

Let's scaffold a function:

$ faas-cli new --lang ruby openfaas-demo --prefix="<your docker username>"

The output will be something like:

Folder: openfaas-demo created.
  ___                   _____           ____
 / _ \ _ __   ___ _ __ |  ___|_ _  __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
| |_| | |_) |  __/ | | |  _| (_| | (_| |___) |
 \___/| .__/ \___|_| |_|_|  \__,_|\__,_|____/
      |_|


Function created in folder: openfaas-demo
Stack file written: openfaas-demo.yml

Because we set the OPENFAAS_URL environmental variable above, OpenFaaS has picked it up. You will be able to see if you open openfaas-demo.yml in a text editor: It knows about our cluster and OpenFaaS running on it! That means deploying it will be easy.

Let's open up the handler file in our openfaas-demo directory for editing. Currently, it looks like this:

class Handler
  def run(req)
    return "Hello world from the Ruby template"
  end
end

If we were to run this, we would get the same text returned to us on every invocation of the function. Let's play around with it and make it return some lottery numbers for us. I'll have it return six numbers from 1-49. Update handler.rb to be:

class Handler
  def run(req)
    puts "Your lucky numbers are: "
    pick_numbers.each { |element| element }
  end

  def pick_numbers
    array = [*1..49]
    array.sample(6).sort
  end
end

Deploying and Invoking our Function

Make sure you're logged in to your Docker account by running docker login. Now we can deploy our brand-new function! This is done using the faas-cli up command.

$ faas-cli up -f openfaas-demo.yml
[0] > Building openfaas-demo.
Clearing temporary build folder: ./build/openfaas-demo/
Preparing ./openfaas-demo/ ./build/openfaas-demo//function
[...]
Deployed. 202 Accepted.
URL: http:/(your-civo-cluster-id).k8s.civo.com:31112/function/openfaas-demo

If you visit the URL the faas-cli up command prints out, you will see the output of our function. Try it now!

You can also visit the OpenFaaS UI at the URL, leaving out the /function/openfaas-demo/ part. The page will ask you for a username and password. The username is admin and the password is your cluster's administration password. Click through to the openfaas-demo function that should be visible on the left.

OpenFaasUI

If you click "Invoke", which I have done in the above example 83 times, you will get a response returned in the "Request body" field. Alternatively, if you visit the URL specified in the top part of the page, you will be able to get the text returned in your browser. You can also use any API client such as Postman to invoke the function by hitting the endpoint URL.

If you feel like it, you can remove the function using the trash can icon on the top right of the page. You can deploy any number of functions from the store, or use the faas-cli command-line tool.

Conclusion: What's Happening Under the Hood?

In this guide, we have deployed a two-node Kubernetes cluster using Civo's managed Kubernetes service. We installed OpenFaaS as part of the cluster creation process from the Marketplace to allow us to manage and run functions on this cluster.

We then built a simple function and pushed it to Docker Hub. This containerised function was deployed on our cluster through the faas-cli tool. We accessed the OpenFaaS web UI through the OpenFaas gateway on port 31112 on our node, using the system-generated administrator password. This allowed us to view the results of our function, or deploy new functions, accessible by hitting the specified endpoint URL.

OpenFaaS will auto-scale to demand, so if the function you deployed becomes heavily trafficked, more resources will be dedicated to serving it. Conversely, if something happens to your Kubernetes node, the master will take over all pod functions to keep the service going. This means the service is more resilient than a single-node setup.

Contributing

If you have an idea for an application you would like to see on the Marketplace, feel free to submit a Pull Request on the GitHub repository. All the required information is in the Readme here.

Next Steps

This simple example has demonstrated the speed at which you can deploy a Kubernetes cluster and run an application on it as a proof-of-concept. All-in, following this guide should leave you with a deployed cluster and function within 10 minutes, assuming you have the required tools set up.

If you were to operate this function longer-term, it would be advisable to secure the service with TLS.

Alternatively, you could install, say, MinIO for object storage and MariaDB as a database, and implement a Kubernetes-based web application using a framework such as Rails or React.

Sign up to the world's first managed k3s Service and share your experiences in our community Slack channel!