2020-11-24 15:15:51 +05:30
---
2022-07-23 23:45:48 +05:30
stage: Systems
2020-11-24 15:15:51 +05:30
group: Geo
2022-11-25 23:54:43 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments
2020-11-24 15:15:51 +05:30
type: howto
---
2021-03-11 19:13:27 +05:30
# Geo database replication **(PREMIUM SELF)**
2020-11-24 15:15:51 +05:30
2021-12-11 22:18:48 +05:30
This document describes the minimal required steps to replicate your primary
2022-08-27 11:52:29 +05:30
GitLab database to a secondary site's database. You may have to change some
2021-12-11 22:18:48 +05:30
values, based on attributes including your database's setup and size.
2020-11-24 15:15:51 +05:30
2021-02-22 17:27:13 +05:30
NOTE:
2021-12-11 22:18:48 +05:30
If your GitLab installation uses external (not managed by Omnibus GitLab)
PostgreSQL instances, the Omnibus roles cannot perform all necessary
configuration steps. In this case, use the [Geo with external PostgreSQL instances ](external_database.md )
process instead.
2020-11-24 15:15:51 +05:30
2022-08-13 15:12:31 +05:30
NOTE:
2021-12-11 22:18:48 +05:30
The stages of the setup process must be completed in the documented order.
2022-08-13 15:12:31 +05:30
If not, [complete all prior stages ](../setup/index.md#using-omnibus-gitlab ) before proceeding.
Ensure the **secondary** site is running the same version of GitLab Enterprise Edition as the **primary** site. Confirm you have added the [premium or higher licenses ](https://about.gitlab.com/pricing/ ) to your **primary** site.
2020-11-24 15:15:51 +05:30
2021-12-11 22:18:48 +05:30
Be sure to read and review all of these steps before you execute them in your
testing or production environments.
2020-11-24 15:15:51 +05:30
2021-09-04 01:27:46 +05:30
## Single instance database replication
2020-11-24 15:15:51 +05:30
2021-09-04 01:27:46 +05:30
A single instance database replication is easier to set up and still provides the same Geo capabilities
as a clusterized alternative. It's useful for setups running on a single machine
or trying to evaluate Geo for a future clusterized installation.
2021-09-30 23:02:18 +05:30
A single instance can be expanded to a clusterized version using Patroni, which is recommended for a
2021-09-04 01:27:46 +05:30
highly available architecture.
2023-03-04 22:38:38 +05:30
Follow the instructions below on how to set up PostgreSQL replication as a single instance database.
2021-09-30 23:02:18 +05:30
Alternatively, you can look at the [Multi-node database replication ](#multi-node-database-replication )
2021-09-04 01:27:46 +05:30
instructions on setting up replication with a Patroni cluster.
### PostgreSQL replication
2022-08-27 11:52:29 +05:30
The GitLab **primary** site where the write operations happen connects to
2023-03-04 22:38:38 +05:30
the **primary** database server. **Secondary** sites
2022-04-04 11:22:00 +05:30
connect to their own database servers (which are read-only).
2020-11-24 15:15:51 +05:30
2023-03-04 22:38:38 +05:30
You should use [PostgreSQL's replication slots ](https://medium.com/@tk512/replication-slots-in-postgresql-b4b03d277c75 )
2022-08-27 11:52:29 +05:30
to ensure that the **primary** site retains all the data necessary for the **secondary** sites to
2020-11-24 15:15:51 +05:30
recover. See below for more details.
The following guide assumes that:
2023-03-04 22:38:38 +05:30
- You are using Omnibus and therefore you are using PostgreSQL 12 or later,
2021-09-04 01:27:46 +05:30
which includes the [`pg_basebackup` tool ](https://www.postgresql.org/docs/12/app-pgbasebackup.html ).
2022-08-27 11:52:29 +05:30
- You have a **primary** site already set up (the GitLab server you are
2020-11-24 15:15:51 +05:30
replicating from), running Omnibus' PostgreSQL (or equivalent version), and
2022-08-27 11:52:29 +05:30
you have a new **secondary** site set up with the same
2022-08-13 15:12:31 +05:30
[versions of PostgreSQL ](../index.md#requirements-for-running-geo ),
2022-08-27 11:52:29 +05:30
OS, and GitLab on all sites.
2020-11-24 15:15:51 +05:30
2021-02-22 17:27:13 +05:30
WARNING:
2020-11-24 15:15:51 +05:30
Geo works with streaming replication. Logical replication is not supported at this time.
There is an [issue where support is being discussed ](https://gitlab.com/gitlab-org/gitlab/-/issues/7420 ).
2022-08-27 11:52:29 +05:30
#### Step 1. Configure the **primary** site
2020-11-24 15:15:51 +05:30
2022-08-27 11:52:29 +05:30
1. SSH into your GitLab **primary** site and login as root:
2020-11-24 15:15:51 +05:30
```shell
sudo -i
```
2021-10-27 15:23:28 +05:30
1. Edit `/etc/gitlab/gitlab.rb` and add a **unique** name for your site:
2020-11-24 15:15:51 +05:30
```ruby
2021-10-27 15:23:28 +05:30
##
## The unique identifier for the Geo site. See
## https://docs.gitlab.com/ee/user/admin_area/geo_nodes.html#common-settings
##
gitlab_rails['geo_node_name'] = '< site_name_here > '
2020-11-24 15:15:51 +05:30
```
2022-08-27 11:52:29 +05:30
1. Reconfigure the **primary** site for the change to take effect:
2020-11-24 15:15:51 +05:30
```shell
gitlab-ctl reconfigure
```
2022-08-27 11:52:29 +05:30
1. Execute the command below to define the site as **primary** site:
2020-11-24 15:15:51 +05:30
```shell
gitlab-ctl set-geo-primary-node
```
2021-09-04 01:27:46 +05:30
This command uses your defined `external_url` in `/etc/gitlab/gitlab.rb` .
2020-11-24 15:15:51 +05:30
2021-09-04 01:27:46 +05:30
1. Define a password for the `gitlab` database user:
2020-11-24 15:15:51 +05:30
Generate a MD5 hash of the desired password:
```shell
gitlab-ctl pg-password-md5 gitlab
# Enter password: < your_password_here >
# Confirm password: < your_password_here >
# fca0b89a972d69f00eb3ec98a5838484
```
Edit `/etc/gitlab/gitlab.rb` :
```ruby
# Fill with the hash generated by `gitlab-ctl pg-password-md5 gitlab`
postgresql['sql_user_password'] = '< md5_hash_of_your_password > '
# Every node that runs Puma or Sidekiq needs to have the database
# password specified as below. If you have a high-availability setup, this
# must be present in all application nodes.
gitlab_rails['db_password'] = '< your_password_here > '
```
2021-09-30 23:02:18 +05:30
2021-09-04 01:27:46 +05:30
1. Define a password for the database [replication user ](https://wiki.postgresql.org/wiki/Streaming_Replication ).
2020-11-24 15:15:51 +05:30
2023-03-04 22:38:38 +05:30
Use the username defined in `/etc/gitlab/gitlab.rb` under the `postgresql['sql_replication_user']`
setting. The default value is `gitlab_replicator` . If you changed the username to something else, adapt
2021-09-04 01:27:46 +05:30
the instructions below.
2021-09-30 23:02:18 +05:30
2021-09-04 01:27:46 +05:30
Generate a MD5 hash of the desired password:
2020-11-24 15:15:51 +05:30
```shell
2021-09-04 01:27:46 +05:30
gitlab-ctl pg-password-md5 gitlab_replicator
# Enter password: < your_password_here >
# Confirm password: < your_password_here >
# 950233c0dfc2f39c64cf30457c3b7f1e
2020-11-24 15:15:51 +05:30
```
2021-09-04 01:27:46 +05:30
Edit `/etc/gitlab/gitlab.rb` :
```ruby
# Fill with the hash generated by `gitlab-ctl pg-password-md5 gitlab_replicator`
postgresql['sql_replication_password'] = '< md5_hash_of_your_password > '
```
2020-11-24 15:15:51 +05:30
If you are using an external database not managed by Omnibus GitLab, you need
2023-03-04 22:38:38 +05:30
to create the `gitlab_replicator` user and define a password for that user manually:
2020-11-24 15:15:51 +05:30
```sql
--- Create a new user 'replicator'
CREATE USER gitlab_replicator;
--- Set/change a password and grants replication privilege
ALTER USER gitlab_replicator WITH REPLICATION ENCRYPTED PASSWORD '< replication_password > ';
```
2023-05-27 22:25:52 +05:30
1. Edit `/etc/gitlab/gitlab.rb` and set the role to `geo_primary_role` (for more information, see [Geo roles ](https://docs.gitlab.com/omnibus/roles/#gitlab-geo-roles )):
```ruby
## Geo Primary role
roles(['geo_primary_role'])
```
2020-11-24 15:15:51 +05:30
1. Configure PostgreSQL to listen on network interfaces:
For security reasons, PostgreSQL does not listen on any network interfaces
2022-08-27 11:52:29 +05:30
by default. However, Geo requires the **secondary** site to be able to
2023-03-04 22:38:38 +05:30
connect to the **primary** site's database. For this reason, you need the IP address of
2022-08-27 11:52:29 +05:30
each site.
2020-11-24 15:15:51 +05:30
2021-02-22 17:27:13 +05:30
NOTE:
2020-11-24 15:15:51 +05:30
For external PostgreSQL instances, see [additional instructions ](external_database.md ).
2023-03-04 22:38:38 +05:30
If you are using a cloud provider, you can look up the addresses for each
2022-08-27 11:52:29 +05:30
Geo site through your cloud provider's management console.
2020-11-24 15:15:51 +05:30
2023-03-04 22:38:38 +05:30
To look up the address of a Geo site, SSH into the Geo site and execute:
2020-11-24 15:15:51 +05:30
```shell
##
## Private address
##
ip route get 255.255.255.255 | awk '{print "Private address:", $NF; exit}'
##
## Public address
##
2021-02-22 17:27:13 +05:30
echo "External address: $(curl --silent "ipinfo.io/ip")"
2020-11-24 15:15:51 +05:30
```
2021-09-04 01:27:46 +05:30
In most cases, the following addresses are used to configure GitLab
2020-11-24 15:15:51 +05:30
Geo:
2022-07-23 23:45:48 +05:30
| Configuration | Address |
|:----------------------------------------|:----------------------------------------------------------------------|
2022-08-27 11:52:29 +05:30
| `postgresql['listen_address']` | **Primary** site's public or VPC private address. |
| `postgresql['md5_auth_cidr_addresses']` | **Primary** and **Secondary** sites' public or VPC private addresses. |
2020-11-24 15:15:51 +05:30
If you are using Google Cloud Platform, SoftLayer, or any other vendor that
2023-03-04 22:38:38 +05:30
provides a virtual private cloud (VPC), you can use the **primary** and **secondary** sites'
2020-11-24 15:15:51 +05:30
private addresses (corresponds to "internal address" for Google Cloud Platform) for
`postgresql['md5_auth_cidr_addresses']` and `postgresql['listen_address']` .
The `listen_address` option opens PostgreSQL up to network connections with the interface
2021-09-04 01:27:46 +05:30
corresponding to the given address. See [the PostgreSQL documentation ](https://www.postgresql.org/docs/12/runtime-config-connection.html )
2020-11-24 15:15:51 +05:30
for more details.
2021-02-22 17:27:13 +05:30
NOTE:
2023-03-04 22:38:38 +05:30
If you need to use `0.0.0.0` or `*` as the `listen_address` , you also must add
2020-11-24 15:15:51 +05:30
`127.0.0.1/32` to the `postgresql['md5_auth_cidr_addresses']` setting, to allow Rails to connect through
`127.0.0.1` . For more information, see [omnibus-5258 ](https://gitlab.com/gitlab-org/omnibus-gitlab/-/issues/5258 ).
2023-03-04 22:38:38 +05:30
Depending on your network configuration, the suggested addresses may
be incorrect. If your **primary** site and **secondary** sites connect over a local
2020-11-24 15:15:51 +05:30
area network, or a virtual network connecting availability zones like
2023-03-04 22:38:38 +05:30
[Amazon's VPC ](https://aws.amazon.com/vpc/ ) or [Google's VPC ](https://cloud.google.com/vpc/ ),
2022-08-27 11:52:29 +05:30
you should use the **secondary** site's private address for `postgresql['md5_auth_cidr_addresses']` .
2020-11-24 15:15:51 +05:30
Edit `/etc/gitlab/gitlab.rb` and add the following, replacing the IP
addresses with addresses appropriate to your network configuration:
```ruby
##
## Primary address
## - replace '< primary_node_ip > ' with the public or VPC address of your Geo primary node
##
2022-08-27 11:52:29 +05:30
postgresql['listen_address'] = '< primary_site_ip > '
2020-11-24 15:15:51 +05:30
##
# 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']
##
2022-08-27 11:52:29 +05:30
postgresql['md5_auth_cidr_addresses'] = ['< primary_site_ip > /32', '< secondary_site_ip > /32']
2020-11-24 15:15:51 +05:30
##
## Replication settings
##
2022-08-27 11:52:29 +05:30
# postgresql['max_replication_slots'] = 1 # Set this to be the number of Geo secondary nodes if you have more than one
2020-11-24 15:15:51 +05:30
# postgresql['max_wal_senders'] = 10
# postgresql['wal_keep_segments'] = 10
2023-05-27 22:25:52 +05:30
```
2020-11-24 15:15:51 +05:30
2023-05-27 22:25:52 +05:30
1. Disable automatic database migrations temporarily until PostgreSQL is restarted and listening on the private address.
Edit `/etc/gitlab/gitlab.rb` and change the configuration to false:
```ruby
## Disable automatic database migrations
2020-11-24 15:15:51 +05:30
gitlab_rails['auto_migrate'] = false
```
2022-08-27 11:52:29 +05:30
1. Optional: If you want to add another **secondary** site, the relevant setting would look like:
2020-11-24 15:15:51 +05:30
```ruby
2022-08-27 11:52:29 +05:30
postgresql['md5_auth_cidr_addresses'] = ['< primary_site_ip > /32', '< secondary_site_ip > /32', '< another_secondary_site_ip > /32']
2020-11-24 15:15:51 +05:30
```
You may also want to edit the `wal_keep_segments` and `max_wal_senders` to match your
2021-09-04 01:27:46 +05:30
database replication requirements. Consult the [PostgreSQL - Replication documentation ](https://www.postgresql.org/docs/12/runtime-config-replication.html )
2020-11-24 15:15:51 +05:30
for more information.
1. Save the file and reconfigure GitLab for the database listen changes and
the replication slot changes to be applied:
```shell
gitlab-ctl reconfigure
```
Restart PostgreSQL for its changes to take effect:
```shell
gitlab-ctl restart postgresql
```
1. Re-enable migrations now that PostgreSQL is restarted and listening on the
private address.
Edit `/etc/gitlab/gitlab.rb` and **change** the configuration to `true` :
```ruby
gitlab_rails['auto_migrate'] = true
```
Save the file and reconfigure GitLab:
```shell
gitlab-ctl reconfigure
```
1. Now that the PostgreSQL server is set up to accept remote connections, run
2023-03-04 22:38:38 +05:30
`netstat -plnt | grep 5432` to ensure that PostgreSQL is listening on port
2022-08-27 11:52:29 +05:30
`5432` to the **primary** site's private address.
2020-11-24 15:15:51 +05:30
1. A certificate was automatically generated when GitLab was reconfigured. This
2021-09-04 01:27:46 +05:30
is used automatically to protect your PostgreSQL traffic from
2023-03-04 22:38:38 +05:30
eavesdroppers. To protect against active ("man-in-the-middle") attackers,
2022-08-27 11:52:29 +05:30
the **secondary** site needs a copy of the certificate. Make a copy of the PostgreSQL
`server.crt` file on the **primary** site by running this command:
2020-11-24 15:15:51 +05:30
```shell
cat ~gitlab-psql/data/server.crt
```
2023-03-04 22:38:38 +05:30
Copy the output to the clipboard or into a local file. You
2022-08-27 11:52:29 +05:30
need it when setting up the **secondary** site! The certificate is not sensitive
2020-11-24 15:15:51 +05:30
data.
2022-07-16 23:28:13 +05:30
However, this certificate is created with a generic `PostgreSQL` Common Name. For this,
you must use the `verify-ca` mode when replicating the database, otherwise,
2023-03-04 22:38:38 +05:30
the hostname mismatch causes errors.
2022-07-16 23:28:13 +05:30
1. Optional. Generate your own SSL certificate and manually
[configure SSL for PostgreSQL ](https://docs.gitlab.com/omnibus/settings/database.html#configuring-ssl ),
instead of using the generated certificate.
2023-03-04 22:38:38 +05:30
You need at least the SSL certificate and key. Set the `postgresql['ssl_cert_file']` and
2022-07-16 23:28:13 +05:30
`postgresql['ssl_key_file']` values to their full paths, as per the Database SSL docs.
This allows you to use the `verify-full` SSL mode when replicating the database
and get the extra benefit of verifying the full hostname in the CN.
You can use this certificate (that you have also set in `postgresql['ssl_cert_file']` ) instead
2023-03-04 22:38:38 +05:30
of the certificate from the point above going forward. This allows you to use `verify-full`
2022-07-16 23:28:13 +05:30
without replication errors if the CN matches.
2021-09-04 01:27:46 +05:30
#### Step 2. Configure the **secondary** server
2020-11-24 15:15:51 +05:30
2022-08-27 11:52:29 +05:30
1. SSH into your GitLab **secondary** site and login as root:
2020-11-24 15:15:51 +05:30
```shell
sudo -i
```
1. Stop application server and Sidekiq
```shell
gitlab-ctl stop puma
gitlab-ctl stop sidekiq
```
2021-02-22 17:27:13 +05:30
NOTE:
2023-03-04 22:38:38 +05:30
This step is important so you don't try to execute anything before the site is fully configured.
2020-11-24 15:15:51 +05:30
2022-08-27 11:52:29 +05:30
1. [Check TCP connectivity ](../../raketasks/maintenance.md ) to the **primary** site's PostgreSQL server:
2020-11-24 15:15:51 +05:30
```shell
2022-08-27 11:52:29 +05:30
gitlab-rake gitlab:tcp_check[< primary_site_ip > ,5432]
2020-11-24 15:15:51 +05:30
```
2021-02-22 17:27:13 +05:30
NOTE:
2020-11-24 15:15:51 +05:30
If this step fails, you may be using the wrong IP address, or a firewall may
2022-08-27 11:52:29 +05:30
be preventing access to the site. Check the IP address, paying close
2023-03-04 22:38:38 +05:30
attention to the difference between public and private addresses. Ensure
2022-08-27 11:52:29 +05:30
that, if a firewall is present, the **secondary** site is permitted to connect to the
**primary** site on port 5432.
2020-11-24 15:15:51 +05:30
2022-08-27 11:52:29 +05:30
1. Create a file `server.crt` in the **secondary** site, with the content you got on the last step of the **primary** site's setup:
2020-11-24 15:15:51 +05:30
```shell
editor server.crt
```
2022-08-27 11:52:29 +05:30
1. Set up PostgreSQL TLS verification on the **secondary** site:
2020-11-24 15:15:51 +05:30
Install the `server.crt` file:
```shell
install \
-D \
-o gitlab-psql \
-g gitlab-psql \
-m 0400 \
-T server.crt ~gitlab-psql/.postgresql/root.crt
```
2021-09-04 01:27:46 +05:30
PostgreSQL now only recognizes that exact certificate when verifying TLS
2020-11-24 15:15:51 +05:30
connections. The certificate can only be replicated by someone with access
2022-08-27 11:52:29 +05:30
to the private key, which is **only** present on the **primary** site.
2020-11-24 15:15:51 +05:30
2022-08-27 11:52:29 +05:30
1. Test that the `gitlab-psql` user can connect to the **primary** site's database
2020-11-24 15:15:51 +05:30
(the default Omnibus database name is `gitlabhq_production` ):
```shell
sudo \
-u gitlab-psql /opt/gitlab/embedded/bin/psql \
--list \
-U gitlab_replicator \
-d "dbname=gitlabhq_production sslmode=verify-ca" \
-W \
2022-08-27 11:52:29 +05:30
-h < primary_site_ip >
2020-11-24 15:15:51 +05:30
```
2022-07-16 23:28:13 +05:30
NOTE:
2023-03-04 22:38:38 +05:30
If you are using manually generated certificates and want to use
`sslmode=verify-full` to benefit from the full hostname verification,
replace `verify-ca` with `verify-full` when
2022-07-16 23:28:13 +05:30
running the command.
2023-03-04 22:38:38 +05:30
When prompted, enter the _plaintext_ password you set in the first step for the
2020-11-24 15:15:51 +05:30
`gitlab_replicator` user. If all worked correctly, you should see
2023-03-04 22:38:38 +05:30
the list of the **primary** site's databases.
2020-11-24 15:15:51 +05:30
A failure to connect here indicates that the TLS configuration is incorrect.
2022-08-27 11:52:29 +05:30
Ensure that the contents of `~gitlab-psql/data/server.crt` on the **primary** site
match the contents of `~gitlab-psql/.postgresql/root.crt` on the **secondary** site.
2020-11-24 15:15:51 +05:30
2023-05-27 22:25:52 +05:30
1. Edit `/etc/gitlab/gitlab.rb` and set the role to `geo_secondary_role` (for more information, see [Geo roles ](https://docs.gitlab.com/omnibus/roles/#gitlab-geo-roles )):
```ruby
##
## Geo Secondary role
## - configure dependent flags automatically to enable Geo
##
roles(['geo_secondary_role'])
```
2020-11-24 15:15:51 +05:30
1. Configure PostgreSQL:
2023-03-04 22:38:38 +05:30
This step is similar to how you configured the **primary** instance.
You must enable this, even if using a single node.
2020-11-24 15:15:51 +05:30
Edit `/etc/gitlab/gitlab.rb` and add the following, replacing the IP
addresses with addresses appropriate to your network configuration:
```ruby
##
## Secondary address
2022-08-27 11:52:29 +05:30
## - replace '< secondary_site_ip > ' with the public or VPC address of your Geo secondary site
2020-11-24 15:15:51 +05:30
##
2022-08-27 11:52:29 +05:30
postgresql['listen_address'] = '< secondary_site_ip > '
postgresql['md5_auth_cidr_addresses'] = ['< secondary_site_ip > /32']
2020-11-24 15:15:51 +05:30
##
2022-08-27 11:52:29 +05:30
## Database credentials password (defined previously in primary site)
## - replicate same values here as defined in primary site
2020-11-24 15:15:51 +05:30
##
2021-09-04 01:27:46 +05:30
postgresql['sql_replication_password'] = '< md5_hash_of_your_password > '
2020-11-24 15:15:51 +05:30
postgresql['sql_user_password'] = '< md5_hash_of_your_password > '
gitlab_rails['db_password'] = '< your_password_here > '
```
For external PostgreSQL instances, see [additional instructions ](external_database.md ).
2022-08-27 11:52:29 +05:30
If you bring a former **primary** site back online to serve as a **secondary** site, then you also must remove `roles(['geo_primary_role'])` or `geo_primary_role['enable'] = true` .
2020-11-24 15:15:51 +05:30
1. Reconfigure GitLab for the changes to take effect:
```shell
gitlab-ctl reconfigure
```
1. Restart PostgreSQL for the IP change to take effect:
```shell
gitlab-ctl restart postgresql
```
2021-09-04 01:27:46 +05:30
#### Step 3. Initiate the replication process
2020-11-24 15:15:51 +05:30
2023-03-04 22:38:38 +05:30
Below is a script that connects the database on the **secondary** site to
the database on the **primary** site. This script replicates the database and creates the
2020-11-24 15:15:51 +05:30
needed files for streaming replication.
The directories used are the defaults that are set up in Omnibus. If you have
2023-03-04 22:38:38 +05:30
changed any defaults, configure the script accordingly, replacing any directories and paths.
2020-11-24 15:15:51 +05:30
2021-02-22 17:27:13 +05:30
WARNING:
2022-08-27 11:52:29 +05:30
Make sure to run this on the **secondary** site as it removes all PostgreSQL's
2020-11-24 15:15:51 +05:30
data before running `pg_basebackup` .
2022-08-27 11:52:29 +05:30
1. SSH into your GitLab **secondary** site and login as root:
2020-11-24 15:15:51 +05:30
```shell
sudo -i
```
2022-08-27 11:52:29 +05:30
1. Choose a database-friendly name to use for your **secondary** site to
2020-11-24 15:15:51 +05:30
use as the replication slot name. For example, if your domain is
2023-03-04 22:38:38 +05:30
`secondary.geo.example.com` , use `secondary_example` as the slot
2020-11-24 15:15:51 +05:30
name as shown in the commands below.
1. Execute the command below to start a backup/restore and begin the replication
2021-02-22 17:27:13 +05:30
WARNING:
2022-08-27 11:52:29 +05:30
Each Geo **secondary** site must have its own unique replication slot name.
2021-09-04 01:27:46 +05:30
Using the same slot name between two secondaries breaks PostgreSQL replication.
2020-11-24 15:15:51 +05:30
2023-01-13 00:05:48 +05:30
NOTE:
Replication slot names must only contain lowercase letters, numbers, and the underscore character.
When prompted, enter the _plaintext_ password you set up for the `gitlab_replicator`
user in the first step.
2020-11-24 15:15:51 +05:30
```shell
gitlab-ctl replicate-geo-database \
2022-08-27 11:52:29 +05:30
--slot-name=< secondary_site_name > \
--host=< primary_site_ip > \
2022-07-16 23:28:13 +05:30
--sslmode=verify-ca
2020-11-24 15:15:51 +05:30
```
2022-07-16 23:28:13 +05:30
NOTE:
2023-03-04 22:38:38 +05:30
If you have generated custom PostgreSQL certificates, you need to use
2022-07-16 23:28:13 +05:30
`--sslmode=verify-full` (or omit the `sslmode` line entirely), to benefit from the extra
validation of the full host name in the certificate CN / SAN for additional security.
2023-03-04 22:38:38 +05:30
Otherwise, using the automatically created certificate with `verify-full` fails,
as it has a generic `PostgreSQL` CN which doesn't match the `--host` value in this command.
2022-07-16 23:28:13 +05:30
2020-11-24 15:15:51 +05:30
This command also takes a number of additional options. You can use `--help`
2023-03-04 22:38:38 +05:30
to list them all, but here are some tips:
2020-11-24 15:15:51 +05:30
2023-03-04 22:38:38 +05:30
- If PostgreSQL is listening on a non-standard port, add `--port=` .
2021-09-04 01:27:46 +05:30
- If your database is too large to be transferred in 30 minutes, you need
2023-03-04 22:38:38 +05:30
to increase the timeout. For example, use `--backup-timeout=3600` if you expect the
2020-11-24 15:15:51 +05:30
initial replication to take under an hour.
- Pass `--sslmode=disable` to skip PostgreSQL TLS authentication altogether
2021-09-30 23:02:18 +05:30
(for example, you know the network path is secure, or you are using a site-to-site
2022-05-07 20:08:51 +05:30
VPN). It is **not** safe over the public Internet!
2020-11-24 15:15:51 +05:30
- You can read more details about each `sslmode` in the
2023-03-04 22:38:38 +05:30
[PostgreSQL documentation ](https://www.postgresql.org/docs/12/libpq-ssl.html#LIBPQ-SSL-PROTECTION ).
The instructions above are carefully written to ensure protection against
2020-11-24 15:15:51 +05:30
both passive eavesdroppers and active "man-in-the-middle" attackers.
- Change the `--slot-name` to the name of the replication slot
2021-09-04 01:27:46 +05:30
to be used on the **primary** database. The script attempts to create the
2020-11-24 15:15:51 +05:30
replication slot automatically if it does not exist.
2022-08-27 11:52:29 +05:30
- If you're repurposing an old site into a Geo **secondary** site, you must
2020-11-24 15:15:51 +05:30
add `--force` to the command line.
2023-03-04 22:38:38 +05:30
- When not in a production machine, you can disable the backup step (if you
are certain this is what you want) by adding `--skip-backup` .
- If you are using PgBouncer, you need to target the database host directly.
- If you are using Patroni on your primary site, you must target the current leader host.
- If you are using a load balancer proxy (for example HAProxy) and it is targeting the Patroni leader for the primary, you should target the load balancer proxy instead.
2020-11-24 15:15:51 +05:30
The replication process is now complete.
2021-09-04 01:27:46 +05:30
### PgBouncer support (optional)
2020-11-24 15:15:51 +05:30
[PgBouncer ](https://www.pgbouncer.org/ ) may be used with GitLab Geo to pool
2021-09-30 23:02:18 +05:30
PostgreSQL connections, which can improve performance even when using in a
single instance installation.
2021-09-04 01:27:46 +05:30
2023-03-04 22:38:38 +05:30
You should use PgBouncer if you use GitLab in a highly available
2021-09-30 23:02:18 +05:30
configuration with a cluster of nodes supporting a Geo **primary** site and
2023-03-04 22:38:38 +05:30
two other clusters of nodes supporting a Geo **secondary** site. You need two PgBouncer nodes: one for the
2021-09-04 01:27:46 +05:30
main database and the other for the tracking database. For more information,
2021-06-08 01:23:25 +05:30
see [High Availability with Omnibus GitLab ](../../postgresql/replication_and_failover.md ).
2020-11-24 15:15:51 +05:30
2021-11-11 11:23:49 +05:30
### Changing the replication password
To change the password for the [replication user ](https://wiki.postgresql.org/wiki/Streaming_Replication )
when using Omnibus-managed PostgreSQL instances:
2022-08-27 11:52:29 +05:30
On the GitLab Geo **primary** site:
2021-11-11 11:23:49 +05:30
1. The default value for the replication user is `gitlab_replicator` , but if you've set a custom replication
2023-03-04 22:38:38 +05:30
user in your `/etc/gitlab/gitlab.rb` under the `postgresql['sql_replication_user']` setting, ensure you
2021-11-11 11:23:49 +05:30
adapt the following instructions for your own user.
Generate an MD5 hash of the desired password:
```shell
sudo gitlab-ctl pg-password-md5 gitlab_replicator
# Enter password: < your_password_here >
# Confirm password: < your_password_here >
# 950233c0dfc2f39c64cf30457c3b7f1e
```
Edit `/etc/gitlab/gitlab.rb` :
```ruby
# Fill with the hash generated by `gitlab-ctl pg-password-md5 gitlab_replicator`
postgresql['sql_replication_password'] = '< md5_hash_of_your_password > '
```
1. Save the file and reconfigure GitLab to change the replication user's password in PostgreSQL:
```shell
sudo gitlab-ctl reconfigure
```
1. Restart PostgreSQL for the replication password change to take effect:
```shell
sudo gitlab-ctl restart postgresql
```
2022-08-27 11:52:29 +05:30
Until the password is updated on any **secondary** sites, the [PostgreSQL log ](../../logs/index.md#postgresql-logs ) on
2023-03-04 22:38:38 +05:30
the secondaries report the following error message:
2021-11-11 11:23:49 +05:30
```console
FATAL: could not connect to the primary server: FATAL: password authentication failed for user "gitlab_replicator"
```
2022-08-27 11:52:29 +05:30
On all GitLab Geo **secondary** sites:
2021-11-11 11:23:49 +05:30
2022-05-07 20:08:51 +05:30
1. The first step isn't necessary from a configuration perspective, because the hashed `'sql_replication_password'`
2022-08-27 11:52:29 +05:30
is not used on the GitLab Geo **secondary** sites. However in the event that **secondary** site needs to be promoted
to the GitLab Geo **primary** , make sure to match the `'sql_replication_password'` in the **secondary** site configuration.
2021-11-11 11:23:49 +05:30
Edit `/etc/gitlab/gitlab.rb` :
```ruby
# Fill with the hash generated by `gitlab-ctl pg-password-md5 gitlab_replicator` on the Geo primary
postgresql['sql_replication_password'] = '< md5_hash_of_your_password > '
```
1. During the initial replication setup, the `gitlab-ctl replicate-geo-database` command writes the plaintext
password for the replication user account to two locations:
- `gitlab-geo.conf` : Used by the PostgreSQL replication process, written to the PostgreSQL data
directory, by default at `/var/opt/gitlab/postgresql/data/gitlab-geo.conf` .
- `.pgpass` : Used by the `gitlab-psql` user, located by default at `/var/opt/gitlab/postgresql/.pgpass` .
Update the plaintext password in both of these files, and restart PostgreSQL:
```shell
sudo gitlab-ctl restart postgresql
```
2021-09-04 01:27:46 +05:30
## Multi-node database replication
2021-01-29 00:20:46 +05:30
2021-09-04 01:27:46 +05:30
In GitLab 14.0, Patroni replaced `repmgr` as the supported
[highly available PostgreSQL solution ](../../postgresql/replication_and_failover.md ).
2021-01-29 00:20:46 +05:30
2021-09-04 01:27:46 +05:30
NOTE:
If you still haven't [migrated from repmgr to Patroni ](#migrating-from-repmgr-to-patroni ) you're highly advised to do so.
2021-01-29 00:20:46 +05:30
2022-08-27 11:52:29 +05:30
### Migrating from repmgr to Patroni
2023-03-04 22:38:38 +05:30
1. Before migrating, you should ensure there is no replication lag between the **primary** and **secondary** sites and that replication is paused. In GitLab 13.2 and later, you can pause and resume replication with `gitlab-ctl geo-replication-pause` and `gitlab-ctl geo-replication-resume` on a Geo secondary database node.
2022-08-27 11:52:29 +05:30
1. Follow the [instructions to migrate repmgr to Patroni ](../../postgresql/replication_and_failover.md#switching-from-repmgr-to-patroni ). When configuring Patroni on each **primary** site database node, add `patroni['replication_slots'] = { '<slot_name>' => 'physical' }`
2023-03-04 22:38:38 +05:30
to `gitlab.rb` where `<slot_name>` is the name of the replication slot for your **secondary** site. This ensures that Patroni recognizes the replication slot as permanent and doesn't drop it upon restarting.
1. If database replication to the **secondary** site was paused before migration, resume replication after Patroni is confirmed as working on the **primary** site.
2022-08-27 11:52:29 +05:30
### Migrating a single PostgreSQL node to Patroni
Before the introduction of Patroni, Geo had no Omnibus support for HA setups on the **secondary** site.
2023-03-04 22:38:38 +05:30
With Patroni, this support is now possible. To migrate the existing PostgreSQL to Patroni:
2022-08-27 11:52:29 +05:30
1. Make sure you have a Consul cluster setup on the secondary (similar to how you set it up on the **primary** site).
1. [Configure a permanent replication slot ](#step-1-configure-patroni-permanent-replication-slot-on-the-primary-site ).
1. [Configure the internal load balancer ](#step-2-configure-the-internal-load-balancer-on-the-primary-site ).
1. [Configure a PgBouncer node ](#step-3-configure-pgbouncer-nodes-on-the-secondary-site )
1. [Configure a Standby Cluster ](#step-4-configure-a-standby-cluster-on-the-secondary-site )
on that single node machine.
2023-03-04 22:38:38 +05:30
You end up with a “Standby Cluster” with a single node. That allows you to add additional Patroni nodes by following the same instructions above.
2022-08-27 11:52:29 +05:30
2021-09-04 01:27:46 +05:30
### Patroni support
2021-01-29 00:20:46 +05:30
2023-03-04 22:38:38 +05:30
Patroni is the official replication management solution for Geo. Patroni
2021-09-04 01:27:46 +05:30
can be used to build a highly available cluster on the **primary** and a **secondary** Geo site.
2023-03-04 22:38:38 +05:30
Using Patroni on a **secondary** site is optional and you don't have to use the same number of
2021-09-04 01:27:46 +05:30
nodes on each Geo site.
2021-01-29 00:20:46 +05:30
2023-03-04 22:38:38 +05:30
For instructions on how to set up Patroni on the primary site, see the
2021-01-29 00:20:46 +05:30
[PostgreSQL replication and failover with Omnibus GitLab ](../../postgresql/replication_and_failover.md#patroni ) page.
2021-09-04 01:27:46 +05:30
#### Configuring Patroni cluster for a Geo secondary site
2021-06-08 01:23:25 +05:30
2021-11-11 11:23:49 +05:30
In a Geo secondary site, the main PostgreSQL database is a read-only replica of the primary site's PostgreSQL database.
2021-06-08 01:23:25 +05:30
2023-03-04 22:38:38 +05:30
If you are using `repmgr` on your Geo primary site, see [these instructions ](#migrating-from-repmgr-to-patroni )
2021-09-30 23:02:18 +05:30
for migrating from `repmgr` to Patroni.
A production-ready and secure setup requires at least:
- 3 Consul nodes _(primary and secondary sites)_
- 2 Patroni nodes _(primary and secondary sites)_
- 1 PgBouncer node _(primary and secondary sites)_
- 1 internal load-balancer _(primary site only)_
The internal load balancer provides a single endpoint for connecting to the Patroni cluster's leader whenever a new leader is
2023-03-04 22:38:38 +05:30
elected. The load balancer is required for enabling cascading replication from the secondary sites.
2021-02-22 17:27:13 +05:30
2021-09-30 23:02:18 +05:30
Be sure to use [password credentials ](../../postgresql/replication_and_failover.md#database-authorization-for-patroni )
and other database best practices.
2021-01-29 00:20:46 +05:30
2021-09-04 01:27:46 +05:30
##### Step 1. Configure Patroni permanent replication slot on the primary site
2021-03-08 18:12:59 +05:30
2023-03-04 22:38:38 +05:30
To set up database replication with Patroni on a secondary site, you must
2022-08-27 11:52:29 +05:30
configure a _permanent replication slot_ on the primary site's Patroni cluster,
2021-03-08 18:12:59 +05:30
and ensure password authentication is used.
2022-08-27 11:52:29 +05:30
On each node running a Patroni instance on the primary site **starting on the Patroni
2021-03-08 18:12:59 +05:30
Leader instance**:
1. SSH into your Patroni instance and login as root:
```shell
sudo -i
```
1. Edit `/etc/gitlab/gitlab.rb` and add the following:
```ruby
2021-09-04 01:27:46 +05:30
roles(['patroni_role'])
2021-09-30 23:02:18 +05:30
2021-09-04 01:27:46 +05:30
consul['services'] = %w(postgresql)
2021-03-08 18:12:59 +05:30
consul['configuration'] = {
2021-06-08 01:23:25 +05:30
retry_join: %w[CONSUL_PRIMARY1_IP CONSUL_PRIMARY2_IP CONSUL_PRIMARY3_IP]
2021-03-08 18:12:59 +05:30
}
2021-09-30 23:02:18 +05:30
2021-03-08 18:12:59 +05:30
# You need one entry for each secondary, with a unique name following PostgreSQL slot_name constraints:
#
2021-09-04 01:27:46 +05:30
# Configuration syntax is: 'unique_slotname' => { 'type' => 'physical' },
2021-03-08 18:12:59 +05:30
# We don't support setting a permanent replication slot for logical replication type
patroni['replication_slots'] = {
'geo_secondary' => { 'type' => 'physical' }
}
patroni['use_pg_rewind'] = true
patroni['postgresql']['max_wal_senders'] = 8 # Use double of the amount of patroni/reserved slots (3 patronis + 1 reserved slot for a Geo secondary).
patroni['postgresql']['max_replication_slots'] = 8 # Use double of the amount of patroni/reserved slots (3 patronis + 1 reserved slot for a Geo secondary).
2021-09-30 23:02:18 +05:30
patroni['username'] = 'PATRONI_API_USERNAME'
patroni['password'] = 'PATRONI_API_PASSWORD'
2021-09-04 01:27:46 +05:30
patroni['replication_password'] = 'PLAIN_TEXT_POSTGRESQL_REPLICATION_PASSWORD'
2021-03-08 18:12:59 +05:30
2021-10-27 15:23:28 +05:30
# Add all patroni nodes to the allowlist
patroni['allowlist'] = %w[
127.0.0.1/32
PATRONI_PRIMARY1_IP/32 PATRONI_PRIMARY2_IP/32 PATRONI_PRIMARY3_IP/32
PATRONI_SECONDARY1_IP/32 PATRONI_SECONDARY2_IP/32 PATRONI_SECONDARY3_IP/32
]
2021-09-04 01:27:46 +05:30
# We list all secondary instances as they can all become a Standby Leader
postgresql['md5_auth_cidr_addresses'] = %w[
PATRONI_PRIMARY1_IP/32 PATRONI_PRIMARY2_IP/32 PATRONI_PRIMARY3_IP/32 PATRONI_PRIMARY_PGBOUNCER/32
PATRONI_SECONDARY1_IP/32 PATRONI_SECONDARY2_IP/32 PATRONI_SECONDARY3_IP/32 PATRONI_SECONDARY_PGBOUNCER/32
2021-03-08 18:12:59 +05:30
]
postgresql['pgbouncer_user_password'] = 'PGBOUNCER_PASSWORD_HASH'
postgresql['sql_replication_password'] = 'POSTGRESQL_REPLICATION_PASSWORD_HASH'
postgresql['sql_user_password'] = 'POSTGRESQL_PASSWORD_HASH'
2021-09-04 01:27:46 +05:30
postgresql['listen_address'] = '0.0.0.0' # You can use a public or VPC address here instead
2021-03-08 18:12:59 +05:30
```
1. Reconfigure GitLab for the changes to take effect:
```shell
gitlab-ctl reconfigure
```
2021-09-04 01:27:46 +05:30
##### Step 2. Configure the internal load balancer on the primary site
2021-03-11 19:13:27 +05:30
To avoid reconfiguring the Standby Leader on the secondary site whenever a new
2023-03-04 22:38:38 +05:30
Leader is elected on the primary site, you should set up a TCP internal load
balancer. This load balancer provides a single endpoint for connecting to the Patroni
2021-03-11 19:13:27 +05:30
cluster's Leader.
The Omnibus GitLab packages do not include a Load Balancer. Here's how you
could do it with [HAProxy ](https://www.haproxy.org/ ).
2021-09-04 01:27:46 +05:30
The following IPs and names are used as an example:
2021-03-11 19:13:27 +05:30
- `10.6.0.21` : Patroni 1 (`patroni1.internal`)
2021-12-11 22:18:48 +05:30
- `10.6.0.22` : Patroni 2 (`patroni2.internal`)
- `10.6.0.23` : Patroni 3 (`patroni3.internal`)
2021-03-11 19:13:27 +05:30
```plaintext
global
log /dev/log local0
log localhost local1 notice
log stdout format raw local0
defaults
log global
default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions
frontend internal-postgresql-tcp-in
bind *:5000
mode tcp
option tcplog
default_backend postgresql
backend postgresql
option httpchk
http-check expect status 200
server patroni1.internal 10.6.0.21:5432 maxconn 100 check port 8008
server patroni2.internal 10.6.0.22:5432 maxconn 100 check port 8008
2021-12-11 22:18:48 +05:30
server patroni3.internal 10.6.0.23:5432 maxconn 100 check port 8008
2021-03-11 19:13:27 +05:30
```
2023-03-04 22:38:38 +05:30
For further guidance, refer to the documentation for your preferred load balancer.
2021-03-11 19:13:27 +05:30
2022-08-27 11:52:29 +05:30
##### Step 3. Configure PgBouncer nodes on the secondary site
2021-06-08 01:23:25 +05:30
A production-ready and highly available configuration requires at least
2023-03-04 22:38:38 +05:30
three Consul nodes and a minimum of one PgBouncer node. However, it is recommended to have
one PgBouncer node per database node. An internal load balancer (TCP) is required when there is
more than one PgBouncer service node. The internal load balancer provides a single
2021-06-08 01:23:25 +05:30
endpoint for connecting to the PgBouncer cluster. For more information,
see [High Availability with Omnibus GitLab ](../../postgresql/replication_and_failover.md ).
2022-08-27 11:52:29 +05:30
On each node running a PgBouncer instance on the **secondary** site:
2021-06-08 01:23:25 +05:30
1. SSH into your PgBouncer node and login as root:
```shell
sudo -i
```
1. Edit `/etc/gitlab/gitlab.rb` and add the following:
```ruby
# Disable all components except Pgbouncer and Consul agent
2021-09-04 01:27:46 +05:30
roles(['pgbouncer_role'])
2021-06-08 01:23:25 +05:30
# PgBouncer configuration
2021-09-04 01:27:46 +05:30
pgbouncer['admin_users'] = %w(pgbouncer gitlab-consul)
2021-06-08 01:23:25 +05:30
pgbouncer['users'] = {
2021-09-04 01:27:46 +05:30
'gitlab-consul': {
# Generate it with: `gitlab-ctl pg-password-md5 gitlab-consul`
password: 'GITLAB_CONSUL_PASSWORD_HASH'
},
2021-06-08 01:23:25 +05:30
'pgbouncer': {
2021-09-04 01:27:46 +05:30
# Generate it with: `gitlab-ctl pg-password-md5 pgbouncer`
2021-06-08 01:23:25 +05:30
password: 'PGBOUNCER_PASSWORD_HASH'
}
}
# Consul configuration
consul['watchers'] = %w(postgresql)
consul['configuration'] = {
retry_join: %w[CONSUL_SECONDARY1_IP CONSUL_SECONDARY2_IP CONSUL_SECONDARY3_IP]
}
consul['monitoring_service_discovery'] = true
```
1. Reconfigure GitLab for the changes to take effect:
```shell
gitlab-ctl reconfigure
```
1. Create a `.pgpass` file so Consul is able to reload PgBouncer. Enter the `PLAIN_TEXT_PGBOUNCER_PASSWORD` twice when asked:
```shell
gitlab-ctl write-pgpass --host 127.0.0.1 --database pgbouncer --user pgbouncer --hostuser gitlab-consul
```
2021-09-04 01:27:46 +05:30
1. Reload the PgBouncer service:
2021-06-08 01:23:25 +05:30
```shell
2021-09-04 01:27:46 +05:30
gitlab-ctl hup pgbouncer
2021-06-08 01:23:25 +05:30
```
2021-09-04 01:27:46 +05:30
##### Step 4. Configure a Standby cluster on the secondary site
2021-03-08 18:12:59 +05:30
NOTE:
2022-08-27 11:52:29 +05:30
If you are converting a secondary site with a single PostgreSQL instance to a Patroni Cluster, you must start on the PostgreSQL instance. It becomes the Patroni Standby Leader instance,
2023-03-04 22:38:38 +05:30
and then you can switch over to another replica if you need to.
2021-03-08 18:12:59 +05:30
2022-08-27 11:52:29 +05:30
For each node running a Patroni instance on the secondary site:
2021-03-08 18:12:59 +05:30
1. SSH into your Patroni node and login as root:
```shell
sudo -i
```
1. Edit `/etc/gitlab/gitlab.rb` and add the following:
```ruby
2021-09-04 01:27:46 +05:30
roles(['consul_role', 'patroni_role'])
2021-03-08 18:12:59 +05:30
consul['enable'] = true
consul['configuration'] = {
2021-06-08 01:23:25 +05:30
retry_join: %w[CONSUL_SECONDARY1_IP CONSUL_SECONDARY2_IP CONSUL_SECONDARY3_IP]
2021-03-08 18:12:59 +05:30
}
postgresql['md5_auth_cidr_addresses'] = [
'PATRONI_SECONDARY1_IP/32', 'PATRONI_SECONDARY2_IP/32', 'PATRONI_SECONDARY3_IP/32', 'PATRONI_SECONDARY_PGBOUNCER/32',
# Any other instance that needs access to the database as per documentation
]
2021-10-27 15:23:28 +05:30
# Add patroni nodes to the allowlist
patroni['allowlist'] = %w[
127.0.0.1/32
PATRONI_SECONDARY1_IP/32 PATRONI_SECONDARY2_IP/32 PATRONI_SECONDARY3_IP/32
]
2021-03-08 18:12:59 +05:30
patroni['standby_cluster']['enable'] = true
2021-03-11 19:13:27 +05:30
patroni['standby_cluster']['host'] = 'INTERNAL_LOAD_BALANCER_PRIMARY_IP'
patroni['standby_cluster']['port'] = INTERNAL_LOAD_BALANCER_PRIMARY_PORT
2021-03-08 18:12:59 +05:30
patroni['standby_cluster']['primary_slot_name'] = 'geo_secondary' # Or the unique replication slot name you setup before
2021-09-30 23:02:18 +05:30
patroni['username'] = 'PATRONI_API_USERNAME'
patroni['password'] = 'PATRONI_API_PASSWORD'
2021-03-08 18:12:59 +05:30
patroni['replication_password'] = 'PLAIN_TEXT_POSTGRESQL_REPLICATION_PASSWORD'
patroni['use_pg_rewind'] = true
patroni['postgresql']['max_wal_senders'] = 5 # A minimum of three for one replica, plus two for each additional replica
patroni['postgresql']['max_replication_slots'] = 5 # A minimum of three for one replica, plus two for each additional replica
2021-09-30 23:02:18 +05:30
2021-09-04 01:27:46 +05:30
postgresql['pgbouncer_user_password'] = 'PGBOUNCER_PASSWORD_HASH'
postgresql['sql_replication_password'] = 'POSTGRESQL_REPLICATION_PASSWORD_HASH'
postgresql['sql_user_password'] = 'POSTGRESQL_PASSWORD_HASH'
postgresql['listen_address'] = '0.0.0.0' # You can use a public or VPC address here instead
2021-09-30 23:02:18 +05:30
2021-09-04 01:27:46 +05:30
gitlab_rails['dbpassword'] = 'POSTGRESQL_PASSWORD'
gitlab_rails['enable'] = true
gitlab_rails['auto_migrate'] = false
2021-03-08 18:12:59 +05:30
```
1. Reconfigure GitLab for the changes to take effect.
2023-03-04 22:38:38 +05:30
This step is required to bootstrap PostgreSQL users and settings.
2021-03-08 18:12:59 +05:30
2021-09-30 23:02:18 +05:30
- If this is a fresh installation of Patroni:
```shell
gitlab-ctl reconfigure
```
- If you are configuring a Patroni standby cluster on a site that previously had a working Patroni cluster:
```shell
gitlab-ctl stop patroni
rm -rf /var/opt/gitlab/postgresql/data
/opt/gitlab/embedded/bin/patronictl -c /var/opt/gitlab/patroni/patroni.yaml remove postgresql-ha
gitlab-ctl reconfigure
gitlab-ctl start patroni
```
2021-03-08 18:12:59 +05:30
2022-08-27 11:52:29 +05:30
### Migrating a single tracking database node to Patroni
2021-03-08 18:12:59 +05:30
2023-03-04 22:38:38 +05:30
Before the introduction of Patroni, Geo provided no Omnibus support for HA setups on
2022-08-27 11:52:29 +05:30
the secondary site.
2021-03-08 18:12:59 +05:30
2023-03-04 22:38:38 +05:30
With Patroni, it's now possible to support HA setups. However, some restrictions in Patroni
prevent the management of two different clusters on the same machine. You should set up a new
Patroni cluster for the tracking database by following the same instructions above.
2021-03-11 19:13:27 +05:30
2022-08-27 11:52:29 +05:30
The secondary nodes backfill the new tracking database, and no data
synchronization is required.
2021-03-08 18:12:59 +05:30
2021-06-08 01:23:25 +05:30
### Configuring Patroni cluster for the tracking PostgreSQL database
2022-08-27 11:52:29 +05:30
**Secondary** sites use a separate PostgreSQL installation as a tracking database to
2021-06-08 01:23:25 +05:30
keep track of replication status and automatically recover from potential replication issues.
2021-09-04 01:27:46 +05:30
Omnibus automatically configures a tracking database when `roles(['geo_secondary_role'])` is set.
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
If you want to run this database in a highly available configuration, don't use the `geo_secondary_role` above.
Instead, follow the instructions below.
2021-06-08 01:23:25 +05:30
2023-03-04 22:38:38 +05:30
A production-ready and secure setup for the tracking PostgreSQL DB requires at least three Consul nodes: two
Patroni nodes, and one PgBouncer node on the secondary site.
2021-06-08 01:23:25 +05:30
2022-04-04 11:22:00 +05:30
Because of [omnibus-6587 ](https://gitlab.com/gitlab-org/omnibus-gitlab/-/issues/6587 ), Consul can't track multiple
2022-05-07 20:08:51 +05:30
services, so these must be different than the nodes used for the Standby Cluster database.
2022-04-04 11:22:00 +05:30
2021-09-30 23:02:18 +05:30
Be sure to use [password credentials ](../../postgresql/replication_and_failover.md#database-authorization-for-patroni )
and other database best practices.
2022-08-27 11:52:29 +05:30
#### Step 1. Configure PgBouncer nodes on the secondary site
2021-06-08 01:23:25 +05:30
2022-08-27 11:52:29 +05:30
On each node running the PgBouncer service for the PostgreSQL tracking database:
2021-06-08 01:23:25 +05:30
1. SSH into your PgBouncer node and login as root:
```shell
sudo -i
```
1. Edit `/etc/gitlab/gitlab.rb` and add the following:
```ruby
# Disable all components except Pgbouncer and Consul agent
2021-09-04 01:27:46 +05:30
roles(['pgbouncer_role'])
2021-06-08 01:23:25 +05:30
# PgBouncer configuration
pgbouncer['users'] = {
'pgbouncer': {
password: 'PGBOUNCER_PASSWORD_HASH'
}
}
pgbouncer['databases'] = {
gitlabhq_geo_production: {
user: 'pgbouncer',
password: 'PGBOUNCER_PASSWORD_HASH'
}
}
# Consul configuration
consul['watchers'] = %w(postgresql)
consul['configuration'] = {
retry_join: %w[CONSUL_TRACKINGDB1_IP CONSUL_TRACKINGDB2_IP CONSUL_TRACKINGDB3_IP]
}
consul['monitoring_service_discovery'] = true
# GitLab database settings
gitlab_rails['db_database'] = 'gitlabhq_geo_production'
gitlab_rails['db_username'] = 'gitlab_geo'
```
1. Reconfigure GitLab for the changes to take effect:
```shell
gitlab-ctl reconfigure
```
1. Create a `.pgpass` file so Consul is able to reload PgBouncer. Enter the `PLAIN_TEXT_PGBOUNCER_PASSWORD` twice when asked:
```shell
gitlab-ctl write-pgpass --host 127.0.0.1 --database pgbouncer --user pgbouncer --hostuser gitlab-consul
```
1. Restart the PgBouncer service:
```shell
gitlab-ctl restart pgbouncer
```
#### Step 2. Configure a Patroni cluster
2022-08-27 11:52:29 +05:30
On each node running a Patroni instance on the secondary site for the PostgreSQL tracking database:
2021-06-08 01:23:25 +05:30
1. SSH into your Patroni node and login as root:
```shell
sudo -i
```
1. Edit `/etc/gitlab/gitlab.rb` and add the following:
```ruby
# Disable all components except PostgreSQL, Patroni, and Consul
2021-09-04 01:27:46 +05:30
roles(['patroni_role'])
2021-06-08 01:23:25 +05:30
# Consul configuration
consul['services'] = %w(postgresql)
consul['configuration'] = {
server: true,
retry_join: %w[CONSUL_TRACKINGDB1_IP CONSUL_TRACKINGDB2_IP CONSUL_TRACKINGDB3_IP]
}
# PostgreSQL configuration
postgresql['listen_address'] = '0.0.0.0'
postgresql['hot_standby'] = 'on'
postgresql['wal_level'] = 'replica'
postgresql['pgbouncer_user_password'] = 'PGBOUNCER_PASSWORD_HASH'
postgresql['sql_replication_password'] = 'POSTGRESQL_REPLICATION_PASSWORD_HASH'
postgresql['sql_user_password'] = 'POSTGRESQL_PASSWORD_HASH'
postgresql['md5_auth_cidr_addresses'] = [
'PATRONI_TRACKINGDB1_IP/32', 'PATRONI_TRACKINGDB2_IP/32', 'PATRONI_TRACKINGDB3_IP/32', 'PATRONI_TRACKINGDB_PGBOUNCER/32',
# Any other instance that needs access to the database as per documentation
]
2021-10-27 15:23:28 +05:30
# Add patroni nodes to the allowlist
patroni['allowlist'] = %w[
127.0.0.1/32
PATRONI_TRACKINGDB1_IP/32 PATRONI_TRACKINGDB2_IP/32 PATRONI_TRACKINGDB3_IP/32
]
2021-06-08 01:23:25 +05:30
# Patroni configuration
2021-09-30 23:02:18 +05:30
patroni['username'] = 'PATRONI_API_USERNAME'
patroni['password'] = 'PATRONI_API_PASSWORD'
2021-06-08 01:23:25 +05:30
patroni['replication_password'] = 'PLAIN_TEXT_POSTGRESQL_REPLICATION_PASSWORD'
patroni['postgresql']['max_wal_senders'] = 5 # A minimum of three for one replica, plus two for each additional replica
# GitLab database settings
gitlab_rails['db_database'] = 'gitlabhq_geo_production'
gitlab_rails['db_username'] = 'gitlab_geo'
2021-09-04 01:27:46 +05:30
gitlab_rails['enable'] = true
2021-06-08 01:23:25 +05:30
# Disable automatic database migrations
gitlab_rails['auto_migrate'] = false
```
1. Reconfigure GitLab for the changes to take effect.
2023-03-04 22:38:38 +05:30
This step is required to bootstrap PostgreSQL users and settings:
2021-06-08 01:23:25 +05:30
```shell
gitlab-ctl reconfigure
```
2022-08-27 11:52:29 +05:30
#### Step 3. Configure the tracking database on the secondary sites
2021-06-08 01:23:25 +05:30
For each node running the `gitlab-rails` , `sidekiq` , and `geo-logcursor` services:
1. SSH into your node and login as root:
```shell
sudo -i
```
2022-05-07 20:08:51 +05:30
1. Edit `/etc/gitlab/gitlab.rb` and add the following attributes. You may have other attributes set, but the following must be set.
2021-06-08 01:23:25 +05:30
```ruby
# Tracking database settings
geo_secondary['db_username'] = 'gitlab_geo'
geo_secondary['db_password'] = 'PLAIN_TEXT_PGBOUNCER_PASSWORD'
geo_secondary['db_database'] = 'gitlabhq_geo_production'
geo_secondary['db_host'] = 'PATRONI_TRACKINGDB_PGBOUNCER_IP'
geo_secondary['db_port'] = 6432
geo_secondary['auto_migrate'] = false
# Disable the tracking database service
geo_postgresql['enable'] = false
```
1. Reconfigure GitLab for the changes to take effect.
```shell
gitlab-ctl reconfigure
```
1. Run the tracking database migrations:
```shell
2022-07-16 23:28:13 +05:30
gitlab-rake db:migrate:geo
2021-06-08 01:23:25 +05:30
```
2020-11-24 15:15:51 +05:30
## Troubleshooting
Read the [troubleshooting document ](../replication/troubleshooting.md ).