1. Answers
  2. Setting up Kubernetes-hosted PostgreSQL with Ingress

How do I set up Kubernetes-hosted PostgreSQL with Ingress?

In this guide, we will set up a PostgreSQL instance on a Kubernetes cluster using the postgresql.cnpg.io custom resource and expose it via an Ingress resource for secure access. We will define the necessary Kubernetes resources including the PostgreSQL instance, a Service to expose it, and an Ingress to manage external access.

import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

// Define the PostgreSQL instance
const postgresInstance = new k8s.apiextensions.CustomResource("postgresInstance", {
    apiVersion: "postgresql.cnpg.io/v1",
    kind: "Cluster",
    metadata: {
        name: "my-postgres",
    },
    spec: {
        instances: 1,
        imageName: "postgres:13",
        storage: {
            size: "1Gi",
        },
    },
});

// Define a Service to expose the PostgreSQL instance
const postgresService = new k8s.core.v1.Service("postgresService", {
    metadata: {
        name: "postgres-service",
    },
    spec: {
        selector: {
            app: "my-postgres",
        },
        ports: [
            {
                port: 5432,
                targetPort: 5432,
            },
        ],
    },
});

// Define an Ingress to expose the PostgreSQL service
const postgresIngress = new k8s.networking.v1.Ingress("postgresIngress", {
    metadata: {
        name: "postgres-ingress",
        annotations: {
            "nginx.ingress.kubernetes.io/rewrite-target": "/",
        },
    },
    spec: {
        rules: [
            {
                host: "postgres.example.com",
                http: {
                    paths: [
                        {
                            path: "/",
                            pathType: "Prefix",
                            backend: {
                                service: {
                                    name: "postgres-service",
                                    port: {
                                        number: 5432,
                                    },
                                },
                            },
                        },
                    ],
                },
            },
        ],
    },
});

export const ingressUrl = postgresIngress.metadata.apply(m => `http://${m?.name}`);

Key Points

  • A PostgreSQL instance is created using the postgresql.cnpg.io custom resource.
  • A Kubernetes Service is defined to expose the PostgreSQL instance internally.
  • An Ingress resource is set up to manage external access to the PostgreSQL service, routing traffic to the appropriate service and port.

Conclusion

We have successfully set up a PostgreSQL instance on Kubernetes, exposed it via a Service, and configured an Ingress resource to handle external access. This setup allows secure and managed access to the PostgreSQL database from outside the Kubernetes cluster.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up