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.
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 |
kubectl apply -f pv.yaml |
kubectl apply -f postgresql-deployment.yaml |
helm list kubectl get pods |
- 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 |
- 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.
- 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 |
- 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 |
- 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.
- 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.