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
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: {}
Secret
Service
apiVersion: v1
kind: Service
metadata:
name: postgres-service
spec:
selector:
app: postgres
ports:
- protocol: TCP
port: 5432
targetPort: 5432
type: NodePort
Run
kubectl apply -f postgre-secret.yaml
kubectl apply -f postgre-deployment.yaml
kubectl apply -f postgre-service.yaml
Access
kubectl get svc
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.
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
...
PV
apiVersion: v1
kind: PersistentVolume
metadata:
name: postgres-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /tmp/postgres-data
type: Directory
PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
kubectl apply -f postgre-pv.yaml
kubectl apply -f postgre-pvc.yaml
kubectl apply -f postgre-deployment.yml
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 |
Top comments (0)