34
And, deploying to Kubernetes Containerizin g a REST microservice Ashley Roach – Cisco DevNet Principal Engineer & Evangelist @aroach

Containerizing a REST API and Deploying to Kubernetes

Embed Size (px)

Citation preview

Page 1: Containerizing a REST API and Deploying to Kubernetes

And, deploying to Kubernetes

Containerizing a REST microserviceAshley Roach – Cisco DevNetPrincipal Engineer & Evangelist@aroach

Page 2: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

About Me

• API & Cloud Evangelist• 10+ Yrs Technical Product Mgmt• Life-long, self-taught developer• Denver, CO• github.com/aroach & github.com/ciscodevnet• Podcast: devtools.libsyn.com• slideshare.net/aroach

Page 3: Containerizing a REST API and Deploying to Kubernetes

3

DevNet VisionHelp developers build solutions

and grow their careers.

Learn Code Inspire

Page 4: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

• §1: Why and What

• §2: REST API + Container

• §3: Deploying to Kubernetes

Page 5: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

§1: Why and WhatDrag picture to placeholder or click icon to add

Page 6: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

• Quickly build a REST API using Swagger• How to containerize it• How to deploy it

What is this guy talking about?

Page 7: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

• It’s faster• Saves you from writing boilerplate code• Useful for mocking REST APIs• More reliable deployment mechanism

Why build a REST API this way

Page 8: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

§ 2: REST + ContainersDrag picture to placeholder or click icon to add

Page 9: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

Inspiration

• Created background “mini-hacks” activity at sales conference

• Needed a way for them to submit answers

• Why not make them do it via an API?!

Page 10: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

Infrastructure Architecture

DBaaS

CI/CD

Scheduler

Page 11: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

Heart of the Matter

Page 12: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

OpenAPI Spec (fka Swagger)

• Open specification for describing REST APIs• A top-down approach where you would use the 

Swagger Editor to create your Swagger definition and then use the integrated Swagger Codegen tools to generate server implementation.

• A bottom-up approach where you have an existing REST API for which you want to create a Swagger definition. 

Page 13: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

Swagger-node

• Runtime environment that includes Swagger Editor• swagger project <command>• Start• Edit

• node app.js for proper deployments

Page 14: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

Swagger Editor

Page 15: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

Demo

Page 16: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

DockerfileFROM node:5.11.1 # Create app directory RUN mkdir -p /usr/src/app # Establish where your CMD will execute WORKDIR /usr/src/app # Bundle app source into the container COPY ./node_modules /usr/src/app/node_modules COPY ./api /usr/src/app/api COPY ./config /usr/src/app/config COPY ./app.js /usr/src/app/ # Expose the port for the app EXPOSE 10010 # Execute "node app.js" CMD ["node", "app.js"]

Page 17: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

Makefile

run:docker run --rm --name $(NAME)-$(INSTANCE) $

(LINK) $(PORTS) $(VOLUMES) $(ENV) $(NS)/$(REPO):$(VERSION)

$ make run$ docker run --rm --name swagger-default -p 8080:10010 ciscodevnet/rest-api-swagger:latest

Page 18: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

Demo

Page 19: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

§ 3: KubernetesDrag picture to placeholder or click icon to add

Page 20: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

• Container Orchestration• Keeping your containers up, scaling them, routing

traffic to them• Kubernetes != Docker though K8S uses Docker

(or CoreOS rkt)

What is Kubernetes?

Page 21: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

• MiniKube (local workstation)• Installers (on-prem, hybrid, custom)

• Kops (part of core kubernetes.io github)• Kubespray (Ansible + Terraform)• Etc, etc…

• Cloud• Google Container Engine (GKE )• Azure Container Service• Etc…

Installation options

Page 22: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

• Step-by-step tutorial of how to assemble a kubernetes cluster

• https://github.com/kelseyhightower/kubernetes-the-hard-way

Sidebar: K8S the hard way

Page 23: Containerizing a REST API and Deploying to Kubernetes

Source: http://x-team.com/2016/07/introduction-kubernetes-architecture/

Page 24: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

Infrastructure Architecture

Persistence

CI/CD

Kubernetes Registry

Page 25: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

• Kubectl & ~/.kube/config• Minikube CLI• The Real Way™: CI system

Deploying Containers

Page 26: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

K8S templates: deployment# k8s/dev/api-deployment.yamlapiVersion: extensions/v1beta1kind: Deploymentmetadata: name: rest-api-swaggerspec: replicas: 2 template: metadata: labels: app: rest-api-swagger spec: containers: - name: rest-api-swagger image: ciscodevnet/rest-api-swagger:latest ports: - containerPort: 10010

Page 27: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

K8S templates: service# k8s/services/api-service-lb.yamlkind: ServiceapiVersion: v1metadata: name: rest-api-swaggerspec: type: LoadBalancer # or NodePort, etc. ports: - name: http port: 8080 targetPort: 10010 protocol: TCP selector: app: rest-api-swagger

Page 28: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

Manual kubectl deployment

$ kubectl apply -f k8s/dev/api-deployment.yaml$ kubectl apply -f k8s/services/api-service-lb.yaml$ kubectl describe deployment$ kubectl describe service rest-api-swagger$ kubectl delete -f k8s/dev/api-deployment.yaml$ kubectl delete -f k8s/services/api-service-lb.yaml

Page 29: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

Drone CI kubectl deploymentdeploy: k8s: image: containers.ex.com/devnet/drone-kubectl apiserver: https://your-gke-api-endpoint #kubectl cluster-info token: $$K8S_TOKEN commands: - 'kubectl apply -f k8s/services/*.yaml’ - 'kubectl apply -f k8s/dev/*.yaml --record’ - 'kubectl describe service ${SERVICE_NAME}’ when: branch: master

Page 30: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

Möar Demo

Page 31: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

• Swagger-node provides fast REST API creation• Prototyping, mocking• Spec-first development was an adjustment• Container-based workflows made deployment

super simple

Takeaways

Page 32: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

Helpful Links• https://

communities.cisco.com/people/asroach/blog/2016/09/19/building-the-devnet-api-scavenger-hunt

• https://communities.cisco.com/people/asroach/blog/2016/08/11/creating-a-cisco-spark-membership-via-google-forms

• https://github.com/swagger-api/swagger-node

• https://github.com/CiscoDevNet/rest-api-swagger

• https://scotch.io/tutorials/authenticate-a-node-js-api-with-json-web-tokens

• http://blog.mongodb.org/post/32866457221/password-authentication-with-mongoose-part-1

• http://www.itnotes.de/docker/development/tools/2014/08/31/speed-up-your-docker-workflow-with-a-makefile/

• http://sahatyalkabov.com/how-to-implement-password-reset-in-nodejs/

• https://marcqualie.com/2015/07/docker-dotenv

Page 33: Containerizing a REST API and Deploying to Kubernetes

@[email protected]

Thank you!

Page 34: Containerizing a REST API and Deploying to Kubernetes