I have a kubernetes cluster running an app. Part of the cluster is a postgresql pod, currently running version 10.4. Unfortunately, I discovered that I need to upgrade the postgresql version.
The postgres yaml is as follow:
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres
spec:
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:10.4
imagePullPolicy: "IfNotPresent"
ports:
- containerPort: 5432
envFrom:
- configMapRef:
name: postgres-config
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgredb
volumes:
- name: postgredb
persistentVolumeClaim:
claimName: postgres-pv-claim
The postgresql database already has some data in it. I need to find a way to upgrade the cluster while in production.
If I simply try to change the image to 12.0 and run kubectl apply
I get an error:
2020-11-15 22:48:08.332 UTC [1] DETAIL: The data directory was initialized by PostgreSQL version 10, which is not compatible with this version 12.5 (Debian 12.5-1.pgdg100+1).
So I understand that I need to manually upgrade the postgres database inside the cluster, and only then I will be able to fix the yaml. Is that correct?
3
Answers
I tried @Justin method, but I encountered an issue that I couldn't stop current running postgres process inside the pod (for some reason inside the container there is no access to postgresql service. You can see more about that issue here)
Since I couldn't upgrade the postgresql specifically inside the pod, what I did at the end is creating a parallel postgres pod in Kubernetes which holds the new version. Then I dumped database from old server, copied it to the new server, and used it to initialize the database there.
Here are the steps one by one:
Create a parallel postgres service with the new version
In old version pod:
ssh to new-pod
extra step that I needed, becuase for some reason new pod didn't had 'postgres' user created: get into postgres client using your credentials:
then exit postgres and exit to normal user
Using this How to upgrade postgresql database from 10 to 12 without losing data for openproject as the basis for my post. I’m converting it to a container-with-volume friendly approach. I am assuming you’re using the official Postgresql image on Docker Hub.
Backup the data – Out of scope for this answer. There are other people better suited to answering that question.
Upgrade postgres from inside the pod and migrate the data
Get a shell in your postgres pod
Run the following inside the container
The next bit is verbatim taken from the linked post:
Now change the deployment YAML image reference to the Postgres 12 and
kubectl apply
Check the logs to see if it started up correctly.
Thanks to @justadev for the answer. Some additions:
I had to add the
-d keycloak
database flag because while thepsql
log was OK during the import, the data was missing in the database afterwards. You need to explicitly indicate the database forpsql
.So, check the
psql
flags here: https://www.postgresql.org/docs/current/app-psql.htmlBy the way, I managed to upgrade from Postgres 11 to Posgres 14.5 this way.
Also, I want to add this:
tar
may be absent on a pod, this means thatkubectl cp
will not work.Here is the workaround: