Manage Kubernetes custom resources
Custom Resource Definitions (CRDs) extend Kubernetes to allow you to manage
resources controlled by in-cluster applications with the same tools and workflow
as built-in Kubernetes resources, such as pods and nodes. You can manage CRDs
with the kubernetes_manifest
Terraform resource type. Managing the resources
running inside your Kubernetes clusters with Terraform allows you to use a
single workflow to manage your infrastructure and take advantage of Terraform's
workflow and features.
OpenFaaS, an open source Functions as a Service (FaaS) platform that you can run inside your Kubernetes cluster, includes a Kubernetes operator that defines a CRD you can use to manage OpenFaaS functions.
In this tutorial, you will create a Kubernetes cluster on your local machine with Docker and kind. Then, you will deploy OpenFaaS to your cluster with Terraform. Next, you will use Terraform to deploy a function to your Kubernetes cluster using the OpenFaaS functions CRD. Finally, you will configure CPU and memory limits for your function and scale it to run on multiple pods.
Prerequisites
This tutorial assumes that you are familiar with the standard Terraform workflow. If you are new to Terraform, complete the Get Started tutorials first.
For this tutorial, you will need:
- The Terraform CLI (1.0.1+).
- Docker Desktop installed and running.
- kubectl.
- kind.
Clone the example configuration
Clone the Manage Kubernetes Custom Resources with Terraform GitHub repository for this tutorial.
Change to the repository directory.
Start Kubernetes cluster
Create a Kubernetes cluster running in Docker on your local machine.
Verify that your cluster exists by listing your kind clusters.
Then, use kubectl
to print out information about your cluster. The context is
kind-
followed by the name of your cluster.
Review example configuration
Open main.tf
. This configuration uses the Kubernetes provider to create
namespaces for OpenFaaS and the Helm provider to deploy OpenFaaS on your
Kubernetes cluster.
First, the configuration sets up the Kubernetes provider with client certificate
authentication using four variables. You will configure these variables in the
next section. The configuration creates two namespaces: one for the OpenFaaS
application, named openfaas
, and another for your functions, named
openfaas-fn
.
Then, the helm
provider block configures Kubernetes authentication using the
same values as the Kubernetes provider. The time_sleep
resource avoids an
error from Helm when you destroy your OpenFaaS cluster.
Finally, the helm_release.openfaas
resource defines your OpenFaaS chart. This
resource configures your OpenFaaS deployment, sets the namespace for your
functions, enables basic authentication, and creates the operator that you will
use to deploy functions using a CRD.
Deploy OpenFaaS
Terraform must authenticate with Kubernetes to deploy resources to your cluster.
Use the kubectl config view
command to generate a terraform.tfvars
file from
the template in the example repository. This file sets values for variables in
the example configuration, which the Kubernetes provider will use to
authenticate with your Kubernetes cluster.
The terraform.tfvars
file now contains values the Kubernetes provider will use
to authenticate with your cluster. These values will be different from the
example below.
Now initialize Terraform.
Apply the configuration to deploy OpenFaaS to your Kubernetes cluster. Respond
yes
to the prompt to confirm the apply.
Verify OpenFaaS deployment
List the deployments in your OpenFaaS namespace.
List the two CRDs installed with the OpenFaaS operator.
Inspect the functions.openfaas.com
CRD.
You will use this CRD with the Kubernetes Terraform provider to manage your OpenFaaS functions.
Forward OpenFaaS API gateway
The OpenFaaS gateway allows you to call OpenFaaS functions over HTTP. Before you deploy a function to OpenFaaS, forward the port so you can connect to the gateway running inside your Kubernetes cluster.
This command will continue running in your current terminal until you cancel it. Open a new terminal window or tab to proceed with this tutorial.
In that new terminal, navigate to the top-level learn-terraform-k8s-faas-crd
directory you cloned from GitHub earlier in this tutorial. Verify your current
directory with pwd
.
Deploy an OpenFaaS function
The
kubernetes_manifest
resource type allows you to use Terraform to manage resources defined by a
Kubernetes manifest. In this section, you will use a manifest to deploy a
function to OpenFaaS with the functions.openfaas.com
CRD.
First, change into the function's directory. This function returns a randomly selected ASCII art featuring cows.
Review main.tf
.
The configuration in main.tf
does not define any resources. You will add a
kubernetes_manifest
resource to this configuration to manage your OpenFaaS
function.
First, reconfigure kubectl
to point to your openfaas
Kubernetes cluster.
Generate a terraform.tfvars
file to store your Kubernetes authentication
secrets as you did earlier.
Initialize this configuration to install the provider and set up Terraform.
Review cows.yaml
. This file is a Kubernetes manifest that defines the function
you will deploy with the OpenFaaS functions CRD.
You must convert this manifest from YAML to HCL to use it with the Terraform Kubernetes provider.
Convert cows.yaml
to HCL with the yamldecode()
Terraform function and the
terraform console
command.
Add a kubernetest_manifest
resource for the showcow
function. Assign the
value of the manifest
attribute to the HCL generated by the previous
command.
The HCL manifest you pasted into your configuration includes additional
whitespace and does not line up with the rest of your configuration. Save
main.tf
and use terraform fmt
to format your configuration.
Apply your configuration to deploy the function to your OpenFaaS cluster.
Respond yes
to the prompt to confirm the apply.
Now, call your function by using the curl
command to send a request to the
gateway you set up earlier. The function will return a randomly selected piece
of cow-related ASCII art.
List OpenFaaS function pods
Your OpenFaaS functions run on pods in the openfaas-fn
namespace in your
Kubernetes cluster.
List the pods in your openfaas-fn
namespace.
Right now, there is only one instance of the showcow
function running on your
Kubernetes cluster.
Scale the function
Add the following configuration to the "spec"
section of your
openfaas_fn_showcow
resource in main.tf
to manage scaling and resource
limits for the function. This configuration ensures that there are between four
and six instances of your function running at any given time. It also sets CPU
and memory usage limits for each instance.
Your configuration should now match the following.
Apply the new configuration. Respond yes
to the prompt to confirm the apply
and update your function.
Now, list your OpenFaas function pods again to confirm that Terraform scaled your function.
It may take a few minutes for Kubernetes to terminate the old pod and make the
new ones available. When this process is complete, your cluster will have four
running showcow-*
pods.
Retrieve more cow-related ASCII art from your function.
Clean up resources
In this section, you will delete your OpenFaaS function, remove OpenFaaS, and destroy your Kubernetes cluster.
Working in your learn-terraform-k8s-faas-crd/functions/cows
directory, use the terraform destroy
command to remove your function from OpenFaaS. Respond yes
to the prompt to confirm the destroy.
Switch to the terminal window running the kubectl port-forward
command, and
press <ctrl-c> to cancel it.
Ensure that you are in the root directory of the example repository.
Run terraform destroy
again to remove OpenFaaS and the associated resources from your cluster.
Respond yes
to the prompt to confirm the destroy.
Finally, delete your cluster.
Next steps
In this tutorial, you used Terraform to deploy OpenFaaS to a Kubernetes cluster. You then used the Kubernetes Terraform provider to deploy a function defined as a CRD to OpenFaaS. You also modified the CRD to scale your function.
Learn more about how to use Terraform to manage Kubernetes resources with CRDs and the OpenFaaS project by reviewing the following resources:
The Kubernetes provider documentation includes other examples of managing resources with
kubernetes_manifest
resource.The OpenFaaS Project website includes links to documentation and other resources.