Slide 1

Slide 1 text

Hands-on with Cloud-Native CI/CD Dave Stanke • Craig Barber DevOpsWorld Lisbon 2019-12-03

Slide 2

Slide 2 text

DevOps Advocate Software Engineer

Slide 3

Slide 3 text

1. Intro: Cloud-Native Computing and Cloud-native CI/CD 2. Container basics 3. Containerize an application 4. Continuous* Integration & Delivery 5. Serverless runtime 6. Continuous* Deployment 7. Triggering 8. Continuous!!! Integration, Delivery, & Deployment 9. Advanced Cloud-Native CI/CD Agenda

Slide 4

Slide 4 text

What is cloud computing?

Slide 5

Slide 5 text

On-demand self-service Broad network access Resource pooling Rapid elasticity Measured service NIST: Cloud computing is...

Slide 6

Slide 6 text

Elite performers are 24x more likely to have met all cloud characteristics get the report @ cloud.google.com/devops

Slide 7

Slide 7 text

What is cloud-native computing?

Slide 8

Slide 8 text

Cloud-Native Computing Cloud native technologies empower organizations to build and run scalable applications in modern, dynamic environments such as public, private, and hybrid clouds. Containers, service meshes, microservices, immutable infrastructure, and declarative APIs exemplify this approach. These techniques enable loosely coupled systems that are resilient, manageable, and observable. Combined with robust automation, they allow engineers to make high-impact changes frequently and predictably with minimal toil.

Slide 9

Slide 9 text

Cloud-Native Computing Cloud native technologies empower organizations to build and run scalable applications in modern, dynamic environments such as public, private, and hybrid clouds. Containers, service meshes, microservices, immutable infrastructure, and declarative APIs exemplify this approach. These techniques enable loosely coupled systems that are resilient, manageable, and observable. Combined with robust automation, they allow engineers to make high-impact changes frequently and predictably with minimal toil. 1 3 4 7 2 5 6 Containerization Immutable Infrastructure Declarative APIs Observability Loose Coupling Automation Predictability

Slide 10

Slide 10 text

Runtime infra vs. Tooling infra bare metal VMs cloud instances kubernetes serverless

Slide 11

Slide 11 text

Runtime infra vs. Tooling infra bare metal VMs cloud instances kubernetes serverless Application platform? CI/CD platform?

Slide 12

Slide 12 text

Sheepdog

Slide 13

Slide 13 text

So what? So… ● Security ● Scalability ● Portability ● Adaptability ● Economy

Slide 14

Slide 14 text

Cloud-Native CI/CD ● Use Cloud-Native tooling to build and deploy cloud-native [or not] applications ● Benefit from: ○ Security ○ Scalability ○ Portability ○ Adaptability ○ Economy

Slide 15

Slide 15 text

SETUP 1. Go to github.com/davidstanke/cloudbuild-demo 2. Click Use this template 3. Name your repo cloudbuild-demo 4. Make it Private

Slide 16

Slide 16 text

6. From the repo’s README, click Open in Cloud Shell Click accept/proceed when prompted 7. Follow the instructions under “Preparing Google Cloud to run this demo” 1. Enable the needed APIs 2. Grant Cloud Build permission to deploy to Cloud Run 3. configure Git 8. Run git push in the console 1. At the prompt, log in with your GitHub credentials 2. TIP: if you use two-factor authentication with GitHub, you’ll need to use a personal access token as your password. Visit github.com/settings/tokens to get one. Challenge: make yourself the “author” of this application.

Slide 17

Slide 17 text

Our application github.com/davidstanke/cloudbuild-demo

Slide 18

Slide 18 text

Exercise: install and run the app “locally” Install the app: npm install Run the app: npm start Preview on port 8080

Slide 19

Slide 19 text

Cloud-Native: Containerization

Slide 20

Slide 20 text

What’s a container? ≠

Slide 21

Slide 21 text

A container is... ● A packaging mechanism ● With layers ● It’s basically a decomposable TAR file

Slide 22

Slide 22 text

H A R D W A R E

Slide 23

Slide 23 text

A container provides isolation from its host environment ● Only the files inside the container are visible (by default) ● Processes inside and outside the container can’t communicate (by default)

Slide 24

Slide 24 text

What’s a container? ≈

Slide 25

Slide 25 text

Containerization: Dockerfile FROM ubuntu COPY ./dir . CMD server.sh EXPOSE 80, 443 Docker build ubuntu copied files server.sh

Slide 26

Slide 26 text

1. If the application is still running, press CTRL-C to terminate it. 2. Use the skeleton file provided at ./Dockerfile 3. Replace the “???”s so the build will: a. Pull an appropriate base image b. Copy source files to the image c. Install the application d. Add a command to start the application when the container is run e. Expose the application port Docker build -t gcr.io/$PROJECT_ID/hello . Exercise: write a Dockerfile Hint: the container is going to run the same application that you ran “locally.” How did you prepare it to run? What’s the command you used to run it?

Slide 27

Slide 27 text

1. docker build -t gcr.io/$PROJECT_ID/hello . Exercise: build a container Don’t forget the dot!

Slide 28

Slide 28 text

Containerization: Registry ● A repository of container images ○ ~artifact registry (e.g. artifactory) ● Stores multiple images, multiple tagged versions of each ● Layer-aware ● Public registry: Dockerhub ● Private registry: GCP (AWS, Azure…)

Slide 29

Slide 29 text

1. docker push gcr.io/$PROJECT_ID/hello Exercise: push a container

Slide 30

Slide 30 text

Exercise: install and run the app “locally” in a container 1. docker run -d -p 8080:8080 \ gcr.io/$PROJECT_ID/hello 2. Preview on port 8080 BREAK: start again at 14:40

Slide 31

Slide 31 text

Serverless ● Automatic, on-demand provisioning and deprovisioning ● User has no need (or ability!) to manage the platform ● User is billed only while requests are actively being processed ● Well-defined boundary between application code and platform ○ → e.g. inside a container vs. outside

Slide 32

Slide 32 text

A serverless runtime: Cloud Run ● Serve a container as an HTTP service ● Autoscaling ● HTTPS termination ○ Request on port 443 → container port 8080

Slide 33

Slide 33 text

Demo: deploying to Cloud Run

Slide 34

Slide 34 text

Exercise: Deploy a serverless service 1. Deploy your application container to Cloud Run 2. View your running application

Slide 35

Slide 35 text

1. git add . 2. git commit -am "add Dockerfile" 3. git push Exercise: Push updated code to GitHub

Slide 36

Slide 36 text

Container ENTRYPOINT ● Containers can be run as executables, with arguments ● The program to be run inside the container is specified with ENTRYPOINT in the Dockerfile ● A container’s entrypoint can be overridden at run time > docker run weather “Lisbon, Portugal” 16º and sunny! ENTRYPOINT [“forecast”] > docker run weather --entrypoint=geocode “Lisbon, Portugal” 38.7223° N, 9.1393° W

Slide 37

Slide 37 text

GCP’s Serverless CI/CD: Cloud Build CI/CD automation service for executing builds on GCP ● Trigger builds on source events ● Run tests and build artifacts ● Builds are executed as a series of containerized tasks ● All resources provisioned on-demand: scales from and to zero

Slide 38

Slide 38 text

Cloud Build execution flow Build step Build step Build step Build step /workspace Source code Artifact

Slide 39

Slide 39 text

Google Cloud Builders docker gcr.io/cloud-builders/docker docker example go gcr.io/cloud-builders/go go example gcloud gcr.io/cloud-builders/gcloud gcloud example gradle gcr.io/cloud-builders/gradle gradle example maven gcr.io/cloud-builders/mvn maven example kubectl gcr.io/cloud-builders/kubectl kubectl example npm gcr.io/cloud-builders/npm npm example >75 containers with common languages and tools installed in them that you can use in build steps (or bring your own)

Slide 40

Slide 40 text

Cloud Build config: cloudbuild.yaml The cloudbuild file defines the work to be done by Cloud Build steps: - name: CONTAINER_TO_RUN entrypoint: COMMAND_TO_RUN args: ['arg1','arg2','arg3'] - name: CONTAINER_TO_RUN entrypoint: args: options: - optionA - optionB

Slide 41

Slide 41 text

Demo: Use Cloud Build to containerize an application Docker build Docker push /workspace GitHub Google Container Registry

Slide 42

Slide 42 text

Exercise: write a Cloud Build config file 1. Complete cloudbuild.yaml to build and push a Docker image 2. Run gcloud builds submit 3. View the container registry and see the updated container image 4. Commit the latest changes and push to GitHub Docker build Docker push /workspace GitHub Google Container Registry

Slide 43

Slide 43 text

Demo: automated deployment

Slide 44

Slide 44 text

Exercise: add automated deployment 1. Make an application code change (e.g. make a text change to line 18 of app.js) 2. Add a deploy step to cloud build config (see: cloudbuild_deploy_snippet.html) Deploy: 3. gcloud builds submit 4. Refresh the application in your browser and verify the change 5. Commit your changes and push them to GitHub Challenge: modify your Dockerfile to produce a smaller container image

Slide 45

Slide 45 text

Automated build triggers CI/CD System Source Repository Build branch Build PR Push code change to branch notify via webhook Open a new pull request notify via webhook post results to PR Developer

Slide 46

Slide 46 text

Demo: configure triggering

Slide 47

Slide 47 text

Exercise: set up triggering 1. Add a Cloud Build trigger a. Choose first option for trigger: “GitHub ” b. Say yes to all the things 2. Make a code change → commit → push to GitHub 3. View the running build in Google Cloud Console > Cloud Build History 4. Reload the application to see your change

Slide 48

Slide 48 text

More CI: multi-stage pipelines

Slide 49

Slide 49 text

More CI: Build step sources steps: - name: gcr.io/cloud-builders/ GCP-provided builders steps: - name: Dockerhub 1. Clone builder repo (or write your own) 2. Docker build 3. Push to your Google Container Registry Community builders, or write-your-own steps: - name: gcr.io/$PROJECT_ID/

Slide 50

Slide 50 text

Demo: install and test application

Slide 51

Slide 51 text

Exercise: add `npm` build steps 1. Add an ‘install’ step using npm (from Dockerhub) a. Hint: your docker image knows how to pull the node image from dockerhub b. Hint: remember that the command to install is ‘npm install’ 2. Add a ‘test’ step using npm (from Dockerhub) a. Hint: The command to test is ‘npm test’ 3. Git commit and push 4. View build output in Cloud Build > History Got errors? Fix and re-submit until the build is green! CHALLENGE: add a linter step DOUBLE CHALLENGE: run the linter in parallel with the test step

Slide 52

Slide 52 text

Teardown (if desired) delete the repo from your GitHub account Your Google Cloud project and all the resources it contains will be automatically deleted within 1-2 days.

Slide 53

Slide 53 text

Thank you Feedback please! Using the DWJW app... 1. Select "Schedule" on the lower menu bar. 2. Select the workshop title in the mobile app. 3. Scroll to Post-Session Survey within the workshop details. 4. Complete the survey. Stay in touch! @davidstanke craigdbarber Questions?