Kubernetes Tutorial for Swift on the Server

In this tutorial, you’ll learn how to use Kubernetes to deploy a Kitura server that’s resilient, with crash recovery and replicas. You’ll start by using the kubectl CLI, then use Helm to combine it all into one command. By David Okun.

Leave a rating/review
Download materials
Save for later
Share

As you learn more about developing apps on the server-side, you’ll encounter multiple situations where you require tooling to handle processes outside of your source code. You’ll need to handle things like:

  • Deployment
  • Dependency management
  • Logging
  • Performance monitoring

While Swift on the server continues to mature, you can draw from a collection of tools that are considered “Cloud Native” to accomplish these things with a Swift API!

In this tutorial, you’ll:

  • Use Kubernetes to deploy and keep your app in flight.
  • Use Kubernetes to replicate your app for high availability.
  • Use Helm to combine all of the previous work you did with Kubernetes into one command.

This tutorial will use Kitura to build the API you will be working with, which is called RazeKube. Behold — the KUBE!

Swift Bird in a Cube

While the Kube is many things (all seeing, all knowing), there is one thing that the Kube isn’t: Resilient! You are going to use Cloud Native tools to make it so!

In this tutorial, you will use the following:

  • Kitura 2.7, or higher
  • macOS 10.14, or higher
  • Swift 5.0, or higher
  • Xcode 10.2, or higher
  • Terminal

Cloud Native Development and Swift

Here’s a short intro about what the Swift Server work group (SSWG) is working on and what Cloud Native Development entails.

Swift on the server draws motivation from many different ecosystems for inspiration. Vapor, Kitura and Perfect all draw their architecture from different projects in different programming languages. The concept of Futures and Promises in SwiftNIO isn’t native to Swift, although SwiftNIO is developed with a goal in mind to be a library that stands tall as a standard on its own.

A group of people who comprise the Swift Server work group meet bi-weekly to discuss advancements in the ecosystem. They have a few goals, but this one sticks out as relevant to this tutorial:

The Swift Server work group will … define and run an incubation process for these efforts to reduce duplication of effort, increase compatibility and promote best practices.

Regardless of how you may feel about adopting third-party libraries, the concept of reducing repeated code is important and worth pursuing.

Cloud Native Computing Foundation

Another collective in pursuit of this goal is the Cloud Native Computing Foundation (CNCF). The primary charter of the CNCF is as follows:

The Foundation’s mission is to make cloud native computing ubiquitous. The CNCF Cloud Native Definition v1.0 says: Cloud native technologies empower organizations to build and run scalable apps in modern, dynamic environments such as public, private, and hybrid clouds. Containers, service meshes, microservices, immutable infrastructure, and declarative APIs exemplify this approach.

Swift and Kubernetes

Swift developers who have focused their efforts on mobile devices haven’t worried too much about standardization with other platforms. Apple has a reputation for forming iOS-centric guidelines for design, and there are a number of tools to accomplish similar goals, but all on one ecosystem — iOS.

In the world of server computing, multiple languages can solve almost any problem. There’s plenty of times when one programming language makes more sense than another. Rather than get into a “holy-war” discussion about when Swift makes more sense than other languages, you’ll focus on using Swift as a means to an end. You’ll get a taste of the tools that can help you solve problems while you do it!

Kubernetes is the first tool you’re going to use. While Kubernetes is an important tool for deployment in current-day server-side development, it serves a number of other purposes as well, and you’ll explore those in this tutorial! You’ll dive right in after you make sure your app is working the way it needs to.

Getting Started

Click the Download Materials button at the top or bottom of this tutorial to get the project files you’ll use to build the sample Kitura app.

Next, you need to install Docker for desktop and the Kitura CLI to proceed with this tutorial.

  1. Audrey Tam has written an absolutely brilliant tutorial about how to use Docker here. Docker is going to be discussed in this tutorial as a building block for other components, and I recommend giving Audrey’s tutorial a read before proceeding. She has also written a tutorial on how to deploy a Kitura app with Kubernetes here, which is also worth a read to familiarize yourself with some of the basic concepts used here too!
  2. Docker for Desktop seems to be the ideal way to use Kubernetes on your Mac lately, but you do have alternatives! You can try MiniKube or another cloud provider to set up an online Kubernetes service, but Docker for desktop includes support for Kubernetes as well as other things that will prove helpful throughout this tutorial.
Note: There are two things I would like to point out before diving into this tutorial:
  1. Audrey Tam has written an absolutely brilliant tutorial about how to use Docker here. Docker is going to be discussed in this tutorial as a building block for other components, and I recommend giving Audrey’s tutorial a read before proceeding. She has also written a tutorial on how to deploy a Kitura app with Kubernetes here, which is also worth a read to familiarize yourself with some of the basic concepts used here too!
  2. Docker for Desktop seems to be the ideal way to use Kubernetes on your Mac lately, but you do have alternatives! You can try MiniKube or another cloud provider to set up an online Kubernetes service, but Docker for desktop includes support for Kubernetes as well as other things that will prove helpful throughout this tutorial.

Installing Docker and Kubernetes

If you’ve already installed Docker, start it up, then skip down to the next section: Enabling Kubernetes in Docker.

In a web browser, open https://www.docker.com/products/docker-desktop, and click the Download Desktop for Mac and Windows button:

Docker Desktop Installation

On the next page, sign in or create a free Docker Hub account, if you don’t already have one. Then, proceed to download the installer by clicking the Download Docker Desktop for Mac button:

Docker Desktop Download

You’ll download a file called Docker.dmg. Once downloaded, double-click the file. You’ll see a dialogue that wants you to drag the Docker Whale into your applications folder.

Dragging Docker to the Applications Folder

When the installer appears, you will have to allow privileged access for your Mac. Make sure you install the Stable version — previous versions of Docker for desktop only included Kubernetes on the Edge version.