Serverless On Kubernetes Gorilla Guide
Serverless On Kubernetes Gorilla Guide
The
Gorilla
Guide to... ®
Express Edition
Serverless
on Kubernetes
Joep Piscaer
I N S I D E T H E G U I D E:
• How Serverless is Changing the Game
• Getting the Most out of Kubernetes
• Fission and Kubernetes Offer Freedom of Choice
Compliments of
Take a quick walk
through the IT jungle!
SERVERLESS ON KUBERNETES 1
THE GORILLA GUIDE TO...
Serverless on Kubernetes
Express Edition
AUTHOR
Joep Piscaer
All rights reserved. This book or any portion thereof may not be
reproduced or used in any manner whatsoever without the express
written permission of the publisher except for the use of brief
quotations in a book review.
Printed in the United States of America.
ACT UA LT E C H M E D I A
Okatie Village Ste 103-157
Bluffton, SC 29909
www.actualtechmedia.com
TABLE OF CONTENTS
Chapter 1: The Serverless Revolution 5
Serverless Greases the DevOps Wheel 9
Serverless’s Big Benefits 11
Chapter 2: The Serverless Landscape 16
The Players 16
AWS Lambda 17
Microsoft Azure Functions 19
Google Cloud Functions 20
The Alternatives 21
Introducing Fission 22
Functions 24
Environment 25
Trigger 25
Technical Overview 26
Chapter 3: Serverless in the Cloud and
On-Premises 29
and Jelly
Kubernetes and Serverless: Like Peanut Butter
Avoiding Lock-In
29
30
and Fission
Freedom of Choice with Kubernetes
Monitoring and Metrics 36
Balancing Cost and Performance 37
Chapter 4: Serverless in the Real World 39
Example 1: Banking Site 39
Example 2: Carpool 41
Example 3: Internet of Things 42
The Unstoppable Force 42
CHAPTER 1
Functions-as-a-Service (FaaS) is a
technology for serverless computation.
In a FaaS system, the unit of execution is
a function of code written in a programming
language (most FaaS systems support a wide
range of languages). A developer specifies
one or more functions and the conditions
(events) under which those functions shall
execute. Since it is serverless, the FaaS
system automatically provisions resources
to host and execute the functions when
the specified conditions are met, and later
tears them down when no longer needed.
Since FaaS is the most widely used form
of serverless, this guide will use those two
terms interchangeably.
SERVERLESS ON KUBERNETES 6
P ROV I D E R S D O M O RE, TENANTS DO LESS
VM VM VM
1
https://ifttt.com/
SERVERLESS ON KUBERNETES 7
A Clean Slate
A key characteristic of serverless
is its statelessness. Functions are
invoked from a clean state every
time; any persistent state required
for the function needs to be external-
ly stored. This is similar to the twelve-factor app con-
cept1 for building Software-as-a-Service (SaaS) apps.
Generally, functions will use a database, and a distrib-
uted cache or object store to store state across requests.
1
https://12factor.net/
SERVERLESS ON KUBERNETES 8
Serverless Greases the DevOps
Wheel
Although at first glance serverless may seem counter-
intuitive for those in DevOps-culture organizations
where development teams do their own operations,
it really isn’t. Serverless decouples the bits that make
up the runtime (language-specific environments,
containers, operating systems, VMs, physical hardware,
networks, storage, and so on) and the tooling in the
developer’s pipeline (to build, test, and deploy code)
from the actual code put in production, minimizing the
Waste Not
Looking at this from the lean
software development perspec-
tive1, we see that most steps
in the developer pipeline are
‘waste.’ Waste, in this context, re-
fers to technically necessary steps, like
compiling or packaging, that provide no value to the
customer. Even if those steps increase the quality of the
code, like writing unit tests or deployment specifica-
tions, they provide little actual customer value.
1
https://en.wikipedia.org/wiki/Lean_software_development
SERVERLESS ON KUBERNETES 9
This means that it’s important to reduce variation
and other waste in the pipeline, as this leads to better
customer value, delivered more quickly.
SERVERLESS ON KUBERNETES 10
While cost benefits are often cited as the major reason for
using FaaS, it’s reduction in lead time that may be the
most exciting improvement. Instead of spending time on
inefficiencies, product development teams can now focus
more time on continuous experimentation, which will
lead to more innovation and greater market advantages.
SERVERLESS ON KUBERNETES 11
In contrast to typical container-based
microservices approaches, the container
images – which are still operating systems
and file systems at heart – aren’t part of
the developer pipeline and lifecycle. They
are instead part of the stack the operations
team controls and manages; this is a more
natural fit, as they have necessary skills
to manage the non-functional aspects
of the infrastructure like security and
performance.
SERVERLESS ON KUBERNETES 12
Flexible Pricing
The industry pricing model for
serverless function execution is the
same across the board for major
cloud providers. Pricing varies with
the amount of memory you allocate to
your function. This is a pricing overview
across Amazon, Microsoft and Google from October 2018:
SERVERLESS ON KUBERNETES 13
a developer needs additional package dependencies, like
libraries, in the execution environment. In a container-
based microservices approach, this would have been
the developer’s problem; in the serverless approach,
it’s part of the solution, abstracted away from the
developer. The containers that execute the functions
are short-lived, automatically created and destroyed by
the FaaS platform based on runtime need.
SERVERLESS ON KUBERNETES 14
Time isn’t the only thing that’s saved, either. Because
of the fine-grained level of execution, FaaS services
are metered and billed per millisecond of runtime or
per number of requests (i.e., triggers). Idle functions
aren’t billed.
This also means that you’re not stuck with the amor-
tization of investments or long-term contracts, but
free to change consumption monthly or even daily.
This allows teams to change direction or try some-
thing new on an extremely small scale, with similarly
small operational costs associated with experimen-
tation. This fosters a culture of trying new things,
taking small steps, and learning from mistakes early,
which are basic tenets of any agile organization.
SERVERLESS ON KUBERNETES 15
CHAPTER 2
Framework
Chalice
AWS SAM
HOS T ED
INS T ALLABLE
AWS Lambda
Amazon launched Lambda in 2014, which was the first
commercially available serverless platform. It’s part of
the Amazon Web Services (AWS) cloud computing port-
SERVERLESS ON KUBERNETES 17
folio, and is tightly integrated in that ecosystem. It runs
in the AWS cloud, with no option to run on-premises
and only limited options for running locally on a devel-
oper’s machine. Future offerings may include the ability
to run Lambda functions closer to the edge. There’s also
a serverless database option, called Aurora Serverless.
• Database changes
• File and object storage changes
• Messages in a publish/subscribe queue
• Scheduling
• Authentication
• HTTP requests (via an API Gateway)
SERVERLESS ON KUBERNETES 18
seconds of an event, but there are limits to the total
duration of a function; it’s currently capped at fifteen
minutes. Although Lambda functions are elastic and
scale automatically, they’re also limited to 1,000 con-
current executions by default in a given region, per ac-
count. This limit can be easily reached, especially when
combining production and testing.
SERVERLESS ON KUBERNETES 19
event. As any developer will recognize, being able to
breakpoint a remotely running function is very useful.
SERVERLESS ON KUBERNETES 20
The Alternatives
Besides the “big three,” there are other serverless
options, broadly divided into three categories:
SERVERLESS ON KUBERNETES 21
run on top of container platforms like Docker and
Kubernetes. Examples of these include OpenFaaS,
serverless.com, and Fission.
Introducing Fission
Fission is an open source, Kubernetes-native serverless
functions framework with support for public, private,
and hybrid clouds. Support for Kubernetes enables
Smooth Starting
Getting started with Fission is
easy, needing just a couple of
Helm or kubectl commands to
deploy it on a laptop, in an on-
premises Kubernetes cluster or in
a cloud service. The installation steps
can be found on the project website.1
1
https://docs.fission.io/latest/installation/
SERVERLESS ON KUBERNETES 22
FUNCT ION
TRIGGE R
ENVIR ONMENT
Sync HTTP, NATS, Kafka, Azure
Storage Queues, Kubernetes
Watches, Timers, ... NodeJS, Python, Go, Ruby,
C#, PHP, Bash (!!!), Perl
1. Functions
2. Triggers
3. Environments
SERVERLESS ON KUBERNETES 23
Functions
A function is something that Fission executes. It’s
the code a developer has written; for instance, a
piece of business logic. It adheres to certain technical
characteristics commonly found in event-driven
programming.
SERVERLESS ON KUBERNETES 24
Environments
Environments are the language-specific parts of
Fission. An environment contains just enough software
to build and run the function. It consists of a container
with the language runtime, a web server, and fission-
specific parts that allow functions to load dynamically.
Trigger
Functions are invoked on the occurrence of an event; a
trigger is what configures Fission to use that event to
invoke a function. In other words, a trigger is a binding
of events to function invocations.
2. Time (cron)
SERVERLESS ON KUBERNETES 25
For HTTP requests, the fission router handles the map-
ping of triggers to functions, keeps track of which actual
containers run a given function, and forwards requests
(and sends responses back) to and from functions.
Technical Overview
You’ve seen the functional concepts of Fission; now
let’s look under the hood. Fission is made up of a set of
microservices running on Kubernetes.
HTTP Requests
Fission
CLI
Controller Router
poolmgr ...
“Generic” “Specific”
pods Function pods
SERVERLESS ON KUBERNETES 26
Fission provides CLI tools for generating these
specification files, validating them, and applying
them to a Fission installation. Note that running apply
more than once is equivalent to running it once: if the
desired state as defined in the configuration is reached,
it won’t change.
Fission Services
Fission consists of various
services, each running in
containers (see Figure 4):
SERVERLESS ON KUBERNETES 27
• The Fission CLI. This is the user interface, used to
interact with the fission system. It uses a declarative,
file-based approach for Fission objects, like functions
and environments. This way, you can track the
Fission specifications along with the source code in
the version control system. This allows Fission to be
integrated seamlessly into the developer workflow
and existing (CI/CD) pipelines.
SERVERLESS ON KUBERNETES 28
CHAPTER 3
Avoiding Lock-In
Most current serverless offerings are services: they’re
part of a portfolio of technologies in one of the major
clouds, and run as part of that ecosystem. These
commit customers to the cloud provider’s ecosystem,
forcing them to use cloud-specific services. This means
that functions in one cloud aren’t portable to another
cloud provider, requiring refactoring when moving a
function to a different provider.
SERVERLESS ON KUBERNETES 30
Lock-in like this happens in subtle ways;
for instance, being forced to use the
packaged (and often monetized) monitoring
solution. This almost defeats the purpose
of Kubernetes, which is a freely available
technology that works across clouds, in on-
premises environments and locally. Fission
prevents this lock-in and dependency, and
promotes decoupling, re-use of code, and
portability, all of which reduce friction during
the lifetime of the function. This allows
developers to modernize applications, even
when using on-premises infrastructure.
SERVERLESS ON KUBERNETES 31
investments in private data centers end up with
spare server capacity, like CPU and memory; that’s
just the way physical hardware is bought. Fission can
run on this idle and already paid-for server capacity,
effectively giving you a free FaaS platform. Free
serverless is a major advantage of the pay-per-use
model of public cloud providers, which can become
expensive in a hurry.
SERVERLESS ON KUBERNETES 32
eliminates the cloud vendors’ notoriously expensive
ingress and egress fees.
SERVERLESS ON KUBERNETES 33
services and middleware (databases, message queues,
key/value stores), web servers, and more. This is why
Fission also integrates with open source projects like
Prometheus, which we’ll talk about a little later.
• Node.js
• Python
• Go
• Ruby
• Java
• C# / .NET
• Binary (for executables or scripts)
• Perl
• PHP
• And more
SERVERLESS ON KUBERNETES 34
Deployment and Operations
Fission supports declarative deployments using build
specs. These specs describe Fission resources like
functions and triggers and allow developers to deploy
functions anywhere. This helps manage the complexity
of deployments across different environments, making
sure that a function is deployed across environments
consistently.
SERVERLESS ON KUBERNETES 35
experiencing issues, Fission will re-route users to older,
more stable code so the issue can be resolved.
SERVERLESS ON KUBERNETES 36
Fission is also integrated with Prometheus, the de facto
standard metrics system. Fission automatically tracks
the number of requests (function call count), timing
(execution time and overhead) and success/failure rate
metrics, response size, and error codes for all functions.
These are fed into Prometheus automatically, without
adding any code to the functions. In addition, it adds
contextual information (cold vs. hot starts) to these
metrics to allow better interpretation.
For instance, how do you make sure the cost for idle
functions is small, while keeping latency low for
often-used functions?
SERVERLESS ON KUBERNETES 37
This is part of a larger issue: That the
actual cost of public cloud FaaS services
depend heavily on the usage pattern of
functions. In some cases, running the same
code in containers or even VMs can be much
cheaper than running them as serverless
functions. Also, cost across clouds vary.
In many pay-per-use models, which is
popular with the public cloud, there is a real
danger of costs getting out of hand, even
when compared to containers or VMs.
SERVERLESS ON KUBERNETES 38
CHAPTER 4
Serverless in the
Real World
In this last part of the guide, we’ll show some practical
examples of serverless. The first one is a common
banking application using web and API technologies. It
uses a database running on Kubernetes, CockroachDB,
with various functions in Fission interacting directly.
Database
A JAX AJ AX AJ AX
Browser
2
https://github.com/fission/fission-bank-sample
SERVERLESS ON KUBERNETES 40
CarPool
Validator
CarPool
Validator
CarPool
Validator
CarPool
Validator
Example 2: Carpool
The carpool application is a great example of combining
multiple functions into a single workflow, parallelizing
certain functions to optimize the flow.
3
https://github.com/fission/fission-workflow-sample
SERVERLESS ON KUBERNETES 41
Monorepo – Spring (ƒ) Web
Boot functions Dashboard
jQuery, Chart.js,
(ƒ) Kafka (ƒ) Distribution Python & HTML
Producer
(ƒ) Speed Data
As with the others, you can run this sample use case on
any Fission environment.4
4
https://github.com/fission/fission-kafka-sample
SERVERLESS ON KUBERNETES 42
worry about, increasing their velocity, simplifying the
pipeline, and shortening the feedback loops.
SERVERLESS ON KUBERNETES 43