2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

40
CI/CD with Kubernetes Sandeep Parikh @crcsmnky Head of Solutions, US East Google Cloud Platform

Transcript of 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Page 1: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

CI/CD with Kubernetes

Sandeep Parikh@crcsmnkyHead of Solutions, US EastGoogle Cloud Platform

Page 2: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Why Is This ImportantBulletproof process to go from code to production

Centralized, repeatable building/tagging/pushing

Iterate quickly and reliably

Hands off my kubectl

Page 3: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Continuous Integration

TestCode Commit Push TestBuild

The ideal development flow

Page 4: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Continuous Integration

TestCode Commit Push TestBuild

The ideal development flow

TestD’oh!Code Commit Push Test

Crap!Build

Whoops!

The real development flow

Page 5: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Continuous Delivery

TestBuild Deploy

Testing

Staging

Page 6: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Continuous Deployment

DeployApproveDeliver $$$

Page 7: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Why Continuous?Fail fast

The build broke!

Triage fast

Why did the build break?

Deliver faster

Better software in customers hands!

Page 8: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Tools

Page 9: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Jenkins

Page 10: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Kubernetes Cluster (GKE)

Kubernetes Cluster

Node 1 Node 3Node 2

Page 11: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Kubernetes Cluster (GKE)

Jenkins Leader

Node 1 Node 3Node 2

Jenkins Leader

Page 12: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Kubernetes Cluster (GKE)

Jenkins Service

Node 1 Node 3Node 2

Jenkins Leader

Jenkins Service

Page 13: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Kubernetes Cluster (GKE)

Jenkins Ingress

Node 1 Node 3Node 2

Jenkins Leader

Jenkins Service

Google Cloud Load Balancer

Page 14: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Kubernetes Cluster (GKE)

Jenkins Builders

Node 1 Node 3Node 2

Jenkins Leader

Jenkins Builder

Jenkins Builder

Jenkins Builder

Jenkins Builder

Jenkins Service

Jenkins Builder

Google Cloud Load Balancer

Page 15: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Microservices

Page 16: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Backend

Page 17: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Frontend

Page 18: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Kubernetes Cluster (GKE)

Node 1

Microservices

Node 2

Page 19: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Kubernetes Cluster (GKE)

Node 1

Microservices

Node 2

BackendBackend Backend

Page 20: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Kubernetes Cluster (GKE)

Microservices

Node 2Node 1

Backend

Backend

Service

Backend Backend

Google Cloud Load Balancer

Page 21: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Kubernetes Cluster (GKE)

Microservices

Node 2Node 1

Backend

Frontend Frontend

Backend

Service

Frontend

Backend Backend

Google Cloud Load Balancer

Page 22: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Kubernetes Cluster (GKE)

Microservices

Node 2Node 1

Backend

Frontend Frontend

Backend

Service

Frontend

Frontend

Service

Backend Backend

Google Cloud Load Balancer

Page 23: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Microservices & NamespacesVirtual clusters backed by the same physical cluster

Divide cluster resources by different use cases

Supports quotas for managing resources

Test, Staging, Production, etc.

Page 24: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Pods

Time

Microservices & Rolling Update DeploymentsReplace Replication Controller Deployment A with Replication Controller Deployment B by updating one Pod at a time.

A

B

A

B

AB A

B

A

B

A

B

Page 25: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Rolling Update vs. DeploymentsImperative vs Declarative

Rollback

kubectl edit and kubectl apply

Server-side

...

Page 26: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Workflow

Page 27: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Kubernetes Workflow

CreatePushPackage Expose

Page 28: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Mapping To JenkinsWorkflow plugin, Freestyle doesn’t fit

Define a flexible, extensible, script-based CD pipeline

Groovy for scripting actions

Supports human input/approval

Page 29: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

node('docker') { checkout scm // Kubernetes cluster info def cluster = 'gtc' def zone = 'us-central1-f' def project = 'REPLACE_WITH_YOUR_PROJECT_NAME' // Run tests stage 'Go tests' docker.image('golang:1.5.1').inside { sh('go get -d -v') sh('go test') } // Build image with Go binary stage 'Build Docker image' def img = docker.build("gcr.io/${project}/gceme:${env.BUILD_TAG}") sh('gcloud docker -a') img.push()

Jenkinsfile

Page 30: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

// Deploy image to cluster in dev namespace stage 'Deploy to QA cluster' docker.image('buildpack-deps:jessie-scm').inside { sh('apt-get update -y ; apt-get install jq') sh('export CLOUDSDK_CORE_DISABLE_PROMPTS=1 ; curl https://sdk.cloud.google.com | bash') sh("/root/google-cloud-sdk/bin/gcloud container clusters get-credentials ${cluster} --zone ${zone}") sh('curl -o /usr/bin/kubectl https://storage.googleapis.com/.../release/v1.0.1/bin/linux/amd64/kubectl ; chmod +x /usr/bin/kubectl') sh("kubectl --namespace=staging rollingupdate gceme-frontend --image=${img.id}") sh("kubectl --namespace=staging rollingupdate gceme-backend --image=${img.id}") sh("echo http://`kubectl --namespace=staging get service/gceme --output=json | jq -r '.status.loadBalancer.ingress[0].ip'`> staging") }

Jenkinsfile

Page 31: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

// Deploy to prod if approved stage 'Approve, deploy to prod' def url = readFile('staging').trim() input message: "Does staging at $url look good? ", ok: "Deploy to production" sh('gcloud docker -a') img.push('latest') docker.image('buildpack-deps:jessie-scm').inside { sh('apt-get update -y ; apt-get install jq') sh('export CLOUDSDK_CORE_DISABLE_PROMPTS=1 ; curl https://sdk.cloud.google.com | bash') sh("/root/google-cloud-sdk/bin/gcloud container clusters get-credentials ${cluster} --zone ${zone}") sh('curl -o /usr/bin/kubectl https://storage.googleapis.com/.../release/v1.0.1/bin/linux/amd64/kubectl ; chmod +x /usr/bin/kubectl') sh("kubectl --namespace=production rollingupdate gceme-frontend --image=${img.id}") sh("kubectl --namespace=production rollingupdate gceme-backend --image=${img.id}") sh("echo http://`kubectl --namespace=production get service/gceme --output=json | jq -r '.status.loadBalancer.ingress[0].ip'`") }}

Jenkinsfile

Page 32: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Workflow

Repo

Kubernetes Cluster (GKE)Github

Jenkins

µservice

µservice

Default Staging

Production

Page 33: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Workflow

Repo

Kubernetes Cluster (GKE)Github

Jenkins

µservice

µservice

Default Staging

Production1

Push1

Page 34: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Workflow

Repo

Kubernetes Cluster (GKE)Github

Jenkins

µservice

µservice

Default Staging

Production1 2

Push

Build

1

2

Page 35: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Workflow

Repo

Kubernetes Cluster (GKE)Github

Jenkins

µservice

µservice

Default Staging

Production1 2

3

Push

Build

Clone

1

2

3

Page 36: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Workflow

Repo

Kubernetes Cluster (GKE)Github

Jenkins

µservice

µservice

Default Staging

Production1 2

3

4

Push

Build

Clone

Stage

1

2

3

4

Page 37: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Workflow

Repo

Kubernetes Cluster (GKE)Github

Jenkins

µservice

µservice

Default Staging

Production1 2

3

5

4

Push

Build

Clone

Stage

Approve

1

2

3

4

5

Page 38: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Workflow

Repo

Kubernetes Cluster (GKE)Github

Jenkins

µservice

µservice

Default Staging

Production1 2

3

5

4

Push

Build

Clone

Stage

Approve

Deploy

1

2

3

4

5

6

6

Page 39: 2016 - Continuously Delivering Microservices in Kubernetes using Jenkins

Questions, Comments@crcsmnkyRepo

https://github.com/GoogleCloudPlatform/continuous-deployment-on-kubernetesUpdates coming (built pre-Jenkins 2.0, which includes Workflow)Changes include Ingress, Deployments, Jenkins+K8S Plugin (auto-spawn builders)Docs

Automated Image Builds with Jenkins, Packer, and KubernetesDistributed Load Testing using KubernetesReal-time data analysis with Kubernetes, Google Cloud Pub/Sub, and BigQuery

Resources