99 lines
3.2 KiB
Markdown
99 lines
3.2 KiB
Markdown
---
|
|
title: OpenFaaS and Kubernetes
|
|
date: 2017-08-27 18:30:00
|
|
---
|
|
|
|
Recently there has been a lot of news about [OpenFaaS](https://github.com/alexellis/faas)
|
|
and I wanted to try it out. As the rest of my tests have been done on
|
|
Kubernetes, I looked more closely to the [FaaS-Netes](https://github.com/alexellis/faas-netes).
|
|
|
|
Everything here was done on a local minikube deployment and using my test
|
|
Docker registry deployed on a server in the local network.
|
|
|
|
## Deployment
|
|
The first deployment was really easy. Once minikube is operational, it's
|
|
only two commands to deploy everything needed :
|
|
`git clone https://github.com/alexellis/faas-netes`
|
|
and then `kubectl apply -f ./faas.yml,monitoring.yml,rbac.yml`.
|
|
|
|
You can check on the status of everything either through the Kubernetes
|
|
dashboard (using `minikube dashboard` if you don't know the IP) or using
|
|
the OpenFaaS dashboard (through the port *31112*).
|
|
|
|
The second part is to get the OpenFaaS-cli tools that allows easy function
|
|
deployment. As I'm not a huge fan of the `curl -sSL ... | sudo sh` install
|
|
method, I downloaded manually the executable from the [release page](https://github.com/alexellis/faas-cli/releases)
|
|
and added it the PATH.
|
|
|
|
## Use of a private Docker registry
|
|
Before this, I always deployed my stuff by hand of the Kubernetes cluster
|
|
and thus had no problems with the authentication using the *imagePullSecrets*
|
|
in the yaml deployment file. This time as OpenFaaS deploys containers as needed,
|
|
I had to configure the nodes to authenticate automatically. Fortunately,
|
|
it was this easy :
|
|
|
|
* Create the secret
|
|
|
|
``` bash
|
|
$ kubectl create secret docker-registry myregistrykey \
|
|
--docker-server=registry.local \
|
|
--docker-username=user \
|
|
--docker-password=pass \
|
|
--docker-email none@registry.local
|
|
```
|
|
|
|
* Modify the service account to add `imagePullSecrets` to every image pull
|
|
|
|
``` bash
|
|
$ kubectl patch serviceaccount default \
|
|
-p '{"imagePullSecrets": [{"name": "myregistrykey"}]}'
|
|
```
|
|
|
|
* It works.
|
|
|
|
## First test
|
|
|
|
I the followed the [Your first serverless Python function with OpenFaaS](https://blog.alexellis.io/first-faas-python-function/)
|
|
blog post by the creator of the system. First creating a new directory
|
|
for this function and adding the basic content needed for an Hello World
|
|
|
|
``` bash
|
|
$ mkdir -p ~/functions/hello-python
|
|
$ cd ~/functions
|
|
$ cat > hello-python/handler.py <<EOF
|
|
def handle(req):
|
|
print("Hello! You said: " + req)
|
|
EOF
|
|
$ cat > stack.yml <<EOF
|
|
provider:
|
|
name: faas
|
|
gateway: http://192.168.42.99:31112
|
|
|
|
functions:
|
|
hello-python:
|
|
lang: python
|
|
handler: ./hello-python/
|
|
image: registry.local/faas-hello-python:1.0
|
|
EOF
|
|
```
|
|
|
|
This makes the basic structure for a function. All in all, it's really
|
|
easy: One file contains the function that will be called, the other
|
|
contains the description of the deployment.
|
|
|
|
The to deploy our function, three commands will do everything needed
|
|
|
|
|
|
``` bash
|
|
$ faas-cli -action build -f ./stack.yml
|
|
$ faas-cli -action push -f ./stack.yml
|
|
$ faas-cli -action deploy -f ./stack.yml
|
|
```
|
|
|
|
And bam, everything is deployed and ready to recieve requests
|
|
|
|
```
|
|
curl http://192.168.42.99:31112/function/hello-python -d test
|
|
```
|
|
|
|
I think that I will play for a long time with this now ..
|