Skip to content

About

a Python Flask API and modern visual dashboard that dynamically responds to configuration injected at runtime via Kubernetes.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Project: Chameleon-API

"Same code, different colors."

A lightweight Python Flask API and modern visual dashboard that dynamically responds to environment settings injected at runtime via Kubernetes ConfigMaps and Secrets. This project demonstrates the core concepts of Immutable Infrastructure, decoupling configuration from code, and cloud-native application patterns.


Directory Structure

chameleon-api/
├── app/
│   ├── app.py                # Flask application code (info, health, root UI)
│   ├── requirements.txt      # Python dependencies (Flask, Gunicorn)
│   └── templates/
│       └── index.html        # Premium glassmorphic HTML dashboard (dynamic styling)
├── k8s/
│   ├── configmap.yaml        # Public configurations (environment name, theme color)
│   ├── secret.yaml           # Private credentials (API Key)
│   ├── deployment.yaml       # Deployment template with health probes & env injections
│   └── service.yaml          # NodePort Service to expose the app locally
├── Dockerfile                # Multi-stage optimized Docker build instruction
├── .dockerignore             # Exclusions list to keep docker context small
├── .gitignore                # Git exclusions list (venv, caches, etc.)
└── README.md                 # Complete project guide and learning checklist

Step-by-Step Deployment & Testing Guide

To run this project inside Kubernetes, follow these steps.

Step 1: Start your local Kubernetes Cluster

If you do not have a cluster running, start one using Minikube or Kind:

# For Minikube
minikube start --driver=docker

# Or For Kind
kind create cluster --name chameleon-cluster

Step 2: Build the Container Image

Since we want to build the Docker image without uploading it to a public registry, build it directly inside your local cluster node:

Option A: If using Minikube

Run the environment hook to direct your host's Docker command line to the Minikube internal Docker daemon:

# In PowerShell:
& minikube -p minikube docker-env | Invoke-Expression

# Build the image directly inside Minikube
docker build -t chameleon-api:v1 .

Option B: If using Kind

Build the image locally on your machine, then load it into the Kind cluster:

# Build image locally (Ensure Docker Desktop is running)
docker build -t chameleon-api:v1 .

# Load image into the Kind cluster
kind load docker-image chameleon-api:v1 --name chameleon-cluster

Step 3: Deploy the Kubernetes Manifests

Apply all files inside the k8s/ directory in one command:

kubectl apply -f k8s/

Verify that the pods are running and the services are deployed:

# Check Pod status
kubectl get pods -l app=chameleon-api

# Check Service details
kubectl get service chameleon-service

Step 4: Access the Dashboard

Option A: Port Forwarding (Easiest & most reliable)

Run this command to bind port 5000 on your localhost directly to the service:

kubectl port-forward service/chameleon-service 5000:80

Open your browser and navigate to: http://localhost:5000.

Option B: Minikube Service Tunnel

If using Minikube, you can open the NodePort directly:

minikube service chameleon-service

The "Chameleon" Hot-Swap Test

Now, verify the power of dynamic configuration injection:

  1. Open http://localhost:5000 in your browser. Notice the primary theme is Green (the default in configmap.yaml).
  2. Open k8s/configmap.yaml and change APP_COLOR from "Green" to another color (e.g. "Red", "Blue", "Purple", or "Orange"):
    data:
      ENVIRONMENT_NAME: "Production"
      APP_COLOR: "Red"
      FEATURE_BETA: "Disabled"
  3. Apply the ConfigMap changes:
    kubectl apply -f k8s/configmap.yaml
  4. Restart the rollout to replace the pods with the new configuration:
    kubectl rollout restart deployment chameleon-api
  5. Refresh your browser page. The dashboard interface dynamically updates to a high-end glassmorphic Red theme.
    • Notice that this change happened instantly without rebuilding or repushing the Docker image!

Learning Checklist (The "Why")

1. Immutable Infrastructure: Why is it better to use a ConfigMap instead of editing app.py and rebuilding?

  • Consistency: The exact same binary/container image is guaranteed to run in Development, Staging, and Production. If we rebuilt the container for each environment, we risk compiling different dependencies or versions, introducing silent bugs.
  • Speed: Rebuilding, tagging, and pushing a Docker container image to a remote registry takes minutes. Modifying a ConfigMap and restarting a pod takes milliseconds.
  • Separation of Concerns: Developers own the code logic (app.py), and platform/DevOps operators control the runtime variables (configmap.yaml) without needing access to code repositories.

2. Env Injection: What is the difference between env (manual pairs) and envFrom (entire ConfigMap)?

  • env: Used to inject individual variables, often mapping them to a custom environment variable name. It is ideal when you want to rename keys or inject sensitive fields (like a single value from a Secret via secretKeyRef).
  • envFrom: Maps all key-value pairs inside a ConfigMap (or Secret) directly as environment variables in the container. This is very clean and convenient when your app accepts configurations matching the ConfigMap key names directly, avoiding duplicate lines in deployment YAMLs.

3. Service Discovery: Why do we need a Service if we can see the Pod IP?

  • Pods are Ephemeral: Pods in Kubernetes are temporary and can be destroyed, rescheduled, or autoscaled at any time. When a pod is replaced, it receives a completely new IP address.
  • Load Balancing: A Service provides a single, stable IP address and DNS name. It automatically routes traffic to the underlying active pods (load balancing) and filters out unhealthy pods based on the readiness probes.

4. Observability: How do I view the logs of my Flask app inside the cluster?

You can stream logs using the kubectl logs command:

# View logs for a single pod
kubectl logs <pod-name>

# Stream logs in real-time
kubectl logs -f <pod-name>

# View logs for all pods in the deployment
kubectl logs deployment/chameleon-api -f --tail=100

About

a Python Flask API and modern visual dashboard that dynamically responds to configuration injected at runtime via Kubernetes.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages