Skip to content

Commit b283179

Browse files
committed
Add post on the function custom resource
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent c165fd9 commit b283179

File tree

4 files changed

+246
-1
lines changed

4 files changed

+246
-1
lines changed

_layouts/page.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ <h2 class="subtitle is-size-4">
2727
<section class="gumroad-cta">
2828
<h3 class="gumroad-cta-title">Checkout the official eBook and video workshop for OpenFaaS</h3>
2929
<a href="https://gumroad.com/l/serverless-for-everyone-else">
30-
<img src="https://static-2.gumroad.com/res/gumroad/2028406193591/asset_previews/741f2ad46ff0a08e16aaf48d21810ba7/retina/social4.png" alt="Serverless eBook cover">
30+
<img src="https://www.alexellis.io/serverless.png" alt="Serverless eBook cover">
3131
</a>
3232
<p>Learn use-cases for open-source serverless functions and how to integrate them into your existing workflows.</p>
3333
<p>Through the hands-on exercises you will build your own JavaScript functions using Node.js. You'll add custom npm modules, integrate with other HTTP APIs, enable authentication and monitor the endpoints with Grafana.</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
---
2+
title: "Learn how to manage your functions with kubectl"
3+
description: "Learn an alternative to the OpenFaaS API for managing your serverless functions"
4+
date: 2021-05-12
5+
image: /images/2021-06-kubectl-functions/background.jpg
6+
categories:
7+
- enterprise
8+
- rbac
9+
- functions
10+
- kubernetes
11+
author_staff_member: alex
12+
dark_background: true
13+
14+
---
15+
16+
Learn an alternative to the OpenFaaS API for managing your serverless functions
17+
18+
## Introduction
19+
20+
In the 5 years that we've been building OpenFaaS, knowing who is using the project and for what has been one of the greatest challenges. This isn't unique to our space, but a problem with Open Source in general.
21+
22+
If you are an end-user, please send your use-case into our [ADOPTERS.md](https://github.com/openfaas/faas/blob/master/ADOPTERS.md) file and consider [becoming a GitHub Sponsor](https://github.com/sponsors/openfaas) so that we can continue our work.
23+
24+
### Two ways to operate
25+
26+
As we understand it, most of you are deploying your functions using our [faas-cli](http://github.com/openfaas/faas-cli) command, but there is an alternative available for Kubernetes users using a [Custom Resource](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/). It may not be for everyone, and there are pros and cons that I'll mention below, before showing you how to use this option, should you decide to change.
27+
28+
![Using the CRD to deploy functions with kubectl](/images/2021-06-kubectl-functions/operator-crd.png)
29+
> Pictured: Using the CRD to deploy functions with kubectl
30+
31+
The Custom Resource only contains information on how to deploy the function once it's already been built, where as the stack.yml file contains this along with how to build the function itself.
32+
33+
You can use `kubectl apply` instead of the OpenFaaS CLI and API which has several implications:
34+
35+
Pros:
36+
* You can use `kubectl apply/create/delete/edit` to deploy and manage functions
37+
* You can create a helm chart of functions, and edit values like image tags using a `values.yaml` file
38+
* You can use GitOps tools like Flux and ArgoCD to continuously deploy functions
39+
40+
Cons:
41+
* Still requires a separate OpenFaaS stack.yaml for the build configuration such as the template name and `build_args`
42+
* Harder to use with simple REST calls than the OpenFaaS API
43+
* Requires access to the Kubernetes API server
44+
* Requires an account with correct RBAC permissions to be created and managed
45+
* Requires `kubectl` to be installed
46+
47+
### The Custom Resource Definition (CRD) and Operator mode
48+
49+
This approach requires OpenFaaS to have been installed with its Function Custom Resource Definition (CRD) and the [faas-netes controller](https://github.com/openfaas/faas-netes) will need the `--operator` flag too.
50+
51+
Here is a minimal example of the Function Custom Resource:
52+
53+
```yaml
54+
# nodeinfo.yaml
55+
apiVersion: openfaas.com/v1
56+
kind: Function
57+
metadata:
58+
name: nodeinfo
59+
namespace: openfaas-fn
60+
spec:
61+
name: nodeinfo
62+
image: functions/nodeinfo:latest
63+
```
64+
65+
The equivalent in an OpenFaaS stack.yml file would be:
66+
67+
```yaml
68+
# stack.yml
69+
functions:
70+
nodeinfo:
71+
image: functions/nodeinfo:latest
72+
```
73+
74+
See also: [OpenFaaS YAML reference](https://docs.openfaas.com/reference/yaml/)
75+
76+
And for a CLI deployment without any YAML files would be:
77+
78+
```bash
79+
faas-cli deploy nodeinfo \
80+
--image functions/nodeinfo:latest
81+
```
82+
83+
All fields available within the OpenFaaS stack YAML file are available in the Custom Resource, apart from fields which describe how to build the function such as the template name or the code path.
84+
85+
Here is a more complete example:
86+
87+
```yaml
88+
# complete-example.yaml
89+
apiVersion: openfaas.com/v1
90+
kind: Function
91+
metadata:
92+
name: nodeinfo
93+
namespace: openfaas-fn
94+
spec:
95+
name: nodeinfo
96+
handler: node main.js
97+
image: functions/nodeinfo:latest
98+
labels:
99+
com.openfaas.scale.min: "2"
100+
com.openfaas.scale.max: "15"
101+
annotations:
102+
current-time: Mon 6 Aug 23:42:00 BST 2018
103+
next-time: Mon 6 Aug 23:42:00 BST 2019
104+
environment:
105+
write_debug: "true"
106+
limits:
107+
cpu: "200m"
108+
memory: "256Mi"
109+
requests:
110+
cpu: "10m"
111+
memory: "128Mi"
112+
constraints:
113+
- "cloud.google.com/gke-nodepool=default-pool"
114+
secrets:
115+
- nodeinfo-secret1
116+
```
117+
118+
### Generating the Function Custom Resource
119+
120+
Remember that earlier I said that Custom Resource only contains information on how to deploy the function once it's already been built? Well that means that you still need your `stack.yml` file for the build information.
121+
122+
So instead of maintaining two files, we added a command to generate the Kubernetes YAML. This way your workflow is to update and maintain just the existing file and generate a Kubernetes manifest whenever you need it, even at deployment time if you like.
123+
124+
Generate a YAML from a store function:
125+
126+
```bash
127+
faas-cli generate \
128+
--from-store nodeinfo \
129+
> nodeinfo-store.yaml
130+
```
131+
132+
Gives:
133+
134+
```yaml
135+
# nodeinfo-store.yaml
136+
---
137+
apiVersion: openfaas.com/v1
138+
kind: Function
139+
metadata:
140+
name: nodeinfo
141+
namespace: openfaas-fn
142+
spec:
143+
name: nodeinfo
144+
image: ghcr.io/openfaas/nodeinfo:latest
145+
labels: {}
146+
```
147+
148+
Generate YAML from all the functions within a single stack.yml file:
149+
150+
```bash
151+
faas-cli new --lang go fn1
152+
faas-cli new --lang go fn2 --append fn1.yml
153+
mv fn1.yml stack.yml
154+
155+
faas-cli generate \
156+
> functions.yaml
157+
```
158+
159+
Gives:
160+
161+
```yaml
162+
# functions.yaml
163+
---
164+
apiVersion: openfaas.com/v1
165+
kind: Function
166+
metadata:
167+
name: fn1
168+
namespace: openfaas-fn
169+
spec:
170+
name: fn1
171+
image: fn1:latest
172+
---
173+
apiVersion: openfaas.com/v1
174+
kind: Function
175+
metadata:
176+
name: fn2
177+
namespace: openfaas-fn
178+
spec:
179+
name: fn2
180+
image: fn2:latest
181+
```
182+
183+
If you only want one of the functions then you can use the `--filter` flag as follows:
184+
185+
```bash
186+
faas-cli generate --filter fn2
187+
```
188+
189+
The generate command can also generate a spec for Knative serving, so that if you happen to be a Knative user, or have both OpenFaaS and Knative within your organisation, you can deploy functions you've built with `faas-cli`.
190+
191+
See `faas-cli generate --help` for more.
192+
193+
A handy trick for deploying functions quickly is to bypass saving the generate command's output to a file, and applying it directly to your cluster.
194+
195+
```bash
196+
faas-cli generate \
197+
--from-store figlet \
198+
| kubectl apply -f -
199+
```
200+
201+
### Exploring functions
202+
203+
Once your functions have been created, you can go ahead and use all the `kubectl` commands you would expect to manage them.
204+
205+
```bash
206+
# List them
207+
kubectl get functions \
208+
-n openfaas-fn
209+
210+
# Output their data as YAML
211+
kubectl get functions \
212+
-n openfaas-fn -o yaml
213+
214+
# Edit one and save a change
215+
kubectl edit function/figlet \
216+
-n openfaas-fn
217+
218+
# Delete a function
219+
kubectl delete function/figlet \
220+
-n openfaas-fn
221+
```
222+
223+
## Summing up
224+
225+
I hope that you've enjoyed learning about the alternative to the OpenFaaS API and CLI for deploying and managing functions on Kubernetes. For some of you this may be the first time you've heard of it and for others, you may still be wondering if you should be moving over to it.
226+
227+
### Upgrade path
228+
229+
If you are finding that the OpenFaaS CLI, REST API and everything else is suiting your needs, then there's no reason to change at this time. If you are a Kubernetes advocate and want to use tools like Argo or Flux, then you should consider deploying the operator and getting familiar with it.
230+
231+
If you do go ahead and upgrade, just bear in mind that you will need to delete any functions that you have deployed in the cluster beforehand. The operator can only manage functions that it has created, so bear this in mind.
232+
233+
We can offer [help and support](https://openfaas.com/support) if you would like to migrate or start taking advantage of GitOps to manage your functions.
234+
235+
### You may also like
236+
237+
You may also like our recent post on deploying functions [Argo CD](https://www.openfaas.com/blog/bring-gitops-to-your-openfaas-functions-with-argocd/) or [Flux v1](https://www.openfaas.com/blog/openfaas-flux/). Stay tuned for a post on Flux v2 which we have in the works, keep in touch by [following us on Twitter](https://twitter.com/openfaas).
238+
239+
Of course deploying functions is only part of the story, you'll also need to build them, so checkout my examples in my latest eBook on OpenFaaS: [Serverless For Everyone Else](https://gumroad.com/l/serverless-for-everyone-else). You'll find an example with GitHub Actions that works on regular cloud, ARM servers and Raspberry Pi
240+
241+
Do you have questions, comments or suggestions?
242+
243+
* Browse the [OpenFaaS documentation](https://docs.openfaas.com)
244+
* Follow [OpenFaaS on Twitter](https://twitter.com/openfaas)
245+
* Join [OpenFaaS Slack](https://slack.openfaas.io/)
Loading
Loading

0 commit comments

Comments
 (0)