This guide walks you through the process of running an example Rust application on a Kubernetes cluster. It is a "Hello World!" application inside a web server. The first steps are to create the Rust 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.
Rust applications are generally suitable for running in containers because they can be compiled as a single binary that contains all the dependencies they need to run. The compiled binary can run without Rust runtime, so container images are smaller.
Assumptions and Prerequisites
This guide will show you how to deploy an example Rust application in a Kubernetes cluster running on Minikube. The example application is a typical "Hello World!" application.
This guide makes the following assumptions:
- You have basic knowledge of the Rust programming language and have run Rust applications before.
- You have basic knowledge of Cargo--the Rust community's package manager and have used it before.
- You have basic knowledge of Docker Compose and have used it before.
- You have a Rust development environment.
- You have basic knowledge of Helm charts and how to create them.
- You have a Docker environment running.
- You have an account in a container registry (this tutorial assumes that you are using Docker Hub).
- You have Minikube installed on your local computer.
- You have a Kubernetes cluster running.
- You have the kubectl command line (kubectl CLI) installed.
- You have Helm v3.x installed.
To create your own application in Rust and deploy it on Kubernetes using Helm, you will typically follow these steps:
- Step 1: Build the Rust binary
- 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: Build the Rust binary
To begin the process, clone the repository for the sample application:
git clone https://github.com/bitnami/tutorials cd tutorials/rust-k8s
This will clone the entire tutorials repository. In this repository, you will find the following:
- Dockerfile: a text file that contains instructions on how to build the Docker image that will serve the Rust binary. Each line contains an instruction and a command for assembling all the pieces that comprise the image.
- build directory: a folder that contains the application source code and the Dockerfile we will use for the binary building.
- helm-chart directory: a folder that contains all the files that comprise the ready-made Helm chart to be used in this tutorial.
To build the Rust binary, follow the steps below:
Go to the build/ directory and run the docker-compose up command:
cd build/ docker-compose run --rm compile
This will mount the app-code/ and the binary directories as volumes in the container. The container will read the source files from that volume and will place the binary in the binary volume.
You will see this output during the execution process:
After the docker-compose command has finished the execution, you will have the Rust binary compiled in the binary/ directory.
Step 2: Build the Docker image
To build the Docker image that serves the Rust binary, follow the steps below:
Run the docker build command in the tutorials/rust-k8s directory. Add a tag to identify the current version of the application: 0.1.0. Remember to replace the USERNAME placeholder with your Docker ID.
cd .. docker build -t USERNAME/rust-k8s:0.1.0 .
You will see this output during the build process:
Step 3: Publish the Docker image
Now that your Docker image is built and 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 choice such as:
To upload the image to Docker Hub, follow the steps below:
Log in to Docker Hub:
Push the image to your Docker Hub account. Replace the DOCKER_USERNAME placeholder with the username of your Docker Hub account and my-custom-app:latest with the name and the version of your Docker image:
docker push DOCKER_USERNAME/my-custom-app:latest
Confirm that you see the 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:
rust-k8s |-- helm-chart | |-- rust-k8s | |-- Chart.yaml | |-- requirements.lock | |-- requirements.yaml | |-- values.yaml | |-- templates | |-- service.yaml | |-- NOTES.txt | |-- _helpers.tpl | `-- deployment.yaml
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 Rust application. Learn more about Services.
- templates/deployment.yaml: This manifest file creates a service endpoint for the container. Learn more about Deployments.
For a more in-depth look at chart creation, we recommend reading our Helm Chart tutorial.
With the scaffolding in place, we'll edit the values.yaml file. This file declares variables to be passed into the templates. We have modified the file to include our Docker image, tag, service name and port values. USERNAME is a placeholder. Remember to replace it with your Docker ID.
Step 5: Deploy the example application in Kubernetes
Before performing the following steps, make sure you have a Kubernetes cluster running with Helm v3.x installed. For detailed instructions, refer to our starter 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 Rust application within a Kubernetes cluster. To deploy the example application using the current Helm chart, follow these steps:
Make sure you can connect to your Kubernetes cluster by executing the command below:
Deploy the Helm chart by executing the following. RELEASE-NAME is a placeholder, so please make sure you replace it with the right value:
helm install RELEASE-NAME ./helm-chart/rust-k8s/
This will create a single pod within the cluster.
Once the chart has been installed, you will see a lot of useful information about the deployment. The application won't be available until the container has been deployed. Follow these instructions to check the status of the application:
Run the kubectl get pods command to get a list of running pods:
kubectl get pods
Once the container has been deployed, 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 two service names:
minikube service list
Option 2: Check the application service using the minikube service SERVICE command (SERVICE is a placeholder. Remember to replace it with the name of the service you want to check):
minikube service SERVICE --url
Congratulations! You have successfully deployed your Rust application 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:
Compile the new binary and copy it to the tutorials/rust-k8s/app-code/ folder.
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/rust-k8s:0.2.0 .
Before performing the next step, make sure that you are logged in to 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.
docker push USERNAME/rust-k8s:0.2.0
Change to the helm-chart/rust-k8s/ 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. RELEASE-NAME is a placeholder, replace it with the right value:
helm upgrade RELEASE-NAME helm-chart/rust-k8s/
In your browser, refresh the page to see the changes:
See what revisions have been made to the chart by executing the helm history command:
helm history RELEASE-NAME
Follow these steps every time you want to update your Docker image and Helm chart.
To perform a rollback of your application chart, refer to our 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: