70
Matthias Lübken Container Patterns

Patterns in a Containerized World (Matthias Luebken Technology Stream)

Embed Size (px)

Citation preview

Page 1: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Matthias Lübken

Container Patterns

Page 2: Patterns in a Containerized World (Matthias Luebken Technology Stream)

@luebken

Page 3: Patterns in a Containerized World (Matthias Luebken Technology Stream)

github.com/openshift/origin

Page 4: Patterns in a Containerized World (Matthias Luebken Technology Stream)
Page 5: Patterns in a Containerized World (Matthias Luebken Technology Stream)
Page 6: Patterns in a Containerized World (Matthias Luebken Technology Stream)
Page 7: Patterns in a Containerized World (Matthias Luebken Technology Stream)

projectatomic.io

Page 8: Patterns in a Containerized World (Matthias Luebken Technology Stream)

github.com/opencontainers

Page 9: Patterns in a Containerized World (Matthias Luebken Technology Stream)

“ Easily create lightweight, portable, self-sufficient containers

from any application.”

Page 10: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Since containers are a great way of package and deliver software …

Page 11: Patterns in a Containerized World (Matthias Luebken Technology Stream)

What if we use containers to modularise our application?

Page 12: Patterns in a Containerized World (Matthias Luebken Technology Stream)

“Containers […] are destined to become

analogous to objects in object-oriented software

systems”

Page 13: Patterns in a Containerized World (Matthias Luebken Technology Stream)

“This makes a container not just a program, but rather, a part of a program.”

“A Container Is A

Function Call”.

Page 14: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Independently

releasableSeparate processing types

Different loadsDifferent teams

Reuse

Crash isolation

Use different languages / versions / libraries

POCs

Page 15: Patterns in a Containerized World (Matthias Luebken Technology Stream)

1. Properties of a module container

Outline

2. Patterns for assembling these

Page 16: Patterns in a Containerized World (Matthias Luebken Technology Stream)
Page 17: Patterns in a Containerized World (Matthias Luebken Technology Stream)

github.com/luebken/container-patterns

Page 18: Patterns in a Containerized World (Matthias Luebken Technology Stream)

“Module-Container”

Page 19: Patterns in a Containerized World (Matthias Luebken Technology Stream)

A Module Container is

1. Linux process

2. API

3. Descriptive

4. Disposable

5. Immutable

6. Self-contained

7. Small

8. Adaptive

Page 20: Patterns in a Containerized World (Matthias Luebken Technology Stream)

1. Linux Process

Page 21: Patterns in a Containerized World (Matthias Luebken Technology Stream)

1. Linux Process• React to signals

• Return exit codes

• Use standard streams

• Handle arguments

Examples: module-container.md#1-linux-process

Page 22: Patterns in a Containerized World (Matthias Luebken Technology Stream)

2. API

Page 23: Patterns in a Containerized World (Matthias Luebken Technology Stream)

2. API• ENV variables

• Available ports

• Volume mounts

• Lifecycle hooks

Examples: module-container.md#2-api

Page 24: Patterns in a Containerized World (Matthias Luebken Technology Stream)

3. Descriptive

Page 25: Patterns in a Containerized World (Matthias Luebken Technology Stream)

3. Descriptive• Use standard labels (e.g. proposal generic labels)

• url, summary, vcs-url …

• Use custom labels:

• io.openshift.wants • io.openshift.expose-services

Examples: module-container.md#3-descriptive

Page 26: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Standardising Labels

• https://github.com/projectatomic/ContainerApplicationGenericLabels

• http://label-schema.org/rc1/

Page 27: Patterns in a Containerized World (Matthias Luebken Technology Stream)

4. Disposable

Page 28: Patterns in a Containerized World (Matthias Luebken Technology Stream)

4. Disposable

• Don’t rely on a particular instance

• Be aware of shots at your cattle

• Be robust against sudden deathExamples: module-container.md#4-disposable

Page 29: Patterns in a Containerized World (Matthias Luebken Technology Stream)

5. Immutable

Page 30: Patterns in a Containerized World (Matthias Luebken Technology Stream)

5. Immutable

• Don’t change your container after build

• Strive or a dev/prod parity

Examples: module-container.md#5-immutable

Page 31: Patterns in a Containerized World (Matthias Luebken Technology Stream)

6. Self-contained

Page 32: Patterns in a Containerized World (Matthias Luebken Technology Stream)

6. Self-contained

• Add dependencies on build time

• Sensible defaultsExamples: module-container.md#6-self-contained

Page 33: Patterns in a Containerized World (Matthias Luebken Technology Stream)

7. Small

Page 34: Patterns in a Containerized World (Matthias Luebken Technology Stream)

7. Small

• Don’t use large base images

• Use the minimal footprintExamples: module-container.md#6-small

Page 35: Patterns in a Containerized World (Matthias Luebken Technology Stream)

8. Adaptive

Page 36: Patterns in a Containerized World (Matthias Luebken Technology Stream)

8. Adaptive

• Anticipate the environment & react to change

• Software appliance

• Container reinitiates of config changes

• http://autopilotpattern.io

Page 37: Patterns in a Containerized World (Matthias Luebken Technology Stream)

A Module Container is

1. Linux process

2. API

3. Descriptive

4. Disposable

5. Immutable

6. Self-contained

7. Small

8. Adaptive

Page 38: Patterns in a Containerized World (Matthias Luebken Technology Stream)

1. Properties of a module container

Outline

2. Patterns for assembling these

Page 39: Patterns in a Containerized World (Matthias Luebken Technology Stream)

nginxProxy

RailsFrontend

A Go Backend

A NodeBackend

Page 40: Patterns in a Containerized World (Matthias Luebken Technology Stream)

nginxProxy

RailsFrontend

A Go Backend

A NodeBackend

Redis Cache

Logging Adapter

Service Ambassador

Page 41: Patterns in a Containerized World (Matthias Luebken Technology Stream)

A group of closely related containers. Deployed as a single unit and share namespaces.

Page 42: Patterns in a Containerized World (Matthias Luebken Technology Stream)

A NodeBackend

Redis Cache

Logging Adapter

Service Ambassador

Page 43: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Pattern: Sidecar / SidekickEnhance & extend the main container. K8S: transparently. Netflix: platform features.

UDSA NodeBackend

MAIN CONTAINER

RedisCache

SIDECAR

Pod

Page 44: Patterns in a Containerized World (Matthias Luebken Technology Stream)

A NodeBackend

Redis Cache

Logging Adapter

Service Ambassador

Page 45: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Pattern: AdapterStandardise and normalize output. E.g. logging and metrics.

localhost or

A NodeBackend

MAIN CONTAINER

Monitoring Adapter

ADAPTER

Pod

Page 46: Patterns in a Containerized World (Matthias Luebken Technology Stream)

A NodeBackend

Redis Cache

Logging Adapter

Service Ambassador

Page 47: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Proxy a local connection to the world: Service Discovery, Client Side LB, Circuit Breaker

A NodeBackend

MAIN CONTAINER

Service Discovery

AMBASSADOR

Pattern: Ambassador

localhost

(Pod)

Page 48: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Pattern: Container chainsDefined order of starting and stopping sidecar containers.

A NodeBackend

MAIN CONTAINER

StorageConfig

SIDECAR

Discovery

SIDECAR

NetworkConfig

SIDECAR

(Pod)

Page 49: Patterns in a Containerized World (Matthias Luebken Technology Stream)

SummaryModule container 1. Linux process 2. API 3. Descriptive 4. Disposable 5. Immutable 6. Self-contained 7. Small

Assembling • Sidecar • Adapter • Ambassador • Chains

Properties of a module container.

Patterns of assembling

github.com/luebken/container-patterns @luebken

Page 50: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Dankeschön.

@luebken

Page 51: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Credits• https://www.flickr.com/photos/skynoir/8241460998 (Cover image)

• https://www.flickr.com/photos/amlz/8664728590 (Lego)

• https://www.flickr.com/photos/guidedbycthulhu/6810361241 (Socket)

• https://www.flickr.com/photos/seektan/2074853585/ (Label)

• https://www.flickr.com/photos/gullevek/2122873934 (Trash)

• https://www.flickr.com/photos/grantmac/4852826923 (Lock)

• https://www.flickr.com/photos/mhirano/13236048424 (Hand)

Page 52: Patterns in a Containerized World (Matthias Luebken Technology Stream)
Page 53: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Old slides

Page 54: Patterns in a Containerized World (Matthias Luebken Technology Stream)
Page 55: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Other image guidelines

Page 56: Patterns in a Containerized World (Matthias Luebken Technology Stream)

General Docker image guidelines by the OpenShift team

• Reuse images

• Maintain compatibility within tags

• Avoid multiple processes

• Use exec in wrapper scripts

• Clean temporary files

• Place instructions in the proper order

• Mark important ports

• Set Environment Variables

• Avoid Default Passwords

• Avoid SSHD

• Use volumes for persistent data

https://docs.openshift.com/container-platform/3.3/creating_images/guidelines.html

Page 57: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Guidance for Docker Image Authors by Project Atomic

• Use MAINTAINER

• Know the difference between CMD and ENTRYPOINT

• Always use exec in wrapper scripts

• Always EXPOSE important ports

• Use Volumes appropriately

• Use USER

http://www.projectatomic.io/docs/docker-image-author-guidance/

Page 58: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Best practices for writing Dockerfiles by Docker

• Containers should be ephemeral

• Use a .dockerignore file

• Avoid installing unnecessary packages

• Run only one process per container

• Minimize the number of layers

• Sort multi-line arguments

• Build cache

https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/

Page 59: Patterns in a Containerized World (Matthias Luebken Technology Stream)
Page 60: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Container Patterns• For designing “cloud” applications.

• Container runtime agnostic.

• Are there general applicable patterns?

• How would we describe them?

• What are concrete examples and best-practices?

Differentiate Not app per container Not host container

Page 61: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Related Work

• 12factor.net apps

• Cloud-native application architectures:Matt Stine Free Ebook

• Microservices

• Continuous Delivery

Page 62: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Closely Related Work

Page 63: Patterns in a Containerized World (Matthias Luebken Technology Stream)

nginxProxy

RailsFrontend

A Go Backend

A NodeBackend

An Application

Page 64: Patterns in a Containerized World (Matthias Luebken Technology Stream)

POC github.com/luebken/container-api

Page 65: Patterns in a Containerized World (Matthias Luebken Technology Stream)

nginxProxy

RailsFrontend

A Go Backend

A NodeBackend

Redis Cache

Logging Adapter

Service Ambassador

TODO clean up notes

Page 66: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Links / Referenceshttp://blog.james-carr.org/2013/09/04/parameterized-docker-containers/ https://docs.docker.com/articles/dockerfile_best-practices/ http://tldp.org/LDP/abs/html/exitcodes.html (Exit Codes for “Proper Linux Process”) http://www.theregister.co.uk/2013/03/18/servers_pets_or_cattle_cern/ (Pets vs Cattle) http://www.projectatomic.io/docs/docker-image-author-guidance/ (Dockerfile) http://www.hokstad.com/docker/patterns (Dev patterns) http://blog.kubernetes.io/2015/06/the-distributed-system-toolkit-patterns.html (Composite Patterns) http://static.googleusercontent.com/media/research.google.com/de//pubs/archive/43438.pdf (Borg by

Google inspiration for Kubernetes / Pods) http://techblog.netflix.com/2014/11/prana-sidecar-for-your-netflix-paas.html (Sidecar Netflix)

WALK THROUGH

Page 67: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Other / old slides

Page 68: Patterns in a Containerized World (Matthias Luebken Technology Stream)

More info: ● https://docs.giantswarm.io/fundamentals/user-services/container-injection/ ● https://docs.giantswarm.io/fundamentals/user-services/service-discovery/

Example: Service Discovery @ Giant Swarm

Page 69: Patterns in a Containerized World (Matthias Luebken Technology Stream)

Reasons for splitting upIndependently releasable Separate processing types Different loads Different teams Reuse of containers Crash isolation Different release cycles Use different languages / versions / libraries

Page 70: Patterns in a Containerized World (Matthias Luebken Technology Stream)