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


  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 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

    • ~16 OSS options listed • Why not use something that claims it's elegant? Based on the PHP micro-framework Lumen. •
  7. Basic vapor setup

  8. Step 2: Setup the Dev Env with brew • Install • $ brew install kylef/formulae/swiftenv • Install Vapor • $ brew tap qutheory/tap && brew install vapor-swift-2 • $ vapor new HelloSwift
  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. "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:
  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: *
  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
  19. Base image already exists!

  20. Linux Swift Dockerfile (base) Based on

  21. Linux Swift Dockerfile (base) Based on

  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.
  26. Upload Image to GCE Based on this tutorial: •

    docker build -t<PROJECT ID>/ helloswift:v1 . • gcloud docker push<PROJ ID>/ helloswift:v1
  27. Create a cluster

  28. Create a pod • $ gcloud container clusters get- credentials

    helloswift # auth • $ kubectl run helloswift --<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:// * 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 • ab -l -r -n 100 -c 10 -k 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