Deploying OpenFaaS on Kubernetes Using Codefresh

Deploying OpenFaaS on Kubernetes Using Codefresh

5 min read

OpenFaas is a framework for building Serverless functions on top of containers. If you want to take advantage of the new functions and programming style, but still benefit from the robustness of containers for shipping and managing your code in production, then OpenFass combines the best of these 2 worlds. OpenFass is available on GitHub: https://github.com/openfaas/faas .

In this blog post, we will explore using OpenFaaS together with Codefresh. Codefresh has native integration with Kubernetes, and is the best way to continuously deploy code to Kubernetes.

Prerequisites

Before we begin, let’s make sure you are set up with the basics:

Setup OpenFaaS on Kubernetes

Follow the documentation to install.

We can view the Kubernetes resources right in Codefresh under the Kubernetes tab on your Codefresh home page. Codefresh provides an aggregative Kubernetes dashboard that shows Kubernetes entities in a user-friendly, service-oriented way. In this view, you can see that everything is successfully created and is in a healthy state. You can see I’ve deployed using the two namespace model using Helm.

 

Next, let’s go the the OpenFaaS dashboard: navigate to the node where the API Gateway is deployed, and access the UI with the port defined in the service. The default port is 31112. At this point you should clone the faas-cli and deploy the sample functions, so people can at least try one function after all the investment so far.

Building with Codefresh

Next, let’s create our function. There are many ways to accomplish this, but all you need to do is create a Docker container that can respond to a REST call. For this tutorial, we created a very simple Python script that accepts a string as a payload and returns a sentence along with that string. This is then deployed in a Docker container.

There is a command line tool used by the OpenFaaS community to build functions into Docker containers. To use this tool, follow the instructions here. This is the method we used for this guide.

At this stage you should have created a GitHub repository, with a Dockerfile and function folder. The repo we will use in this guide is located here for your reference: https://github.com/badamsbb/sample-func .

Add your Function to Codefresh

Once you’ve uploaded your repository to GitHub:

  1. Go to Codefresh (http://g.codefresh.io), and select Create Build.
  2. Select the repository that contains your function code and continue.
  3. The repo I used for this process is called “sample-func”.
  4. Select your repository, and click “NEXT.”
  5. We already have a Dockerfile in our repo so we’ll use that Dockerfile to run our build in Codefresh.
  6. After selecting the Dockerfile-based build, Codefresh will show you the execution command it will use to build your container. For this example, we need to set the build context to the “hello-python” folder, and the image name to our sample image. Because we are using the internal Codefresh registry, our image name must use the following format: <username>/<image>:<tag>.
  7. The next step allows us to review the Dockerfile, accept that, and select “Create” to finalize your pipeline.
  8. Selecting Build will immediately build this image, you can go ahead and do so.

Deploying with Codefresh

Link Codefresh Registry

Before we get to the Codefresh deployment, there is one other link we must make between our Kubernetes cluster and our Codefresh platform. To do that, perform the following steps:

We need to give Kubernetes access to our internal Codefresh registry. We can do that by first creating access credentials from within our dashboard.

Follow the instructions here to generate an API token, then use the following kubectl command to create a docker-registry secret within your Kubernetes cluster.

kubectl create secret docker-registry regsecret 
--docker-server=r.cfcr.io 
--docker-username=<codefresh-user> 
--docker-password=<api-token> 
--docker-email=<codefresh-email>

Run this command where you previously installed the kubectl binary.

Replace the codefresh-user , api-token , and codefresh-email  with your own values.

Remember what you name this secret, we’ll need it in the configuration of our deployment file.

Deployment file

The ultimate goal of this pipeline is to build and deploy a service to Kubernetes, that makes available a function in OpenFaaS.

A YAML file called deployment.yml  is contained within this repo. This file contains the Kubernetes spec needed to create the function within our cluster. I’ve posted it below.

Note: Pay close attention to the labels on the objects; they tell the OpenFaaS framework to register this service as an available function. The “imagePullSecret” is the value from the previous step. It allows any Pods created here to access our private Codefresh registry and pull the image. This image is named with the same name we gave it in the Build step in Codefresh.

kind: Deployment
apiVersion: extensions/v1beta1
metadata: null
name: hello-python
namespace: default
labels: null
faas_function: hello-python
spec: null
replicas: 1
selector: null
matchLabels: null
template: null
containers:
  -
    name: hello-python
image: r.cfcr.io/badamsbb/hello-python
ports:
  -
    containerPort: 8080
protocol: TCP
env:
  -
    name: fprocess
value: 'python index.py'
imagePullPolicy: IfNotPresent
imagePullSecrets:
  -
    name: regsecret
restartPolicy: Always

Deployment Step

To configure your build pipeline to deploy to your Kubernetes cluster, follow the steps below:

  1. Navigate to your repositories in Codefresh and select your function repository.
  2. Choose the first header option, Pipelines.
  3. Scroll down to the Deploy Script section, and choose Kubernetes.
  4. Select your cluster from the drop-down and fill in the required values.
  5. Select Kubernetes Deployment File in Repository, and enter the name of the deployment file to be used. In our case, it is simply deployment.yml .
  6. Scroll down, save the changes, and build.
  7. You’ll be able to see the build running on the Builds tab. Follow along if you’d like. You can walk through the steps it takes to build our container according to the Dockerfile, push to its internal repo, then deploy to the linked Kubernetes cluster using the deployment.yml  file.
  8. Once that’s finished, there’s one last step to take. We need to expose the service we’ve created in order for OpenFaaS to expose it as a function. This last step is performed by a simple kubectl  command:
kubectl expose deployment hello-python

Once that is run, take a look at the list of services in your Kubernetes cluster, you’ll see your deployed function.

If you navigate to your OpenFaaS dashboard, you should be able to see and interact with your function.

Summary

And that’s it! We learned how to easily deploy OpenFaaS on Kubernetes, and then how to deploy new functions to your OpenFaaS instance using Codefresh. Phew! Normally this would take a lot of work, but Codefresh makes this entire process very easy.

New to Codefresh? Create Your Free Account Today!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Ready to Get Started?
  • safer deployments
  • More frequent deployments
  • resilient deployments