111
Developing Kubernetes Services @MELANIECEBULA / MARCH 2019 / CON LONDON at Airbnb Scale

Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

Developing Kubernetes Services

@MELANIECEBULA / MARCH 2019 / CON LOND ON

at Airbnb Scale

Page 2: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

What is kubernetes?@MELANIECEBULA

Page 3: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

Who am I?

@MELANIECEBULA

Page 4: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

A BRIEF HISTORY

Page 5: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

MONOLITH LO C

0

1000000

2000000

3000000

4000000

2009 2010 2011 2012 2013 2014 2015 2016 2017 2018

😳

@MELANIECEBULA

Why Microservices?

Page 6: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

ENGINEERING TE AM

0

250

500

750

1000

2009 2010 2011 2012 2013 2014 2015 2016 2017 2018

😲

@MELANIECEBULA

Why Microservices?

Page 7: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

S CALING CONTINUOUS DELIVERY

@MELANIECEBULA

Why Microservices?

Page 8: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

Deploys per week (all apps, all environments)

@MELANIECEBULA

Why Microservices?

Page 9: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

Monolith production deploys per week

@MELANIECEBULA

Why Microservices?

Page 10: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

125,000production deploys

per year

@MELANIECEBULA

Why Microservices?

Page 11: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

Manually configuring boxes

Automate configuration of applications with Chef

Automate configuration and orchestration of containerized applications with Kubernetes

@MELANIECEBULA

Why kubernetes?EVOLUTION OF CONFIGURATION MANAGEMENT

Page 12: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

@MELANIECEBULA

Why kubernetes?

• portable

• immutable

• reproducible

• declarative

• efficient scheduling

• extensible API

• human-friendly data

• standard format

Page 13: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

TODAY

Page 14: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

50% of servicesin kubernetes

@MELANIECEBULA

migration progress

Page 15: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

250+ critical servicesin kubernetes

@MELANIECEBULA

migration progress

Page 16: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

• complex configuration

• complex tooling

• integrating with your current infrastructure

• open issues

• scaling

• … and more!

@MELANIECEBULA

Challenges with kubernetes?

Page 17: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

• complex configuration

• complex tooling

• integrating with your current infrastructure

• open issues

• scaling

• … and more!

@MELANIECEBULA

Challenges with kubernetes?

solvable problems!

Page 18: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

@MELANIECEBULA

you are not alone.

Page 19: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

1. abstract away k8s configuration

2. generate service boilerplate

3. version + refactor configuration

4. opinionated kubectl

5. custom ci/cd + validation

@MELANIECEBULA

Solutions?

Page 20: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

AB STRACT AWAY CONFIGURATION

Page 21: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

P

kubernetes

@MELANIECEBULA

kubectl apply

Production Deployment

Canary Deployment

Production ConfigMap

Canary ConfigMap

Production Service

Canary Service

kubernetes cluster

Dev Deployment

Dev ConfigMap

Dev

Service

kubernetes config files

Page 22: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

P

kubernetes

@MELANIECEBULA

kubectl apply

Production Deployment

Canary Deployment

Production ConfigMap

Canary ConfigMap

Production Service

Canary Service

kubernetes cluster

Dev Deployment

Dev ConfigMap

Dev

Service

kubernetes config files

lots of boilerplate

repetitive by environment

reso

urce

s

environments

Page 23: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

Reducing k8s boilerplate OUR REQUIREMENTS

• Prefer templating over file inheritance

• Input should be templated YAML files

• Make it easier to migrate 100s of legacy services

• Make it easier to retrain 1000 engineers

@MELANIECEBULA

Page 24: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

Project

Apps

Containers

Files

Volumes

Dockerfile

kube-gen

generatekubectl apply

kubernetes cluster

@MELANIECEBULA

kubectl apply

Production Deployment

Canary Deployment

Production ConfigMap

Canary ConfigMap

Production Service

Canary Service

Dev Deployment

Dev ConfigMap

Dev

Service

kubernetes config files

generating k8s configs

Page 25: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

Project

Apps

Containers

Files

Volumes

Dockerfile

kube-gen

generatekubectl apply

kubernetes cluster

@MELANIECEBULA

kubectl apply

Production Deployment

Canary Deployment

Production ConfigMap

Canary ConfigMap

Production Service

Canary Service

Dev Deployment

Dev ConfigMap

Dev

Service

kubernetes config fileskube-gen!

generating k8s configs

Page 26: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

Reducing k8s boilerplate WHAT WE WENT WITH

@MELANIECEBULA

Project

Apps

Containers

Files

Volumes

Dockerfile

sets params per

environment

other files

access params

Page 27: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

@MELANIECEBULA

Project

Apps

Containers

Files

Volumes

Dockerfile

kube-gen

generate

generating k8s configs

Page 28: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

@MELANIECEBULA

Project

Apps

Containers

Files

Volumes

Dockerfile

kube-gen

generatestandardized

namespaces based on environments!

generating k8s configs

Page 29: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

kube-gen COMP ONENTS

@MELANIECEBULA

Project

Apps

Containers

Files

Volumes

Dockerfile

Which shared components to use?

nginx logging statsd

example components

Page 30: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

kube-gen COMP ONENTS

@MELANIECEBULA

Main Container

Containers

Main App

Volumes

Files

Dockerfile

nginx component

• common pattern abstracted into component

• component yams is merged into project on generate

• components can require or set default params

Page 31: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

Reducing k8s boilerplate OPEN S OURCE OPTIONS

@MELANIECEBULA

1. Combine with package management (ex: helm)

2. Override configuration via file inheritance (ex: kustomize)

3. Override configuration via templating (ex: kapitan)

Page 32: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

•Reduce kubernetes boilerplate •Standardize on environments and namespaces

Takeaways

Page 33: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

GENERATE SERVICE BOILERPLATE

Page 34: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

Everything about a service is in one place in git, and managed with one

process.

Page 35: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

Everything about a service is in one place in git

• All configuration lives in _infra alongside project code • Edit code and configuration with one pull request • Easy to add new configuration • Statically validated in CI/CD

Configuration LIVES IN ONE PLACE

@MELANIECEBULA

Page 36: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

What we support:

• kube-gen files • framework boilerplate • API boilerplate • CI/CD • docs • AWS IAM roles • project ownership • storage • .. and more!

Configuration LIVES IN ONE PLACE

@MELANIECEBULA

Page 37: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

Configuration LIVES IN ONE PLACE

@MELANIECEBULA

this “hello world” service was created

in one command

Page 38: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

Configuration LIVES IN ONE PLACE

@MELANIECEBULA

collection of config generators (ex: docs,

ci)

Page 39: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

Configuration LIVES IN ONE PLACE

@MELANIECEBULA

collection of framework-specific generators (ex: Rails,

Dropwizard)

Page 40: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

• make best practices the default (ex: deploy pipeline, autoscaling, docs)

• run generators individually or as a group • support for review, update, commit

Configuration CAN BE GENERATED

@MELANIECEBULA

Page 41: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

•Everything about a service should be in one place in git

•Make best practices the default by generating configuration

Takeaways

Page 42: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

VERSION CONFIGURATION

Page 43: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

Why do we version our kube configuration?@MELANIECEBULA

• add support for something new (ex: k8s version)

• want to change something (ex: deployment strategy)

• want to drop support for something (breaking change)

• know which versions are bad when we make a regression 😅

• support release cycle and cadence

kube.yml

v1

kubev1 deploy

kubev2 deploy

kubernetes clusterkube.yml

v2

Page 44: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

How do we version our kube configuration?@MELANIECEBULA

1. version field

2. publish binaries for each version

3. channels point to binaries (ex: stable)

4. generate and apply using the appropriate binary

bonk kube-gen.yml

Page 45: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

Why do we version our generated configuration?@MELANIECEBULA

• what our project generators generate changes over time

• best practices change

• and bugs in the generators are found! 😅

kube.yml

generated by

sha1

kubev1 deploy

kubernetes clusterkube.yml

generated by sha2

generator at sha2 has a bug

Page 46: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

How do we version our generated configuration?@MELANIECEBULA

generator tags generated files with

version, sha, and timestamp

Page 47: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

REFACTOR CONFIGURATION

Page 48: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

• services should be up-to-date with latest best practices

• update configuration to the latest supported versions

• apply security patches to images

• configuration migrations should be automated

Why do we refactor configuration?FOR HUNDREDS OF SERVICES

Page 49: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

250+ critical servicesin kubernetes

@MELANIECEBULA

(we don’t want to manually refactor)

Page 50: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

How do we refactor configuration?@MELANIECEBULA

• collection of general purpose scripts

• scripts are modular

• scripts cover the lifecycle of a refactor

list-pr-urls.py

get-repos.py

update-prs.py

refactor.py

close.py

status.py

refactorator

Page 51: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

The lifecycle of a refactor

Checks out repo, finds project, runs refactor job, tags owners, creates PR

Comments on the PR, reminding owners to verify, edit, and merge the PR

Merges the PR with different levels of force

Run Refactor MergeUpdate

@MELANIECEBULA

Page 52: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

How do we refactor configuration?@MELANIECEBULA

• refactorator will run a refactor for all services given a refactor job

• refactor job updates _infra file(s)

• ex: upgrade kube version to stable

refactoratorupgrade-kube.py

refactor job

Page 53: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

Bumping stable version@MELANIECEBULA

• bump stable version

• cron job uses refactorator and with the upgrade-kube.py refactor job to create PRs

• another cron job handling updating and merging PRs

runs daily on weekdays

k8s cron job

refactoratorupgrade-kube.py

refactor job

Page 54: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

•Configuration should be versioned and refactored automatically.

Takeaways

Page 55: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

OPINIONATED KUBECTL

Page 56: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

P

kubernetes

@MELANIECEBULA

kubectl apply

Production Deployment

Canary Deployment

Production ConfigMap

Canary ConfigMap

Production Service

Canary Service

kubernetes cluster

Dev Deployment

Dev ConfigMap

Dev

Service

kubernetes config files

lots of boilerplate

repetitive by environment

reso

urce

s

environments

Page 57: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

P

kubernetes

@MELANIECEBULA

kubectl apply

Production Deployment

Canary Deployment

Production ConfigMap

Canary ConfigMap

Production Service

Canary Service

kubernetes cluster

Dev Deployment

Dev ConfigMap

Dev

Service

kubernetes config files

verbose

repetitive by namespace

Page 58: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

k tool KUBECTL WRAPPER

@MELANIECEBULA kubernetes cluster

kubectl apply

Production Deployment

Canary Deployment

Production ConfigMap

Canary ConfigMap

Production Service

Canary Service

Dev Deployment

Dev ConfigMap

Dev

Service

@MELANIECEBULA

calls kubectl commands (incl.

plugins)

Page 59: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

k tool OPINIONATED KUBECTL

@MELANIECEBULA

Page 60: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

k tool USES ENV VARS

• Runs in the project home directory: $ cd /path/to/bonk $ k status

• Environment variables for arguments:

$ k status ENV=staging• Prints the command that it will execute:

$ k status ENV=staging

kubectl get pods --namespace=bonk-staging

@MELANIECEBULA

standardized namespaces!

Page 61: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

k tool SIMPLIFIES BUILDS AND DEPLOYS

• k generate generates kubernetes files

• k build performs project build, docker build and docker push with tags

• k deploy creates namespace, applies/replaces kubernetes files, sleeps and checks deployment status

• can chain commands; ex: k all

@MELANIECEBULA

Page 62: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

k tool A DEBUG GING TO OL

• defaults to random pod, main container:

$ k ssh ENV=staging• specify particular pod, specific container:

$ k logs ENV=staging POD=… CONTAINER=bonk• automates debugging with k diagnose ENV=staging

@MELANIECEBULA

Page 63: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

k tool A DEBUG GING TO OL

• defaults to random pod, main container:

$ k ssh ENV=staging• specify particular pod, specific container:

$ k logs ENV=staging POD=… CONTAINER=bonk• automates debugging with k diagnose ENV=staging

@MELANIECEBULA

call kubectl diagnose

Page 64: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

What are kubectl plugins?@MELANIECEBULA

Page 65: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

What are kubectl plugins?@MELANIECEBULA

Page 66: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

k diagnose SETUP

@MELANIECEBULA

deploy bonk service with failing

command

new pod in CrashLoopBackoff

Page 67: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

k diagnose MANUALLY

@MELANIECEBULA

1. use “get pods -o=yaml” and look

for problems

2. grab logs for unready container

Page 68: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

k diagnose MANUALLY

@MELANIECEBULA

3. get k8s events related to this pod

Page 69: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

kubectl podevents KUBECTL PLUGIN

@MELANIECEBULA

kubectl podevents plugin

Page 70: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

kubectl diagnose USES COBRA G O CLI

@MELANIECEBULA

// defines CLI command and flags

var Namespace string

var rootCmd = &cobra.Command{

Use: “kubectl diagnose —namespace<namespace>"

Short: “diagnoses a namespace with pods in CrashLoopBackOff”

Run: func(cmd *cobra.Command, arg[]string) {

// Fill in with program logic

}

}

func Execute() {

rootCmd.Flags().StringVarP(&Namespace, "namespace", "n", “”)

rootCmd.MarkFlagRequired("namespace")

if err := rootCmd.Execute(); err != nil {

fmt.Println(err)

os.Exit(1)

}

Page 71: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

kubectl diagnose USES K8S CLIENT-G O

@MELANIECEBULA

// get pods (assume Namespace is defined)

kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube","config")

config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)

if err != nil { … }

clientset, err := kubernetes.NewForConfig(config)

if err != nil { … }

pods, err := clientset.CoreV1().Pods(Namespace).List(metav1.ListOptions{})

fmt.Printf("There are %d pods in the namespace %s\n", len(pods.Items), Namespace)

for _, pod := range pod.Items {

podName := pod.Name

for _, c := range pod.Status.ContainerStatuses {

if c.Ready != true {

// print c.LastTerminatedState and c.State

}

}

uses k8s client-go and Namespace

param to get pods

Page 72: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

kubectl diagnose USES K8S CLIENT-G O

@MELANIECEBULA

// get pods (assume Namespace is defined)

kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube","config")

config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)

if err != nil { … }

clientset, err := kubernetes.NewForConfig(config)

if err != nil { … }

pods, err := clientset.CoreV1().Pods(Namespace).List(metav1.ListOptions{})

fmt.Printf("There are %d pods in the namespace %s\n", len(pods.Items), Namespace)

for _, pod := range pod.Items {

podName := pod.Name

for _, c := range pod.Status.ContainerStatuses {

if c.Ready != true {

// print c.LastTerminatedState and c.State

}

}

prints info for all unready containers

Page 73: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

kubectl diagnose USES OS/EXEC (WHEN LAZY)

@MELANIECEBULA // get pod events for namespace and pod

cmd := exec.Command("kubectl", "podevents", Namespace, podName)

var out bytes.Buffer

var stderr bytes.Buffer

cmd.Stdout = &out

cmd.Stderr = &stderr

err := cmd.Run()

if err != nil {

fmt.Println(fmt.Sprint(err) + ": " + stderr.String())

log.Fatal(err)

} else {

fmt.Println("Events: \n" + out.String())

}

}

// also grab logs

cmd = exec.Command("kubectl", "logs", podname, fmt.Sprintf("--namespace=%s", Namespace), "-c", "bonk")

podevents kubectl plugin

Page 74: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

kubectl diagnose G O KUBECTL PLUGIN

@MELANIECEBULA

Page 75: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

kubectl diagnose G O KUBECTL PLUGIN

@MELANIECEBULA

1. unready container info

Page 76: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

kubectl diagnose G O KUBECTL PLUGIN

@MELANIECEBULA

1. unready container info

2. kubectl podevents

Page 77: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

kubectl diagnose G O KUBECTL PLUGIN

@MELANIECEBULA

1. unready container info

2. kubectl podevents

3. pod logs for unready containers

Page 78: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

•Create an opinionated kubectl wrapper

•Automate common k8s workflows with kubectl plugins

Takeaways

Page 79: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

CI/CD

Page 80: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance
Page 81: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

Each step in our CI /CD jobs are RUN steps in a build Dockerfile

Page 82: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

runs k commands

Page 83: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

DEPLOY PRO CESS

Page 84: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

A single deploy process for every change

Write code and config under your project

Open a PR and merge your code to master

Deploy all code and config changes

Develop DeployMerge

@MELANIECEBULA

Page 85: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

A single deploy process for every change

Deployment

ConfigMap

Service

AWS

Alerts

Dashboards

Project ownership

Docs

Secrets

kubectl apply

kubernetes cluster

kubectl apply

Storage

Service Discovery

API Gateway Routes

@MELANIECEBULA

Page 86: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

How do we apply k8s configuration?

Deployment

ConfigMap

Service

“kubectl apply”

kubernetes cluster

• kubectl apply all files

• in some cases where apply fails, replace files without force

• always restart pods on deploy to pick up changes

• return atomic success or failure state by sleeping and checking status

@MELANIECEBULA

Page 87: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

How do you always restart pods on deploy?

Deployment

ConfigMap

Service

kubectl apply

kubernetes cluster

kubectl apply

We add a date label to the pod spec,

which convinces k8s to relaunch all pods

@MELANIECEBULA

Page 88: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

How do we apply custom configuration?@MELANIECEBULA

Page 89: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

How do we apply custom configuration?

aws.yml

kubectl apply

kubernetes cluster

kubectl apply

AWS CRDAWS

Controller

AWS

webhook

@MELANIECEBULA

Page 90: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

How do we apply custom configuration?

aws.yml

kubectl apply

kubernetes cluster

kubectl apply

AWS CRDAWS

Controller

AWS

webhook

1. Create a custom resource definition

for aws.yml

@MELANIECEBULA

Page 91: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

How do we apply custom configuration?

aws.yml

kubectl apply

kubernetes cluster

kubectl apply

AWS CRDAWS

Controller

AWS

webhook

2. Create a controller that calls a web hook

when aws.yml is applied

@MELANIECEBULA

Page 92: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

How do we apply custom configuration?

aws.yml

kubectl apply

kubernetes cluster

kubectl apply

AWS CRDAWS

Controller

AWS

webhook

3. Create a web hook that updates a

custom resource

@MELANIECEBULA

Page 93: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

How do we apply custom configuration?

AWS CRDAWS

Controller

AWS

webhook

@MELANIECEBULA

AWS lambda

4. AWS lambda exposes web hook to

be called

Page 94: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

•Code and configuration should be deployed with the same process

•Use custom resources and custom controllers to integrate k8s with your infra

Takeaways

Page 95: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

VALIDATION

Page 96: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

• enforce best practices • at build time with validation scripts • at deploy time with admission controller Configuration

SHOULD BE VALIDATED

@MELANIECEBULA

Page 97: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

How do we validate configuration at build time?@MELANIECEBULA

Page 98: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

How do we validate configuration at build time?@MELANIECEBULA

kube validation

script

job dispatcher

project.yml validation

script

aws .yml validation

script

global jobs repo

project

build

global

validation

jobs

docs

build

bonk CI jobs

Page 99: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

How do we validate configuration at build time?@MELANIECEBULA

kube validation

script

job dispatcher

project.yml validation

script

aws .yml validation

script

global jobs repo

project

build

global

validation

jobs

docs

build

bonk CI jobs1. Define global job in global jobs repo

Page 100: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

How do we validate configuration at build time?@MELANIECEBULA

kube validation

script

job dispatcher

project.yml validation

script

aws .yml validation

script

global jobs repo

project

build

global

validation

jobs

docs

build

bonk CI jobs2. job dispatcher

always dispatches global jobs to

projects

Page 101: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

How do we validate configuration at build time?@MELANIECEBULA

kube validation

script

job dispatcher

project.yml validation

script

aws .yml validation

script

global jobs repo

project

build

global

validation

jobs

docs

build

bonk CI jobs

3. global job runs alongside project

jobs

Page 102: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

What do we validate at build time?@MELANIECEBULA

• invalid yaml

• invalid k8s configuration

• bad configuration versions

• max namespace length (63 chars)

• valid project name

• valid team owner in project.yml

kube validation

script

job dispatcher

project.yml validation

script

aws .yml validation

script

global jobs repo

Page 103: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

How do we validate configuration at deploy time?

project.yml

kubectl apply

kubernetes cluster

kubectl apply

@MELANIECEBULA

admission controller

admission controller intercepts requests to the k8s api server prior to persistence

of the object

Page 104: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

How do we validate configuration at deploy time?

project.yml

@MELANIECEBULA

admission controller

• metadata is encoded as annotations at generate time

• admission controller checks for required annotations

• reject any update to resources that are missing required annotations

Page 105: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

What do we validate with admission controller?

project.yml

@MELANIECEBULA

admission controller • project ownership

annotations

• configuration stored in git

• configuration uses minimally supported version

Page 106: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

What do we validate with admission controller?

project.yml

@MELANIECEBULA

admission controller

• production images must be uploaded to production ECR

• prevent deployment of unsafe workloads

• prevent deployment of development namespaces to production clusters

Page 107: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

What do we validate with admission controller?

project.yml

@MELANIECEBULA

admission controller

• production images must be uploaded to production ECR

• prevent deployment of unsafe workloads

• prevent deployment of development namespaces to production clusters

standardized namespaces!

Page 108: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

•CI/CD should run the same commands that engineers run locally

•CI/CD should run in a container •Validate configuration as part of CI/CD

Takeaways

Page 109: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

1. Abstract away complex kubernetes configuration

2. Standardize on environments and namespaces

3. Everything about a service should be in one place in git

4. Make best practices the default by generating configuration

5. Configuration should be versioned and refactored automatically.

6. Create an opinionated kubectl wrapper that automates common workflows

7. CI/CD should run the same commands that engineers run locally, in a container

8. Code and configuration should be deployed with the same process

9. Use custom resources and custom controllers to integrate with your infrastructure

10. Validate configuration as part of CI/CD

10 Takeaways@MELANIECEBULA

Page 110: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance

keno

• thousands of services running in k8s

• moving all configuration to gitops workflow w/ custom controllers

• scaling the cluster / scaling etcd / multi cluster support

• stateful services / high memory requirements

• tighter integration with kubectl plugins

• … and more!

2019 Challenges@MELANIECEBULA

Page 111: Developing Kubernetes Services - QCon London€¦ · • extensible API • human-friendly ... Combine with package management (ex: helm) 2. Override configuration via file inheritance