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 using Helm charts using minikube.
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.
Tip
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).
You can verify that the application compiles and runs locally:
Tip
You can build the Rust application using Cargo:
cargo run --package fibonacci --bin fibonacci -- 10
# Output:
Time taken by fibonacci_match: ...
Time taken by fibonacci_memo: ...
Time taken by fibonacci_iterative: ...
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 build -t fibonacci_rust:latest .
docker tag fibonacci_rust:latest ${DOCKER_USERNAME}/fibonacci_rust:latest
docker login --username ${DOCKER_USERNAME}
docker push ${DOCKER_USERNAME}/fibonacci_rust:latest
docker tag fibonacci_rust:latest ${DOCKER_USERNAME}/fibonacci_rust:v2
docker push ${DOCKER_USERNAME}/fibonacci_rust:v2
Tip
You can deploy the application to Kubernetes using the provided deployment.yaml file:
kubectl apply -f deployment.yaml
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 \
[email protected]
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 .
Tip
If you already have the chart installed and want to upgrade it:
helm upgrade --install fibonacci .
Tip
Check the deployment status:
helm status fibonacci
kubectl get pods
This program just prints to the STDOUT
Tip
You can access the logs of the fibonacci-deployment pod to see the output.
kubectl logs deployment/fibonacci-deployment
Warning
This will uninstall helm release.
helm uninstall fibonacci
Caution
This will delete the deployment and service and the minikube cluster.
kubectl delete secret regcred
minkube delete