A production-ready Flask API that provides Hello World and date/time endpoints, fully containerized with Docker and deployable to Kubernetes.
- Hello World endpoints (
/
,/hello
) - Date and time information (
/datetime
,/time
,/date
) - Docker containerization with optimized Dockerfile
- Kubernetes deployment with proper manifests
- Ingress configuration for clean URL routing
- Production-ready structure and configuration
python-app/
├── src/ # Source code
│ ├── app.py # Flask application
│ ├── requirements.txt # Python dependencies
│ └── .dockerignore # Docker ignore file
├── Docker/ # Docker configuration
│ └── Dockerfile # Multi-stage Docker build
├── k8s/ # Kubernetes manifests
│ ├── deployment.yaml # Flask app deployment
│ ├── service.yaml # Internal service
│ └── ingress.yaml # External access
├── scripts/ # Deployment scripts
│ └── deploy.sh # Kubernetes deployment script
├── Makefile # Build and deploy commands
└── README.md # This file
GET /
- Returns Hello World messageGET /hello
- Returns Hello World message
GET /datetime
- Current date, time, and timestampGET /time
- Current time onlyGET /date
- Current date with day and month names
- Python 3.11+
- Docker with Docker Compose
- Kubernetes (minikube for local development)
- kubectl command-line tool
# Clone the repository
git clone <your-repo-url>
cd python-app
# Install dependencies
cd src
pip install -r requirements.txt
# Run locally
python app.py
Test locally:
curl http://localhost:8080/
curl http://localhost:8080/datetime
# Build Docker image
cd python-app
make build
# Or manually
docker build -t danielaxhammar/flask-app:v2 Docker/
# Run container
docker run -p 8080:8080 danielaxhammar/flask-app:v2
Test Docker:
curl http://localhost:8080/
curl http://localhost:8080/datetime
# Start minikube (if not running)
minikube start
# Deploy to Kubernetes
cd python-app
make deploy
# Or manually
kubectl apply -f k8s/
Set up local access:
# Add hostname to /etc/hosts
echo "127.0.0.1 flask-app.local" | sudo tee -a /etc/hosts
# Port forward Ingress controller
kubectl port-forward -n ingress-nginx service/ingress-nginx-controller 8080:80
Test Kubernetes deployment:
# Test all endpoints
curl -H "Host: flask-app.local" http://localhost:8080/
curl -H "Host: flask-app.local" http://localhost:8080/hello
curl -H "Host: flask-app.local" http://localhost:8080/datetime
curl -H "Host: flask-app.local" http://localhost:8080/time
curl -H "Host: flask-app.local" http://localhost:8080/date
# Build image
make build
# Run container
docker run -p 8080:8080 danielaxhammar/flask-app:v2
# Push to registry
docker tag danielaxhammar/flask-app:v2 your-registry/flask-app:v2
docker push your-registry/flask-app:v2
# Deploy
make deploy
# Check status
kubectl get deployment,service,ingress
kubectl get pods
# View logs
kubectl logs -l app=flask-app
# Clean up
make clean
# Or manually
kubectl delete -f k8s/
{
"message": "Hello World!"
}
{
"date": "2024-01-15",
"time": "14:30:25",
"datetime": "2024-01-15 14:30:25",
"timestamp": 1705327825.123456
}
{
"current_time": "14:30:25",
"timezone": "local"
}
{
"current_date": "2024-01-15",
"day_of_week": "Monday",
"month": "January"
}
FLASK_APP=app.py
- Flask application entry pointFLASK_ENV=production
- Environment modeFLASK_RUN_HOST=0.0.0.0
- Bind to all interfacesFLASK_RUN_PORT=8080
- Application port
- Memory: 64Mi request, 128Mi limit
- CPU: 50m request, 100m limit
- Replicas: 1 (configurable in deployment.yaml)
- Direct Python execution
- Port 8080 on localhost
- Containerized application
- Port mapping: 8080:8080
- minikube cluster
- Ingress routing with port forwarding
- Clean URLs:
http://localhost:8080/
- Production cluster deployment
- Load balancer integration
- SSL/TLS termination
- Auto-scaling capabilities
# Test all endpoints
make test
# Manual testing
curl -H "Host: flask-app.local" http://localhost:8080/
curl -H "Host: flask-app.local" http://localhost:8080/datetime
curl -H "Host: flask-app.local" http://localhost:8080/time
curl -H "Host: flask-app.local" http://localhost:8080/date
make build # Build Docker image
make deploy # Deploy to Kubernetes
make clean # Clean up deployment
make test # Test all endpoints
make help # Show available commands
-
Port 80 permission denied
- Use higher port (8080, 3000) for port forwarding
kubectl port-forward -n ingress-nginx service/ingress-nginx-controller 8080:80
-
Connection refused
- Ensure minikube is running:
minikube status
- Check pod status:
kubectl get pods
- Verify service:
kubectl get services
- Ensure minikube is running:
-
Host not found
- Add to /etc/hosts:
127.0.0.1 flask-app.local
- Use
-H "Host: flask-app.local"
in curl commands
- Add to /etc/hosts:
# Check deployment status
kubectl describe deployment flask-app
# Check pod logs
kubectl logs -l app=flask-app
# Check service endpoints
kubectl get endpoints flask-app
# Check ingress status
kubectl describe ingress flask-app-ingress
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Flask framework for the web framework
- Kubernetes for container orchestration
- minikube for local Kubernetes development
- Docker for containerization
Happy coding!