kubernetes patterns...one shot action before pod starts needs to be idempotent has own resource...

65
" m " for menu, "? " for other shortcuts KUBERNETES PATTERNS Devoxx Morocco, 2018-11-28, Marrakesh Roland Huß, Red Hat, @ro14nd

Upload: others

Post on 20-May-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

"m " fo r menu , "? " fo r o the r sho r tcu ts

KUBERNETESPATTERNS

DevoxxMorocco, 2018-11-28,Marrakesh

RolandHuß,RedHat, @ro14nd

Page 2: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

AGENDA

KubernetesPatternsCategories:

FoundationalPatternsStructuralPatternsConfigurationalPatternsAdvancedPatterns

Page 3: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container
Page 4: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

KUBERNETESOpenSourcecontainerorchestrationsystem

SchedulingSelf-healingHorizontalscalingServicediscoveryRolloutandRollbacks

Declarativeresource-centricRESTAPI

Page 5: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

DesignPatterns

M i c h a e l M a n d i b e rg , CC BY - SA 2 . 0 , h t t p s : / / fl i c . k r /p / 67Cb6 Jm

Page 6: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

DESIGNPATTERN

ADesignPatterndescribesarepeatablesolutiontoasoftwareengineeringproblem.

Page 7: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container
Page 8: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

https://leanpub.com/k8spatterns

Page 9: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

STRUCTUREProblemPatterns:

NameSolution

http://www.martinfowler.com/articles/writingPatterns.html

Page 10: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

FOUNDATIONALPATTERNS

Page 11: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

AutomatableUnit

HowcanwecreateandmanageapplicationswithKubernetes?

Pods:AtomicunitofcontainersServices:EntrypointtopodsGroupingviaLabels,Annotations,Namespaces

Page 12: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

rhuss/log-sidecar:2.3

10.1.29.2 name:pong

version:1

rhuss/pong:1

POD

KubernetesAtomOneormorecontainerssharing:

IPandportsVolumes

EphemeralIPaddress

Page 13: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

PODDECLARATION apiVersion: v1 kind: Pod metadata: name: pong labels: name: pong version: "1" spec: containers: - image: "rhuss/pong:1" name: pong ports: - containerPort: 8080 - image: "rhuss/log-sidecar:2.3" name: log

Page 14: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

REPLICASETResponsibleformanagingPodsreplicas:NumberofPodcopiestokeepLabelselectorchoosesPodsHoldsatemplateforcreatingnewPods

Page 15: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

10.1.29.3

name:pong

version:1

10.1.29.4

name:pong

version:1

name:pong

version:1Selector:

ReplicaSet

10.1.29.2

name:pong

version:1

replicas: 3

Page 16: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

SERVICEEntrypointforasetofPodsPodschosenbyLabelselectorPermanentIPaddress

10.1.29.3

name:pong

version:1

10.1.29.4

name:pong

version:1

name:pongSelector:

10.1.29.2

name:pong

version:1

10.200.100.251

Page 17: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

CronJob

DaemonSet ReplicaSet StatefulSet JobReplication

Controller

Pod

ServiceHorizontalPod

Autoscaler

Container

(yourcode)

PodDisruption

BudgetIngress

Volume

ConfigMapPersistent

VolumeClaimSecret

Deployment

Page 18: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

PredictableDemands

Howcanwehandleresourcerequirementsdeterministically?

Requirementsshouldbedeclaredtohelpin:

MatchinginfrastructureservicesSchedulingdecisionsCapacityplanning

Page 19: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

RUNTIMEDEPENDENCIES

PersistentVolumesHostportsConfigurationviaConfigMapsandSecrets

Page 20: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

RESOURCEPROFILESResources:

CPU,Network(compressible)Memory(incompressible)

App:DeclarationofresourcerequestsandlimitsPlatform:Resourcequotasandlimitranges

Page 21: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

apiVersion: v1kind: Podmetadata: name: http-serverspec: containers: - image: nginx name: nginx resources: requests: cpu: 200m memory: 100Mi limits: cpu: 300m memory: 200Mi

Page 22: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

QOSCLASSESBestEffort

Norequestsorlimits

Burstablerequests<limits

Guaranteedrequests==limits

Page 23: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

DeclarativeDeployment

Howcanapplicationsbedeployedandupdated?

DeclarativeversusImperativedeploymentVariousupdatestrategies

Page 24: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

DEPLOYMENTHoldstemplateforPodCreatesReplicaSetontheflyAllowsrollbackUpdatestrategiesdeclarableInspiredbyDeploymentConfigfromOpenShift

Page 25: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

ROLLING

v1.0 v1.0 v1.0

Service

v1.1 v1.1

Page 26: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

FIXED

v1.0 v1.0 v1.0

Service

v1.1 v1.1 v1.1

Page 27: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

CANARY

v1.0 v1.0 v1.0

Service

v1.1

Page 28: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

BLUE-GREEN

v1.0 v1.0 v1.0

Service

v1.1 v1.1 v1.1

Page 29: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

SUMMARY

time

instances

time

instances

time

instances

time

instances

RollingDeployment RecreateDeployment

Blue-GreenRelease CanaryRelease

0…1capacity

2xcapacity

Page 30: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

STRUCTURALPATTERNS

Page 31: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

Initializer

HowcanIinitializemycontainerizedapplications?

Initcontainer:PartofaPodOneshotactionbeforePodstartsNeedstobeidempotentHasownresourcerequirements

Page 32: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

Pod

app containers

init containers

Container Container Container

Container Container

Page 33: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

Sidecar

HowcanIextendthefunctionalityofanexistingcontainer?

RuntimecollaborationofcontainersConnectedviasharedresources:

NetworkVolumes

Page 34: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

Pod

Sidecar

git

Main Container

node.js

Disk

Page 35: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

Ambassador

Howtodecoupleacontainer'saccesstotheoutsideworld?

AlsoknownasProxySpecializationofaSidecarE.g.infrastructureservices

CircuitbreakerTracing

Page 36: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

Pod

localhost

Container

memcached

Container

python

Page 37: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

Adapter

Howtodecoupleaccesstoacontainerfromtheoutsideworld?

OppositeofAmbassadorUniformaccesstoapplicationExamples:

MonitoringLogging

Page 38: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

Pod

Sidecar

monitoring

Main Container

java

Disk

Page 39: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

CONFIGURATIONALPATTERNS

Page 40: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

Howcanapplicationsbeconfiguredfordifferentenvironments?

Page 41: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

EnvVarConfigurationUniversalapplicableRecommendedbytheTwelveFactorAppmanifestoCanbeonlysetduringstartupofapplication

Page 42: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

kind: Podspec: containers: - env: - name: DB_HOST value: "prod-database.prod.intranet" - name: DB_PASSWORD valueFrom: secretKeyRef: name: "db-passwords" key: "monogdb.password" - name: DB_USER valueFrom: configMapKeyRef: name: "db-users" key: "mongodb.user" image: acme/bookmark-service:1.0.4

Page 43: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

ConfigurationResource

ConfigMapandSecret:IntrinisicK8sresourcesCanbeusedintwoways:

ReferenceforenvironmentvariablesFilesmappedtoavolume

Page 44: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

kind: ConfigMapmetadata: name: spring-boot-configdata: JAVA_OPTIONS: "-Xmx512m" application.properties: | welcome.message=Hello !!! server.port=8080

kind: Podspec: containers: - name: web volumeMounts: - name: config-volume mountPath: /etc/config # ... volumes: - name: config-volume configMap: name: spring-boot-config

Page 45: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

ConfigurationTemplate

ConfigMapnotsuitableforlargeconfigurationManagingsimilarconfigurationIngredients:

Init-containerwithtemplateprocessorandtemplatesParametersfromaConfigMapVolume

Page 46: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

CONFIGURATIONTEMPLATE

Pod

app container

init-container

Template Processor

Application

emptyDir volume

config-map volume

Configuration Files

Template ParametersConfiguration

Templates

Page 47: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

DISCUSSIONGoodforlarge,similarconfigurationsetsperenvironmentParameterisationviaConfigMapseasyMorecomplex

Page 48: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

ImmutableConfiguration

ConfigurationisputintoacontaineritselfConfigurationcontainerislinkedtoapplicationcontainerduringruntime

Page 49: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

CONTAINERVOLUMES

app container

Application

config container

Configuration Files

/config /configVolumemount

NotdirectlysupportedbyK8sdocker-flexvol:K8sFlexVolumedriverforDockervolumes

Page 50: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

kind: Podmetadata: name: nginxspec: containers: - name: nginx image: nginx volumeMounts: - name: test mountPath: /data ports: - containerPort: 80 volumes: - name: test flexVolume: driver: "dims.io/docker-flexvol" options: image: "my-container-image" name: "/data-store"

Page 51: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

INITIALIZER

Pod

app container init-container

Configuration ImageApplication

emptyDir volume

Configuration Files

/var/config /config

mount&copymount&use

Page 52: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

Dockerfileforinitcontainer:

Buildconfigimage:

FROM busybox

ADD dev.properties /config-src/demo.properties# ... add more to /config-src

# Using a shell here in order to resolve wildcardsENTRYPOINT [ "sh", "-c", \ "cp /config-src/* $1", "--" ]

docker build -t k8spatterns/config-dev:1 .

Page 53: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

spec: initContainers: - image: k8spatterns/config-dev:1 name: init args: - "/config" volumeMounts: - mountPath: "/config" name: config-directory containers: - image: k8spatterns/demo:1 name: demo volumeMounts: - mountPath: "/config" name: config-directory volumes: - name: config-directory emptyDir: {}

Page 54: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

DISCUSSIONImmutableConfiguration...

canbeversionedcanbedistributedviaaregistryisimmutablecanbearbitrarylarge

ParameterisationviaOpenShiftTemplates

Page 55: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

ADVANCEDPATTERNS

Page 56: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

Controller

HowcanIextendtheplatformitselfwithoutchangingit?

WatchingresourcesbyregisteringforKuberneteseventsReactingonchangesinresourcedeclarations

Page 57: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

CONTROLLERManagedpodlisteningforKubernetesAPIeventsStateReconciliation:Makethecurrentstatelikethedeclareddesiredstate

Page 58: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

CATEGORIESExtensionController:ExtendtheKubernetesplatformitselfApplicationController:CombineKuberneteswithanapplicationspecificdomain

Page 59: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

Operator

HowcanIintroducedomainspecificresourcesandreactonthem?

CustomResourceDefinition(CRD)managedbyKubernetesAccessibleviatheKubernetesAPIWatchedbyControllers

Page 60: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

EXAMPLECRDapiVersion: apiextensions.k8s.io/v1beta1kind: CustomResourceDefinitionmetadata: name: prometheuses.monitoring.coreos.comspec: group: monitoring.coreos.com names: kind: Prometheus plural: prometheuses scope: Namespaced version: v1 validation: ....

Page 61: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

EXAMPLECRDapiVersion: monitoring.coreos.com/v1kind: Prometheusmetadata: name: prometheusspec: serviceMonitorSelector: matchLabels: team: frontend resources: requests: memory: 400Mi

Page 62: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

OPERATORFRAMEWORK

OperatorSDKScaffoldingforaGolangOperatorProjectHighlevelabstractionforobservingeventsMarshallingofcustomresources

OperatorLifecycleManagerOperatorMetering

Page 63: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

SPECTRUMOperator with CRD

ExtensionController ApplicationController

Expose Controller

ConfigMap Controller

etcd Operator

Prometheus Operator

EAI Operator

Page 64: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

QUESTIONS?

Twitter ro14nd

Book https://leanpub.com/k8spatterns

Slides https://github.com/ro14nd-talks/kubernetes-patterns

Page 65: KUBERNETES PATTERNS...One shot action before Pod starts Needs to be idempotent Has own resource requirements Pod app containers init containers Container Container Container Container

https://leanpub.com/k8spatterns