Using kubernetes serving.knative.dev with postgresql.cnpg.io
TypeScriptKnative is a Kubernetes-based platform that provides a set of middleware components for building modern, source-centric, and container-based applications that can run anywhere. It primarily focuses on simplifying the deployment and management of serverless workloads. Similarly, the PostgreSQL Operator for Kubernetes by Crunchy Data (cnpg.io) makes it easy to deploy and manage PostgreSQL clusters within a Kubernetes environment.
In this program, you're going to see how to use Knative Serving to manage serverless workloads that might interact with a PostgreSQL database managed by the PostgreSQL Operator for Kubernetes. To accomplish this task, we will define two main resources:
-
Knative Service: A Knative Service (
serving.knative.dev
) manages the entire lifecycle of our serverless workload. It will automatically create a route, a configuration, and a new revision for each change to the service, along with autoscaling (including scaling down to zero when not in use). -
PostgreSQL Database: We will be deploying a PostgreSQL database using the
postgresql.cnpg.io
operator. This creates a managed PostgreSQL cluster within our Kubernetes environment that the application can interact with.
For the sake of this program, we assume that the Knative Serving and PostgreSQL Operator components are already installed in your Kubernetes cluster.
Detailed Steps:
-
PostgreSQL Database Setup: Declare a PostgreSQL database instance. You would typically provide parameters such as the database size, version, and configuration specifics. In this case, we will use placeholder values as the actual configuration can vary heavily based on your needs.
-
Knative Service Deployment: Define a Knative service that represents our application. We will point out where the application's code or container image would be specified. Knative will take care of deploying the container and autoscaling, including scaling down to zero when the workload is idle.
Let's dive into the code:
import * as k8s from "@pulumi/kubernetes"; // Define a new PostgreSQL database instance using the PostgreSQL Operator. const postgresDatabase = new k8s.apiextensions.CustomResource("postgres-database", { apiVersion: "postgresql.cnpg.io/v1", kind: "Postgresql", spec: { // Specify database version, size, name, and other relevant parameters. // These values need to be configured according to your specific requirements. version: "12", storageSize: "10Gi", users: [{name: "appuser", db: "appdb", options: ["createrole", "createdb"]}], databases: [{name: "appdb"}], // If you have a custom image for your PostgreSQL image: "custom-postgres-image:latest", } }); // Define a Knative service that represents our serverless application workload. const knativeService = new k8s.apiextensions.CustomResource("knative-app-service", { apiVersion: "serving.knative.dev/v1", kind: "Service", metadata: { name: "app-service", }, spec: { // Specify the details of the container image for your application. // This container should have the business logic for your application, which can connect to a PostgreSQL database. template: { spec: { containers: [ { image: "gcr.io/knative-samples/helloworld-go", // replace with your container image env: [ { name: "POSTGRES_HOST", value: "postgresql", // This would typically match your PostgreSQL service in the Kubernetes cluster }, { name: "POSTGRES_USER", value: "appuser", }, { name: "POSTGRES_DB", value: "appdb", }, // Set other necessary environment variables needed for the application. ], }, ], }, }, }, }); // Here we can export some values that will be helpful to interact with our services. export const postgresDatabaseName = postgresDatabase.metadata.name; export const knativeServiceName = knativeService.metadata.name; export const knativeServiceUrl = knativeService.status.url; // This will give us the URL to access our Knative app.
Note: Before running the above program, ensure that:
- Pulumi is installed and configured for TypeScript.
- You have access to a Kubernetes cluster with Knative Serving and PostgreSQL Operator installed.
- You have configured your Pulumi program to connect to your Kubernetes cluster (e.g., by setting your
KUBECONFIG
environment variable).
This Pulumi program defines resources declaratively, which means you describe the desired state of the resources and Pulumi makes the necessary changes to achieve that state in the target environment. When you run the program with
pulumi up
, Pulumi will deploy the PostgreSQL instance and Knative application service according to the specifications provided in theCustomResource
definitions.Remember to replace placeholders such as image URLs or configuration parameters with actual values that suit your application requirements. The exported values can be used to interact with your deployed resources, for example, to access your Knative application via the provided URL.
-