Top Banner
How to Code, Deploy, and Operate Cloud-Native Apps Using Kubernetes Aditya Satrya Head of IT Development Jabar Digital Service https://digitalservice.jabarprov.go.id
47

How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

May 22, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

How to Code, Deploy, and Operate Cloud-Native Apps Using Kubernetes

Aditya SatryaHead of IT Development Jabar Digital Servicehttps://digitalservice.jabarprov.go.id

Page 2: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Outline

● Cloud-Native & 12-Factor App● Kubernetes● 12-Factor App using Kubernetes

Page 3: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Cloud-Native & 12-Factor App

Page 4: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Cloud-Native Application

● Operability: Expose control of application/system lifecycle.

● Observability: Provide meaningful signals for observing state, health, and performance.

● Elasticity: Grow and shrink to fit in available resources and to meet fluctuating demand.

● Resilience: Fast automatic recovery from failures.● Agility: Fast deployment, iteration, and reconfiguration

Page 5: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Cloud-Native Trail Map1. Containerization2. CI/CD3. Orchestration

--below this are optional--4. Observability5. Service Discovery6. Networking & Policy7. Distributed database & storage8. Streaming & messaging9. Container registry

10. Software distribution

Page 6: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

12-Factor App

● Methodology to build app optimized for the cloud (cloud-native)

● Drafted by developers at Heroku (2011)

● http://12factor.net

Page 7: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

TheTwelveFactor

Page 8: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

CodeI. One CodebaseOne codebase tracked in revision control, many deploys

II. DependenciesExplicitly declare and isolate dependencies

III. ConfigStore config in the environment

VI. ProcessesExecute the app as one or more stateless processes

IV. Backing servicesTreat backing services as attached resources

V. Build, release, runStrictly separate build and run stagesX. Dev/prod parityKeep development, staging, and production as similar as possibleVII. Port bindingExport services via port binding

VIII. ConcurrencyScale out via the process model

IX. DisposabilityMaximize robustness with fast startup and graceful shutdown

XI. LogsTreat logs as event streams

XII. Admin processesRun admin tasks as one-off processes

Deploy Operate

Page 9: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Kubernetes

Page 10: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

What is Kubernetes?

● Open-source system for automating: ○ deployment○ scaling○ management of containerized

applications

Page 11: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control
Page 12: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Worker 1 Worker 2 Worker 3 Worker 4 Worker 5

Master 1 Master 2 Master 3

Workers

Page 13: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Worker 1 Worker 2 Worker 3 Worker 4 Worker 5

Master 1 Master 2 Master 3

Workers

Page 14: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Worker 1 Worker 2 Worker 3 Worker 4 Worker 5

Master 1 Master 2 Master 3

Workers

Run 5 instances of frontend:latest

Page 15: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Worker 1 Worker 2 Worker 3 Worker 4 Worker 5

Master 1 Master 2 Master 3

Workers

Run 5 instances of frontend:latest

Page 16: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Worker 1 Worker 2 Worker 3 Worker 4 Worker 5

Master 1 Master 2 Master 3

Workers

Run 5 instances of frontend:latest

frontend: 4/5

X

Page 17: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Worker 1 Worker 2 Worker 3 Worker 4 Worker 5

Master 1 Master 2 Master 3

Workers

Run 5 instances of frontend:latest

frontend: 4/5

Page 18: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Worker 1 Worker 2 Worker 3 Worker 4 Worker 5

Master 1 Master 2 Master 3

Workers

Run 5 instances of frontend:latest

frontend: 5/5

Page 19: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Worker 1 Worker 2 Worker 3 Worker 4 Worker 5

Master 1 Master 2 Master 3

Workers

Run 5 instances of frontend:latest

Run 3 instances of backend:latest

Page 20: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Worker 1 Worker 2 Worker 3 Worker 4 Worker 5

Master 1 Master 2 Master 3

Workers

Run 5 instances of frontend:latest

Run 3 instances of backend:latest

Run 10 instances of analytics:latest

Page 21: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Worker 1 Worker 2 Worker 3 Worker 4 Worker 5

Master 1 Master 2 Master 3

Workers

Run 5 instances of frontend:latest

Run 3 instances of backend:latest

Run 10 instances of analytics:latest

Page 22: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Worker 1 Worker 2 Worker 3 Worker 4 Worker 5

Master 1 Master 2 Master 3

Workers

Run 5 instances of frontend:latest

Run 3 instances of backend:latest

Run 10 instances of analytics:latest

analytics: 7/10frontend: 4/5backend: 3/3

Page 23: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Worker 1 Worker 2 Worker 3 Worker 4 Worker 5

Master 1 Master 2 Master 3

Workers

Run 5 instances of frontend:latest

Run 3 instances of backend:latest

Run 10 instances of analytics:latest

analytics: 10/10frontend: 5/5backend: 3/3

Page 24: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Worker 1 Worker 2 Worker 3 Worker 4 Worker 5

Page 25: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Worker 1 Worker 2 Worker 3 Worker 4 Worker 5

10.x.x.xlabel: analyticsServices

Page 26: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Worker 1 Worker 2 Worker 3 Worker 4 Worker 5

10.x.x.xlabel: analytics

10.x.x.xlabel: backend

10.x.x.xlabel: frontendServices

Page 27: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Worker 1 Worker 2 Worker 3 Worker 4 Worker 5

10.x.x.xlabel: analytics

10.x.x.xlabel: backend

10.x.x.xlabel: frontendServices

Internet

Page 28: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Worker 1 Worker 2 Worker 3 Worker 4 Worker 5

10.x.x.xlabel: analytics

10.x.x.xlabel: backend

10.x.x.xlabel: frontend

Cloud Load Balancer

Services

Internet

Page 29: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Worker 1 Worker 2 Worker 3 Worker 4 Worker 5

10.x.x.xlabel: analytics

10.x.x.xlabel: backend

10.x.x.xlabel: frontend

Cloud Load Balancer

stats.example.com api.example.com example.com

Services

Internet

Page 30: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

12-Factor App Using Kubernetes

Page 31: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

CodeI. One CodebaseOne codebase tracked in revision control, many deploys

II. DependenciesExplicitly declare and isolate dependencies

III. ConfigStore config in the environment

VI. ProcessesExecute the app as one or more stateless processes

IV. Backing servicesTreat backing services as attached resources

V. Build, release, runStrictly separate build and run stagesX. Dev/prod parityKeep development, staging, and production as similar as possibleVII. Port bindingExport services via port binding

VIII. ConcurrencyScale out via the process model

IX. DisposabilityMaximize robustness with fast startup and graceful shutdown

XI. LogsTreat logs as event streams

XII. Admin processesRun admin tasks as one-off processes

Deploy Operate

Page 32: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

CodeI. One CodebaseOne codebase tracked in revision control, many deploys

II. DependenciesExplicitly declare and isolate dependencies

III. ConfigStore config in the environment

VI. ProcessesExecute the app as one or more stateless processes

Dockerfile

Kubernetes cluster

Node

Pod

Node

Podyaml

Pod Pod

Page 33: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

CodeI. One CodebaseOne codebase tracked in revision control, many deploys

II. DependenciesExplicitly declare and isolate dependencies

III. ConfigStore config in the environment

VI. ProcessesExecute the app as one or more stateless processes

Kubernetes cluster<staging>

yaml Kubernetes cluster<test>

Kubernetes cluster<prod>

Page 34: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

CodeI. One CodebaseOne codebase tracked in revision control, many deploys

II. DependenciesExplicitly declare and isolate dependencies

III. ConfigStore config in the environment

VI. ProcessesExecute the app as one or more stateless processes

Page 35: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

CodeI. One CodebaseOne codebase tracked in revision control, many deploys

II. DependenciesExplicitly declare and isolate dependencies

III. ConfigStore config in the environment

VI. ProcessesExecute the app as one or more stateless processes

fmt.Fprintf(w, "ENV: %s\n", os.Getenv("ENV"))

fmt.Fprintf(w, "DB_HOST: %s\n", os.Getenv("DB_HOST"))

fmt.Fprintf(w, "DB_PORT: %s\n", os.Getenv("DB_PORT"))

fmt.Fprintf(w, "DB_USER: %s\n", os.Getenv("DB_USER"))

fmt.Fprintf(w, "DB_PASSWORD: %s \n", os.Getenv("DB_PASSWORD"))

Application code:

containers:

- name: demo-app

image: asatrya/alpine-k8s-pod-lb-demo

env:

- name: DB_HOST

valueFrom:

configMapKeyRef:

name: demo-configmap

key: DB_HOST

- name: DB_PORT

valueFrom:

configMapKeyRef:

name: demo-configmap

key: DB_PORT

k8s yaml:

Page 36: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

CodeI. One CodebaseOne codebase tracked in revision control, many deploys

II. DependenciesExplicitly declare and isolate dependencies

III. ConfigStore config in the environment

VI. ProcessesExecute the app as one or more stateless processes

● Share nothing● Do not write persistent data to

node memory/filesystem

Page 37: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

IV. Backing servicesTreat backing services as attached resources

V. Build, release, runStrictly separate build and run stagesX. Dev/prod parityKeep development, staging, and production as similar as possibleVII. Port bindingExport services via port binding

Deploy

configMap.yaml

DB_HOST=mydbhostDB_PORT=3306

Pod

deployment.yaml

image: imagenameenv: ….

secret.yaml

DB_USER=mydbuserDB_PASS=mydbpass

Docker Registry

image

os.Getenv(‘DB_HOST’)os.Getenv(‘DB_PORT’)os.Getenv(‘DB_USER’)os.Getenv(‘DB_PASS’)

Page 38: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

IV. Backing servicesTreat backing services as attached resources

V. Build, release, runStrictly separate build and run stagesX. Dev/prod parityKeep development, staging, and production as similar as possibleVII. Port bindingExport services via port binding

Deploy

Build image

Registry

Deploy & Run<staging>

Deploy & Run<prod>

push image (release)

pull imagepull image

Config for staging

Config for prod

Page 39: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

IV. Backing servicesTreat backing services as attached resources

V. Build, release, runStrictly separate build and run stagesX. Dev/prod parityKeep development, staging, and production as similar as possibleVII. Port bindingExport services via port binding

Deploy

Page 40: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

IV. Backing servicesTreat backing services as attached resources

V. Build, release, runStrictly separate build and run stagesX. Dev/prod parityKeep development, staging, and production as similar as possibleVII. Port bindingExport services via port binding

Deploy

Node 1NodePort

PodTargetPort

Load Balancer

Service

Port

PodTargetPort

Service

Port

Node 2NodePort

PodTargetPort

Service

Port

PodTargetPort

Service

Port

Page 41: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

VIII. ConcurrencyScale out via the process model

IX. DisposabilityMaximize robustness with fast startup and graceful shutdown

XI. LogsTreat logs as event streams

XII. Admin processesRun admin tasks as one-off processes

Operate● Manual Scaling

○ kubectl scale● Autoscaling

○ based on CPU utilization○ based on custom metrics

Page 42: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

VIII. ConcurrencyScale out via the process model

IX. DisposabilityMaximize robustness with fast startup and graceful shutdown

XI. LogsTreat logs as event streams

XII. Admin processesRun admin tasks as one-off processes

OperateService

Healthy Unhealthy(Loading 45%)

Liveness PASSReadiness PASS

Liveness PASSReadiness FAIL

Page 43: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

VIII. ConcurrencyScale out via the process model

IX. DisposabilityMaximize robustness with fast startup and graceful shutdown

XI. LogsTreat logs as event streams

XII. Admin processesRun admin tasks as one-off processes

Operate

Page 44: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

VIII. ConcurrencyScale out via the process model

IX. DisposabilityMaximize robustness with fast startup and graceful shutdown

XI. LogsTreat logs as event streams

XII. Admin processesRun admin tasks as one-off processes

Operate● CronJob● Job

Page 45: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

DemoYou can access demo source code athttps://github.com/asatrya/k8s-12-factor-demo

Note: Read README first.

Page 46: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Summary

● Code: optimize for automation● Deploy: portability● Operate: scalability, resiliency

Page 47: How to Code, Deploy, and Operate Cloud-Native Apps Using … · 12-Factor App using Kubernetes. Cloud-Native & 12-Factor App. Cloud-Native Application Operability: Expose control

Thank you!