Skip to content

Commit c6cf1e6

Browse files
committed
Publish blog post on kim
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
1 parent ca7450e commit c6cf1e6

File tree

2 files changed

+282
-0
lines changed

2 files changed

+282
-0
lines changed

_posts/2021-05-12-kim.md

Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
---
2+
title: "Learn how to build functions faster using Rancher's kim and K3s"
3+
description: "Learn how the kim tool from Rancher can be used to build functions directly into a K3s cluster"
4+
date: 2021-05-12
5+
image: /images/2021-05-kim/background-race.jpg
6+
categories:
7+
- live
8+
- use-cases
9+
- functions
10+
author_staff_member: alex
11+
dark_background: true
12+
13+
---
14+
15+
Learn how the kim tool from Rancher can be used to build functions directly into a K3s cluster
16+
17+
## Introduction
18+
19+
The workflow for building an OpenFaaS function involves creating a container image, pushing it into a registry and then deploying it into a cluster. That cluster could be remote, or running on your own PC, but during development this push and pull phase can cause a significant lag for testing changes.
20+
21+
In this blog post we'll explore how to test out kim, a new project written at Rancher Labs which can build a container image directly into a node's image library. That means that we can shave off several seconds whenever we want to build a new function and deploy it compared to using `faas-cli up` with Docker or buildkit.
22+
23+
![Matt and I talking about kim](https://pbs.twimg.com/media/E1HuFdsWEAEUE9C?format=jpg&name=medium)
24+
> Matt and I are talking about kim and Open Source funding [on my YouTube channel](https://youtu.be/l9j45bvv7Rk?t=63)
25+
26+
There is some prior art here, and Rancher certainly aren't the only ones trying to help. The term "inner-loop" is often used with productivity tools to describe the period between thinking of a change, writing the code and having it deployed onto your cluster.
27+
28+
Prior art:
29+
30+
* [Okteto](https://okteto.com/) - focused on sharing multiple namespaces between teams and providing a live-debug experience.
31+
* [Garden](https://garden.io/) - opinionated platform for making local development faster through "inner-loop"
32+
* [skopeo](https://github.com/containers/) - from Google, it deploys containers whenever you hit save
33+
* [tilt](https://tilt.dev/) - similar to skopeo, tilt provides a DSL for defining how to redeploy your code
34+
* [ko](https://github.com/google/ko) - specific to only Golang, ko is another Google project that is designed to deploy Go code to a Kubernetes cluster
35+
* [Live reloading with docker-compose](https://simonemms.com/blog/2020/08/12/live-reload-for-openfaas/) - an approach by Simon Emms to run the function outside of openfaas and reload code live with docker-compose
36+
37+
kim isn't revolutionary here, but it does provide a pragmatic tool that speeds up openfaas function development. Feel free to follow along with me, or try it out for yourself, next time you need to build a function.
38+
39+
> [Arkade](https://get-arkade.dev) is an open source marketplace for Kubernetes and has 40+ apps and 40+ CLIs available for download.
40+
41+
We'll use [arkade 0.7.14](https://github.com/alexellis/arkade/releases/tag/0.7.14) or newer to download the various CLIs we need and to install OpenFaaS. You can of course source these yourself if you prefer to do that for some reason.
42+
43+
## Quickstart
44+
45+
You'll need a Virtual Machine (VM) since kim currently does not work with k3d out of the box.
46+
47+
All commands need to be run on your client, not on the VM. Do not log into the VM, it's unnecessary.
48+
49+
### Create a VM for K3s
50+
51+
You can create a VM on your own computing using [Multipass](https://multipass.run).
52+
53+
```bash
54+
multipass launch \
55+
--cpus 2 \
56+
--disk 20G \
57+
--mem 8G \
58+
--name k3s-kim
59+
```
60+
61+
You can use `multipass exec k3s-kim bash` to add your public SSH key to the `.ssh/authorized_keys` file. Then find the IP using `multipass info`
62+
63+
Or pick your favourite cloud. Linode are a homepage sponsor for OpenFaaS, [you can get 100 USD of free credit to create VMs](https://linode.dev).
64+
65+
Once you have your VM, install your SSH key if you haven't already:
66+
67+
```bash
68+
export IP=""
69+
ssh-copy-id root@IP
70+
```
71+
72+
### Install K3s with k3sup
73+
74+
The easiest way to get k3sup is via arkade:
75+
76+
```bash
77+
curl -sLS https://dl.get-arkade.dev | sh
78+
sudo mv arkade /usr/local/bin/
79+
```
80+
81+
Followed by:
82+
83+
```bash
84+
arkade get k3sup
85+
```
86+
87+
Then use [k3sup](https://k3sup.dev/) to install K3s:
88+
89+
```bash
90+
export IP=""
91+
k3sup install \
92+
--ip $IP \
93+
--user root \
94+
--k3s-channel latest
95+
```
96+
97+
Once installed you'll have a KUBECONFIG file you can use:
98+
99+
```
100+
export KUBECONFIG=`pwd`/kubeconfig
101+
```
102+
103+
Alternatively, merge the file into your Kubernetes context for permanent use:
104+
105+
```bash
106+
export IP=""
107+
k3sup install \
108+
--ip $IP \
109+
--user root \
110+
--k3s-channel latest \
111+
--merge \
112+
--local-file $HOME/.kube/config \
113+
--context k3s-kim
114+
```
115+
116+
Then download `kubectx` and set your context:
117+
118+
```bash
119+
arkade get kubectx
120+
kubectx k3s-kim
121+
```
122+
123+
### Install OpenFaaS
124+
125+
Install the OpenFaaS CLI:
126+
127+
```bash
128+
arkade get faas-cli
129+
```
130+
131+
Now install OpenFaaS and set the image pull policy for functions to "IfNotPresent", otherwise the approach kim takes will not work.
132+
133+
```bash
134+
arkade install openfaas \
135+
--pull-policy IfNotPresent
136+
```
137+
138+
Run the commands given to you to start port-forwarding the OpenFaaS gateway and to log in. If you forget the commands just type `arkade info openfaas` to get them back again.
139+
140+
### Deploy kim
141+
142+
kim has a server and client component. The server (also known as an agent) needs to be installed.
143+
144+
Download the client from arkade:
145+
146+
```bash
147+
arkade get kim
148+
```
149+
150+
```bash
151+
kim builder install
152+
```
153+
154+
Run `kubectl get pods -n kube-image -w` and you should see the pod created for the build agent.
155+
156+
### Alias docker to kim
157+
158+
The `kim` CLI is similar to `docker`, so the easiest thing we can do to continue using the workflow we know with `faas-cli` is to make kim point at docker.
159+
160+
```bash
161+
sudo mv /usr/local/bin/docker{,2}
162+
sudo ln -s /usr/local/bin/kim /usr/local/bin/docker
163+
164+
docker --version
165+
kim version v0.1.0-alpha.12 (ac0a8eb3d8801e0e8808b1d6d5303b70c2b3beb0)
166+
```
167+
168+
In a future version of [faas-cli](https://github.com/openfaas/faas-cli), we may add a flag to `faas-cli build` such as `--kim` to switch the command from `docker` to this new tool. For the time being, this is a temporary workaround.
169+
170+
Later on, you can restore your Docker CLI with:
171+
172+
```bash
173+
sudo mv /usr/local/bin/docker{2,}
174+
```
175+
176+
### Create a function and deploy it.
177+
178+
```bash
179+
# Fetch the template
180+
faas-cli template store pull node12
181+
182+
# Create a new function
183+
faas-cli new --lang node12 jsbot
184+
185+
# cat ./jsbot/handler.js
186+
```
187+
188+
You'll see the handler as follows:
189+
190+
```js
191+
'use strict'
192+
193+
module.exports = async (event, context) => {
194+
const result = {
195+
'body': JSON.stringify(event.body),
196+
'content-type': event.headers["content-type"]
197+
}
198+
199+
return context
200+
.status(200)
201+
.succeed(result)
202+
}
203+
```
204+
205+
Now change the code as follows:
206+
207+
```js
208+
'use strict'
209+
210+
module.exports = async (event, context) => {
211+
return context.status(200).succeed("OK")
212+
}
213+
```
214+
215+
Run:
216+
217+
```bash
218+
faas-cli up -f jsbot.yml --skip-push
219+
```
220+
221+
The `--skip-push` option is required to prevent the image being transferred to a registry and back down again. That's what we're trying to avoid.
222+
223+
Invoke the endpoint:
224+
225+
```bash
226+
curl -i http://127.0.0.1:8080/function/jsbot
227+
OK
228+
```
229+
230+
Update the code and run the command again:
231+
232+
```js
233+
'use strict'
234+
235+
module.exports = async (event, context) => {
236+
return context.status(200).succeed("That was more than just OK!")
237+
}
238+
```
239+
240+
Build an image with kim and deploy it:
241+
242+
```bash
243+
faas-cli up -f jsbot.yml --skip-push
244+
```
245+
246+
Invoke the endpoint:
247+
248+
```bash
249+
curl -i http://127.0.0.1:8080/function/jsbot
250+
That was more than just OK!
251+
```
252+
253+
## Wrapping up
254+
255+
In a very short period of time we were able to increase the speed of building openfaas functions for local development. This works with every template and language that you may want to try, and doesn't need a lot of extra steps or for you to learn new concepts.
256+
257+
The kim project is still nascent, and likely to change and improve over time.
258+
259+
These statements are true at time of writing and may change:
260+
261+
* kim doesn't work "out of the box" with K3d and [requires additional configuration](https://twitter.com/dweomer/status/1392207985380757505?s=20)
262+
* I haven't been able to confirm whether it works with Apple M1 yet. The new [Rancher Desktop tool](https://github.com/rancher-sandbox/rd) that ships with kim built-in does not work with Apple M1
263+
* The `--squash` flag is not yet available in kim
264+
* kim also doesn't work for multi-node setups because it was designed to only accelerate local development
265+
266+
> It should also be noted that kim will not work with [faasd](https://github.com/openfaas/faasd), however if there is enough demand, we could look at creating a similar tool for the community.
267+
268+
Why don't you try it out next time you find yourself building, pushing and pulling down images for OpenFaaS or another application?
269+
270+
> If you've decided that kim is not for you, why don't you try enabling buildkit instead? It's a faster way to build functions and works with both OpenFaaS on Kubernetes and K3d. Just prefix your `faas-cli up` command with `DOCKER_BUILDKIT=1 `, or set it as an environment variable.
271+
272+
You can watch [me](https://twitter.com/alexellisuk) and [Matt Farina](https://twitter.com/mattfarina) from Rancher/SUSE exploring kim live:
273+
274+
{% include youtube.html id="l9j45bvv7Rk?t=63" %}
275+
276+
> Disclaimer: Rancher is a client of OpenFaaS Ltd, however neither this post, K3sup, or the livestream were sponsored or compensated. Linode is a sponsor of OpenFaaS.com.
277+
278+
### Already using OpenFaaS?
279+
280+
Join GitHub Sponsors for 25 USD / mo for access to discounts, offers, and updates on OpenFaaS going back to mid-2019. By taking this small step, you are enabling me to continue to work on OpenFaaS and the other tools we have spoken about today.
281+
282+
Join now: [OpenFaaS GitHub Sponsors](https://github.com/sponsors/openfaas/)
158 KB
Loading

0 commit comments

Comments
 (0)