This guide will show you how to deploy the Kitura application you created by following our Getting Started guide to a Kubernetes based environment. The guide assumes you already have an environment to deploy to. If not, you can sign up to IBM Cloud and create a Kubernetes cluster for free, or set one up locally using Minikube.
Your generated Kitura project already contains Docker related files and a Helm chart used to describe the set of Kubernetes resources it requires.
To deploy your application to Kubernetes, we first need to build it into a Docker container for Kubernetes to execute. Your project includes the following files for Docker support:
The .dockerignore file contains the files/directories that should not be included in the built Docker image. By default this file contains the Dockerfile and Dockerfile-tools. It can be modified as required.
Dockerfile defines the specification of the default Docker image for running the application. This image can be used to run the application.
Dockerfile-tools is a Docker specification file similar to a Dockerfile, except it includes the tools required for compiling the application. This image can be used to compile the application.
You need to build the Docker image that will run your Kitura application and will be deployed to your Kubernetes cluster. The following commands should all be run from the directory containing your Dockerfiles.
First, build the image that will be used to compile your app.
docker build -t myapp-build -f Dockerfile-tools .
Then, use this image to compile your application code into an executable:
docker run -v $PWD:/swift-project -w /swift-project myapp-build /swift-utils/tools-utils.sh build release
You’ll need to use the default Dockerfile to build the image that will run the executable you just created:
docker build -t myapp-run .
You can check that your container runs correctly by running the following command and then visiting localhost:8080 in your browser:
docker run -p 8080:8080 -it myapp-run
Once you are happy with your Docker image, tag it appropriately for the registry you are using and push it. If you are yet to choose a registry and have no specific cloud provider requirements, we suggest you try Docker Hub.
Before you deploy your application to your Kubernetes cluster, you need to make sure that the Helm chart is referencing the Docker image you’ve just pushed to your registry. You do this by editing the repository variable in the values.yaml file in your /chartdirectory.
The Helm chart provided configures a number of default resource options, such as memory allowance and replica policies. You can make changes to this depending on your or your cloud provider’s requirements.
To install the chart into your cluster, run the following from the /chart/<your app name> directory:
helm install --name myapp .
You may have to wait a few minutes for it to be available, but your application should now be deployed to your Kubernetes cluster. To access your application, you have to find the external IP of the worker node it has been deployed to. This process varies, so check with your cloud provider on how to do so. The port that your application had been deployed on is described by the
NodePort which you can find by running the following command on your cluster:
kubectl describe service <your app name>-service
Congratulations! You have now successfully deployed your Kitura application using Kubernetes! If you visit <external IP>:<NodePort> you should see the Kitura splash page.