Skip to content

Commit

Permalink
Migrating cnpg in mattermost and updating the logical backup templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
Deepak Tiwari committed Jan 18, 2025
1 parent 3a498a6 commit 85a4826
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 76 deletions.
70 changes: 70 additions & 0 deletions argocd-helm-charts/mattermost-team-edition/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,73 @@ velero-pgdata-mattermost-pgsql-0-lzzqs true pgdata-mattermost-pg
32Gi velero-snapshot snapcontent-4f967d06-d9ce-4052-bd51-f2bd717cad01 27d 27d
sbdtu5498@sbdtu5498-TUF-Gaming-FX505DT-FX505DT:~/Videos/Personal Stuff$ kubectl get volumesnapshot velero-mattermost-team-edition-d4l78 -n mattermost -o yaml
```

### Migrating the postgress operator to cnpg

1. Backup existing mattermost database.

```sh
pg_dump -c -U mattermost | gzip > mattermost_dbdump_2024-01-15.sql.gz
# Copy the data to local machine
kubectl cp mattermost/mattermost-pgsql-0:/mattermost_dbdump_2024-01-15.sql.gz ./mattermost_dbdump_2024-01-15.sql.gz
```

* Create the PR for migrating postgress operator [kubeaid-config-Reflink](https://gitea.obmondo.com/EnableIT/kubeaid-config-enableit/pulls/1033/files) [Kubeaid-Ref-link](https://gitea.obmondo.com/EnableIT/KubeAid/pulls/592)
* Log in to Argocd and update the manifest, ensuring your branch is set in the targetRevision.
* Review the diff carefully and make sure not to remove the existing PostgreSQL deployment **acid.zalan.do/postgresql/mattermost/mattermost-pgsql**
* Once all the steps above are completed, sync the application in Argocd.
* Confirm that the new pods for mattermost-pgsql-1 and mattermost-team-edition are up and running.
* To copy the database backup to the new PostgreSQL pod, create a new Ubuntu pod within the Mattermost namespace, then import the database from there.

```sh
vim ubuntu-sleep.yaml ## Add the below content
apiVersion: v1
kind: Pod
metadata:
name: ubuntu
labels:
app: ubuntu
spec:
containers:
- image: ubuntu
command:
- "sleep"
- "604800"
imagePullPolicy: IfNotPresent
name: ubuntu
restartPolicy: Always
kubectl apply -f ubuntu-sleep.yaml -n mattermost
```

* Install Postgresql package inside the Ubuntu pod and import the database.

```sh
apt update
apt install postgress
```

* Transfer the backup database file to the Ubuntu pod.

```sh
kubectl cp mattermost_dbdump_2024-01-15.sql.gz mattermost/ubuntu:/tmp
cd /tmp
gzip -d mattermost_dbdump_2024-01-15.sql.gz
```

* Retrieve database user password by accessing the secrets (mattermost-pgsql-app) in k9s.
* Obtain the host IP of the Postgres pod from service using k9s.
* Execute the command to import the backup database into PostgreSQL.

```sh
psql -h 10.98.77.222 -p 5432 -d mattermost -U mattermost < mattermost_dbdump_2024-01-15.sql
or
pg_restore -h 10.98.77.222 -p 5432 -d mattermost -U mattermost < mattermost_dbdump_2024-01-15.sql
```

* Start the mattermost-team-edition application by syncing the deployment from the Argo CD UI.
* Once the application is up and running, log in to Mattermost to confirm that the old chats, uploaded files, and images are accessible.
* After verifying the application works as expected, delete the Ubuntu pod.
Original file line number Diff line number Diff line change
@@ -1,75 +1,98 @@
{{ if (.Values.logicalbackup).enabled }}

{{ if (.Values.postgres.logicalbackup).enabled }}
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: postgres-logical-backup
spec:
concurrencyPolicy: Forbid
failedJobsHistoryLimit: 1
schedule: {{ (.Values.logicalbackup).schedule | default "30 00 * * *" }}
schedule: {{ (.Values.postgres.logicalbackup).schedule | default "30 00 * * *" }}
successfulJobsHistoryLimit: 3
jobTemplate:
spec:
template:
spec:
containers:
- name: logical-backup
image: ghcr.io/obmondo/logical-backup:latest
image: ghcr.io/obmondo/logical-backup:1.0.1
imagePullPolicy: IfNotPresent
env:
- name: POD_NAMESPACE
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
- name: LOGICAL_BACKUP_PROVIDER
value: {{ (.Values.logicalbackup).provider | default "s3" }}
- name: LOGICAL_BACKUP_S3_BUCKET_SCOPE_SUFFIX
value: {{ (.Values.logicalbackup).s3bucketscopesuffix | default "logicalBackup" }}
- name: LOGICAL_BACKUP_S3_BUCKET
value: {{ (.Values.logicalbackup).s3bucket }}
- name: PG_VERSION
value: "{{ (.Values.logicalbackup).pgversion | default "15" }}"
- name: PG_PORT
value: "{{ (.Values.logicalbackup).pgport | default "5432" }}"
- name: PGSSLMODE
value: {{ (.Values.logicalbackup).pgsslmode | default "require" }}
- name: PGHOST
value: {{ .Values.postgres.host | default "mattermost-pgsql-rw" }}
- name: PGPASSWORD
valueFrom:
secretKeyRef:
name: mattermost-pgsql-app
key: password
- name: PGPORT
value: "{{ .Values.postgres.port | default 5432 }}"
- name: PGUSER
value: {{ (.Values.logicalbackup).pguser | default "mattermost" }}
value: {{ .Values.postgres.user | default "mattermost" }}
- name: PGDATABASE
value: {{ (.Values.logicalbackup).pgdatabase | default "mattermost" }}
value: {{ .Values.postgres.db | default "mattermost" }}
- name: CLUSTER_NAME_LABEL
value: {{ (.Values.logicalbackup).pgOperatorClusterName | default "mattermost-pgsql" }}
value: {{ (.Values.postgres.logicalbackup).pgOperatorClusterName | default "mattermost-pgsql" }}

{{- if eq .Values.logicalbackup.provider "az" }}
{{- if eq .Values.postgres.logicalbackup.provider "az" }}
- name: LOGICAL_BACKUP_PROVIDER
value: "az"
- name: LOGICAL_BACKUP_AZURE_STORAGE_ACCOUNT_NAME
value: {{ (.Values.logicalbackup).azurecontainer }}
value: {{ (.Values.postgres.logicalbackup).storeageaccount }}
- name: LOGICAL_BACKUP_AZURE_STORAGE_CONTAINER
value: {{ (.Values.logicalbackup).s3bucket }}
value: {{ (.Values.postgres.logicalbackup).azurecontainer }}
- name: LOGICAL_BACKUP_AZURE_STORAGE_ACCOUNT_KEY
valueFrom:
secretKeyRef:
key: LOGICAL_BACKUP_AZURE_STORAGE_ACCOUNT_KEY
name: mattermost-pgsql-postgres-pod-env
- name: LOGICAL_BACKUP_S3_BUCKET
value: {{ (.Values.postgres.logicalbackup).azurecontainer }}
- name: LOGICAL_BACKUP_S3_BUCKET_SCOPE_SUFFIX
value: {{ (.Values.postgres.logicalbackup).s3bucketscopesuffix | default "logicalbackup" }}

{{- else if eq .Values.logicalbackup.provider "s3" }}
{{- else if eq .Values.postgres.logicalbackup.provider "s3" }}
- name: LOGICAL_BACKUP_PROVIDER
value: "s3"
- name: AWS_ACCESS_KEY_ID
value: {{ (.Values.logicalbackup).awsaccesskeyid }}
- name: LOGICAL_BACKUP_S3_REGION
value: {{ (.Values.logicalbackup).s3region }}
valueFrom:
secretKeyRef:
key: AWS_ACCESS_KEY_ID
name: mattermost-pgsql-postgres-pod-env
- name: AWS_SECRET_ACCESS_KEY
valueFrom:
secretKeyRef:
key: AWS_SECRET_ACCESS_KEY
name: mattermost-pgsql-postgres-pod-env
- name: LOGICAL_BACKUP_S3_ENDPOINT
value: {{ (.Values.logicalbackup).s3endpoint }}
value: {{ (.Values.postgres.logicalbackup).s3endpoint }}
- name: LOGICAL_BACKUP_PROVIDER
value: "s3"
- name: LOGICAL_BACKUP_S3_BUCKET_SCOPE_SUFFIX
value: {{ (.Values.postgres.logicalbackup).s3bucketscopesuffix | default "logicalbackup" }}
- name: LOGICAL_BACKUP_S3_REGION
value: {{ (.Values.postgres.logicalbackup).s3region }}
- name: LOGICAL_BACKUP_S3_BUCKET
value: {{ (.Values.postgres.logicalbackup).s3bucket }}
- name: LOGICAL_BACKUP_S3_RETENTION_TIME
value: {{ (.Values.postgres.logicalbackup).retention }}
{{- end }}

{{- toYaml .Values.logicalbackup.extraEnvVars | nindent 16 }}

- name: PG_VERSION
value: "{{ (.Values.postgres.logicalbackup).pgversion | default 16 }}"
- name: POSTGRES_OPERATOR
value: cngp
resources:
limits:
cpu: "1"
memory: 500Mi
requests:
cpu: 100m
memory: 100Mi
memory: 128Mi
securityContext:
allowPrivilegeEscalation: true
privileged: false
Expand All @@ -78,9 +101,5 @@ spec:
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Never
serviceAccount: "{{ .Values.logicalbackup.serviceAccount | default "postgres-pod" }}"
serviceAccountName: "{{ .Values.logicalbackup.serviceAccountName | default "postgres-pod" }}"
terminationGracePeriodSeconds: 300

{{ end }}

127 changes: 91 additions & 36 deletions argocd-helm-charts/mattermost-team-edition/templates/postgresql.yaml
Original file line number Diff line number Diff line change
@@ -1,42 +1,97 @@
apiVersion: acid.zalan.do/v1
kind: postgresql
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
{{ if (.Values.postgres).recover }}
name: mattermost-pgsql-recover
{{ else }}
name: mattermost-pgsql
labels:
velero.io/exclude-from-backup: "true"
{{ end }}
spec:
enableLogicalBackup: {{ .Values.postgresql.logicalBackup | default "true" }}
env:
{{- if .Values.postgresql.access_key_id }}
- name: AWS_ACCESS_KEY_ID
value: {{ .Values.postgresql.access_key_id }}
{{- end }}
{{- if .Values.postgresql.access_secret }}
- name: AWS_SECRET_ACCESS_KEY
valueFrom:
secretKeyRef:
name: {{ .Values.postgresql.access_secret | default "mattermost-pgsql-postgres-pod-env" }}
key: AWS_SECRET_ACCESS_KEY
{{- end }}
databases:
mattermost: mattermost
enableMasterLoadBalancer: false
numberOfInstances: {{ ((.Values.postgresql).instance) | default 1 }}
postgresql:
version: "12"
teamId: mattermost
users:
mattermost:
- superuser
volume:
size: {{ .Values.postgresql.size }}
{{- if .Values.postgresql.storageClass }}
storageClass: {{ .Values.postgresql.storageClass }}
{{- end }}
instances: {{ ((.Values.postgres).instance) | default 1 }}
{{ if (.Values.postgres).recover }}
bootstrap:
recovery: mattermost-pgsql
{{ else }}
bootstrap:
initdb:
database: mattermost
owner: mattermost
{{ end }}
resources:
limits:
memory: {{ .Values.postgresql.resources.limits.memory | default "800Mi" }}
cpu: {{ .Values.postgresql.resources.limits.cpu | default "250m" }}
memory: {{ (((.Values.postgres).resources).limits).memory | default "800Mi" }}
requests:
memory: {{ .Values.postgresql.resources.requests.memory | default "500Mi" }}
cpu: {{ .Values.postgresql.resources.requests.cpu | default "250m" }}
memory: {{ (((.Values.postgres).resources).requests).memory | default "500Mi" }}
cpu: {{ (((.Values.postgres).resources).requests).cpu | default "100m" }}
storage:
size: {{ (.Values.postgres).size }}
{{- if (.Values.postgres).storageClass }}
storageClass: {{ (.Values.postgres).storageClass }}
{{- end }}
{{- if (.Values.postgres).recover }}
externalClusters:
name: mattermost-pgsql
barmanObjectStore:
destinationPath: {{ ((.Values.postgres).backup).destinationPath }}
endpointURL: {{ ((.Values.postgres).backup).endpointURL }}
{{- if eq ((.Values.postgres).backup).cloud "aws" }}
s3Credentials:
accessKeyId:
name: mattermost-pgsql-backup-creds
key: ACCESS_KEY_ID
secretAccessKey:
name: mattermost-pgsql-backup-creds
key: ACCESS_SECRET_KEY
{{- end }}
{{- if eq ((.Values.postgres).backup).cloud "azure" }}
azureCredentials:
connectionString:
name: mattermost-pgsql-backup-creds
key: AZURE_CONNECTION_STRING
storageAccount:
name: mattermost-pgsql-backup-creds
key: AZURE_STORAGE_ACCOUNT
storageKey:
name: mattermost-pgsql-backup-creds
key: AZURE_STORAGE_KEY
storageSasToken:
name: mattermost-pgsql-backup-creds
key: AZURE_STORAGE_SAS_TOKEN
{{- end }}
wal:
maxParallel: 8
{{- end }}
{{- if (.Values.postgres).backup }}
backup:
barmanObjectStore:
destinationPath: {{ ((.Values.postgres).backup).destinationPath }}
endpointURL: {{ ((.Values.postgres).backup).endpointURL }}
{{- if eq ((.Values.postgres).backup).cloud "aws" }}
s3Credentials:
accessKeyId:
name: mattermost-pgsql-backup-creds
key: ACCESS_KEY_ID
secretAccessKey:
name: mattermost-pgsql-backup-creds
key: ACCESS_SECRET_KEY
{{- end }}
{{- if eq ((.Values.postgres).backup).cloud "azure" }}
azureCredentials:
connectionString:
name: mattermost-pgsql-backup-creds
key: AZURE_CONNECTION_STRING
storageAccount:
name: mattermost-pgsql-backup-creds
key: AZURE_STORAGE_ACCOUNT
storageKey:
name: mattermost-pgsql-backup-creds
key: AZURE_STORAGE_KEY
storageSasToken:
name: mattermost-pgsql-backup-creds
key: AZURE_STORAGE_SAS_TOKEN
{{- end }}
wal:
compression: gzip
encryption: AES256
retentionPolicy: "30d"
{{ end }}
11 changes: 6 additions & 5 deletions argocd-helm-charts/mattermost-team-edition/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ mattermost-team-edition:
- name: MM_DB_USERNAME
valueFrom:
secretKeyRef:
name: mattermost.mattermost-pgsql.credentials.postgresql.acid.zalan.do
name: mattermost-pgsql-app
key: username
- name: MM_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mattermost.mattermost-pgsql.credentials.postgresql.acid.zalan.do
name: mattermost-pgsql-app
key: password
- name: MM_SQLSETTINGS_DATASOURCE
value: postgres://$(MM_DB_USERNAME):$(MM_DB_PASSWORD)@mattermost-pgsql:5432/mattermost?sslmode=require&connect_timeout=100
value: postgres://$(MM_DB_USERNAME):$(MM_DB_PASSWORD)@mattermost-pgsql-rw:5432/mattermost?sslmode=require&connect_timeout=100
- name: MM_GITLABSETTINGS_ID
valueFrom:
secretKeyRef:
Expand Down Expand Up @@ -47,9 +47,10 @@ mattermost-team-edition:
readinessProbe:
initialDelaySeconds: 150

postgresql:
postgres:
size: 8Gi
logicalBackup: true
recover: false
pass: mattermost-pgsql-app
resources:
limits:
memory: 800Mi
Expand Down

0 comments on commit 85a4826

Please sign in to comment.