Loading deck

Building Serverless

Functions for AKS!

 

Sajeetharan Sinnathurai

AKS workshop,Colombo 2019 | www.sajeetharan.com

THE CHALLENGE

"Build Serveless functions for AKS with OpenFaas"

  1. Do sentiment analysis with OpenFass store function
  2. Build custom function to send a tweet

THE CHALLENGE!

and we need only 30 steps to go there!

Micro-service architecture?

Serverless !== No Server

Event Driven

Stateless

Microservices

Why serverless?

👍 Reduce time-to-market

👍 Easier deployment

👍 Scale automatically

👍 Focus on business logic

👍$$$ Reduction

When to use?

👍 Stateless

 

👍 Short Job

 

👍 Event-driven stuff, e.g. Time-based / webhook

 

👍 Simple application with less dependency

 

 

 

Kubernetes is a portable, extensible open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation

Packing Things Together

At Scale

Faas Model/Patterns

Short Lived

No ports

No states

Serves single purpose

 

 

Frameworks for serverless

OpenFaas Revolution is here

"Serverless Functions Made Simple"

  • Anything can be a function
  • Leverage existing skills in teams  
  • Avoid vendor lock-in
  • Run anywhere - cloud or on-prem
  • 13,000 stars more than anyone else

OpenFaas Architecture

What languages does it support?

Any

Let's fight together

PreRequisites

Azure Subscription

 Docker installed locally and a docker hub account

 Python installed locally

Access to guideliness

Let's Kick Off

Step 1: Launch Azure Cloud shell or Install Azure CLI

             https://shell.azure.com/bash

 

Step 2: Run the login command or login https://aka.ms/devicelogin 

            az login -u <username> -p <password>

Step 3: Create a resource group

 az group create --name aksColomboRG --location eastus

Step 4: Create AKS cluster

az aks create  --resource-group aksColomboRG  --name openFaasCluster --node-vm-size Standard_A2_v2   --node-count 1 --enable-addons monitoring --generate-ssh-keys

Step 6: Connect to the cluster

Step 7: List the cluster nodes

az aks get-credentials --resource-group aksColomboRG --name openFaasCluster --admin -a  --overwrite-existing
kubectl get all -n kube-system

Congratulations

Step 8: Install and Init Helm

Step 9: Install OpenFaas

Step 10: Create namespace OpenFaas

Step 11: Create second namespace for OpenFass Functions

Step 12 : Check you have a tiller pod in ready state

helm init --upgrade
git clone https://github.com/openfaas/faas-netes
kubectl create ns openfaaskubectl
kubectl create ns openfaas-fn
kubectl -n kube-system get po

Step 13: Manually start the tiller

Step 14: Resolve cannot list configmaps in the "kube-s"

kubectl create serviceaccount --namespace kube-system tiller
kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller
kubectl patch deploy --namespace kube-system tiller-deploy -p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}'      
helm init --service-account tiller --upgrade
kubectl logs --namespace kube-system tiller-deploy-66cdfd5bc9-46sxv

Step 16: A Helm chart for OpenFaaS is included in the cloned repository. Use this chart to deploy OpenFaaS into your AKS cluster.

helm upgrade --install --namespace openfaas --set functionNamespace=openfaas-fn --set async=true --set serviceType=LoadBalancer openfaas openfaas/openfaas

Step 17: See OpenFaas live

kubectl get all -n openfaas
helm repo add openfaas https://openfaas.github.io/faas-netes/

 You did it!

If Not

Deploy new Function from store 

Deploy Custom function

Step 2: Install faas-cli

Step 3: Login to docker hub

docker login

Step 1: Run Docker Desktop

To install the faas-cli on Windows go to Releases and download the latest faas-cli.exe.

set OPENFAAS_URL=http://23.101.130.111:8080

Step 4: set OpenFaas url

Step 6: List the functions

faas-cli list

Step 7: Remove the function

faas-cli rm -f AKSdemo.yml 
faas-cli up -f helloaks.yml

Step 6: Deploy the function

// Create Folder functions
mkdir functions

//Create new function named helloAKS
faas-cli new --lang=node --prefix=sajeetharan helloaks

Step 5: Create first function

Step 2: Get the twitter app for you

Step 3: Add the environment variables

provider:
  name: faas
  gateway: http://23.101.130.111:8080
functions:
  tweetme:
    lang: python
    handler: ./tweetme
    image: sajeetharan/tweetme:latest
    environment:
        consumer_key: "fdsr43eegegsdvsdfewfrewrewr"
        consumer_secret: "wefdsfsdtrewtgdsgds"
        access_token: "146725639-f"
        access_token_secret: "fsdfsfdffds"

Step 1: Create the function

faas-cli new --lang=python --prefix=sajeetharan tweetme

Let's create tweetme App

Step 6: Deploy the app

Step 4: Add dependency in the requirements.txt

tweepy

Let's create tweetme App

faas-cli up -f tweetme.yml

Step 5: handler.py

import os
import tweepy

def get_api(cfg):
    auth = tweepy.OAuthHandler(cfg['consumer_key'], cfg['consumer_secret'])
    auth.set_access_token(cfg['access_token'], cfg['access_token_secret'])
    return tweepy.API(auth)

def handle(tweet):
    cfg = { 
        "consumer_key"        : os.environ['consumer_key'],
        "consumer_secret"     : os.environ['consumer_secret'],
        "access_token"        : os.environ['access_token'],
        "access_token_secret" : os.environ['access_token_secret'] 
      }
    
    api = get_api(cfg)
    
    status = api.update_status(status=tweet)
    print('Tweet sent.')
    print(status)

Wake up! I'M done

Where to go from here?

Explore by yourself and learn.