Restore Keycloak
This guide shows you how to restore Keycloak in your Rhize environment.
Restoring Keycloak to a running instance involves downtime.
Typically, this downtime lasts less than a minute. The exact duration needed depends on network constraints, backup size, and the performance of the Kubernetes cluster.
Prerequisites
Before you start, ensure you have the following:
Steps
Confirm the cluster and namespace are correct:
## context kubectl config current-context ## namespace kubectl get namespaceTo change the namespace for all subsequent
kubectlcommands tolibre, run this command:kubectl config set-context --current --namespace=libreFor a reference of useful
kubectlcommands, refer to the official kubectl Cheat Sheet.Retrieve the Keycloak user password using the following command, replacing
<NAMESPACE>with your namespace:kubectl get secret keycloak-<NAMESPACE>-postgresql -o jsonpath="{.data.postgres-password}" | base64 --decodeExtract your backup file:
gzip -d keycloak-postgres-backup-YYYYMMDDTHHMMAA.sqlTo prevent new records from being created while the backup is restored, scale down the Keycloak replicas to
0. Keycloak will be unavailable after this command.kubectl scale statefulsets keycloak --replicas=0Scale down the replicas of PostgreSQL to 0, so that existing persistent volume claims and persistent volumes can be removed:
kubectl scale statefulsets keycloak-postgresql --replicas=0Remove the Postgres persistent volume claim:
kubectl delete pvc data-keycloak-postgresql-0Identify the Keycloak Postgres volumes:
kubectl get pv | grep keycloakThis displays a list of persistent volume claims related to Keycloak. For example:
pvc-95176bc4-88f4-4178-83ab-ee7b256991bc 10Gi RWO Delete Terminating libre/data-keycloak-postgresql-0 hostpath 48dNote the names of the ´pvc-*` items. You’ll need them for the next step.
Remove the persistent volumes with this command, replacing
<PVC_FROM_PREVIOUS_STEP>with thepvc-*name from the previous step:$ kubectl delete pv <PVC_FROM_PREVIOUS_STEP>Scale up the replicas of PostgreSQL to 1:
kubectl scale statefulsets keycloak-postgresql --replicas=1Restore the backup:
cat keycloak-postgres-backup-YYYYMMDDTHHMMAA.sql | kubectl exec -i keycloak-postgresql-0 -- psql postgresql://postgres:<your-postgres-password>@localhost:5432 -U postgresScale up the replicas of Keycloak to
1:kubectl scale statefulsets keycloak --replicas=1Proxy the web portal of Keycloak:
kubectl port-forward svc/keycloak 5101:80
Confirm access by checking http://localhost:80.