Kubernetes automation in production

82
@ pbakker # Kubernetes Kubernetes Automation Paul Bakker @pbakker paulbakker.io

Transcript of Kubernetes automation in production

Page 1: Kubernetes automation in production

@pbakker#Kubernetes

Kubernetes Automation

Paul Bakker @pbakker paulbakker.io

Page 2: Kubernetes automation in production

@pbakker

Paul Bakker Software architect at Luminis Technologies

Page 3: Kubernetes automation in production

@pbakker

Paul Bakker Software architect at Luminis Technologies

Page 4: Kubernetes automation in production

Why Kubernetes

• Run Docker in clusters

• scheduling containers on machines

• networking

• storage

• automation

Page 5: Kubernetes automation in production

The basics

Page 6: Kubernetes automation in production

Docker container

Docker container

Docker containerDocker

container

Node

Docker container

Docker container

Docker containerDocker

container

Pods

Master

Node

Pods

API

etcdetcdetcd

Page 7: Kubernetes automation in production

Docker container

Docker container

Docker containerDocker

container

Node

Docker container

Docker container

Docker containerDocker

container

Pods

Docker container

Docker container

Docker containerReplication Controller

Master

schedules

schedules

Node

Pods

Page 8: Kubernetes automation in production

nginx

web files

Pod

• May contain multiple containers

• Lifecycle of these containers bound together

• Containers in pod see each other on localhost

• Env vars for services

pod

REDIS_SERVICE_HOST=10.201.159.165 REDIS_PORT_6379_TCP_PORT=6379

Container

Container

Page 9: Kubernetes automation in production

Networking

• We run many pods on a single machine

• Pods may expose the same ports

• How to avoid conflicts!?

Page 10: Kubernetes automation in production

Dynamic IP addresses

• Each pod gets a virtual IP

• Ports not shared with other pods

Page 11: Kubernetes automation in production

pod

pod

Docker container

Docker container

Docker containerService

Services

Fixed, virtual IP address

Dynamic IP address

Dynamic IP address

Page 12: Kubernetes automation in production

Multi component deployments

• Each component deployed as a pod

• Individually update and scale pods

• Use services for component communication

Page 13: Kubernetes automation in production

Multi component deployments

frontendbackend service 1

backend service 2

Redis

pod pod

pod

pod

backend service 1backend service 1backend service 1

backend service 2backend service 2backend service 2

serv i c e

serv i c e

serv i c e

Page 14: Kubernetes automation in production

Multi component deployments

frontendbackend service 1

backend service 2

Redis

pod pod

pod

pod

backend service 1backend service 1backend service 1

backend service 2backend service 2backend service 2

serv i c e

serv i c e

serv i c e

application

Page 15: Kubernetes automation in production

Multi component deployments

frontendbackend service 1

backend service 2

Redis

pod pod

pod

pod

backend service 1backend service 1backend service 1

backend service 2backend service 2backend service 2

serv i c e

serv i c e

serv i c e

component / service

Page 16: Kubernetes automation in production

Namespaces

pod

service

rcrcrcpodpod

serviceservice

pod

service

rcrcrcpodpod

serviceservicepod

service

rcrcrcpodpod

serviceservice

Namespace A

Namespace B

Namespace C

Page 17: Kubernetes automation in production

kubectl

kubectl create -f my-rc.yml

kubectl create -f my-service.yml

Page 18: Kubernetes automation in production

apiVersion: v1 kind: ReplicationController metadata: name: nginx spec: replicas: 3 selector: app: nginx template: metadata: name: nginx labels: app: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80

Page 19: Kubernetes automation in production

apiVersion: v1 kind: ReplicationController metadata: name: nginx spec: replicas: 3 selector: app: nginx template: metadata: name: nginx labels: app: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80

On how many nodes should this run?

Page 20: Kubernetes automation in production

apiVersion: v1 kind: ReplicationController metadata: name: nginx spec: replicas: 3 selector: app: nginx template: metadata: name: nginx labels: app: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80

On how many nodes should this run?

Describes our Docker container Ports, storage needs, etc.

Page 21: Kubernetes automation in production

apiVersion: v1 kind: ReplicationController metadata: name: nginx spec: replicas: 3 selector: app: nginx template: metadata: name: nginx labels: app: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80

On how many nodes should this run?

Labels, this (loosely) couples controllers,

pods and services together

Describes our Docker container Ports, storage needs, etc.

Page 22: Kubernetes automation in production

DEMO

Page 23: Kubernetes automation in production

HTTP Load balancing

Page 24: Kubernetes automation in production

HTTP load balancing

• Expose Kubernetes services to the outside world

• SSL offloading

• Gzip

• Redirects

Page 25: Kubernetes automation in production

Kubernetes ingress

• Built in support for GCE load balancers

• Future support for extensions (not quite there yet)

• What about your own environment!?

Page 26: Kubernetes automation in production

Using a custom load balancer

• Use Ha-proxy in front of Kubernetes

• Configure Ha-proxy dynamically

• The same works for nginx, apache…

Page 27: Kubernetes automation in production

pod

pod

Docker container

Docker container

Docker containerService

Dynamic IP address

Dynamic IP address

Load balancer node

ha-proxyHTTPS

SSL offloading

Page 28: Kubernetes automation in production

pod

pod

Docker container

Docker container

Docker containerService

Dynamic IP address

Dynamic IP address

Load balancer node

ha-proxyHTTPS

AWS

E LB

SSL offloading

Page 29: Kubernetes automation in production

pod

pod

Docker container

Docker container

Docker containerService

Dynamic IP address

Dynamic IP address

Load balancer node

ha-proxyHTTPS

AWS

E LB

Virtual private network

Page 30: Kubernetes automation in production

How does ha-proxy know about our services?

• Ha-proxy uses a static config file

• Auto-generate it based on data in etcd

• Confd

Page 31: Kubernetes automation in production

Automation

Page 32: Kubernetes automation in production

Using the API

• /v1/namespaces/mynamespace/pods

• /v1/namespaces/mynamespace/services

• /v1/namespaces/mynamespace/replicationcontrollers

REST API that gives access to everything

Page 33: Kubernetes automation in production

Client libraries

• Amdatu Kubernetes OSGi

• Amdatu Kubernetes Go

• Clojure, Node, Python etc…

kubernetes.listNodes().subscribe(nodes -> { nodes.getItems() .forEach(System.out::println); });

pods, err := kubernetes.ListPods(TEST_NAMESPACE) if err != nil { panic(err) }

for _,pod := range pods.Items { log.Println(pod.Name) }

Java

Go

Page 34: Kubernetes automation in production

Blue-green deployment

• Deployment without downtime

• Only one version is active at a time

• Rolls back on failed deployment

Page 35: Kubernetes automation in production

Docker container

Docker container

Docker container

pod v1

ha-proxyHTTPS

Page 36: Kubernetes automation in production

Docker container

Docker container

Docker container

pod v1

ha-proxyHTTPS

Page 37: Kubernetes automation in production

Docker container

Docker container

Docker container

pod v1

ha-proxyHTTPS

deploy new versionv2v2v2pod v2

deployer

Page 38: Kubernetes automation in production

Docker container

Docker container

Docker containerv1

ha-proxyHTTPS

health check…v2v2v2v2

deployer

Page 39: Kubernetes automation in production

Docker container

Docker container

Docker containerv1

ha-proxyHTTPS

health check…v2v2v2v2

deployer

Page 40: Kubernetes automation in production

Docker container

Docker container

Docker containerv1

ha-proxyHTTPS

v2v2v2v2

confd

Update configdeployer

Page 41: Kubernetes automation in production

v1

ha-proxyHTTPS

v2v2v2v2

v1v1v1

Page 42: Kubernetes automation in production

ha-proxyHTTPS

v2v2v2v2

Page 43: Kubernetes automation in production

Deployer

The Deployer

Page 44: Kubernetes automation in production

Kubernetes API

Deployer

Create RC

The Deployer

Page 45: Kubernetes automation in production

Kubernetes API

Deployer

pod pod pod pod

Create RC

service

Creates

The Deployer

Page 46: Kubernetes automation in production

Kubernetes API

Deployer

pod pod pod pod

GET /health Create RC

service

Creates

The Deployer

Page 47: Kubernetes automation in production

Kubernetes API etcd

Deployer

pod pod pod pod

GET /health Create RC

confdWatch

Switch Load Balancer Backend

service

Creates

The Deployer

Page 48: Kubernetes automation in production

Kubernetes API

HAProxy

etcd

Deployer

pod pod pod pod

GET /health Create RC

generate config

confdWatch

Switch Load Balancer Backend

service

Creates

The Deployer

Page 49: Kubernetes automation in production

Deployer

Page 50: Kubernetes automation in production

Kubernetes API

Deployer

1- Create RC

Page 51: Kubernetes automation in production

Kubernetes API

Deployer

pod pod pod pod

1- Create RC

service

2- Creates

Page 52: Kubernetes automation in production

Kubernetes API

Deployer

pod pod pod pod

3- GET /health 1- Create RC

service

2- Creates

Page 53: Kubernetes automation in production

Kubernetes API etcd

Deployer

pod pod pod pod

3- GET /health 1- Create RC

confd5- Watch

4- Switch Load Balancer Backend

service

2- Creates

Page 54: Kubernetes automation in production

Kubernetes API

HAProxy

etcd

Deployer

pod pod pod pod

3- GET /health 1- Create RC

6- generate config

confd5- Watch

4- Switch Load Balancer Backend

service

2- Creates

Page 55: Kubernetes automation in production

Amdatu Kubernetes Deployer

• Kubernetes deployment orchestration

• Load balancer configuration

• Blue-green deployment

• Apache licensed

• Go

Page 56: Kubernetes automation in production

{ "deploymentType": "blue-green", "namespace": "default", "useHealthCheck": true, "newVersion": "#", "appName": "cloudrti-demo", "replicas": 2, "frontend": "cloud-rti-demo.amdatu.com", "podspec": {} }

Page 57: Kubernetes automation in production

Amdatu Deploymentctl

• UI for setting up deployments

• Deployment history

• Webhooks for triggering from external events

• OSGi / Vertx / Angular 2

Page 58: Kubernetes automation in production

DEMO

Page 59: Kubernetes automation in production

Build / deploy pipelinesBuild Server

Docker Hub

builds image

alpha

Deployer

webhook

deploys

Page 60: Kubernetes automation in production

Scaling

Page 61: Kubernetes automation in production

Kubernetes node

How to scale a Kubernetes cluster?

Page 62: Kubernetes automation in production

Kubernetes node

pod pod pod

pod pod pod

How to scale a Kubernetes cluster?

Page 63: Kubernetes automation in production

How to scale a Kubernetes cluster?

Kubernetes node

pod pod pod

pod pod pod

pod pod pod

pod pod pod

Page 64: Kubernetes automation in production

How to scale a Kubernetes cluster?

Kubernetes nodeKubernetes nodeKubernetes node

pod pod pod

pod pod pod

pod pod pod

pod pod pod

pod pod pod

pod pod pod

pod pod pod

pod pod pod

Page 65: Kubernetes automation in production

How to scale a Kubernetes cluster?

Kubernetes nodeKubernetes nodeKubernetes node

pod pod pod

pod pod pod

pod pod pod

pod pod pod

pod pod pod

pod pod pod

pod pod pod pod pod pod

Page 66: Kubernetes automation in production

How to scale a Kubernetes cluster?

Kubernetes nodeKubernetes node

pod pod pod

pod pod pod

pod pod pod

pod pod pod

pod pod pod

pod pod pod

pod pod pod pod pod pod

Page 67: Kubernetes automation in production

How to scale a Kubernetes cluster?

Kubernetes nodeKubernetes nodeKubernetes node

Page 68: Kubernetes automation in production

Scaling up

1. Use AWS API to start new nodes (ScalingGroup)

2. Cloud-init to register node to Kubernetes cluster

Page 69: Kubernetes automation in production

Scaling down

1. Set node to “unschedulable”

2. Drain node (relocate pods to other machines)

3. Remove node from Kubernetes

4. Use AWS API to terminate nodes (ScalingGroup)

Page 70: Kubernetes automation in production

Amdatu scalerd

• CLI to add/remove nodes to a cluster

• Node draining to prevent downtime

• Scheduled automated scaling

Page 71: Kubernetes automation in production

{ "name": "night", "cron": "0 0 21 * * *", "description": "Switch to half capacity at night", "desiredCapacity": 2, "appScaleTemplates": [ { "app": "demo", "replicationControllerScaleTemplates": [ { "replicationController": "*", "replicas": 1 } ] } ] }

scalerctl create nighttime.json

Page 72: Kubernetes automation in production

How and where to run these tools?

• In Kubernetes of course!

• Bootstrap using kubectl scripts

Page 73: Kubernetes automation in production

MasterAPI

etcdetcdetcd

Kubernetes Node

Kubernetes Node

Kubernetes Node

Kubernetes Node

HA-Proxy

VPN

Page 74: Kubernetes automation in production

MasterAPI

etcdetcdetcd

Kubernetes Node

Kubernetes Node

Kubernetes Node

Kubernetes Node

HA-Proxy

VPN

What about my

database!?

Page 75: Kubernetes automation in production

Datastores in Kubernetes

• Kubernetes does have persistent volumes

• Most data stores require lots of tuning

• … don’t auto scale

• … require manual steps to configure cluster

Page 76: Kubernetes automation in production

MasterAPI

etcdetcdetcd

Kubernetes Node

Kubernetes Node

Kubernetes Node

Kubernetes Node

HA-Proxy

VPN

etcdetcdmongo

etcdetcdKafka

Page 77: Kubernetes automation in production

• Fully managed Kubernetes

• Centralised logging

• Application / cluster monitoring

Page 78: Kubernetes automation in production
Page 79: Kubernetes automation in production
Page 80: Kubernetes automation in production
Page 81: Kubernetes automation in production
Page 82: Kubernetes automation in production

@YourTwitterHandle#DVXFR14{session hashtag} @pbakker#Kubernetes

Q & A

https://bitbucket.org/amdatulabs

Open source projects: