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.

2ad55a66b8325eb097612c069ab76231?s=128

Clay Smith

May 04, 2016
Tweet

Transcript

  1. Server-Side Swift with Containers + Kubernetes Swift Mission Meetup, May

    3, 2016 @smithclay
  2. We can run Swift on Linux... now what?

  3. Server-side swift = writing a web app (in this case)

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

    https://www.lullabot.com/articles/what-is-an-isomorphic- application
  5. 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).
  6. 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
  7. Basic vapor setup

  8. 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
  9. 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
  10. None
  11. You are here ^ Summit of Production-Ready Swift

  12. Getting Production-ready with Containers

  13. 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."
  14. Operating System Container Container Container App App App libs libs

    libs Further reading: https://www.docker.com/what-docker
  15. 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.
  16. 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
  17. Dockerize it! Creating Swift app images using Dockerfiles

  18. 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/
  19. Base image already exists! https://hub.docker.com/r/swiftdocker/swift/

  20. Linux Swift Dockerfile (base) Based on https://hub.docker.com/r/swiftdocker/swift/~/dockerfile/

  21. Linux Swift Dockerfile (base) Based on https://hub.docker.com/r/swiftdocker/swift/~/dockerfile/

  22. 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.
  23. 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
  24. Swift 3.0 "at (fake) scale" Kubernetes

  25. 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
  26. 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
  27. Create a cluster https://console.cloud.google.com/kubernetes

  28. 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
  29. That worked?!?

  30. Mega-scale • Can increase the number of pods to a

    crazy number. • $ kubectl scale deployment helloswift --replicas=4
  31. 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.
  32. 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
  33. 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.
  34. Thanks! Questions? slides on twitter: @smithclay