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.
Before we begin, let’s make sure you are set up with the basics:
- You have a functioning Kubernetes cluster. We have a blog post on setting up a cluster here: https://docs.codefresh.io/v1.0/docs/adding-non-gke-kubernetes-cluster
- You have a Codefresh account. Codefresh is free to sign at https://g.codefresh.io/signup .
- Your Codefresh account is configured to connect with your Kubernetes cluster. This is easy to set up as described here: Adding a Kubernetes cluster to Codefresh.
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:
- Go to Codefresh (http://g.codefresh.io), and select Create Build.
- Select the repository that contains your function code and continue.
- The repo I used for this process is called “sample-func”.
- Select your repository, and click “NEXT.”
- We already have a Dockerfile in our repo so we’ll use that Dockerfile to run our build in Codefresh.
- 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>.
- The next step allows us to review the Dockerfile, accept that, and select “Create” to finalize your pipeline.
- 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 \
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.
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.
value: 'python index.py'
To configure your build pipeline to deploy to your Kubernetes cluster, follow the steps below:
- Navigate to your repositories in Codefresh and select your function repository.
- Choose the first header option, Pipelines.
- Scroll down to the Deploy Script section, and choose Kubernetes.
- Select your cluster from the drop-down and fill in the required values.
- 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 .
- Scroll down, save the changes, and build.
- 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.
- 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.
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!