Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Server-side Swift with Docker and Kubernetes

Server-side Swift with Docker and Kubernetes

Overview of creating server-side Swift applications with Docker and scaling on Kubernetes. Presented at the Swift Mission Meetup 5/3/16.

Clay Smith

May 04, 2016
Tweet

More Decks by Clay Smith

Other Decks in Programming

Transcript

  1. The dream of isomorphic client-server code Hello.swift iOS Hello.swift Linux

    https://www.lullabot.com/articles/what-is-an-isomorphic- application
  2. What else? • It will probably annoy a lot of

    people you know. • It's fun. • Probably not ready for something really serious (which is fun).
  3. If you're going to server-side swift... • Look at https://github.com/vsouza/awesome-ios

    • ~16 OSS options listed • Why not use something that claims it's elegant? Based on the PHP micro-framework Lumen. • https://github.com/qutheory/vapor
  4. Step 2: Setup the Dev Env with brew • Install

    https://github.com/kylef/swiftenv • $ brew install kylef/formulae/swiftenv • Install Vapor • $ brew tap qutheory/tap && brew install vapor-swift-2 • $ vapor new HelloSwift https://github.com/qutheory/vapor
  5. Install the right swift version... and build • $ cat

    .swift-version • $ swiftenv install DEVELOPMENT- SNAPSHOT-2016-03-24-a • $ vapor build • $ .build/debug/App Run the server Verify what version is needed
  6. https://en.wikipedia.org/wiki/LXC "LXC (Linux Containers) is an operating-system- level virtualization method

    for running multiple isolated Linux systems (containers) on a control host using a single Linux kernel."
  7. Operating System Container Container Container App App App libs libs

    libs Further reading: https://www.docker.com/what-docker
  8. Why use a container? • More lightweight than full fledged

    virtual machines: "better utilization" • Single artifact through the entire development process. Dev equals Prod!?! Easier deployment?!? • Better abstraction. Don't think of where the app runs (the server), think of the service the app provides.
  9. What's Docker? • Open-source tooling (and a private company) to

    help package apps using containers. • Funky layered filesystem that's hard to grok. • Much improved Mac beta on the way* • Until then: https://docs.docker.com/mac/ *https://medium.com/@nzoschke/docker-for-mac-beta-review-b91692289eb5
  10. Docker app image: best practice Base Image Main Image Runtime

    environment Ubuntu + Swift + Deps App environment Just the compiled vapor app https://blog.newrelic.com/2015/06/18/zero-to-docker/
  11. App Dockerfile (app) The default Dockerfile that comes with a

    vapor-CLI generated app, except we're using a custom build of the swift development snapshot (smithclay/swift:v03-23-a) vapor needs.
  12. Build it, run it # Build image of current vapor

    app, tagged v1 $ docker build -t smithclay/helloswift:v1 . # Run container on port 80 $ docker run -d -p 80:8080 smithclay/helloswift:v1 # Verify. Did it work? # Can also just open Safari :) $ curl -s -D - $(docker-machine ip) -o /dev/null
  13. What's Kubernetes? • Open-source system to manage apps in containers

    across multiple servers. From Google originally.* • Handles the hard part of building distributed systems: maintenance, deployment, scaling, etc. • Definitely overkill for small and simple stuff. http://queue.acm.org/detail.cfm?id=2898444
  14. Upload Image to GCE Based on this tutorial: http://kubernetes.io/docs/hellonode/ •

    docker build -t gcr.io/<PROJECT ID>/ helloswift:v1 . • gcloud docker push gcr.io/<PROJ ID>/ helloswift:v1
  15. Create a pod • $ gcloud container clusters get- credentials

    helloswift # auth • $ kubectl run helloswift -- image=gcr.io/<PROJ-ID>/helloswift:v1 --port 8080 # create pod • $ kubectl expose deployment helloswift --type="LoadBalancer" # external • $ kubectl get services # get IP
  16. Mega-scale • Can increase the number of pods to a

    crazy number. • $ kubectl scale deployment helloswift --replicas=4
  17. Simple Benchmarking* • ab -l -r -n 100 -c 10

    -k http:// 23.236.51.28:8080/ * benchmarking is really hard and there's lots of bogus data out there.
  18. node.js vs swift • ab -l -r -n 100 -c

    10 -k http://104.154.114.66:8080/ • ab -l -r -n 100 -c 10 -k http://104.197.153.164:8080/json Vapor node.js
  19. Lessons • Containers are great for portability across different clouds,

    environments, etc. • Containers hide the complexity of building deploying server-side swift. • Kubernetes is definitely overkill for "Hello World" in Swift. • Not mentioned: logging, security, monitoring, health checks... you get the idea.