This guide walks you through the process of running an example Swift application on a Kubernetes cluster. It uses a sample Perfect application based on the Bitnami Swift container. The first steps are to create the Swift program binary, insert it into a minimal Dockerfile, and use it as a starting point for creating a custom Helm chart to automate the application deployment in a Kubernetes cluster. Once the application is deployed and working, we'll also explore how to modify the source code for publishing a new application release and how to perform rolling updates in Kubernetes using the Helm CLI.
Assumptions and prerequisites
This guide will show you how to deploy an example Swift application in a Kubernetes cluster running on Minikube. The example application is a typical "Hello world" web application.
This guide makes the following assumptions:
- You have basic knowledge of the Swift programming language and have run Swift applications before.
- You have basic knowledge of how to build Docker images.
- You have basic knowledge of Helm charts and how to create them.
- You have a Docker environment running.
- You have Minikube installed in your local computer.
- You have a Kubernetes cluster running.
- You have the kubectl command line (kubectl CLI) installed.
- You have Helm and Tiller installed.
To create your own application in Swift and deploy it on Kubernetes using Helm you will typically follow these steps:
- Step 1: Obtain the application source code
- Step 2: Build the Docker image
- Step 3: Publish the Docker image
- Step 4: Create the Helm Chart
- Step 5: Deploy the example application in Kubernetes
- Step 6: Update the source code and the Helm chart
Step 1: Obtain the application source code
To begin the process, clone the repository for the sample application:
$ git clone https://github.com/bitnami/tutorials $ cd tutorials/swift-k8s
|NOTE: In case that you already have your own application source code, you can copy it in the tutorials/app-code directory. If you don't, clone the application code as shown in the next step.|
Clone the application project:
$ git clone https://github.com/PerfectlySoft/PerfectTemplate.git app-code
Now you've cloned the entire Tutorials repository and set the working directory to tutorials/swift-k8s. In this repository you will find the following:
- Dockerfile: a text file that contains instructions on how to build a Docker image. Each line indicates an instruction and a command for assembling all the pieces that comprise the image.
- app-code directory: a folder that contains the application source code.
- helm-chart directory: a folder that contains all the files that comprise the ready-made Helm chart will be used in this tutorial.
We will use the Perfect template application as an example for this guide.
|IMPORTANT: The following steps must be executed within the tutorials/swift-k8s directory.|
Step 2: Build the Docker image
We are ready to build our application's container. The source code already contains the Dockerfile to build the Docker Image.
Run the docker build command in the directory containing the Dockerfile. Add a tag to identify the current version of the application: 0.1.0. Remember to replace the USERNAME placeholder with your Docker ID.
$ docker build -t USERNAME/swift-app:0.1.0 .
You will see this output like during the build process:
(Optional) You can check that the image has been created in your local repository by executing the command below:
$ docker images | grep swift-app
Step 3: Publish the Docker image
Now you've created a Docker image which contains your application code, you can upload it into a public registry. This tutorial uses Docker Hub, but you can select one of your own, such as:
To upload the image to your Docker Hub account, follow the steps below:
Log in to Docker Hub:
$ docker login
Push the image to your Docker Hub account. Replace the USERNAME placeholder with your Docker ID:
$ docker push USERNAME/swift-app:0.1.0
You will see this output during the publishing process:
You should now see the new image in your Docker Hub repositories dashboard.
Step 4: Create the Helm chart
To create a brand new chart, you just need to run the helm create command. This creates a scaffold with sample files that you can modify to build your custom chart. For this tutorial, we'll provide you with a ready-made Helm chart. If you examine the repository you just downloaded, there is a directory named helm-chart that already contains the files you need. Check out the chart's file structure:
helm-chart |-- Chart.yaml |-- charts |-- values.yaml |-- templates |-- NOTES.txt |-- _helpers.tpl |-- deployment.yaml |-- ingress.yaml `-- service.yaml
|TIP: For a more in-depth look at chart creation, we recommend reading our Helm Chart tutorial.|
Here's a brief explanation of the most important components of this Helm chart:
- Chart.yaml: This file contains the metadata of the Helm chart, such as the version or the description.
- values.yaml: This file declares the variables to be passed to the templates.
- templates/service.yaml: This file defines a Kubernetes Service for the Swift application. Learn more about Services.
- templates/deployment.yaml: This manifest file creates a service endpoint for the container. Learn more about Deployments.
With the scaffolding in place, we'll perform the following steps:
- Edit the values.yaml file
- Edit the requirements.yaml file
Edit the values.yaml file
We have modified this file to include our Docker image, tag, service name, ports, and type.
Edit the templates/deployment.yaml file
The container might take more time to start up than what Kubernetes expects by default so we must set a longer initial delay for the livenessProbe and readinessProbe. We have modified this file to set less strict defaults.
Step 5: Deploy the example application in Kubernetes
|NOTE: Before performing the following steps, make sure you have a Kubernetes cluster running and Helm and Tiller are installed correctly. For detailed instructions, refer to our Getiing start tutorial.|
So far, you've built a Docker image, published it in a Container Registry and created your custom Helm chart. It's now time to deploy the example Swift within a Kubernetes cluster. To deploy the example application using the current Helm chart, follow these steps:
Make sure that you can to connect to your Kubernetes cluster by executing the command below:
$ kubectl cluster-info
Deploy the Helm chart by executing the following in the helm-chart directory. This will create a single pod within the cluster:
$ helm install -n swift-k8s .
Once the chart has been installed, you can access the application. To obtain the application URL, run the commands shown in the "Notes" section below.
You can also check the application service to get the application's URL. Since this guide uses Minikube, there are two ways to do this:
Option 1: List the services by executing the minikube service list command. You'll notice one service name:
$ minikube service list
Option 2: Check the application service using the minikube service command (SERVICE is a placeholder. Remember to replace it with the name of the service you want to check):
$ minikube service SERVICE --url
Enter the resultant URL in your browser to access the application. Congratulations! Your Swift application has been successfully deployed on Kubernetes.
Step 6: Update the source code and the Helm chart
As a developer, you'll be only too aware that your application may need new features or bug fixes in the future. To release a new Docker image, you only have to perform a few basic steps: change your application source code, then rebuild and republish the image in your selected container registry. Once the new image release has been pushed to the registry, you need to update the Helm chart. Follow these instructions to complete the application update process:
- In the app-code/Sources directory, change the "Hello world" string in the main.swift file. Save the file.
Build the new image and tag it as 0.2.0. Remember to replace the USERNAME placeholder with your Docker ID in the following steps.
$ docker build -t USERNAME/swift-app:0.2.0 .
|NOTE: Before performing the next step, make sure that you are logged in Docker Hub. Run the docker login command to access your account (if applicable).|
Publish the new image following the same steps as in Step 3 but using the new version number. Remember to replace USERNAME placeholder with your Docker ID:
$ docker push USERNAME/swift-app:0.2.0
Change to the helm-chart directory, where you have the Helm chart files. Edit the values.yaml file to replace the current image tag with the new one:
Run the helm upgrade command followed by the name of the chart. After that, you will see the information about the new deployment:
$ helm upgrade swift-k8s helm-chart/
See what revisions have been made to the chart by executing the helm history command:
$ helm history swift-k8s .
In your browser, enter the application URL you found in the step 5. You should see the new welcome message:
Follow these steps every time you want to update your Docker image and Helm chart.
|IMPORTANT: For performing a rollback of your application chart, refer to our how to tutorial for deploying, scaling and upgrading an application on Kubernetes with Helm.|
To learn more about the topics discussed in this guide, use the links below: