Skip to content

Instantly share code, notes, and snippets.

@ivanbrennan
Last active April 1, 2024 20:46
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ivanbrennan/cf20e26de6e7cbf517d101e7cc9d4ca0 to your computer and use it in GitHub Desktop.
Save ivanbrennan/cf20e26de6e7cbf517d101e7cc9d4ca0 to your computer and use it in GitHub Desktop.
PostgreSQL in minikube
# create/update resources
kubectl --context=minikube apply -f ./postgres.yaml
# In order for the service to reach the statefulset, the following should
# be true:
# statefulset.spec.selector.matchLabels.app == service.spec.selector.app
# statefulset.spec.selector.matchLabels.role == service.spec.selector.role
# give the server some time to start up
# ...
# test the connection from outside the cluster
psql --host=$(minikube ip) \
--port=$(minikube service postgres --url --format={{.Port}}) \
--username=postgres \
--dbname=postgres
# test the connection from within the cluster
url=$(kubectl --context=minikube get service postgres \
--output=jsonpath='{.spec.clusterIP}:{.spec.ports[0].port}')
kubectl --context=minikube run pgbox --image=postgres:9.6 \
--rm -it --restart=Never -- \
bash -c "read &&
psql --host=${url%:*} --port=${url#*:} \
--username=postgres --dbname=postgres \
--command='SELECT refobjid FROM pg_depend LIMIT 1'"
# remove resources
kubectl --context=minikube delete -f ./postgres.yaml --ignore-not-found
# Data will survive the above operation, and be available next time you revive postgres.
apiVersion: v1
kind: PersistentVolume
metadata:
name: postgres
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 2Gi
hostPath:
path: /data/postgres
storageClassName: standard
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
volumeName: postgres
---
apiVersion: v1
kind: Secret
metadata:
name: postgres
type: Opaque
data:
POSTGRES_USER: cG9zdGdyZXM= # printf postgres | base64
POSTGRES_PASSWORD: cGFzc3dvcmQ= # printf password | base64
---
apiVersion: apps/v1beta2
kind: StatefulSet
metadata:
name: postgres
labels:
app: postgres
role: service
spec:
replicas: 1
selector:
matchLabels:
app: postgres
role: service
serviceName: postgres
template:
metadata:
labels:
app: postgres
role: service
spec:
containers:
- name: postgres
image: postgres:9.6
env:
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
key: POSTGRES_USER
name: postgres
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
key: POSTGRES_PASSWORD
name: postgres
ports:
- containerPort: 5432
name: postgres
protocol: TCP
volumeMounts:
- name: postgres
mountPath: /var/lib/postgresql/data
volumes:
- name: postgres
persistentVolumeClaim:
claimName: postgres
---
apiVersion: v1
kind: Service
metadata:
name: postgres
labels:
app: postgres
role: service
spec:
selector:
app: postgres
role: service
type: NodePort
ports:
- name: postgres
port: 5432
targetPort: 5432
protocol: TCP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment