Deploying Kitura with Docker & Kubernetes: Getting Started

Kitura servers built in Swift are pretty cool, but it’s even cooler to deploy them in the cloud! See how to build a Docker image, then deploy to Kubernetes. By Audrey Tam.

Leave a rating/review
Download materials
Save for later
Share

Creating a Kitura server in Swift is pretty cool, but it’s even cooler to deploy it live, in the cloud! You want it scalable and load-balanced, so it’s always available. And you want to be able to monitor it.

Kubernetes is a popular platform for deploying and managing any application composed from Docker images. And Kitura apps come with everything you need to build a Docker image, then deploy to Kubernetes.

Note: The word kubernetes is ancient Greek for helmsman, so think of it as a system that steers the container ship that is your app. You deploy apps with the Helm package manager: the local Helm client interacts with a Tiller server, which interacts with Kubernetes. Tiller installs, upgrades and uninstalls Helm charts — application definitions that specify how to deploy your app in Kubernetes.

All the main players have a Kubernetes cloud service where you can deploy and manage your apps:

In this tutorial, you’ll build and push a Docker image for an app named EmojiJournal to Docker Hub, then deploy it to IBM Cloud Kubernetes Service. EmojiJournal is a pre-built Kitura app that allows you to save all your favorite emoji.

Note: This tutorial assumes you know how to create a Kitura app (as in Kitura Tutorial: Getting Started) and you’re comfortable using Docker’s command line interface (as in Docker on macOS: Getting Started).

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.

Running Docker

If you don’t have a Docker account, go to Docker’s web page, click the Please Login to Download button, create an account, then respond to the activation email when it arrives.

If you don’t have the Docker Desktop app, download Docker Desktop for Mac, then install and run the app. Moby the whale should appear in your Mac’s status bar:

Docker Desktop in menu bar

Note: If the direct download link above doesn’t work, download from the Docker download page after you activate and sign in with your Docker account.

Setting up Kubernetes on IBM Cloud

Sign up for an IBM Cloud account, or click the Log in button if you already have one.

Note: This is a two-step process. First you sign up for an IBMid and then for an IBM Cloud account. If your IBM Cloud account is not confirmed within a few minutes, switch to using Chrome to trigger the confirmation email. It looks like the IBM site might not be fully-compatible with Safari.

IBM Cloud Signup

Click Catalog:

IBM Cloud Catalog

Next select Kubernetes Service (clear the filter field and click Filter if you are not seeing the Featured Offerings or Kubernetes Service):

Featured Offerings

At this point you will have to use the Upgrade button to upgrade from the free account by providing credit card details, but once you have done that, there is a Lite plan which provides one cluster for free.

When you are back on the Kubernetes page, click Create (you may end up on the Create a new cluster page automatically after entering your credit card details):

Create new cluster

In the cluster configuration page, select Free, choose a location near you, give it the cluster name emojijournalcluster, finally click Create Cluster:

Creating a free cluster

You won’t see any feedback — just wait a short time and you will be directed to the page shown below. You’re limited to one free cluster, and it expires in one month.

In Terminal, run all the commands listed in the Access tab of your new cluster:

Cluster access commands

Note: Use the handy “copy” button.

The Prerequisites command installs helm, kubectl and ibmcloud CLIs.

Commands 1 and 2 are customized with the IBM Cloud and Kubernetes regions that match your location. I’m in Melbourne, Australia so my commands specify au-syd and ap-south. Scroll down if you need to see commands 3, 4 and 5.

Building & Pushing a Docker Image

In this section, you’ll build a Docker image for the sample app, then push it to your Docker account.

You should have already started Docker Desktop, back in the Running Docker section — click on Moby the whale, and sign in to your Docker account (if you’re not already signed in):

Sign in to Docker

Building

In Finder, locate the starter folder EmojiJournalServer, then open Terminal, and cd to this directory:

cd <drag the EmojiJournalServer folder from Finder to the terminal window> 
Note: Check that your full path doesn’t contain any spaces, like Tutorial Projects/Deploy Kitura/starter/EmojiJournalServer — some deeply-buried commands don’t like odd characters in path names. To play it safe, stick to CamelCase for all enclosing folders.

This directory contains the Dockerfile and Dockerfile-tools files you’ll use to build your Docker image.

Next, enter this command:

docker build -t emojijournal-build -f Dockerfile-tools .

You first build the emojijournal-build image that you’ll use to compile your app.

Then, enter this command:

docker run -v $PWD:/swift-project -w /swift-project emojijournal-build /swift-utils/tools-utils.sh build release

You use emojijournal-build to compile your application code into an executable file. This may take a while…

And then, enter this command:

docker build -t emojijournal-run .

You’re finally building the image that you’ll push to Docker Hub, using the default Dockerfile.

Now check your image by running it in a container:

docker run --rm -it -p 8080:8080 emojijournal-run

In your browser, open http://localhost:8080/client:

EmojiJournal on localhost

The app is already hooked up to a CouchDB (IBM-alias Cloudant) database, with entries added by David Okun, when he recorded his Kitura video course. Go ahead and add your own emoji: click the smiley button to open the character menu, select an emoji, then click the big plus sign. It takes a few seconds to update the database and reload the page to show your new emoji.

Note: To stop the server, press Control-C in the terminal window where the container is running. The --rm option in the docker run command removes the container from your system.