Deploy the python-fastapi-postgres helm chart on Digital Ocean Kubernetes Service
TypeScriptTo deploy a Python FastAPI application with a PostgreSQL database using a Helm chart on Digital Ocean Kubernetes Service (DOKS), you would perform the following steps:
- Create a Digital Ocean Kubernetes Cluster.
- Deploy the PostgreSQL database inside the cluster.
- Install the Helm chart which encapsulates the FastAPI application and is configured to use the PostgreSQL database.
Here's a detailed walkthrough of the Pulumi program written in TypeScript that accomplishes these tasks:
Step 1: Set up a new Kubernetes cluster on Digital Ocean
Using the
digitalocean.KubernetesCluster
resource, we instantiate a new DOKS cluster. We need to define relevant properties like the region, version, node size, and the number of nodes. Make sure the region you select supports Kubernetes, and theversion
field is populated with a valid Kubernetes version available on Digital Ocean at the time you run this code.Step 2: Deploy PostgreSQL database
The
kubernetes.helm.sh/v3.Chart
resource is used to deploy a Helm chart. We'll assume there's a Helm chart namedpython-fastapi-postgres
that you have access to, and we'll install it to our cluster. This chart includes the definition for a PostgreSQL database that our application will use.Step 3: Install the FastAPI application with Helm
To deploy the FastAPI application, we'll use the same
kubernetes.helm.sh/v3.Chart
resource and specify the chart name along with any required values. These values typically include configuration parameters for your FastAPI and PostgreSQL deployment.Below is the program that puts these steps into practice.
import * as pulumi from "@pulumi/pulumi"; import * as digitalocean from "@pulumi/digitalocean"; import * as k8s from "@pulumi/kubernetes"; const config = new pulumi.Config(); // Step 1: Create a Digital Ocean Kubernetes (DOKS) cluster const cluster = new digitalocean.KubernetesCluster("fastapi-cluster", { region: "nyc1", // Replace with your desired region version: "latest", // Use a specific version or "latest" nodePool: { name: "default", size: "s-2vcpu-2gb", // The size of the Droplet to use for workers nodeCount: 2, // Number of nodes to have in this node pool. }, }); // Step 2: Deploy PostgreSQL using a Helm chart const postgresChart = new k8s.helm.v3.Chart("postgres", { chart: "postgresql", fetchOpts: { repo: "https://charts.bitnami.com/bitnami", // Replace with the PostgreSQL Helm chart repository URL }, values: { postgresqlUsername: "fastapi", // Replace with the username for PostgreSQL postgresqlPassword: "fastapi-password", // Replace with the password for PostgreSQL postgresqlDatabase: "fastapidb", // Replace with the desired database name }, }, { provider: new k8s.Provider("k8s-provider", { kubeconfig: cluster.kubeConfigs[0].rawConfig }) }); // Step 3: Install the FastAPI application using Helm const fastapiChart = new k8s.helm.v3.Chart("fastapi", { chart: "python-fastapi-postgres", // The name of the chart values: { // Here we would define any necessary values required by the Helm chart. // For instance, you might need to refer to credentials for the database, // resource requirements, service type definitions, and more. }, fetchOpts: { repo: "http://your-helm-chart-repo.com/charts", // Replace with your actual Helm chart repository URL }, }, { provider: new k8s.Provider("k8s-provider", { kubeconfig: cluster.kubeConfigs[0].rawConfig }) }); // Export the public IP to access the FastAPI service export const fastapiServiceIp = fastapiChart.getResourceProperty("v1/Service", "fastapi", "status").apply(status => status.loadBalancer.ingress[0].ip);
In this program:
- We first create a Kubernetes cluster on Digital Ocean using Pulumi's Digital Ocean provider.
- Next, we install PostgreSQL using the official Helm chart from Bitnami's Helm repository. Configuring the database credentials and the name according to your application needs.
- After setting up the database, we deploy the FastAPI application using its provided Helm chart. Here, you would need to replace placeholders like the repo URL and chart-specific configurations with your actual values.
- An output variable
fastapiServiceIp
is then exported, which will give you the public IP address for the FastAPI service. This assumes that your Helm chart defines a LoadBalancer service for FastAPI. If it's a different kind of service (like ClusterIP or NodePort), you would extract the appropriate details for access.
After running this Pulumi program, you will have a Digital Ocean Kubernetes Service cluster running your Python FastAPI application with PostgreSQL.
Remember to replace placeholders with actual values that pertain to your deployment.
When you are ready to deploy, run
pulumi up
in your terminal within the directory where this Pulumi program is saved. Pulumi CLI will execute the program and provision the resources as defined. After successful deployment, Pulumi will output the service IP for the FastAPI.