How do I deploy the postgresql:14-alpine Docker image on Kubernetes with TypeScript?
In this guide, we will deploy a PostgreSQL instance using the postgresql:14-alpine
Docker image on a Kubernetes cluster. We will use Pulumi with TypeScript to define and deploy our Kubernetes resources.
Key Points:
- Define a Kubernetes
Namespace
for isolation. - Create a
PersistentVolume
andPersistentVolumeClaim
for data persistence. - Deploy a
StatefulSet
to manage the PostgreSQL pods. - Expose the PostgreSQL service using a
Service
.
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";
// Create a namespace for our PostgreSQL deployment
const namespace = new k8s.core.v1.Namespace("postgresql-ns", {
metadata: {
name: "postgresql-ns",
},
});
// Define a PersistentVolume for PostgreSQL data
const persistentVolume = new k8s.core.v1.PersistentVolume("postgresql-pv", {
metadata: {
name: "postgresql-pv",
},
spec: {
capacity: {
storage: "10Gi",
},
accessModes: ["ReadWriteOnce"],
hostPath: {
path: "/mnt/data",
},
},
});
// Define a PersistentVolumeClaim to request the PersistentVolume
const persistentVolumeClaim = new k8s.core.v1.PersistentVolumeClaim("postgresql-pvc", {
metadata: {
namespace: namespace.metadata.name,
name: "postgresql-pvc",
},
spec: {
accessModes: ["ReadWriteOnce"],
resources: {
requests: {
storage: "10Gi",
},
},
},
});
// Deploy PostgreSQL using a StatefulSet
const statefulSet = new k8s.apps.v1.StatefulSet("postgresql", {
metadata: {
namespace: namespace.metadata.name,
name: "postgresql",
},
spec: {
serviceName: "postgresql",
replicas: 1,
selector: {
matchLabels: {
app: "postgresql",
},
},
template: {
metadata: {
labels: {
app: "postgresql",
},
},
spec: {
containers: [
{
name: "postgresql",
image: "postgres:14-alpine",
ports: [
{
containerPort: 5432,
name: "postgresql",
},
],
volumeMounts: [
{
name: "postgresql-storage",
mountPath: "/var/lib/postgresql/data",
},
],
env: [
{
name: "POSTGRES_DB",
value: "mydatabase",
},
{
name: "POSTGRES_USER",
value: "admin",
},
{
name: "POSTGRES_PASSWORD",
value: "adminpassword",
},
],
},
],
},
},
volumeClaimTemplates: [
{
metadata: {
name: "postgresql-storage",
},
spec: {
accessModes: ["ReadWriteOnce"],
resources: {
requests: {
storage: "10Gi",
},
},
},
},
],
},
});
// Expose the PostgreSQL service
const service = new k8s.core.v1.Service("postgresql", {
metadata: {
namespace: namespace.metadata.name,
name: "postgresql",
},
spec: {
ports: [
{
port: 5432,
targetPort: "postgresql",
},
],
selector: {
app: "postgresql",
},
},
});
Summary
In this guide, we deployed a PostgreSQL instance on a Kubernetes cluster using Pulumi with TypeScript. We created a namespace for isolation, defined a persistent volume and claim for data persistence, deployed PostgreSQL using a StatefulSet, and exposed it with a service. This setup ensures that our PostgreSQL database is highly available and persistent.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.