The declarative programming model of Kubernetes is markedly different from what most developers are used to. That the API is a set of resources rather than a list of methods on objects is a bit mind bending. But this programming model is not entirely new – rather, it smacks quite heavily of functional programming.
Functional programming had mostly been relegated to academic endeavors until recently. What’s changed that is that our apps are now distributed systems and are simply too complex for us to reason about without help. Kubernetes helps.
In order to effectively use Kubernetes to deploy and manage your workloads you need to understand some of the principles of functional programming and how they surface in K8s. In this session I will cover these underlying principles of the K8s programming model so that you can up the robustness and manageability of your application deployments.
Presented at KubeCon Barcelona, May 2019
1. You Might Just be a
Functional Programmer Now
Cornelia Davis
VP of Technology
Pivotal
@cdavisafc
Kubecon Barcelona
May 2019
2. Me?
❖ Cal State Northridge: Computer Science
❖ Indiana University: Computer Science
(ABD)
❖ Pivotal: Product Strategy (PCF, PCC, PKS)
❖ And now…
Ops
as much as
Dev
now
Discount code 40% off!: ctwkubeconeur19
4. [(0, 5), (5, 17), (17, 20), (20, 33)]
Merge short Segments together
[(0, 17), (17, 33)]
5. def mergeSegments(s_list, threshold):
list_len = len(s_list)
i = 0
while i < list_len:
seg_length = s_list[i][1]-s_list[i][0]
if seg_length < threshold:
s_list[i+1] =
(s_list[i][0], s_list[i+1][1])
del s_list[i]
list_len -= 1
else:
i += 1
6. 0 5 5 17 17 20 20 33
i
0 1 2 3
4
List Length
0
0 17 17 20 20 33
17
i
3
0 17 17 20 20 33
i
3
0 17 17 33
i
2
7. Hallmarks of Imperative Programs
❖ Sequential - we control every step in the process.
❖ Variables … with side effects
❖ Hairy edge cases
❖ Difficult to parallelize
@cdavisafc
10. Hallmarks of Functional Programs
❖ Declarative
❖ NO side effects
❖ Recursive
❖ Easier to program in a way that allows for parallelization
❖ Far fewer edge cases
❖ Provably correct
@cdavisafc
11. Gene Kim
3000 Lines of Objective C
To
1500 Lines of Typescript & React
To
500 Lines of Clojure
18. Scripted Deployments Declarative Deployments
ssh Immutable Infrastructure
Long Running Context (Ephemeral) Containers
Middleware Sidecars
We cannot reason about every detail so we need
a model
that allows machines to reason for us.
Imperative Functional
Systems Programming
20. My app:
- image: posts
type: pod
replicas: 3
- image: connections
type: pod
replicas: 5
- image: aggregator
type: pod
replicas: 2
Compliance:
- image: audit
type: daemonset
P
P
P
CC
C
C
C
A
A
Audit
Audit Audit
Audit
21. My app:
- image: posts
type: pod
replicas: 3
- image: connections
type: pod
replicas: 5
- image: aggregator
type: pod
replicas: 2
Compliance:
- image: audit
type: daemonset
P
P
P
CC
C
C
A
Audit
Audit Audit
C
A
AuditA
C
Control Loop
22. My app:
- image: posts
type: pod
replicas: 3
- image: connections
type: pod
replicas: 5
- image: aggregator
type: pod
replicas: 2
Compliance:
- image: audit
type: daemonset
P
P
P
CC
C
C
A
Audit
Audit Audit
A
C
Independent Control Loops!!!
ReplicaSet Controller
DaemonSet Controller
23. (define sumToN
(lambda (n)
(if (= n 1)
1
(+ n (sumToN (- n 1)))
)))
A bit like Functional Programming
Base Case
General Case
(Bit of a leap of faith)
45. OS Image
Runtime Layer
Application Layer
HOST
Host OS
(Kernel)
OS Image
Runtime Layer
Application Layer
HOST
Host OS
(Kernel)
You can
Repave
the entire
environment
VERY Often!!!
Like several
times per week
“often”!