Tip
- This project provides various implementations of the Fibonacci sequence in Rust, including recursive, memoized, iterative, and dynamic programming approaches.
- It also includes a Kubernetes deployment with Helm charts deploying to minikube.
- Cluster has also Prometheus and Grafana installed to monitor the application.
Tip
The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1.
Tip
The sequence starts: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Tip
The Fibonacci sequence is defined by the recurrence relation: F(n) = F(n-1) + F(n-2) with base cases F(0) = 0 and F(1) = 1.
Tip
The Fibonacci sequence grows exponentially, so the recursive implementation is not efficient for large values of n.
Tip
The iterative and dynamic programming implementations are more efficient for large values of n.
Tip
The memoized recursive implementation is more efficient than the simple recursive implementation because it avoids redundant calculations.
Tip
The pattern matching implementation is similar to the simple recursive implementation but uses pattern matching instead of if-else statements.
Tip
The dynamic programming implementation uses an array to store the Fibonacci numbers and avoids redundant calculations.
The following implementations are provided:
fibonacci
: A simple recursive implementation.fibonacci_match
: A recursive implementation using pattern matching.fibonacci_dp
: An implementation using dynamic programming.fibonacci_memo
: A memoized recursive implementation.fibonacci_iterative
: An iterative implementation.
Important
Before you begin, ensure you have the following tools installed:
- Rust & Cargo (for local builds/verification, optional if you only build in Docker).
- Docker (to build and push container images).
- Helm (to manage Kubernetes deployments).
- Minikube or a Kubernetes Cluster (for testing).
- Docker Hub Account (to push container images).
Tip
You can build the Docker image using the provided Dockerfile Tag the image after its building Using your username login to Docker Hub and push the image to Docker Hub.
docker login --username ${DOCKER_USERNAME}
docker build -t fibonacci_rust:latest .
docker tag fibonacci_rust:latest ${DOCKER_USERNAME}/fibonacci_rust:${DOCKER_TAG}
docker push ${DOCKER_USERNAME}/fibonacci_rust:${DOCKER_TAG}
Tip
This repository includes a simple Terraform module (in the terraform/ directory) that deploys the fibonacci container into Kubernetes. The key files are:
- main.tf – Terraform configuration.
Tip
Navigate to the terraform/ directory and run the following commands to deploy the application:
terraform init
terraform validate
terraform apply -var="docker_username=your_docker_username" -var="docker_image_tag=your_docker_image_tag"
- Once all resources are deployed you can start the minikube service to access the application.
minikube service fibonacci-service
- You can go to this section to see about monitoring 2.5 Monitoring the Application with Prometheus and Grafana
Tip
This repository includes a simple Helm chart (in the fibonacci/ directory, for example) that deploys the fibonacci container into Kubernetes. The key files are:
- Chart.yaml – Chart metadata.
- values.yaml – Default values (e.g., image, replicas, etc.).
- templates/ – Contains Kubernetes manifests (Deployment, Service, etc.).
Tip
Create a secret to pull the image from Docker Hub:
kubectl create secret docker-registry regcred \
--docker-server=docker.io \
--docker-username=myusername \
--docker-password=MY_PERSONAL_ACCESS_TOKEN \
--docker-email=myemail@example.com
Then reference it in your values.yaml or deployment.yaml:
Tip
Add the following to your deployment.yaml file:
imagePullSecrets:
- name: regcred
Tip
Navigate to the fibonacci/ directory and install the Helm chart for the first time:
helm install fibonacci . --set image.repository=${DOCKER_USERNAME}/fibonacci_rust --set image.tag=${DOCKER_TAG}
Warning
Do not execute helm install again as it will overwrite the existing deployment.
Tip
If you already have the chart installed and want to upgrade it:
helm upgrade --install fibonacci . --set image.repository=${DOCKER_USERNAME}/fibonacci_rust --set image.tag=${DOCKER_TAG}
Tip
Check the deployment status:
helm status fibonacci
kubectl get pods
kubectl get services
kubectl get deployments
Tip
Launch a service to expose the application. Your default web browser will open the application.:
minikube service fibonacci-service
Tip
You can access the logs of the pod in /var/log/fibonacci.log go inside the pod and check the logs
kubectl exec -it <your-pod-name> -- /bin/sh
cat fibonacci.log
Tip
You can monitor the application using Prometheus and Grafana.
Tip
Access the Prometheus dashboard at http://localhost:9090 and the Grafana dashboard at http://localhost:3000.
Important
You need to port forward the Prometheus and Grafana services to access them locally:
kubectl port-forward svc/prometheus 9090:9090
kubectl port-forward svc/grafana 3000:3000
Tip
Go to http://localhost:9090 to access the Prometheus dashboard.
You will see a similar picture as below if you look for the requests_total metric:
Tip
Go to http://localhost:3000 to access the Grafana dashboard. Login with the default username and password (admin/admin). Get your IP with the following command:
kubectl get svc prometheus -n default -o jsonpath='{.spec.clusterIP}'
Tip
Go to the Grafana dashboard and add a new data source with the IP address you just got from the previous command.
http://<your-ip>:9090
Warning
This will uninstall helm release.
helm uninstall fibonacci
Caution
This will delete the deployment, secret, service and the minikube cluster.
kubectl delete secret regcred
minkube delete