Post on 13-Apr-2017
transcript
And, deploying to Kubernetes
Containerizing a REST microserviceAshley Roach – Cisco DevNetPrincipal Engineer & Evangelist@aroach
@aroach@CiscoDevNetdeveloper.cisco.com
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
3
DevNet VisionHelp developers build solutions
and grow their careers.
Learn Code Inspire
@aroach@CiscoDevNetdeveloper.cisco.com
• §1: Why and What
• §2: REST API + Container
• §3: Deploying to Kubernetes
@aroach@CiscoDevNetdeveloper.cisco.com
§1: Why and WhatDrag picture to placeholder or click icon to add
@aroach@CiscoDevNetdeveloper.cisco.com
• Quickly build a REST API using Swagger• How to containerize it• How to deploy it
What is this guy talking about?
@aroach@CiscoDevNetdeveloper.cisco.com
• 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
@aroach@CiscoDevNetdeveloper.cisco.com
§ 2: REST + ContainersDrag picture to placeholder or click icon to add
@aroach@CiscoDevNetdeveloper.cisco.com
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?!
@aroach@CiscoDevNetdeveloper.cisco.com
Infrastructure Architecture
DBaaS
CI/CD
Scheduler
@aroach@CiscoDevNetdeveloper.cisco.com
Heart of the Matter
@aroach@CiscoDevNetdeveloper.cisco.com
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.
@aroach@CiscoDevNetdeveloper.cisco.com
Swagger-node
• Runtime environment that includes Swagger Editor• swagger project <command>• Start• Edit
• node app.js for proper deployments
@aroach@CiscoDevNetdeveloper.cisco.com
Swagger Editor
@aroach@CiscoDevNetdeveloper.cisco.com
Demo
@aroach@CiscoDevNetdeveloper.cisco.com
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"]
@aroach@CiscoDevNetdeveloper.cisco.com
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
@aroach@CiscoDevNetdeveloper.cisco.com
Demo
@aroach@CiscoDevNetdeveloper.cisco.com
§ 3: KubernetesDrag picture to placeholder or click icon to add
@aroach@CiscoDevNetdeveloper.cisco.com
• Container Orchestration• Keeping your containers up, scaling them, routing
traffic to them• Kubernetes != Docker though K8S uses Docker
(or CoreOS rkt)
What is Kubernetes?
@aroach@CiscoDevNetdeveloper.cisco.com
• 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
@aroach@CiscoDevNetdeveloper.cisco.com
• Step-by-step tutorial of how to assemble a kubernetes cluster
• https://github.com/kelseyhightower/kubernetes-the-hard-way
Sidebar: K8S the hard way
Source: http://x-team.com/2016/07/introduction-kubernetes-architecture/
@aroach@CiscoDevNetdeveloper.cisco.com
Infrastructure Architecture
Persistence
CI/CD
Kubernetes Registry
@aroach@CiscoDevNetdeveloper.cisco.com
• Kubectl & ~/.kube/config• Minikube CLI• The Real Way™: CI system
Deploying Containers
@aroach@CiscoDevNetdeveloper.cisco.com
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
@aroach@CiscoDevNetdeveloper.cisco.com
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
@aroach@CiscoDevNetdeveloper.cisco.com
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
@aroach@CiscoDevNetdeveloper.cisco.com
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
@aroach@CiscoDevNetdeveloper.cisco.com
Möar Demo
@aroach@CiscoDevNetdeveloper.cisco.com
• Swagger-node provides fast REST API creation• Prototyping, mocking• Spec-first development was an adjustment• Container-based workflows made deployment
super simple
Takeaways
@aroach@CiscoDevNetdeveloper.cisco.com
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
@aroach@CiscoDevNetdeveloper.cisco.com
Thank you!