共用方式為


OpenFaaS on ACS (Kubernetes)

I try the OpenFaaS which is a serverless framework for Docker & Kubernetes. I tried Kubernetes first. On the next post, I'll try Swarm as well.

Deploy ACS (kubernetes) using the acs engine

If you play with OpenFaas on Azure, I recommend to use acs-engine. The OpenFaaS support RBAC(Rsource Based Access Control). However, the ACS(Kubernetes) from Portal or Azure CLI doesn't support it. (It might be coming soon).

The Azure Container Service Engine (acs-engine) generates ARM (Azure Resource Manager) templates for Docker enabled clusters on Microsoft Azure with your choice of DC/OS, Kubernetes, Swarm Mode, or Swarm orchestrators. Generally speaking, If you want to use the latest version of Kubernetes, please try acs-engine. If you are interested, please see the release note.

download the acs-engine binary

You can download the acs-engine binary from here. This time, I try 0.6.0. Please download the suitable binary. I use Mac as my client PC.

Write kubernetes.json

acs-engine generate ARM template and deploy to the azure. Fill the Kubernetes.json on the repo. I recommend you to fill the LinuxProfile > ssh > Public Keys section at least.  Paste your Public Key (e.g. id_rsa.pub) On this section.

Deploy to the Azure

Using ACS-engine you can deploy the Kubernetes Cluster.  You need to specify the kubernetes.json on the --api-model. You can include --dns-prefix parameter in the kubernetes.json.

 $ acs-engine deploy --subscription-id 51ac25de-afdg-9201-d923-8d8e8e8e8e8e  --dns-prefix contoso-apple --location westus2  --auto-suffix --api-model examples/kubernetes.json

For the detail, you can refer Deploy a Kubernetes Cluster.

Configure Kubernetes

Download the kubectl and set the Path to the binary.

 curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/darwin/amd64/kubectl

Also, you can download config file of the kubectl from the master node and set KUBECONFIG environment environment. You can find the config file on the directory below. sync-17-59c03dc9 will be changed according to your enviornment. You can find a lot of config file according to the region which you use.

 _output/sync-17-59c03dc9/kubeconfig

Deploy OpenFaaS on the Kubernetes. Clone the repo and apply

Just deploy it.

 $ git clone https://github.com/alexellis/faas-nete
$ cd faas-netes
$ kubectl apply -f ./faas.yml,monitoring.yml,rbac.yml

These command will create Gateway/UI and Prometeus

 alertmanager    10.0.228.222   <nodes>         9093:31113/TCP   17h
 faas-netesd     10.0.218.157   <nodes>         8080:31111/TCP   17h
 gateway         10.0.77.126    <nodes>         8080:31112/TCP   17h
 kubernetes      10.0.0.1       <none>          443/TCP          17h
 nodeinfo        10.0.131.19    <none>          8080/TCP         12h
 prometheus      10.0.118.89    <nodes>         9090:31119/TCP   17h

You need to access the gateway. You can expose the gateway to the internet or SSH tunnel it.

For the exposure

 kubectl expose deployment gateway --type=LoadBalancer --name=gatewayexternal

For the tunnel

In case of your master node name is sync-17-59-c03dc9.westus.cloudapp.azure.com, And you want to tunnel to the local 9002 port.

 ssh -f azureuser@sync-17-59c03dc9.westus.cloudapp.azure.com -L 9002:10.0.77.126:8080 -N 

Also you can specify -i ./azureuser_rsa flag for specifying the private key of yours. When I tried, I forgot to specify the public key. Then acs-engine autogenerate the private/public key. If you forget to specify the publicy key, you will find the autogenerated private key at 

 _output/sync-17-59c03dc9/azureuser_rsa

Now you are ready to try Open FaaS

OpenFaaS Sample

Install Open FaaS CLI

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

According to the Your first serverless Python function with OpenFaaS, Let's create the first function by python.

 $ mkdir -p ~/functions/hello-python
$ cd ~/functions

hello-python/handler.py

 def handle(req):  
    print("Hello! You said: " + req)

 

Also you need create stack.yml for the config. You need to change the gateway address according to your enviornment. You need to change image section according to the docker hub repo. This is my case.

 provider:  
  name: faas
  gateway: https://localhost:9002

functions:  
  hello-python:
    lang: python
    handler: ./hello-python/
    image: tsuyoshiushio/faas-hello-python

Then build/push/deploy it. It requires docker hub or docker private repo (e.g. Azure Container Registry)

 $ faas-cli -action build -f ./stack.yml
$ faas-cli -action push -f ./stack.yml
$ faas-cli -action deploy -f ./stack.yml

Now you can try

 curl localhost:8080/function/hello-python -d "it's Tsuyoshi here"
Hello! You said: its Tsuyoshi here

In case of you need to include library, you need create a file. for the detail, please refer Your first serverless Python function with OpenFaaS

Enjoy coding.

Resource