Created
March 5, 2021 07:47
-
-
Save khapota/dbef7084b4c9e998a8d9b763c6970731 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
apiVersion: v1 | |
kind: Namespace | |
metadata: | |
name: test | |
labels: | |
name: test | |
--- | |
apiVersion: v1 | |
kind: ConfigMap | |
metadata: | |
name: test-config | |
namespace: test | |
data: | |
postgres_name: "dvdrental" | |
postgres_host: "postgres" | |
postgres_port: "5432" | |
--- | |
apiVersion: v1 | |
kind: Secret | |
metadata: | |
name: postgres-secret | |
namespace: test | |
data: | |
username: "cG9zdGdyZXM=" | |
password: "cCQxa0IjeU1RWEdu" | |
--- | |
apiVersion: v1 | |
kind: PersistentVolume | |
metadata: | |
name: pv0001 | |
namespace: test | |
spec: | |
accessModes: | |
- ReadWriteOnce | |
capacity: | |
storage: 5Gi | |
hostPath: | |
path: /data_ext/k8s_pv/pv1 | |
--- | |
kind: PersistentVolumeClaim | |
apiVersion: v1 | |
metadata: | |
name: postgres-pvc | |
namespace: test | |
labels: | |
app: postgres | |
spec: | |
# storageClassName: aws-gp2 | |
accessModes: | |
- ReadWriteOnce | |
resources: | |
requests: | |
storage: 2Gi | |
--- | |
apiVersion: v1 | |
kind: ConfigMap | |
metadata: | |
name: upgrade-script | |
namespace: test | |
data: | |
pre.sh: | | |
#!/bin/bash | |
if [ ! -f /var/lib/postgresql/data/pgdata/PG_VERSION ]; then | |
echo "No data in the volume" | |
exit 0 | |
fi | |
if [ `cat /var/lib/postgresql/data/pgdata/PG_VERSION` -eq "$NEW_VER" ]; then | |
echo "Same ver, no need to upgrade" | |
exit 0 | |
fi | |
apt-get update && apt-get install -y postgresql-"$OLD_VER" | |
ls /tmp/scripts | |
su - postgres -c "POSTGRES_USER=$POSTGRES_USER OLD_VER=$OLD_VER NEW_VER=$NEW_VER bash /tmp/scripts/upgrade.sh" | |
upgrade.sh: | | |
#!/bin/bash | |
echo "$OLD_VER - $NEW_VER" | |
mkdir /tmp/pgdata-"$OLD_VER" | |
mv /var/lib/postgresql/data/pgdata/* /tmp/pgdata-"$OLD_VER"/ | |
chmod -R 750 /tmp/pgdata-"$OLD_VER" | |
ls -al /tmp/pgdata-"$OLD_VER" | |
/usr/lib/postgresql/$NEW_VER/bin/initdb -E UTF8 --lc-collate en_US.UTF-8 --lc-ctype en_US.utf8 --username="$POSTGRES_USER" -D /var/lib/postgresql/data/pgdata | |
cd /var/lib/postgresql/; /usr/lib/postgresql/"$NEW_VER"/bin/pg_upgrade -U "$POSTGRES_USER" -b /usr/lib/postgresql/"$OLD_VER"/bin/ -B /usr/lib/postgresql/"$NEW_VER"/bin/ -d /tmp/pgdata-"$OLD_VER" -D /var/lib/postgresql/data/pgdata | |
./delete_old_cluster.sh | |
--- | |
kind: Deployment | |
apiVersion: apps/v1 | |
metadata: | |
name: postgres | |
namespace: test | |
labels: | |
app: postgres | |
spec: | |
replicas: 1 | |
selector: | |
matchLabels: | |
app: postgres | |
template: | |
metadata: | |
labels: | |
app: postgres | |
spec: | |
initContainers: | |
- name: upgrade | |
image: postgres:12 | |
command: ['sh', '-c', 'bash /tmp/scripts/pre.sh'] | |
env: | |
- name: OLD_VER | |
value: "11" | |
- name: NEW_VER | |
value: "12" | |
- name: POSTGRES_DB | |
valueFrom: | |
configMapKeyRef: | |
name: test-config | |
key: postgres_name | |
- name: POSTGRES_USER | |
valueFrom: | |
secretKeyRef: | |
name: postgres-secret | |
key: username | |
- name: POSTGRES_PASSWORD | |
valueFrom: | |
secretKeyRef: | |
name: postgres-secret | |
key: password | |
volumeMounts: | |
- mountPath: /var/lib/postgresql/data | |
name: postgre-storage | |
- name: script | |
mountPath: /tmp/scripts | |
containers: | |
- name: postgres | |
image: postgres:11 | |
ports: | |
- name: postgres | |
containerPort: 5432 | |
env: | |
- name: POSTGRES_DB | |
valueFrom: | |
configMapKeyRef: | |
name: test-config | |
key: postgres_name | |
- name: POSTGRES_USER | |
valueFrom: | |
secretKeyRef: | |
name: postgres-secret | |
key: username | |
- name: POSTGRES_PASSWORD | |
valueFrom: | |
secretKeyRef: | |
name: postgres-secret | |
key: password | |
- name: PGDATA | |
value: /var/lib/postgresql/data/pgdata | |
volumeMounts: | |
- mountPath: /var/lib/postgresql/data | |
name: postgre-storage | |
volumes: | |
- name: postgre-storage | |
persistentVolumeClaim: | |
claimName: postgres-pvc | |
- name: script | |
configMap: | |
name: upgrade-script | |
items: | |
- key: pre.sh | |
path: pre.sh | |
- key: upgrade.sh | |
path: upgrade.sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment