index.md 12.6 KB
Newer Older
1
# Disaster Recovery (Geo) **(PREMIUM ONLY)**
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

Geo replicates your database, your Git repositories, and few other assets.
We will support and replicate more data in the future, that will enable you to
failover with minimal effort, in a disaster situation.

See [Geo current limitations][geo-limitations] for more information.

CAUTION: **Warning:**
Disaster recovery for multi-secondary configurations is in **Alpha**.
For the latest updates, check the multi-secondary [Disaster Recovery epic][gitlab-org&65].

## Promoting a **secondary** Geo node in single-secondary configurations

We don't currently provide an automated way to promote a Geo replica and do a
failover, but you can do it manually if you have `root` access to the machine.

This process promotes a **secondary** Geo node to a **primary** node. To regain
geographic redundancy as quickly as possible, you should add a new **secondary** node
immediately after following these instructions.

### Step 1. Allow replication to finish if possible

If the **secondary** node is still replicating data from the **primary** node, follow
[the planned failover docs][planned-failover] as closely as possible in
order to avoid unnecessary data loss.

### Step 2. Permanently disable the **primary** node

CAUTION: **Warning:**
If the **primary** node goes offline, there may be data saved on the **primary** node
that has not been replicated to the **secondary** node. This data should be treated
as lost if you proceed.

If an outage on the **primary** node happens, you should do everything possible to
avoid a split-brain situation where writes can occur in two different GitLab
instances, complicating recovery efforts. So to prepare for the failover, we
must disable the **primary** node.

1. SSH into the **primary** node to stop and disable GitLab, if possible:

42
   ```shell
M
Marcel Amirault 已提交
43 44
   sudo gitlab-ctl stop
   ```
45

M
Marcel Amirault 已提交
46
   Prevent GitLab from starting up again if the server unexpectedly reboots:
47

48
   ```shell
M
Marcel Amirault 已提交
49 50
   sudo systemctl disable gitlab-runsvdir
   ```
51

M
Marcel Amirault 已提交
52 53
   NOTE: **Note:**
   (**CentOS only**) In CentOS 6 or older, there is no easy way to prevent GitLab from being
54
   started if the machine reboots isn't available (see [Omnibus GitLab issue #3058](https://gitlab.com/gitlab-org/omnibus-gitlab/issues/3058)).
M
Marcel Amirault 已提交
55
   It may be safest to uninstall the GitLab package completely:
56

57
   ```shell
M
Marcel Amirault 已提交
58 59
   yum remove gitlab-ee
   ```
60

M
Marcel Amirault 已提交
61 62 63 64
   NOTE: **Note:**
   (**Ubuntu 14.04 LTS**) If you are using an older version of Ubuntu
   or any other distro based on the Upstart init system, you can prevent GitLab
   from starting if the machine reboots by doing the following:
65

66
   ```shell
M
Marcel Amirault 已提交
67 68 69 70
   initctl stop gitlab-runsvvdir
   echo 'manual' > /etc/init/gitlab-runsvdir.override
   initctl reload-configuration
   ```
71 72

1. If you do not have SSH access to the **primary** node, take the machine offline and
M
Marcel Amirault 已提交
73 74 75 76 77
   prevent it from rebooting by any means at your disposal.
   Since there are many ways you may prefer to accomplish this, we will avoid a
   single recommendation. You may need to:

   - Reconfigure the load balancers.
78
   - Change DNS records (for example, point the primary DNS record to the **secondary**
M
Marcel Amirault 已提交
79 80 81 82 83 84 85
     node in order to stop usage of the **primary** node).
   - Stop the virtual servers.
   - Block traffic through a firewall.
   - Revoke object storage permissions from the **primary** node.
   - Physically disconnect a machine.

1. If you plan to [update the primary domain DNS record](#step-4-optional-updating-the-primary-domain-dns-record),
86 87 88 89
   you may wish to lower the TTL now to speed up propagation.

### Step 3. Promoting a **secondary** node

90 91 92 93 94 95 96 97
Note the following when promoting a secondary:

- A new **secondary** should not be added at this time. If you want to add a new
  **secondary**, do this after you have completed the entire process of promoting
  the **secondary** to the **primary**.
- If you encounter an `ActiveRecord::RecordInvalid: Validation failed: Name has already been taken`
  error during this process, please read
  [the troubleshooting advice](../replication/troubleshooting.md#fixing-errors-during-a-failover-or-when-promoting-a-secondary-to-a-primary-node).
98 99 100 101 102

#### Promoting a **secondary** node running on a single machine

1. SSH in to your **secondary** node and login as root:

103
   ```shell
M
Marcel Amirault 已提交
104 105
   sudo -i
   ```
106 107 108 109

1. Edit `/etc/gitlab/gitlab.rb` to reflect its new status as **primary** by
   removing any lines that enabled the `geo_secondary_role`:

M
Marcel Amirault 已提交
110 111 112
   ```ruby
   ## In pre-11.5 documentation, the role was enabled as follows. Remove this line.
   geo_secondary_role['enable'] = true
113

M
Marcel Amirault 已提交
114 115 116
   ## In 11.5+ documentation, the role was enabled as follows. Remove this line.
   roles ['geo_secondary_role']
   ```
117 118 119

1. Promote the **secondary** node to the **primary** node. Execute:

120
   ```shell
M
Marcel Amirault 已提交
121 122
   gitlab-ctl promote-to-primary-node
   ```
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

1. Verify you can connect to the newly promoted **primary** node using the URL used
   previously for the **secondary** node.
1. If successful, the **secondary** node has now been promoted to the **primary** node.

#### Promoting a **secondary** node with HA

The `gitlab-ctl promote-to-primary-node` command cannot be used yet in
conjunction with High Availability or with multiple machines, as it can only
perform changes on a **secondary** with only a single machine. Instead, you must
do this manually.

1. SSH in to the database node in the **secondary** and trigger PostgreSQL to
   promote to read-write:

138
   ```shell
M
Marcel Amirault 已提交
139 140
   sudo gitlab-pg-ctl promote
   ```
141 142 143 144 145

1. Edit `/etc/gitlab/gitlab.rb` on every machine in the **secondary** to
   reflect its new status as **primary** by removing any lines that enabled the
   `geo_secondary_role`:

M
Marcel Amirault 已提交
146 147 148
   ```ruby
   ## In pre-11.5 documentation, the role was enabled as follows. Remove this line.
   geo_secondary_role['enable'] = true
149

M
Marcel Amirault 已提交
150 151 152
   ## In 11.5+ documentation, the role was enabled as follows. Remove this line.
   roles ['geo_secondary_role']
   ```
153

M
Marcel Amirault 已提交
154 155
   After making these changes [Reconfigure GitLab](../../restart_gitlab.md#omnibus-gitlab-reconfigure) each
   machine so the changes take effect.
156 157 158 159

1. Promote the **secondary** to **primary**. SSH into a single application
   server and execute:

160
   ```shell
M
Marcel Amirault 已提交
161 162
   sudo gitlab-rake geo:set_secondary_as_primary
   ```
163 164 165 166 167 168 169 170 171 172 173 174 175

1. Verify you can connect to the newly promoted **primary** using the URL used
   previously for the **secondary**.
1. Success! The **secondary** has now been promoted to **primary**.

### Step 4. (Optional) Updating the primary domain DNS record

Updating the DNS records for the primary domain to point to the **secondary** node
will prevent the need to update all references to the primary domain to the
secondary domain, like changing Git remotes and API URLs.

1. SSH into the **secondary** node and login as root:

176
   ```shell
M
Marcel Amirault 已提交
177 178
   sudo -i
   ```
179 180 181 182 183

1. Update the primary domain's DNS record. After updating the primary domain's
   DNS records to point to the **secondary** node, edit `/etc/gitlab/gitlab.rb` on the
   **secondary** node to reflect the new URL:

M
Marcel Amirault 已提交
184 185 186 187
   ```ruby
   # Change the existing external_url configuration
   external_url 'https://<new_external_url>'
   ```
188

M
Marcel Amirault 已提交
189 190 191
   NOTE: **Note**
   Changing `external_url` won't prevent access via the old secondary URL, as
   long as the secondary DNS records are still intact.
192 193 194

1. Reconfigure the **secondary** node for the change to take effect:

195
   ```shell
M
Marcel Amirault 已提交
196 197
   gitlab-ctl reconfigure
   ```
198 199 200

1. Execute the command below to update the newly promoted **primary** node URL:

201
   ```shell
M
Marcel Amirault 已提交
202 203
   gitlab-rake geo:update_primary_node_url
   ```
204

M
Marcel Amirault 已提交
205 206
   This command will use the changed `external_url` configuration defined
   in `/etc/gitlab/gitlab.rb`.
207

208
1. For GitLab 11.11 through 12.7 only, you may need to update the **primary**
209 210 211 212 213
   node's name in the database. This bug has been fixed in GitLab 12.8.

   To determine if you need to do this, search for the
   `gitlab_rails["geo_node_name"]` setting in your `/etc/gitlab/gitlab.rb`
   file. If it is commented out with `#` or not found at all, then you will
214
   need to update the **primary** node's name in the database. You can search for it
215 216 217 218 219 220
   like so:

   ```shell
   grep "geo_node_name" /etc/gitlab/gitlab.rb
   ```

221
   To update the **primary** node's name in the database:
222 223 224 225 226

   ```shell
   gitlab-rails runner 'Gitlab::Geo.primary_node.update!(name: GeoNode.current_node_name)'
   ```

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
1. Verify you can connect to the newly promoted **primary** using its URL.
   If you updated the DNS records for the primary domain, these changes may
   not have yet propagated depending on the previous DNS records TTL.

### Step 5. (Optional) Add **secondary** Geo node to a promoted **primary** node

Promoting a **secondary** node to **primary** node using the process above does not enable
Geo on the new **primary** node.

To bring a new **secondary** node online, follow the [Geo setup instructions][setup-geo].

### Step 6. (Optional) Removing the secondary's tracking database

Every **secondary** has a special tracking database that is used to save the status of the synchronization of all the items from the **primary**.
Because the **secondary** is already promoted, that data in the tracking database is no longer required.

The data can be removed with the following command:

245
```shell
246 247 248 249 250 251 252 253 254 255 256 257 258
sudo rm -rf /var/opt/gitlab/geo-postgresql
```

## Promoting secondary Geo replica in multi-secondary configurations

If you have more than one **secondary** node and you need to promote one of them, we suggest you follow
[Promoting a **secondary** Geo node in single-secondary configurations](#promoting-a-secondary-geo-node-in-single-secondary-configurations)
and after that you also need two extra steps.

### Step 1. Prepare the new **primary** node to serve one or more **secondary** nodes

1. SSH into the new **primary** node and login as root:

259
   ```shell
M
Marcel Amirault 已提交
260 261
   sudo -i
   ```
262 263 264

1. Edit `/etc/gitlab/gitlab.rb`

M
Marcel Amirault 已提交
265 266 267
   ```ruby
   ## Enable a Geo Primary role (if you haven't yet)
   roles ['geo_primary_role']
268

M
Marcel Amirault 已提交
269 270 271 272 273
   ##
   # Allow PostgreSQL client authentication from the primary and secondary IPs. These IPs may be
   # public or VPC addresses in CIDR format, for example ['198.51.100.1/32', '198.51.100.2/32']
   ##
   postgresql['md5_auth_cidr_addresses'] = ['<primary_node_ip>/32', '<secondary_node_ip>/32']
274

M
Marcel Amirault 已提交
275 276
   # Every secondary server needs to have its own slot so specify the number of secondary nodes you're going to have
   postgresql['max_replication_slots'] = 1
277

M
Marcel Amirault 已提交
278 279 280 281 282 283
   ##
   ## Disable automatic database migrations temporarily
   ## (until PostgreSQL is restarted and listening on the private address).
   ##
   gitlab_rails['auto_migrate'] = false
   ```
284

M
Marcel Amirault 已提交
285
   (For more details about these settings you can read [Configure the primary server][configure-the-primary-server])
286 287 288 289

1. Save the file and reconfigure GitLab for the database listen changes and
   the replication slot changes to be applied.

290
   ```shell
M
Marcel Amirault 已提交
291 292
   gitlab-ctl reconfigure
   ```
293

M
Marcel Amirault 已提交
294
   Restart PostgreSQL for its changes to take effect:
295

296
   ```shell
M
Marcel Amirault 已提交
297 298
   gitlab-ctl restart postgresql
   ```
299 300 301 302

1. Re-enable migrations now that PostgreSQL is restarted and listening on the
   private address.

M
Marcel Amirault 已提交
303
   Edit `/etc/gitlab/gitlab.rb` and **change** the configuration to `true`:
304

M
Marcel Amirault 已提交
305 306 307
   ```ruby
   gitlab_rails['auto_migrate'] = true
   ```
308

M
Marcel Amirault 已提交
309
   Save the file and reconfigure GitLab:
310

311
   ```shell
M
Marcel Amirault 已提交
312 313
   gitlab-ctl reconfigure
   ```
314 315 316 317 318 319 320 321 322

### Step 2. Initiate the replication process

Now we need to make each **secondary** node listen to changes on the new **primary** node. To do that you need
to [initiate the replication process][initiate-the-replication-process] again but this time
for another **primary** node. All the old replication settings will be overwritten.

## Troubleshooting

M
Marcel Amirault 已提交
323
### I followed the disaster recovery instructions and now two-factor auth is broken
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

The setup instructions for Geo prior to 10.5 failed to replicate the
`otp_key_base` secret, which is used to encrypt the two-factor authentication
secrets stored in the database. If it differs between **primary** and **secondary**
nodes, users with two-factor authentication enabled won't be able to log in
after a failover.

If you still have access to the old **primary** node, you can follow the
instructions in the
[Upgrading to GitLab 10.5][updating-geo]
section to resolve the error. Otherwise, the secret is lost and you'll need to
[reset two-factor authentication for all users][sec-tfa].

[gitlab-org&65]: https://gitlab.com/groups/gitlab-org/-/epics/65
[geo-limitations]: ../replication/index.md#current-limitations
[planned-failover]: planned_failover.md
[setup-geo]: ../replication/index.md#setup-instructions
341
[updating-geo]: ../replication/version_specific_updates.md#updating-to-gitlab-105
342 343 344
[sec-tfa]: ../../../security/two_factor_authentication.md#disabling-2fa-for-everyone
[initiate-the-replication-process]: ../replication/database.html#step-3-initiate-the-replication-process
[configure-the-primary-server]: ../replication/database.html#step-1-configure-the-primary-server