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. Server-Side Swift with
    Containers + Kubernetes
    Swift Mission Meetup, May 3, 2016
    @smithclay

    View Slide

  2. We can run Swift on
    Linux... now what?

    View Slide

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

    View Slide

  4. The dream of isomorphic
    client-server code
    Hello.swift
    iOS
    Hello.swift
    Linux
    https://www.lullabot.com/articles/what-is-an-isomorphic-
    application

    View Slide

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

    View Slide

  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

    View Slide

  7. Basic vapor setup

    View Slide

  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

    View Slide

  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

    View Slide

  10. View Slide

  11. You are here ^
    Summit of Production-Ready Swift

    View Slide

  12. Getting Production-ready
    with Containers

    View Slide

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

    View Slide

  14. Operating System
    Container Container Container
    App
    App
    App
    libs libs libs
    Further reading: https://www.docker.com/what-docker

    View Slide

  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.

    View Slide

  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

    View Slide

  17. Dockerize it!
    Creating Swift app images using
    Dockerfiles

    View Slide

  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/

    View Slide

  19. Base image already exists!
    https://hub.docker.com/r/swiftdocker/swift/

    View Slide

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

    View Slide

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

    View Slide

  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.

    View Slide

  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

    View Slide

  24. Swift 3.0 "at (fake)
    scale" Kubernetes

    View Slide

  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

    View Slide

  26. Upload Image to GCE
    Based on this tutorial: http://kubernetes.io/docs/hellonode/
    • docker build -t gcr.io//
    helloswift:v1 .
    • gcloud docker push gcr.io//
    helloswift:v1

    View Slide

  27. Create a cluster
    https://console.cloud.google.com/kubernetes

    View Slide

  28. Create a pod
    • $ gcloud container clusters get-
    credentials helloswift # auth
    • $ kubectl run helloswift --
    image=gcr.io//helloswift:v1
    --port 8080 # create pod
    • $ kubectl expose deployment helloswift
    --type="LoadBalancer" # external
    • $ kubectl get services # get IP

    View Slide

  29. That worked?!?

    View Slide

  30. Mega-scale
    • Can increase the number of pods to a crazy
    number.
    • $ kubectl scale deployment helloswift
    --replicas=4

    View Slide

  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.

    View Slide

  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

    View Slide

  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.

    View Slide

  34. Thanks! Questions?
    slides on twitter: @smithclay

    View Slide