Start working on upgrade handling

This commit is contained in:
Daniel Berteaud 2023-11-30 21:23:11 +01:00
parent f1ed3ea4bd
commit 879db3287d
5 changed files with 144 additions and 7 deletions

View File

@ -1,7 +1,7 @@
FROM [[ .docker.repo ]][[ .docker.base_images.postgres15.image ]]
MAINTAINER [[ .docker.maintainer ]]
ARG PATRONI_VERSION=3.2.0
ARG PATRONI_VERSION=3.2.1
ENV EDITOR=nano

View File

@ -0,0 +1,18 @@
FROM [[ .docker.repo ]][[ .docker.base_images.alma9.image ]]
MAINTAINER [[ .docker.maintainer ]]
ENV PG_BASE_DATA=/data/db/
RUN set -eux \
dnf update -y &&\
rpm -i https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm &&\
dnf module -y disable postgresql &&\
dnf install -y --setopt=install_weak_deps=0 glibc-langpack-fr glibc-langpack-en &&\
for VER in 11 12 13 14 15 16; do dnf install -y postgresql${VER} postgresql${VER}-server postgresql${VER}-contrib; done &&\
dnf clean all &&\
rm -rf /var/cache/yum/* /var/log/yum/* /var/lib/yum/history*
COPY root/ /
USER postgres
CMD ["pg-upgrade.sh"]

View File

@ -0,0 +1,54 @@
#!/bin/sh
set -euxo pipefail
if [ -z "${PG_FROM}" ]; then
echo "You must set PG_FROM env var to the source version"
exit 1
elif [ -z "${PG_TO}" ]; then
echo "You must set PG_TO env var to the destination version"
exit 1
elif [ ! -d "${PG_BASE_DATA}/${PG_FROM}" ]; then
echo "Source data dir ${PG_BASE_DATA}/${PG_FROM} must already exist"
exit 1
fi
if [ -z "${DO_PG_UPGRADE}" -o "${DO_PG_UPGRADE}" != "1" ]; then
echo "Not running the upgrade. Please set DO_PG_UPGRADE=1"
fi
cd "${PG_BASE_DATA}"
echo "Creating new data dir for version ${PG_TO}"
mkdir -p "${PG_BASE_DATA}/${PG_TO}"
chmod 700 "${PG_BASE_DATA}/${PG_TO}"
echo "Commenting SSL directives (SSL cert not available, nor needed in the upgrade context)"
cp "${PG_BASE_DATA}/${PG_FROM}/postgresql.conf" "${PG_BASE_DATA}/${PG_FROM}/postgresql.conf.old"
sed -i -r 's/^(ssl.*)/#\1/g' "${PG_BASE_DATA}/${PG_FROM}/postgresql.conf"
echo "Replacing pg_hba with a custom one"
cp "${PG_BASE_DATA}/${PG_FROM}/pg_hba.conf" "${PG_BASE_DATA}/${PG_FROM}/pg_hba.conf.old"
cat <<_EOF > "${PG_BASE_DATA}/${PG_FROM}/pg_hba.conf"
local all postgres peer
_EOF
echo "Initializing new PG cluster"
/usr/pgsql-${PG_TO}/bin/initdb --pgdata=${PG_BASE_DATA}/${PG_TO} --data-checksums --encoding UTF-8
echo "Upgrading PG data from ${PG_BASE_DATA}/${PG_FROM} to ${PG_BASE_DATA}/${PG_TO}"
/usr/pgsql-${PG_TO}/bin/pg_upgrade \
--clone \
--old-datadir "${PG_BASE_DATA}/${PG_FROM}" \
--new-datadir "${PG_BASE_DATA}/${PG_TO}" \
--old-bindir /usr/pgsql-${PG_FROM}/bin \
--new-bindir /usr/pgsql-${PG_TO}/bin
if [ -e "${PG_BASE_DATA}/${PG_FROM}/patroni.dynamic.json" ]; then
echo "Keep old patroni.dynamic.json config"
cp ${PG_BASE_DATA}/${PG_FROM}/patroni.dynamic.json ${PG_BASE_DATA}/${PG_TO}/
fi
echo "Restoring configuration"
cp -f "${PG_BASE_DATA}/${PG_FROM}/postgresql.conf.old" "${PG_BASE_DATA}/${PG_FROM}/postgresql.conf"
cp -f "${PG_BASE_DATA}/${PG_FROM}/pg_hba.conf.old" "${PG_BASE_DATA}/${PG_FROM}/pg_hba.conf"

51
upgrade.nomad.hcl Normal file
View File

@ -0,0 +1,51 @@
[[ $c := merge .pg.upgrade . -]]
job "[[ .pg.instance ]]-upgrade" {
[[ template "common/job_start.tpl" $c ]]
type = "batch"
meta {
# Force job to be different for each execution
run_uuid = "${uuidv4()}"
}
group "upgrade" {
volume "data" {
type = [[ .pg.volumes.data.type | toJSON ]]
source = [[ .pg.volumes.data.source | toJSON ]]
[[- if ne .pg.volumes.data.type "host" ]]
access_mode = "single-node-writer"
attachment_mode = "file-system"
[[- end ]]
per_alloc = true
}
task "[[ .pg.instance ]]-upgrade" {
driver = [[ $c.nomad.driver | toJSON ]]
config {
image = [[ $c.image | toJSON ]]
readonly_rootfs = true
}
env {
PG_FROM = [[ $c.from | toJSON ]]
PG_TO = [[ $c.to | toJSON ]]
PG_DO_UPGRADE = [[ $c.do_upgrade | toJSON ]]
}
[[ template "common/file_env.tpl" $c.env ]]
volume_mount {
volume = "data"
destination = "/data"
}
[[ template "common/resources.tpl" $c.resources ]]
}
}
}

View File

@ -8,7 +8,7 @@ pg:
# Postgres server settings
server:
# The image to use
image: danielberteaud/patroni:15.23.11-5
image: danielberteaud/patroni:15.23.11-7
# Number of postgres instance. Patroni will handle leader election and replication
count: 1
@ -18,7 +18,7 @@ pg:
# Eg
# env:
# PGBACKREST_PROCESS_MAX: 4
# PGBACKREST_REPO1_RETENTION_FULL: 30
# PGBACKREST_REPO1_RETENTION_FULL: 1
# PGBACKREST_REPO1_RETENTION_DIFF: 7
# PGBACKREST_REPO1_TYPE: sftp
# PGBACKREST_REPO1_SFTP_HOST: pbs.lapiole.org
@ -40,7 +40,7 @@ pg:
# Recovery configuration to pass to patroni config
recovery_conf: {}
# How many nodes should use synchronous replication. No effet unless count > 1
# How many nodes should use synchronous replication. No effect unless count > 1
synchronous_node_count: 0
# Postgres parameters
@ -138,9 +138,9 @@ pg:
# Schedules for backups. Empty string to disable
cron:
full: 15 02 1 * *
diff: 15 02 2-31 * 0
incr: 15 02 2-31 * 1-6
full: 15 02 * * sun
diff: 15 02 * * mon,tue,wed,thu,fri,sat
incr: ''
# pg_dump based backups
dumps:
@ -321,6 +321,20 @@ pg:
# Additional custom rules to apply (will be appended to default_rules)
rules: []
# Settings for major upgrades
upgrade:
image: danielberteaud/pg-major-upgrade:latest
env: {}
from: ""
to: ""
do_upgrade: false
resources:
cpu: '[[ .pg.server.resources.cpu ]]'
memory: '[[ .pg.server.resources.memory ]]'
# Volumes
volumes:
# The data volume is used to store postgres data