Slide 1

Slide 1 text

Server-Side Swift with Containers + Kubernetes Swift Mission Meetup, May 3, 2016 @smithclay

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Basic vapor setup

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

You are here ^ Summit of Production-Ready Swift

Slide 12

Slide 12 text

Getting Production-ready with Containers

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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.

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Dockerize it! Creating Swift app images using Dockerfiles

Slide 18

Slide 18 text

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/

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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.

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Swift 3.0 "at (fake) scale" Kubernetes

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

That worked?!?

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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.

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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.

Slide 34

Slide 34 text

Thanks! Questions? slides on twitter: @smithclay