DEV Community

Youjung Hong
Youjung Hong

Posted on

How to Run PostgreSQL on my Local Machine.

I am currently making a project helping to memorize english vocabularies.

At first success, I set up PostgreSQL by using Deployment, Service, Secret. Then I realized this is not a best practice. Because it was stateless, which means all tables disappear if a pod is killed. So this is suitable for stateless service like a API server. As I will plan to deploy the API service, I will make some notes.

Structures: Deployment, Service, Secret

Image description

This is my setup details.

Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:13
        ports:
          - containerPort: 5432
        envFrom:
          - secretRef:
              name: postgres-secret
        volumeMounts:
          - name: postgres-storage
            mountPath: /var/lib/postgresql/data
        resources:
          requests:
            memory: "512Mi"
            cpu: "500m"
          limits:
            memory: "1Gi"
            cpu: "1"
      volumes:
        - name: postgres-storage
          emptyDir: {}
Enter fullscreen mode Exit fullscreen mode

Secret


Enter fullscreen mode Exit fullscreen mode

Service

apiVersion: v1
kind: Service
metadata:
  name: postgres-service
spec:
  selector:
    app: postgres
  ports:
    - protocol: TCP
      port: 5432
      targetPort: 5432
  type: NodePort
Enter fullscreen mode Exit fullscreen mode

Run

kubectl apply -f postgre-secret.yaml
kubectl apply -f postgre-deployment.yaml
kubectl apply -f postgre-service.yaml
Enter fullscreen mode Exit fullscreen mode

Access

kubectl get svc 
Enter fullscreen mode Exit fullscreen mode

In the result, you will see the external port like down below. In my case, it was 31477. But if you don't specify NodePort in your service yaml, it will randomly set. Thus yours might be different with mine.
Image description

Then, In your dbms program, you can access to the database.


However, after some talks with ChatGPT, I learned it should be a stateful service. So, I will try to change this in a Stateful method.

Wow, it was stateless because I didn't set PersistenceVolume. Changing this configuration, it doesn't clear all tables when the pod is killed.

Deployment yaml

...
spec:
  template:
    spec:
      volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: postgres-pvc
...
Enter fullscreen mode Exit fullscreen mode

PV

apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgres-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /tmp/postgres-data
    type: Directory

Enter fullscreen mode Exit fullscreen mode

PVC

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Enter fullscreen mode Exit fullscreen mode
kubectl apply -f postgre-pv.yaml
kubectl apply -f postgre-pvc.yaml
kubectl apply -f postgre-deployment.yml 
Enter fullscreen mode Exit fullscreen mode

But, this strategy has a disadvantage. I don't know exactly, there might be something related to count of the pod. I think as this deployment runs only one pod, it can be stateful? might be...?


Kubenetes kinds usages recommended by ChatGPT

Use Case Recommended Resource
Stateless apps (e.g., APIs, frontends) Deployment
Apps needing shared storage (e.g., logs, caches) Deployment + PVC
Databases, Kafka, Redis, Zookeeper StatefulSet + PVC
Apps that require stable network identities StatefulSet

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay