Skip to content

KieuVanHanh/udacityproject3

Repository files navigation

Coworking Space Service Extension

The Coworking Space Service is a set of APIs that enables users to request one-time tokens and administrators to authorize access to a coworking space. This service follows a microservice pattern and the APIs are split into distinct services that can be deployed and managed independently of one another.

1: Set Up a Postgres Database with Helm Chart

Pre-requisites:

  • Have Kubernetes cluster ready.
  • Have kubectl installed and configured to interact with your cluster.
  • Have Helm installed.

Instructions: 1 Configure a Database for the Service 1.1 Create PersistentVolume

kubectl apply -f pv.yaml
1.2 Create PersistentVolumeClaim
kubectl apply -f pv.yaml
1.3 Create Postgres Deployment
kubectl apply -f postgresql-deployment.yaml
2 Verify the database:
helm list
kubectl get pods
3. Port Forwarding
  • Use kubectl port-forward to forward PostgreSQL service port (5432) to local machine.
  • Connect to the database using psql with forwarded port.
kubectl port-forward --namespace default svc/my-postgresql2 5432:5432 &
PGPASSWORD="$POSTGRES_PASSWORD" psql --host 127.0.0.1 -U postgres -d postgres -p 5432 < db/1_create_tables.sql
  1. Seed Database
  • Run SQL seed files located in the db/ directory to create tables and populate them with data.
kubectl port-forward --namespace default svc/my-postgresql2 5432:5432 &
PGPASSWORD="$POSTGRES_PASSWORD" psql --host 127.0.0.1 -U postgres -d postgres -p 5432 < db/2_seed_users.sql>
kubectl port-forward --namespace default svc/my-postgresql2 5432:5432 &
PGPASSWORD="$POSTGRES_PASSWORD" psql --host 127.0.0.1 -U postgres -d postgres -p 5432 < db/2_seed_token.sql>
  • Testing the validity of the populated data.

2: Local Testing of Analytics Application

  • Navigate to the analytics/app directory.
  • Install requirements for the analystics application.
python -m pip install --upgrade pip
pip install -r analytics/app/requirements.txt
  • Run the analytics application locally:
DB_USERNAME=hanhkv DB_PASSWORD=hanh1998 python analystics/app/app.py
  • Test the application using curl commands.
curl http://127.0.0.1:5153/api/reports/daily_usage
curl http://127.0.0.1:5153/api/reports/user_visits

3: Docker Image Creation and Local Deployment

  • Build the Docker image locally: Access to docker file
cd analystics
docker build -t coworking:v1 .
  • Run the Docker container locally.
docker run -e DB_USERNAME=hanhkv -e DB_PASSWORD=hanh1998 --network=host coworking:v1

4: AWS CodeBuild Pipeline

  • Configure CodeBuild to pull the image from the GitHub repository, build it, and push it to the existing ECR repository. Make sure to use the buildspecs.yml file.

5: Kubernetes Deployment

  • Deploy the application using the Deployment file, configMap file, and Secret file.
  • Set DB_HOST to the service name of the Kubernetes PostgreSQL pod.

##6: CloudWatch Agent Setup

  • Set up CloudWatch agent for monitoring.

Saving costs: Implement Autoscaling: Utilize Kubernetes Horizontal Pod Autoscaling (HPA) to automatically adjust the number of replicas based on resource metrics. This helps scale resources up during peak demand and down during low demand, optimizing costs.

Rightsize Resources: Regularly review and adjust resource allocations for your Kubernetes pods based on actual usage. Avoid overprovisioning, and set resource limits and requests appropriately.

Alerts on Costly Resources: Set up alerts based on cost thresholds to be notified when spending exceeds predefined limits. This allows for proactive cost management.

Remember to monitor your application's resource usage and adjust your instance types based on evolving requirements.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published