-
Notifications
You must be signed in to change notification settings - Fork 1
Deployment
To deploy the Docker Image, we will use a Cloud Provider since it is free to host services on these platforms. We have previously used Google Cloud to deploy the server, however, you should be able to host on other platforms like AWS or Azure. Below are the instructions to host on Google Cloud
Before you start ensure the following:
- Access to the MongoDB Atlas related to the DS3 Developer Email
- Created a Google Cloud Platform Account related to the DS3 Developer Email
- Review the cost on GCP Price Calculator
-
Sign in to the DS3 Developers MongoDB Atlas, and check to see if the
aws-ds3-datathoncluster is active, if it not activate it -
Go to
Database Accessand edit the password for usergcp -
Autogenerate and save the PASSWORD for later use
-
Once a GCP Account has been created, go here to Create a Project
-
Enter the PROJECT_NAME as:
DS3 Datathon <YEAR> Leaderboard -
Enter the PROJECT_ID as:
ds3-datathon-<YEAR>-leaderboard -
Create the Project, and wait for the resources to be provisioned
-
Navigate over to the Billing and complete the setup there
-
Setup a Budget and Alert to ensure that an alert is sent if you are billed for resources
-
Once Billing is setup, navigate to APIs & Services to initiate the following services:
- Secret Manager API
- Cloud Run API
- Cloud Scheduler API
Much of the below guide will make use of the cli, so start here and then proceed:
-
Follow the step-by-step quick start guide on the official docs to install
gcloudSDK -
After running
gcloud init, select the above project from the list
Before you deploy any services you must create a GCP Service Account its role is to manage the deployment and ensure you are following the Least Privileges Principle.
-
Create the Service Account with SERVICE_ACCOUNT_NAME as
ds3-leaderboard-runnergcloud iam service-accounts create ds3-leaderboard-runner --display-name "DS3 Leaderboard Runner" -
After creating the Service Account, provide it with the permission for Secret Manager Secret Accessor
gcloud projects add-iam-policy-binding ds3-datathon-<YEAR>-leaderboard \ --member='serviceAccount:ds3-leaderboard-runner@ds3-datathon-<YEAR>-leaderboard.iam.gserviceaccount.com' \ --role='roles/secretmanager.secretAccessor'
-
After creating the Service Account, provide it with the permission for Cloud Run Invoker
gcloud projects add-iam-policy-binding ds3-datathon-<YEAR>-leaderboard \ --member='serviceAccount:ds3-leaderboard-runner@ds3-datathon-<YEAR>-leaderboard.iam.gserviceaccount.com' \ --role='roles/run.invoker'
Before we can deploy the Docker Image, we configure secrets to provide the environment variables during runtime
-
Create secret
KAGGLE_KEY:echo -n <KAGGLE_KEY> | gcloud secrets create KAGGLE_KEY --data-file='-'
-
Create secret
KAGGLE_USERNAME:echo -n <KAGGLE_USERNAME> | gcloud secrets create KAGGLE_USERNAME --data-file='-'
-
Create secret
MONGO_DB:echo -n prod | gcloud secrets create MONGO_DB --data-file='-'
-
Create secret
MONGO_URI:echo -n 'mongodb+srv://gcp:<PASSWORD>@azure-datathon.qzzfe1n.mongodb.net/?retryWrites=true&w=majority' | gcloud secrets create MONGO_URI --data-file='-'
Run the following command below to create an instance of the Leaderboard and store the URL for the next step
gcloud run deploy leaderboard \
--max-instances=1 \
--service-account='ds3-leaderboard-runner@ds3-datathon-<YEAR>-leaderboard.iam.gserviceaccount.com' \
--timeout=300 \
--memory=512Mi \
--port=8000 \
--set-secrets=KAGGLE_KEY=KAGGLE_KEY:latest,KAGGLE_USERNAME=KAGGLE_USERNAME:latest,MONGO_DB=MONGO_DB:latest,MONGO_URI=MONGO_URI:latest \
--image='docker.io/devds3/leaderboard:<TAG_NAME>' \
--cpu-boost \
--allow-unauthenticated \
--description='Updates the Datathon Public Leaderboard' \
--region='us-central1'To be able to constantly update the leaderboard, we make use of a service called Cloud Scheduler which send requests to an endpoint every so often
-
Create a CRONTAB will be used as the schedule, we have previously used the following here
-
Initialize App Engine
gcloud app create --region='us/central1' -
Create the Cloud Scheduler Job
gcloud scheduler jobs create http leaderboard-requester \ --schedule='<CRONTAB>' \ --uri=<URL>/public \ --attempt-deadline='3m' \ --description='Sends POST request to leaderboard server' \ --headers=Content-Type=application/json,User-Agent=Google-Cloud-Scheduler \ --http-method='post' \ --time-zone="EST" \ --message-body="{ \"competitions\": [ \"<COMPETITION_ID>\", \"<COMPETITION_ID>\", \"<COMPETITION_ID>\" ] }"
After the submissions are closed, the public leaderboard will no longer need to be updated and thus services can be removed from usage
-
Delete the Cloud Scheduler Job:
leaderboard-requestergcloud scheduler jobs delete leaderboard-requester -
Delete the Cloud Run Service:
leaderboardgcloud run services delete leaderboard --region='us/central1' -
Delete all environmental variables stores in Secrets Manager:
gcloud secrets delete KAGGLE_KEY gcloud secrets delete KAGGLE_USERNAME gcloud secrets delete MONGO_DB gcloud secrets delete MONGO_URI
-
Delete the Service Account called: ds3-leaderboard-runner@ds3-datathon--leaderboard.iam.gserviceaccount.com
gcloud iam service-accounts delete ds3-leaderboard-runner@ds3-datathon-<YEAR>-leaderboard.iam.gserviceaccount.com
-
Delete the Project with ds3-datathon--leaderboard
gcloud projects delete ds3-datathon-<YEAR>-leaderboard