77
Resilient Microservices with Kubernetes Mete Atamel Developer Advocate for Google Cloud @meteatamel

Mete Atamel "Resilient microservices with kubernetes"

Embed Size (px)

Citation preview

Page 1: Mete Atamel "Resilient microservices with kubernetes"

Resilient Microservices with KubernetesMete AtamelDeveloper Advocate for Google Cloud

@meteatamel

Page 2: Mete Atamel "Resilient microservices with kubernetes"

Confidential & ProprietaryGoogle Cloud Platform 2

Mete AtamelDeveloper Advocate for Google Cloud

@meteatamel

[email protected]

meteatamel.wordpress.com

@meteatamel

Page 3: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Agenda

IntroductionThe Monolith and Microservices

ContainersWhat are containers? How do they help?

KubernetesWhat is Kubernetes? How does it help with management of containers?

Kubernetes building blocksDeployments, pods, labels, selectors, services, replica sets and more

Page 4: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

The Monolith

@meteatamel

Page 5: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

What is the Monolith? @meteatamel

APP SERVER

Module Module Module

DB

Page 6: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Problems with the Monolith

Unnecessary tight coupling among modules

All at once update policy, ignores different development velocities

Hard to scale different parts independently

Hard to establish ownership of the whole system

Hard to debug and test in general, hard to run on a single development machine

@meteatamel

Page 7: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

From The Monolith to Microservices

@meteatamel

Page 8: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

What is a Microservice?

Microservice

DB

@meteatamel

Microservice

DB

Microservice

DB

Page 9: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

New problems

Need to worry about multiple independent systems instead of one

Debugging and testing across multiple services can be hard without proper instrumentation

“But it works on my machine!” problem still applies

Common maintenance problems still apply: Redundancy, resilience, upgrades/downgrades, scaling up/down

@meteatamel

Page 10: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Demo: Simple Microservice

@meteatamel

Page 11: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Containers

@meteatamel

Page 12: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

What is a container?

LightweightHermetically sealedIsolated

Easily deployableIntrospectableComposable

Linux (or Windows) processes

DockerA lightweight way to virtualize applications

@meteatamel

Page 13: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

✕ No isolation✕ Common libs✕ Highly coupled Apps

& OS

Why containers?

app

libs

kernel

libs

app app

kernel

app

libs

libs

kernel

kernel

libs

app

kernel

libs

app

libs

app

libs

app

✓ Isolation✓ No Common Libs✕ Expensive and

Inefficient✕ Hard to manage

✓ Isolation✓ No Common Libs✓ Less overhead✕ Less Dependency on

Host OS

kernellibs

app

app app

app

Shared Machines VMs/Bare Metal Containers

@meteatamel

Page 14: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Google has been developing and using containers to manage our applications for over 12 years.

Images by Connie Zhou

Page 15: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Everything at Google runs in containers

Gmail, Web Search, Maps, ...MapReduce, batch, ...GFS, Colossus, ...Even Google’s Cloud Platform: our VMs run in containers!

We launch over 2 billion containers per week

Page 16: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Demo: Containerised Microservice

@meteatamel

Page 17: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Containers not enough @meteatamel

Containers help to create a lightweight and consistent environment for apps

But it does not solve common app management problems

● Resiliency● Scale up and down● Deploy a new version of your app reliably● Rollback a deployment● Health checks● Graceful shutdown● Etc.

Page 18: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Kubernetes

@meteatamel

Page 19: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Greek for “Helmsman”; also the root of the words “governor” and “cybernetic”

• Manages container clusters

• Inspired and informed by Google’s experiences and internal systems (borg)

• Supports multiple cloud and bare-metal environments

• Supports multiple container runtimes

• 100% Open source, written in Go

Manage applications, not machines

Kubernetes @meteatamel

Page 20: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

kubelet

UI

kubelet CLI

API

users master nodes

etcd

kubelet

scheduler

controllers

apiserver

The 10000 foot view @meteatamel

Page 21: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

UI

APIContainer

Cluster

All you really care about @meteatamel

Page 22: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

1. Setting up the cluster• Choose a cloud: GCE, AWS, Azure, Rackspace, on-premises, ...• Choose a node OS: CoreOS, Atomic, RHEL, Debian, CentOS, Ubuntu, ...• Provision machines: Boot VMs, install and run kube components, ...• Configure networking: IP ranges for Pods, Services, SDN, ...• Start cluster services: DNS, logging, monitoring, ...• Manage nodes: kernel upgrades, OS updates, hardware failures...

Not the easy or fun part, but unavoidable

This is where things like Google Container Engine (GKE) really help

Container clusters: A story in two parts @meteatamel

Page 23: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Kubernetes cluster on GKE @meteatamel

Page 24: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Demo: Create Kubernetes cluster

@meteatamel

Page 25: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

2. Using the cluster• Run Pods & Containers• Replica Sets• Services• Volumes

This is the fun part!

A distinct set of problems from cluster setup and management

Don’t make developers deal with cluster administration!

Accelerate development by focusing on the applications, not the cluster

Container clusters: A story in two parts @meteatamel

Page 26: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Kubernetes Building Blocks

@meteatamel

Page 27: Mete Atamel "Resilient microservices with kubernetes"

Container cluster

Service

Pods

Each pod containers one or more containers

Nodes

Role: frontend

Role: frontend Role: frontend Role: frontend

Replication controllerReplicas: 3

Env: prod

microservice

labels

Service communication channel

Blueprint“pod template”

Env: prod Env: prod Env: prod registry

containers

@meteatamel

Page 28: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Deployments

Page 29: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

A Deployment provides declarative updates for Pods and Replica Sets

Describe the desired state and the Deployment controller will change the actual state to the desired state at a controlled rate for you.

Deployment manages replica changes for you• stable object name• updates are configurable, done server-side• kubectl edit or kubectl apply ...

Deployments @meteatamel

Page 30: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Demo: Create Deployment

@meteatamel

Page 31: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Pods and Volumes

Page 32: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Small group of containers & volumes

Tightly coupled

The atom of scheduling & placement

Shared namespace• share IP address & localhost• share IPC, etc.

Managed lifecycle• bound to a node, restart in place• can die, cannot be reborn with same ID

Example: data puller & web server

ConsumersContent Manager

File Puller

Web Server

Volume

Pod

Pods @meteatamel

Page 33: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Pod-scoped storage

Support many types of volume plugins• Empty dir (and tmpfs)• Host path• Git repository• GCE Persistent Disk• AWS Elastic Block Store• Azure File Storage• iSCSI• Flocker• NFS

• vSphere• GlusterFS• Ceph File and RBD• Cinder• FibreChannel• Secret, ConfigMap,

DownwardAPI• Flex (exec a binary)• ...

Volumes @meteatamel

Page 34: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Labels & Selectors

Page 35: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Arbitrary metadata

Attached to any API object

Generally represent identity

Queryable by selectors• think SQL ‘select ... where ...’

The only grouping mechanism• pods under a ReplicationController• pods in a Service• capabilities of a node (constraints)

Labels @meteatamel

Page 36: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

App: MyApp

Phase: prod

Role: FE

App: MyApp

Phase: test

Role: FE

App: MyApp

Phase: prod

Role: BE

App: MyApp

Phase: test

Role: BE

Selectors @meteatamel

Page 37: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

App: MyApp

Phase: prod

Role: FE

App: MyApp

Phase: test

Role: FE

App: MyApp

Phase: prod

Role: BE

App: MyApp

Phase: test

Role: BE

App = MyApp

Selectors @meteatamel

Page 38: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

App: MyApp

Phase: prod

Role: FE

App: MyApp

Phase: test

Role: FE

App: MyApp

Phase: prod

Role: BE

App: MyApp

Phase: test

Role: BE

App = MyApp, Role = FE

Selectors @meteatamel

Page 39: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

App: MyApp

Phase: prod

Role: FE

App: MyApp

Phase: test

Role: FE

App: MyApp

Phase: prod

Role: BE

App: MyApp

Phase: test

Role: BE

App = MyApp, Role = BE

Selectors @meteatamel

Page 40: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

App: MyApp

Phase: prod

Role: FE

App: MyApp

Phase: test

Role: FE

App: MyApp

Phase: prod

Role: BE

App: MyApp

Phase: test

Role: BE

App = MyApp, Phase = prod

Selectors @meteatamel

Page 41: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

App: MyApp

Phase: prod

Role: FE

App: MyApp

Phase: test

Role: FE

App: MyApp

Phase: prod

Role: BE

App: MyApp

Phase: test

Role: BE

App = MyApp, Phase = test

Selectors @meteatamel

Page 42: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Replication

Page 43: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

A simple control loop

Runs out-of-process wrt API server

One job: ensure N copies of a pod• grouped by a selector• too few? start some• too many? kill some

Layered on top of the public Pod API

Replicated pods are fungible• No implied order or identity

* The evolution of ReplicationControllers

ReplicaSet- name = “my-rc”- selector = {“App”: “MyApp”}- template = { ... }- replicas = 4

API Server

How many?

3

Start 1 more

OK

How many?

4

ReplicaSets* @meteatamel

Page 44: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

ReplicaSets

Replication Controller Pod

frontend

Pod

frontendapp = demo app = demo app = demo

ReplicaSet

#pods = 3app = democolor in (blue,grey)

show: version = v2

color = blue color = blue color = grey

Behavior Benefits

● Keeps Pods running● Gives direct control of Pod #s● Grouped by Label Selector

➔ Recreates Pods, maintains desired state➔ Fine-grained control for scaling ➔ Standard grouping semantics

Pod Pod Pod

Page 45: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Demo: Replication

@meteatamel

Page 46: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Services

Page 47: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Services

Client

Pod

Container

Pod

Container

Pod

Container

A logical grouping of pods that perform the same function (the Service’s endpoints)

• grouped by label selector

Load balances incoming requests across constituent pods

Choice of pod is random but supports session affinity (ClientIP)

Gets a stable virtual IP and port• also a DNS nametype =

Service

Label selector: type = FE

VIP

type = FE type = FE type = FE

@meteatamel

Page 48: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Demo: Services

@meteatamel

Page 49: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Scaling

Page 50: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Scaling @meteatamel

Service

Label selectors: version = 1.0 type = Frontend

Servicename = frontend

Label selector: type = BE

Replication Controller Pod

frontend

Pod

version= v1 version = v1

ReplicaSet

version = v1#pods = 1

show: version = v2 type = FE type = FE

Pod

frontend

Pod

version = v1type = FE

ReplicaSet

version = v1#pods = 2

show: version = v2

Pod

ReplicaSetversion = v1type = FE#pods = 3

show: version = v2

Page 51: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Scaling @meteatamel

Service

Label selectors: version = 1.0 type = Frontend

Servicename = frontend

Label selector: type = BE

Replication Controller Pod

frontend

Pod

version= v1 version = v1

ReplicaSet

version = v1#pods = 1

show: version = v2 type = FE type = FE

Pod

frontend

Pod

version = v1type = FE

ReplicaSet

version = v1#pods = 2

show: version = v2

Pod Pod

ReplicaSetversion = v1type = FE#pods = 4

show: version = v2

version = v1type = FE

Page 52: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Demo: Scaling

@meteatamel

Page 53: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Rolling Update

Page 54: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

ReplicaSet- name: my-app-v1- replicas: 3- selector:

- app: MyApp- version: v1

Service- app: MyApp

Rolling Update @meteatamel

Page 55: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

ReplicaSet- name: my-app-v1- replicas: 3- selector:

- app: MyApp- version: v1

ReplicaSet- name: my-app-v2- replicas: 0- selector:

- app: MyApp- version: v2

Service- app: MyApp

Rolling Update @meteatamel

Page 56: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

ReplicaSet- name: my-app-v1- replicas: 3- selector:

- app: MyApp- version: v1

ReplicaSet- name: my-app-v2- replicas: 1- selector:

- app: MyApp- version: v2

Service- app: MyApp

Rolling Update @meteatamel

Page 57: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

ReplicaSet- name: my-app-v1- replicas: 2- selector:

- app: MyApp- version: v1

ReplicaSet- name: my-app-v2- replicas: 1- selector:

- app: MyApp- version: v2

Service- app: MyApp

Rolling Update @meteatamel

Page 58: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

ReplicaSet- name: my-app-v1- replicas: 2- selector:

- app: MyApp- version: v1

ReplicaSet- name: my-app-v2- replicas: 2- selector:

- app: MyApp- version: v2

Service- app: MyApp

Rolling Update @meteatamel

Page 59: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

ReplicaSet- name: my-app-v1- replicas: 1- selector:

- app: MyApp- version: v1

ReplicaSet- name: my-app-v2- replicas: 2- selector:

- app: MyApp- version: v2

Service- app: MyApp

Rolling Update @meteatamel

Page 60: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

ReplicaSet- name: my-app-v1- replicas: 1- selector:

- app: MyApp- version: v1

ReplicaSet- name: my-app-v2- replicas: 3- selector:

- app: MyApp- version: v2

Service- app: MyApp

Rolling Update @meteatamel

Page 61: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

ReplicaSet- name: my-app-v1- replicas: 0- selector:

- app: MyApp- version: v1

ReplicaSet- name: my-app-v2- replicas: 3- selector:

- app: MyApp- version: v2

Service- app: MyApp

Rolling Update @meteatamel

Page 62: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

ReplicaSet- name: my-app-v2- replicas: 3- selector:

- app: MyApp- version: v2

Service- app: MyApp

Rolling Update @meteatamel

Page 63: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Canary Deployments

Replication ControllerReplicaSetversion = v2type = BE#pods = 1

show: version = v2

Pod

frontend

Pod

version = v2type = BE

@meteatamel

Pod

frontend

Service

Label selectors: version = 1.0 type = Frontend

Servicename = backend

Label selector: type = BE

Replication Controller

Pod

version= v1

ReplicaSetversion = v1type = BE#pods = 2

show: version = v2 type = BE type = BE

Pod

version = v1

Page 64: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Autoscaling

Replication Controller Pod

frontend

Pod

name=locust name=locust

ReplicaSetname=locustrole=worker#pods = 1

show: version = v2

Pod

frontend

Pod

name=locust

ReplicaSetname=locustrole=worker#pods = 2

show: version = v2

Pod Pod

name=locust

ScaleCPU Target% = 50 Heapster

role=worker role=worker role=worker role=worker

ReplicaSetname=locustrole=worker#pods = 4

70% CPU 40% CPU

> 50% CPU< 50% CPU

@meteatamel

Page 65: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Demo: Rolling Update

@meteatamel

Page 66: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

DaemonSets

Page 67: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Problem: how to run a Pod on every node?• or a subset of nodes

Similar to ReplicaSet• principle: do one thing, don’t overload

“Which nodes?” is a selector

Use familiar tools and patterns

Pod

DaemonSets @meteatamel

Page 68: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Jobs

Page 69: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Run-to-completion, as opposed to run-forever• Express parallelism vs. required completions• Workflow: restart on failure• Build/test: don’t restart on failure

Aggregates success/failure counts

Built for batch and big-data work

...

Jobs @meteatamel

Page 70: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

StatefulSets

Page 71: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Goal: enable clustered software on Kubernetes• mysql, redis, zookeeper, ...

Clustered apps need “identity” and sequencing guarantees

• stable hostname, available in DNS• an ordinal index• stable storage: linked to the ordinal & hostname• discovery of peers for quorum• startup/teardown ordering

StatefulSets @meteatamel

Page 72: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

ConfigMaps

Page 73: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Goal: manage app configuration• ...without making overly-brittle container images

12-factor says config comes from the environment

• Kubernetes is the environment

Manage config via the Kubernetes API

Inject config as a virtual volume into your Pods• late-binding, live-updated (atomic)• also available as env vars

node

API

Pod ConfigMap

ConfigMaps @meteatamel

Page 74: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Secrets

Page 75: Mete Atamel "Resilient microservices with kubernetes"

Google Cloud Platform

Goal: grant a pod access to a secured something• don’t put secrets in the container image!

12-factor says config comes from the environment

• Kubernetes is the environment

Manage secrets via the Kubernetes API

Inject secrets as virtual volumes into your Pods• late-binding, tmpfs - never touches disk• also available as env vars

node

API

Pod Secret

Secrets @meteatamel

Page 76: Mete Atamel "Resilient microservices with kubernetes"

Confidential & ProprietaryGoogle Cloud Platform 76

There is more!

Page 77: Mete Atamel "Resilient microservices with kubernetes"

Confidential & ProprietaryGoogle Cloud Platform 77

kubernetes.iocloud.google.com/container-engine

Mete Atamel@meteatamel

[email protected]

Thank You