Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

155 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Time API Infrastructure Project

A cloud-native infrastructure project that deploys a simple World Clock Dashboard App to Azure Kubernetes Service (AKS)

The branch is for a more secure setup that restricts access to the AKS API server to a private VNet and uses a self-hosted GitHub Actions runner (within the same VNet) when there is a need to access it during CI/CD, eliminating the need to expose the cluster publicly.

This project is designed to demonstrate cloud engineering skills, including Infrastructure as Code (IaC), containerization, orchestration, monitoring and CI/CD automation.

πŸ—οΈ Architecture Overview

This project demonstrates a deployment of a simple API written in Python that returns the current UTC time of various locations around the world and a Vite frontend that displays them. It includes the use of the following:

  • API: Simple Flask API that returns current UTC time for various locations
  • Frontend: Vite application that displays the time data from the API
  • Containerisation: Docker for test-running and building the application container
  • Orchestration: Kubernetes for container orchestration
  • Cloud Infrastructure: Azure Kubernetes Service (AKS) for hosting the application
  • Networking: Azure Load Balancer and NGINX Ingress Controller for traffic management
  • CI/CD: GitHub Actions for automated deployment
  • Monitoring: Microsoft Log Analytics and kube-prometheus-stack (Prometheus and Grafana) for monitoring and observability
  • Security: GitHub Secrets, Network Security Group, Microsoft Azure Role Based Access Control, Network policies and isolating API Server access to a private VNet
  • Infrastructure as Code: The use of Terraform and Bash scripts running Microsoft Azure CLI and GitHub CLI commands for scripting/automation purposes
  • SSL/TLS:

πŸ“‹ Prerequisites

Before getting started, ensure you have installed and configured the following tools and services for your local machine and CI/CD environment:

Required Tools

Required Accounts & Services

  • Microsoft Azure Account with a student or paid subscription and appropriate permissions (an Owner role is preferable)
  • Docker Hub Account as a container registry
  • GitHub Repository for version control and CI/CD automation

Required Permissions

  • Microsoft Azure subscription with Owner role is preferable
  • Permission to create and manage Microsoft Azure AD groups and service principals

πŸš€ Quick Start

1. Fork and Clone the Repository

Fork this repository to your GitHub account (to fork other branches too, untick the copy only main branch option)

2. Clone the fork to your local machine or use Codespaces

git clone https://github.com/VictortheGreat7/self-hosted-full-stack.git YOUR-CHOSEN-REPO-NAME/
cd YOUR-CHOSEN-REPO-NAME

3. Set Up Microsoft Azure Service Principal

Create a service principal for GitHub Actions:

# Login to Microsoft Azure
az login --use-device-code

# Update the service_principal.sh script variables with your details before running it. You can find it here:
cd terraform/bash_scripts

# Run the service principal creation script
chmod +x service_principal.sh
./service_principal.sh

Required Variables:

  • SERVICE_PRINCIPAL_NAME: Choose a unique name
  • SCOPE: Your Microsoft Azure subscription ID
  • GITHUB_NAME: Your GitHub username
  • GITHUB_REPO: Your repository name

A secrets.yaml file will be created in whatever repository you execute the script, with the details of the created service principal

4. Configure GitHub Secrets

Set up the required GitHub repository secrets. You can use the provided script:

chmod +x gh_secret.sh
# Edit the script with your secret values first
./gh_secret.sh

Required Values/Secrets:

  • AZURE_CREDENTIALS: JSON object with Microsoft Azure service principal details
  • ARM_CLIENT_ID: Microsoft Azure service principal client ID
  • ARM_CLIENT_SECRET: Microsoft Azure service principal client secret
  • ARM_SUBSCRIPTION_ID: Your Microsoft Azure subscription ID
  • ARM_TENANT_ID: Your Microsoft Azure tenant ID
  • MY_USER_OBJECT_ID: Your Microsoft Azure AD user object ID
  • RUNNER_TOKEN: GitHub Actions runner token for the self-hosted runner. Find out how here. The cloud-init file for the runner vm already follows the instructions listed. All you need to do is copy the time-limited token with ./config.sh in the configure step. Just make sure you select Linux x64 architecture Visual of Token Location
  • DOCKER_USERNAME: Docker Hub username
  • DOCKER_PASSWORD: Docker Hub password/token

Note: For the needed Microsoft Azure account details, check secrets.yaml created by the Service Principal creation step.

5. Update Configuration

Edit the following files with your specific details:

terraform/bash_scripts/pre-apply.sh

# Update these variables
RESOURCE_GROUP_NAME="" # Change this
STORAGE_ACCOUNT_NAME="" # Choose a unique name
CONTAINER_NAME="tfstate"
REGION="eastus"

terraform/backend.tf

terraform {
  backend "azurerm" {
    resource_group_name = "" # Update this
    storage_account_name = ""  # Update this
    container_name = "tfstate"
    key = "terraform.tfstate"
  }
}

terraform/deploy/providers.tf

provider "azurerm" {
  features {}

  subscription_id = "YOUR-SUBSCRIPTION-ID"  # Update this
}

6. Deploy Infrastructure

Uncomment or add on-push trigger in .github/workflows/build.yaml and ensure other workflows will not trigger on push (except the app is up and you need to apply changes will integrate.yaml).

# on:
#   push:
#     branches:
#       - main

Push your changes to trigger the GitHub Actions workflow:

git add .
git commit -m "[YOUR COMMIT MESSAGE]"
git push origin main

The workflow will:

  1. Build and test the Docker image
  2. Push the image to Docker Hub
  3. Provision part of Microsoft Azure infrastructure needed including the self-hosted runner with Terraform using a GitHub hosted Actions runner
  4. Provision the rest of the infrastructure using the self-hosted runner
  5. Deploy the application to AKS

Important: You can only connect to the cluster rom your self-hosted runner. There is also an ssh command in the outputs printed after a successful terraform apply that you can use to connect to the self-hosted runner for any sort of troubleshooting or the other.

πŸ—οΈ Manual Deployment (Alternative)

If you prefer to deploy manually and you have updated the configuraton as specified above:

Main Branch

1. Create or confirm existing Terraform Backend

cd terraform/bash_scripts
chmod +x pre-apply.sh
./pre-apply.sh

2. Provision Infrastructure and Self-Hosted Runner

cd ../
terraform init
terraform apply

3. Deploy Application with Self-Hosted Runner

# Move microservice configs
cd deploy/
mv * ../
cd ../

# Create SSH keys for the self-hosted runner
ssh-keygen -t rsa -b 4096 -C "github-selfhosted-runner" -f ssh_keys/id_rsa

# Apply microservice deployment
terraform init
terraform apply

4. Test API endpoint

In the outputs printed after a successful terraform apply, you will see the ingress IP of the deployed API. You can test it using:

curl http://<your-ingress-ip>

or with just http://<your-ingress-ip> in your browser.

Important: You can only connect to the cluster in this scenario from you self-hosted runner. There is also an ssh command in the outputs printed after a successful terraform apply that you can use to connect to the self-hosted runner for any sort of troubleshooting.

πŸ”§ Local Application/Image Building and/or Testing

Running the Application Locally

# Backend
cd backend
pip install -r requirements.txt
python app.py

# Frontend (in a new terminal)
cd frontend
npm install
npm run dev

Access the application Frontend: http://localhost:5173 (or the port shown in terminal) API: http://localhost:5000/world-clocks

Building and Testing Docker Image

# Build the backend image
cd backend
docker build -t kronos:backend .
docker run -d -p 5000:5000 --name kronos-backend-local kronos:backend

# Build the frontend image
cd frontend
docker build -t kronos:frontend .
docker run -d -p 5173:80 --name kronos-frontend-local kronos:frontend

# Test the endpoint
curl http://localhost:5000/world-clocks
# and access http://localhost:80 in your browser to check frontend

# Clean up
docker stop kronos-frontend-local
docker stop kronos-backend-local
docker rm kronos-frontend-local
docker rm kronos-backend-local

πŸ“Š Monitoring and Observability

The project includes comprehensive monitoring:

  • Grafana Dashboard: Visual monitoring interface
  • Prometheus: Metrics scraping
  • Microsoft Azure Log Analytics: Centralized logging

Access your Grafana dashboard through the terraform ingress outputs after deployment.

πŸ”’ Security Features

  • Network Policies: Restrict namespace communication to an as-needed basis
  • GitHub Secrets: Secure storage of sensitive tokens
  • Service Principals: Secure access for GitHub Actions
  • Microsoft Azure RBAC: Role-based access control
  • Network Security Groups: Microsoft Azure Network-level security
  • Private Subnets: Isolated network segments
  • Private Cluster: Restricted access to the Kubernetes API server

πŸ› οΈ Troubleshooting

Useful Commands

# Get AKS credentials
az aks get-credentials --resource-group RESOURCE_GROUP --name CLUSTER_NAME

# Kubernetes Authentication
kubelogin convert-kubeconfig -l azurecli

# Check application status
kubectl get all -n time-api

# View logs
kubectl logs -f deployment/time-api -n time-api

🧹 Cleanup

To destroy all Terraform resources:

Using GitHub Actions: Trigger the "Destroy Infrastructure" workflow manually from the GitHub Actions tab.

Manual Cleanup:

cd terraform
terraform destroy

Note: This will permanently delete all Microsoft Azure resources created with terraform apply.

πŸ“ Project Structure

self-hosted-full-stack/
β”œβ”€β”€ .github/
β”‚   └── workflows/                    # GitHub Actions CI/CD pipelines
β”‚       β”œβ”€β”€ build.yaml                # Main deployment workflow
β”‚       β”œβ”€β”€ destroy.yaml              # Resource cleanup workflow
β”‚       └── integrate.yaml            # Subsequent infrastructure/deployment changes workflow
β”œβ”€β”€ backend/                          # Flask API application
β”‚   β”œβ”€β”€ app.py                        # Flask application entry point
β”‚   β”œβ”€β”€ Dockerfile                    # Backend container image definition
β”‚   └── requirements.txt              # Python dependencies
β”œβ”€β”€ frontend/                         # Vite React application
β”‚   β”œβ”€β”€ public/                       # Static assets
β”‚   β”œβ”€β”€ src/                          # React source code
β”‚   β”‚   β”œβ”€β”€ assets/                   # Images and other assets
β”‚   β”‚   β”œβ”€β”€ components/               # React components
β”‚   β”‚   β”‚   β”œβ”€β”€ CityCard.css          # City card styles
β”‚   β”‚   β”‚   β”œβ”€β”€ CityCard.jsx          # City card component
β”‚   β”‚   β”‚   β”œβ”€β”€ ClockOrbit.css        # Clock orbit animation styles
β”‚   β”‚   β”‚   β”œβ”€β”€ ClockOrbit.jsx        # Clock orbit component
β”‚   β”‚   β”‚   β”œβ”€β”€ Dashboard.css         # Dashboard styles
β”‚   β”‚   β”‚   └── Dashboard.jsx         # Dashboard component
β”‚   β”‚   β”œβ”€β”€ App.css                   # Application styles
β”‚   β”‚   β”œβ”€β”€ App.jsx                   # Main App component
β”‚   β”‚   β”œβ”€β”€ index.css                 # Global styles
β”‚   β”‚   └── main.jsx                  # Application entry point
β”‚   β”œβ”€β”€ .env                          # Local environment variables
β”‚   β”œβ”€β”€ .env.production               # Production environment variables
β”‚   β”œβ”€β”€ .gitignore                    # Frontend-specific Git ignore rules
β”‚   β”œβ”€β”€ Dockerfile                    # Frontend container image definition
β”‚   β”œβ”€β”€ eslint.config.js              # ESLint configuration
β”‚   β”œβ”€β”€ index.html                    # HTML template
β”‚   β”œβ”€β”€ nginx.conf                    # NGINX configuration for production
β”‚   β”œβ”€β”€ package.json                  # Node.js dependencies and scripts
β”‚   β”œβ”€β”€ README.md                     # Frontend documentation
β”‚   └── vite.config.js                # Vite build configuration
β”œβ”€β”€ screenshots/                      # Project screenshots and documentation images
β”œβ”€β”€ terraform/                        # Infrastructure as Code (IaC) and automation
β”‚   β”œβ”€β”€ bash_scripts/                 # Helper automation scripts
β”‚   β”‚   β”œβ”€β”€ get-aks-cred.sh           # Script to retrieve AKS credentials
β”‚   β”‚   β”œβ”€β”€ gh_secret.sh              # GitHub secrets management script
β”‚   β”‚   β”œβ”€β”€ pre-apply.sh              # Pre-apply script for Terraform backend setup
β”‚   β”‚   └── service_principal.sh      # Azure service principal creation script
β”‚   β”œβ”€β”€ deploy/                       # Application deployment Terraform modules
β”‚   β”‚   β”œβ”€β”€ data.tf                   # Data sources for existing resources
β”‚   β”‚   β”œβ”€β”€ deploy.tf                 # Application deployment resources
β”‚   β”‚   β”œβ”€β”€ ingress.tf                # Ingress controller and routing configuration
β”‚   β”‚   β”œβ”€β”€ monitoring.tf             # Monitoring and observability stack (Prometheus/Grafana)
β”‚   β”‚   β”œβ”€β”€ netpolicy.tf              # Kubernetes network policies
β”‚   β”‚   β”œβ”€β”€ permissions.tf            # Azure RBAC permissions configuration
β”‚   β”‚   └── provision.tf              # Kubernetes cluster provisioning resources
β”‚   β”œβ”€β”€ ssh_keys/                     # SSH keys for self-hosted runner access
β”‚   β”‚   β”œβ”€β”€ id_rsa                    # Private SSH key (git-ignored)
β”‚   β”‚   └── id_rsa.pub                # Public SSH key
β”‚   β”œβ”€β”€ .terraform.lock.hcl           # Terraform dependency lock file
β”‚   β”œβ”€β”€ backend.tf                    # Terraform remote state backend configuration
β”‚   β”œβ”€β”€ cloud-init.yaml.tpl           # Cloud-init template for self-hosted GitHub runner setup
β”‚   β”œβ”€β”€ main.tf                       # Main Terraform entry point (AKS cluster, resource groups, runner VM)
β”‚   β”œβ”€β”€ network.tf                    # Azure networking configuration (VNet, subnets, NSG)
β”‚   β”œβ”€β”€ outputs.tf                    # Terraform output values
β”‚   β”œβ”€β”€ providers.tf                  # Terraform provider configurations
β”‚   β”œβ”€β”€ terraform.tfvars.json         # Terraform variable values (auto-generated from GitHub secrets)
β”‚   └── variables.tf                  # Terraform variable definitions
β”œβ”€β”€ .gitignore                        # Root Git ignore rules
β”œβ”€β”€ Dockerfile                        # Legacy/root Dockerfile (if applicable)
β”œβ”€β”€ gh_secret.sh                      # Root-level GitHub secrets setup script
└── README.md                         # Project documentation

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature-name
  3. Make your changes and commit: git commit -am 'Add feature'
  4. Push to the branch: git push origin feature-name
  5. Submit a pull request

πŸ†˜ Support

If you encounter issues:

  1. Check the Troubleshooting section
  2. Review the GitHub Actions logs
  3. Check Microsoft Azure portal for resource status
  4. Open an issue in this repository

πŸ”— Useful Links


Disclaimer: This project is designed for learning and demonstration purposes. For production use, consider additional security hardening, cost optimization, and compliance requirements specific to your organization.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages