How to Install OpenFaaS CLI on a Vultr Cloud Server

Updated on January 24, 2024
How to Install OpenFaaS CLI on a Vultr Cloud Server header image

Introduction

Open Functions as a Service (OpenFaas) is an open-source serverless computing framework that allows you to build and deploy code as functions in containerized environments. OpenFaas CLI faas-cli is a command line tool used to prepare, package, deploy, and invoke functions on any platform.

This article explains how you can install OpenFaas CLI on a Vultr Cloud Server and create new functions for deployment on your target platform such as Docker or a Vultr Kubernetes Engine (VKE) cluster. You will set up a new Node.Js template function, push it to the Vultr Container Registry, and test access to the new function.

Prerequisites

Before you begin:

Install OpenFaas CLI

To install faas-cli on your system, you can either build the tool using the latest source file available on the OpenFaas CLI releases page or use a package manager. For fast deployments, use the OpenFaas CLI installation script or download the latest faas-cli version using your package manager as described in the system sections below.

Linux

  1. Using the Curl utility, download the latest OpenFaas CLI script and execute it on your server.

    console
    $ curl -sL https://cli.openfaas.com | sudo sh
    

    Output:

    Finding latest version from GitHub
    0.16.18
    Downloading package https://github.com/openfaas/faas-cli/releases/download/0.16.18/faas-cli as /tmp/faas-cli
    Download complete.
    
    Running with sufficient permissions to attempt to move faas-cli to /usr/local/bin
    New version of faas-cli installed to /usr/local/bin
       ___                   _____           ____
      / _ \ _ __   ___ _ __ |  ___|_ _  __ _/ ___|
     | | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
     | |_| | |_) |  __/ | | |  _| (_| | (_| |___) |
      \___/| .__/ \___|_| |_|_|  \__,_|\__,_|____/
           |_|
  2. Verify the installed OpenFaas CLI version.

    console
    $ faas-cli version
    

    Output:

      ___                   _____           ____
     / _ \ _ __   ___ _ __ |  ___|_ _  __ _/ ___|
    | | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
    | |_| | |_) |  __/ | | |  _| (_| | (_| |___) |
     \___/| .__/ \___|_| |_|_|  \__,_|\__,_|____/
          |_|
    
    CLI:
     commit:  9981e9ea7065d5dc2d4a17013aca04a1c97fe4df
     version: 0.16.18

Windows

  1. Install the Chocolatey Package Manager.

  2. Open a new Windows Powershell session as an administrator.

  3. Using Chocolatey, install the latest OpenFaas CLI version.

    console
    > choco install faas-cli
    

    Output:

    faas-cli v0.16.18 [Approved]                                                                                            faas-cli package files install completed. Performing other installation steps.                                           ShimGen has successfully created a shim for faas-cli.exe                                                                The install of faas-cli was successful.                                                                                  Software installed to 'C:\ProgramData\chocolatey\lib\faas-cli'                                                                                                                                                                                Chocolatey installed 1/1 packages.                                                                                       See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
  4. Verify the installed faas-cli version.

    console
    > faas-cli version
    

MacOS

  1. Install the Homebrew Package Manager.

  2. Open a new Mac terminal session.

  3. Using brew, install the latest OpenFaas CLI version.

    console
    $ brew install faas-cli
    
  4. Verify the installed faas-cli version.

    console
    $ faas-cli version
    

    Output:

      ___                   _____           ____
     / _ \ _ __   ___ _ __ |  ___|_ _  __ _/ ___|
    | | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
    | |_| | |_) |  __/ | | |  _| (_| | (_| |___) |
     \___/| .__/ \___|_| |_|_|  \__,_|\__,_|____/
          |_|
    
    CLI:
     commit:  9981e9ea7065d5dc2d4a17013aca04a1c97fe4df
     version: 0.16.18

Deploy Functions using OpenFaas CLI

faas-cli contains a collection of function templates you can modify and deploy on your platform. To test the tool operations, create a new Node.js function, deploy it to your Vultr Container Registry, and run it locally using Docker as described in the steps below.

  1. Using Docker, log in to your Vultr Container Registry. Replace /example, example-user, and registry-password with your actual values.

    console
    $ docker login https://sjc.vultrcr.com/example -u example-user -p registry-password
    
  2. Create a new functions directory.

    console
    $ mkdir sample-function
    
  3. Switch to the directory.

    console
    $ cd sample-function
    
  4. Create a new function using the Node.js template.

    console
    $ faas new function-js --lang node
    
  5. When successful, list files in the directory to verify the template files.

    console
    $ ls
    

    Output:

    function-js  function-js.yml  template
  6. Using a text editor such as Nano, edit the function-js.yml file.

    console
    $ nano function-js.yml
    
  7. Change the image value to include your registry URL, target image name, and version. For example, sjc.vultrcr.com/example/function-js:latest.

    yaml
    version: 1.0
    provider:
      name: openfaas
      gateway: http://127.0.0.1:8080
    functions:
      function-js:
        lang: node
        handler: ./function-js
        image: sjc.vultrcr.com/example/function-js:latest
    

    Save and close the file.

  8. Edit the handler.js file in the function-js directory.

    console
    $ nano function-js/handler.js
    
  9. Change the status value from done to Hello World! This is a new function.

    js
    "use strict"
    
    module.exports = async (context, callback) => {
        return {status: "Hello World! This is a new function"}
    }
    

    Save and close the file.

  10. Using faas-cli, build your new function Docker image.

    console
    $ faas-cli build -f function-js.yml
    
  11. Push the image to your Vultr Container Registry.

    console
    $ faas-cli push -f function-js.yml
    

    Output:

    [0] > Pushing function-js [sjc.vultrcr.com/example/function-js:latest]
    The push refers to repository [sjc.vultrcr.com/example/function-js]
    b112a4aa7899: Pushed 
    aab13da92792: Pushed 
    0ecbb3223988: Mounted from example/function-js 
    fb06df7913c9: Mounted from example/function-js 
    7c12f4f6f311: Mounted from example/function-js 
    175e187866ab: Mounted from example/function-js 
    ef754d6d25f6: Mounted from example/function-js 
    f1bd474f9894: Mounted from example/function-js 
    0ef0faea5d8d: Mounted from example/function-js 
    b247f74b283f: Mounted from example/function-js 
    7f30cde3f699: Mounted from example/function-js 
    fe810f5902cc: Mounted from example/function-js 
    dfd8c046c602: Mounted from example/function-js 
    4fc242d58285: Mounted from example/function-js 
    latest: digest: sha256:b70e4dcb7c5ad3c7907fbb52887b0b2cb3a01eaa92ee59ea54d5952931dac0f6 size: 3448
    [0] < Pushing function-js [sjc.vultrcr.com/example/function-js:latest] done.
    [0] Worker done.
  12. To deploy your new function to an OpenFaas cluster, run the following command to use your kubectl configuration to connect to the cluster.

    console
    $ faas-cli deploy -f function-js.yml --gateway http://openfaas.example.com
    

    For purposes of this article, run the following command to locally test your new function using Docker as a background process.

    console
    $ faas-cli local-run -f function-js.yml &
    

    When successful, verify that the new function Docker container is active and running.

    console
    $ docker ps
    

    Output:

    CONTAINER ID   IMAGE                                        COMMAND                  CREATED          STATUS                    PORTS                                       NAMES
    41b766818845   sjc.vultrcr.com/example/function-js:latest   "docker-entrypoint.s…"   49 minutes ago   Up 49 minutes (healthy)   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   function-js
  13. Using Curl, query the function on the localhost port 8080 defined in your function-js.yml file

    console
    $ curl 127.0.0.1:8080
    

    Output:

    2023/11/23 02:58:19 Forking fprocess.
    2023/11/23 02:58:19 Wrote 49 Bytes - Duration: 0.060841s
    {"status":"Hello World! This is a new function"}

    To handle invoke operations, modify your function code to include Input values.

  14. Each time you modify the template files, deploy the function to build your image and synchronize changes to your cluster

    console
    $ faas-cli deploy -f function-js.yml
    
  15. To stop the background faas-cli function process, verify the Job ID

    console
    $ jobs
    

    Output:

    [1]+  Running                 faas-cli local-run -f function-js.yml &

    Kill the target Job ID. For example 1.

    console
    $ kill %1
    

Conclusion

You have installed the OpenFaas CLI tool on a Vultr Cloud Server. Depending on your environment, you can use faas-cli to package, deploy, and invoke serverless functions on your OpenFaas cluster. For more information and advanced CLI options, visit the following resources: