diff --git a/.env-k8s-sample b/.env-k8s-sample new file mode 100644 index 0000000..1254231 --- /dev/null +++ b/.env-k8s-sample @@ -0,0 +1,23 @@ +PROJECT_ID="ncpi-rti-p01-007-ohsu" +REGION="us-central1" # Choose your preferred region +CLUSTER_NAME="my-hapi-fhir-cluster" +NUM_NODES=3 # Adjust as needed + +ZONE="" +NAMESPACE="fhir" # helm namespace +CLOUD_SQL_INSTANCE="" +DATABASE_NAME="" +DATABASE_USER="" +DATABASE_PASSWORD="" +CHART_REPO="https://hapifhir.github.io/hapi-fhir-jpaserver-starter/" +CHART_NAME="hapi-fhir-jpaserver" + +# Machine Type for the Cloud SQL (Postgres) instance +DATABASE_TIER="db-custom-1-3840" +DATABASE_VERSION=POSTGRES_14 + +# ingress, letsencrypt +NAMESPACE="fhir" +EMAIL="your-email@example.com" +DOMAIN1="hapi.example.com" +DOMAIN2="google-fhir.example.com" diff --git a/README-hapi-k8s.md b/README-hapi-k8s.md new file mode 100644 index 0000000..41e2750 --- /dev/null +++ b/README-hapi-k8s.md @@ -0,0 +1,54 @@ +# Deploying HAPI FHIR JPA Server on GKE with Cloud SQL + +This script deploys the HAPI FHIR JPA server starter on a Google Kubernetes Engine (GKE) cluster using Cloud SQL for PostgreSQL as the database. + +## Prerequisites + +* A Google Cloud project with billing enabled. +* A GKE cluster created. +* A Cloud SQL for PostgreSQL instance created. +* A service account with the necessary permissions to access Cloud SQL. +* `kubectl` and `helm` installed and configured. +* The `hapi-fhir-jpaserver` Helm chart added to your Helm repositories. (If using the repo from examples, make sure its added) + +Before running: + +* Install gcloud: Ensure the Google Cloud SDK is installed and configured with the correct credentials for your project ( ncpi-rti-p01-007-ohsu ). +* Enable Kubernetes Engine API: Make sure the Kubernetes Engine API is enabled in your project. +* Choose a Region: Select an appropriate region ( REGION ) for your cluster. Consider latency and availability requirements. +* Node Count: Adjust NUM_NODES based on your application's resource needs. Start small and scale up as needed. + +* Important Considerations: + +* Networking: This script uses the default VPC network. For more complex networking scenarios (e.g., private clusters), you'll need to specify additional network parameters. +* Node Pools: This script uses cluster autoscaling. For more fine-grained control over node configuration (machine type, etc.), you'll need to explicitly create node pools using gcloud container node-pools create . +* Security: Consider adding appropriate security settings (e.g., IP allowlisting, Kubernetes Roles and RBAC) to secure your cluster. + +After running this script successfully, you can use gcloud container clusters get-credentials to authenticate with your newly created cluster. Remember to always review the Google Cloud documentation for best practices and more advanced configuration options. + +## Configuration + +Before running the script, update the `deploy_hapi_fhir.sh` script with your project-specific values: + +* `PROJECT_ID`: Your Google Cloud project ID. +* `CLUSTER_NAME`: The name of your GKE cluster. +* `ZONE`: The zone of your GKE cluster. +* `NAMESPACE`: The Kubernetes namespace for the deployment (default: `fhir`). +* `CLOUD_SQL_INSTANCE`: The connection name of your Cloud SQL instance. +* `DATABASE_NAME`: The name of your PostgreSQL database. +* `DATABASE_USER`: The username for your PostgreSQL database. +* `DATABASE_PASSWORD`: The password for your PostgreSQL database. + + +## Deployment + +Run the script: `./deploy_hapi_k8s.sh`. + + +## Verification + +After the script completes, verify the deployment: + +```bash +kubectl get pods -n fhir +``` diff --git a/create_hapi_k8s.sh b/create_hapi_k8s.sh new file mode 100755 index 0000000..da42678 --- /dev/null +++ b/create_hapi_k8s.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +# https://bertvv.github.io/cheat-sheets/Bash.html#writing-robust-scripts-and-debugging +set -euo pipefail + +# Check if required environment variables are set +# See .env-k8s-sample + +# --- Configuration --- +# see .env-k8s-sample file +: "${PROJECT_ID:?Need to set PROJECT_ID}" +: "${REGION:?Need to set REGION}" +: "${CLUSTER_NAME:?Need to set CLUSTER_NAME}" +: "${NUM_NODES:?Need to set NUM_NODES}" + + +# --- Create GKE Cluster --- + +gcloud container clusters create "${CLUSTER_NAME}" \ + --project="${PROJECT_ID}" \ + --region="${REGION}" \ + --cluster-autoscaling \ + --min-nodes=1 \ + --max-nodes="${NUM_NODES}" + + +# --- Check Cluster Status --- + +echo "Checking cluster status..." +gcloud container clusters describe "${CLUSTER_NAME}" \ + --project="${PROJECT_ID}" \ + --region="${REGION}" + + +echo "Cluster creation complete. You can now connect to your cluster using:" +echo "gcloud container clusters get-credentials ${CLUSTER_NAME} --region ${REGION} --project ${PROJECT_ID}" + diff --git a/create_hapi_k8s_ingress.sh b/create_hapi_k8s_ingress.sh new file mode 100644 index 0000000..d29277b --- /dev/null +++ b/create_hapi_k8s_ingress.sh @@ -0,0 +1,160 @@ +#!/bin/bash + +# https://bertvv.github.io/cheat-sheets/Bash.html#writing-robust-scripts-and-debugging +set -euo pipefail + +# Check if required environment variables are set +# See .env-k8s-sample + +# --- Configuration --- +# see .env-k8s-sample file +: "${NAMESPACE:?Need to set NAMESPACE}" +: "${EMAIL:?Need to set EMAIL}" +: "${DOMAIN1:?Need to set DOMAIN1}" +: "${DOMAIN2:?Need to set DOMAIN2}" + +# Create the namespace +kubectl create namespace $NAMESPACE + +# Install cert-manager +helm repo add jetstack https://charts.jetstack.io +helm repo update +kubectl create namespace cert-manager +helm install cert-manager jetstack/cert-manager --namespace cert-manager --version v1.11.0 --set installCRDs=true + +# Create the ClusterIssuer +cat < /dev/null; then + echo "User is authenticated with gdcloud" +else + echo "Logging in with gcould auth application-default login..." + gcloud auth application-default login +fi + +# 2. Create Cloud SQL instance +gcloud sql instances create ${CLOUD_SQL_INSTANCE} \ + --database-version=${DATABASE_VERSION} \ + --region=us-central1 \ + --tier=${DATABASE_TIER} \ + --activation-policy=ALWAYS \ + --database-flags="cloudsql.iam_authentication=on" #Consider Private IP for better security + +# 3. Create a database within the instance +gcloud sql databases create ${DATABASE_NAME} --instance=${CLOUD_SQL_INSTANCE} + +# 4. Create a user +gcloud sql users create hapi_user --instance=${CLOUD_SQL_INSTANCE} --password=${DATABASE_PASSWORD} + +# 5. Connect to the instance and grant privileges +gcloud sql connect ${CLOUD_SQL_INSTANCE} --user=postgres <