|
| 1 | += Restore from Backup |
| 2 | + |
| 3 | +When configuring component-vault with `backup.enabled = true`, the component sets up regular backups using k8up. |
| 4 | +This how-to explains how Vault can be restored from such a backup. |
| 5 | + |
| 6 | +== Information on the Vault Unseal Key and Root Token |
| 7 | + |
| 8 | +include::partial$vault-warning.adoc[] |
| 9 | + |
| 10 | +== Prerequisites |
| 11 | + |
| 12 | +* `restic` - command line tool to access backups made by k8up |
| 13 | +* `vault` - command line tool to interact with Vault |
| 14 | +* `kubectl` |
| 15 | +* Write access to the cluster's tenant repository |
| 16 | +* Read access to the restic repository in which k8up stored the Vault backups |
| 17 | +* The Vault instance's *unseal key* and *root token* - these must be backed up manually; they are not part of the automated k8up backup. |
| 18 | + |
| 19 | +== Procedure |
| 20 | + |
| 21 | +=== 1. Set up new Vault instance |
| 22 | + |
| 23 | +* Add the `vault` application to your cluster configuration |
| 24 | +* Initially disable backups by setting `.backups.enabled` to `false` |
| 25 | +* Compile and push the cluster config and wait for Vault to start. |
| 26 | + |
| 27 | +=== 2. Set up permissions for restoring the Vault instance |
| 28 | + |
| 29 | +* Access the Vault UI |
| 30 | ++ |
| 31 | +[source,shell] |
| 32 | +---- |
| 33 | +export VAULT_INSTANCE_NAME="my-vault-instance" |
| 34 | +# Get root token to log in |
| 35 | +kubectl get secret -n $VAULT_INSTANCE_NAME ${VAULT_INSTANCE_NAME}-seal -ojsonpath='{.data.vault-root}' | base64 -d |
| 36 | +kubectl port-forward -n $VAULT_INSTANCE_NAME ${VAULT_INSTANCE_NAME}-0 8200 |
| 37 | +---- |
| 38 | +* Open your browser at http://localhost:8200 and log in with the `Token` method and the root token retreived in the previous step |
| 39 | +* Navigate to `Policies` and open the `backup` policy |
| 40 | +* Edit the policy to the following: |
| 41 | ++ |
| 42 | +[source,hcl] |
| 43 | +---- |
| 44 | +path "sys/storage/raft/snapshot-force" { |
| 45 | + capabilities = ["read", "create", "update", "sudo"] |
| 46 | +} |
| 47 | +---- |
| 48 | +* Save the new policy |
| 49 | + |
| 50 | +NOTE: Editing this policy is unproblematic, as it will be reset once the backup is restored. |
| 51 | + |
| 52 | +=== 3. Retrieve the Vault snapshot |
| 53 | + |
| 54 | +* Set up the restic credentials (values correspond to the component parameters `backup.bucket` and `backup.password`) |
| 55 | ++ |
| 56 | +[source,shell] |
| 57 | +---- |
| 58 | +export AWS_ACCESS_KEY_ID="S3_KEY backup.bucket.accesskey" |
| 59 | +export AWS_SECRET_ACCESS_KEY="S3_SECRET backup.bucket.secretkey" |
| 60 | +export RESTIC_REPOSITORY="s3:https://path.to.my/bucket" |
| 61 | +export RESTIC_PASSWORD="RESTIC_REPO_KEY backup.password" |
| 62 | +---- |
| 63 | +* Retrieve the latest Vault snapshot to your local disk |
| 64 | ++ |
| 65 | +[source,shell] |
| 66 | +---- |
| 67 | +mkdir restore |
| 68 | +restic restore --target restore/ latest |
| 69 | +---- |
| 70 | +* Verify the snapshot file |
| 71 | ++ |
| 72 | +[source,shell] |
| 73 | +---- |
| 74 | +ls restore |
| 75 | +# This should show a file named "[instance name]-backup.snapshot" |
| 76 | +---- |
| 77 | + |
| 78 | +=== 4. Restore the snapshot |
| 79 | + |
| 80 | +* Expose the Vault pod |
| 81 | ++ |
| 82 | +[source,shell] |
| 83 | +---- |
| 84 | +kubectl port-forward -n $VAULT_INSTANCE_NAME ${VAULT_INSTANCE_NAME}-0 8200 |
| 85 | +---- |
| 86 | +* In a separate terminal, prepare the environment to access Vault |
| 87 | ++ |
| 88 | +[source,shell] |
| 89 | +---- |
| 90 | +# Get root token to log in |
| 91 | +export VAULT_TOKEN="$(kubectl get secret -n $VAULT_INSTANCE_NAME ${VAULT_INSTANCE_NAME}-seal -ojsonpath='{.data.vault-root}' | base64 -d)" |
| 92 | +export VAULT_ADDR="http://127.0.0.1:8200" |
| 93 | +---- |
| 94 | +* Restore the backup |
| 95 | ++ |
| 96 | +[source,shell] |
| 97 | +---- |
| 98 | +vault operator raft snapshot restore -force restore/${VAULT_INSTANCE_NAME}-backup.snapshot |
| 99 | +---- |
| 100 | + |
| 101 | +=== 5. Unseal Vault |
| 102 | + |
| 103 | +If you were logged into the Vault UI, you should have gotten logged out now. |
| 104 | +This is expected. |
| 105 | + |
| 106 | +* Open your browser at http://localhost:8200 |
| 107 | +* Use the *Vault Unseal Key* of the Vault instance you've just restored to unseal Vault |
| 108 | +* Use the *Vault root token* of the Vault instance you've just restored to log in with the `Token` method |
| 109 | +* Verify that the restore worked, and secrets are now restored in Vault. |
| 110 | + |
| 111 | +[IMPORTANT] |
| 112 | +==== |
| 113 | +The unseal key and root token of the Vault instance you're restoring need to have been stored separately. |
| 114 | +Without them, the restore procedure cannot be completed. |
| 115 | +==== |
| 116 | + |
| 117 | +=== 6. Update the Vault Secret |
| 118 | + |
| 119 | +NOTE: Without this step, your Vault instance will not be able to auto-unseal. |
| 120 | + |
| 121 | +* Encode the Vault credentials |
| 122 | ++ |
| 123 | +[source,shell] |
| 124 | +---- |
| 125 | +export VAULT_UNSEAL_KEY="OLD_UNSEAL_KEY" |
| 126 | +export VAULT_ROOT_TOKEN="OLD_ROOT_TOKEN" |
| 127 | +
|
| 128 | +echo -n "$VAULT_UNSEAL_KEY" | base64 -w0 |
| 129 | +echo -n "$VAULT_ROOT_TOKEN" | base64 -w0 |
| 130 | +---- |
| 131 | +* Update the Vault secret |
| 132 | ++ |
| 133 | +[source,shell] |
| 134 | +---- |
| 135 | +kubectl edit secret -n ${VAULT_INSTANCE_NAME} ${VAULT_INSTANCE_NAME}-seal |
| 136 | +---- |
| 137 | +* Update the `vault-root` and `vault-unseal-0` keys to reflect the values you have just encoded |
| 138 | +* Save the secret |
0 commit comments