Deploying Kitura with Docker & Kubernetes: Getting Started
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.
All the main players have a Kubernetes cloud service where you can deploy and manage your apps:
- Google Kubernetes Engine: Google created the Kubernetes standards, and hosts the Helm charts repository. Deploying here gives you easy access to Google’s big data and AI tools.
- Microsoft Azure Kubernetes Service: Microsoft, so a natural fit for Windows developers, but also supports Linux images.
- IBM Cloud Kubernetes Service: Kitura is from IBM, so this is a more natural match for Kitura than Google or Microsoft.
- Amazon Elastic Kubernetes Service: The newest kid on the block, but so many organizations already use AWS, it’s probably easiest for them to stay here.
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.
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.
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:
Setting up Kubernetes on IBM Cloud
Sign up for an IBM Cloud account, or click the Log in button if you already have one.
Next select Kubernetes Service (clear the filter field and click Filter if you are not seeing the Featured Offerings or Kubernetes Service):
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):
In the cluster configuration page, select Free, choose a location near you, give it the cluster name emojijournalcluster, finally click Create 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:
The Prerequisites command installs
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
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):
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>
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
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:
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.
--rm option in the
docker run command removes the container from your system.