GitLab Geo database replication
Note: This is the documentation for the Omnibus GitLab packages. For installations from source, follow the database replication for installations from source guide.
Note: If your GitLab installation uses external PostgreSQL, the Omnibus roles will not be able to perform all necessary configuration steps. Refer to the section on External PostreSQL for additional instructions.
Note: The stages of the setup process must be completed in the documented order. Before attempting the steps in this stage, complete all prior stages.
This document describes the minimal steps you have to take in order to replicate your primary GitLab database to a secondary node's database. You may have to change some values according to your database setup, how big it is, etc.
You are encouraged to first read through all the steps before executing them in your testing/production environment.
PostgreSQL replication
The GitLab primary node where the write operations happen will connect to the primary database server, and the secondary nodes which are read-only will connect to the secondary database servers (which are also read-only).
Note: In database documentation you may see "primary" being referenced as "master" and "secondary" as either "slave" or "standby" server (read-only).
We recommend using PostgreSQL replication slots to ensure that the primary retains all the data necessary for the secondaries to recover. See below for more details.
The following guide assumes that:
- You are using Omnibus and therefore you are using PostgreSQL 9.6 or later
which includes the
pg_basebackup
tool and improved Foreign Data Wrapper support. - You have a primary node already set up (the GitLab server you are replicating from), running Omnibus' PostgreSQL (or equivalent version), and you have a new secondary server set up with the same versions of the OS, PostgreSQL, and GitLab on all nodes.
- The IP of the primary server for our examples will be
1.2.3.4
, whereas the secondary's IP will be5.6.7.8
. Note that the primary and secondary servers must be able to communicate over these addresses. More on this in the guide below.
Step 1. Configure the primary server
-
SSH into your GitLab primary server and login as root:
sudo -i
-
Execute the command below to define the node as primary Geo node:
gitlab-ctl set-geo-primary-node
This command will use your defined
external_url
in/etc/gitlab/gitlab.rb
. -
GitLab 10.4 and up only: Do the following to make sure the
gitlab
database user has a password definedGenerate a MD5 hash of the desired password:
gitlab-ctl pg-password-md5 gitlab # Enter password: mypassword # Confirm password: mypassword # fca0b89a972d69f00eb3ec98a5838484
Edit
/etc/gitlab/gitlab.rb
:# Fill with the hash generated by `gitlab-ctl pg-password-md5 gitlab` postgresql['sql_user_password'] = 'fca0b89a972d69f00eb3ec98a5838484' # If you have HA setup, this must be present in all nodes as well gitlab_rails['db_password'] = 'mypassword'
-
Omnibus GitLab already has a replication user called
gitlab_replicator
. You must set the password for this user manually. You will be prompted to enter a password:gitlab-ctl set-replication-password
This command will also read the
postgresql['sql_replication_user']
Omnibus setting in case you have changedgitlab_replicator
username to something else. -
Configure PostgreSQL to listen on network interfaces
For security reasons, PostgreSQL does not listen on any network interfaces by default. However, GitLab Geo requires the secondary to be able to connect to the primary's database. For this reason, we need the address of each node. Note: For external PostgreSQL instances, see additional instructions.
If you are using a cloud provider, you can lookup the addresses for each Geo node through your cloud provider's management console. A table of common terminology is provided below as it varies between vendors.
GitLab Terminology Amazon Web Services Google Cloud Platform Private address Private address Internal address Public address Public address External address To lookup the address of a Geo node, SSH in to the Geo node and execute:
## ## Private address ## ip route get 255.255.255.255 | awk '{print "Private address:", $NF; exit}' ## ## Public address ## echo "External address: $(curl ipinfo.io/ip)"
In most cases, the following addresses will be used to configure GitLab Geo:
Configuration Address postgresql['listen_address']
Primary's private address postgresql['trust_auth_cidr_addresses']
Primary's private address postgresql['md5_auth_cidr_addresses']
Secondary's public addresses The
listen_address
option opens PostgreSQL up to network connections with the interface corresponding to the given address. See the PostgreSQL documentation for more details.Depending on your network configuration, the suggested addresses may not be correct. If your primary and secondary connect over a local area network, or a virtual network connecting availability zones like Amazon's VPC or Google's VPC you should use the secondary's private address for
postgresql['md5_auth_cidr_addresses']
.Edit
/etc/gitlab/gitlab.rb
and add the following, replacing the IP addresses with addresses appropriate to your network configuration:geo_primary_role['enable'] = true ## ## Primary address ## - replace '1.2.3.4' with the primary private address ## postgresql['listen_address'] = '1.2.3.4' postgresql['trust_auth_cidr_addresses'] = ['127.0.0.1/32','1.2.3.4/32'] ## # Secondary addresses # - replace '5.6.7.8' with the secondary public address ## postgresql['md5_auth_cidr_addresses'] = ['5.6.7.8/32'] ## ## Replication settings ## - set this to be the number of Geo secondary nodes you have ## postgresql['max_replication_slots'] = 1 # postgresql['max_wal_senders'] = 10 # postgresql['wal_keep_segments'] = 10 ## ## Disable automatic database migrations temporarily ## (until PostgreSQL is restarted and listening on the private address). ## gitlab_rails['auto_migrate'] = false
-
Optional: If you want to add another secondary, the relevant setting would look like:
postgresql['md5_auth_cidr_addresses'] = ['5.6.7.8/32','9.10.11.12/32']
You may also want to edit the
wal_keep_segments
andmax_wal_senders
to match your database replication requirements. Consult the PostgreSQL - Replication documentation for more information. -
Save the file and reconfigure GitLab for the database listen changes and the replication slot changes to be applied.
gitlab-ctl reconfigure
Restart PostgreSQL for its changes to take effect:
gitlab-ctl restart postgresql
-
Re-enable migrations now that PostgreSQL is restarted and listening on the private address.
Edit
/etc/gitlab/gitlab.rb
and change the configuration totrue
:gitlab_rails['auto_migrate'] = true
Save the file and reconfigure GitLab:
gitlab-ctl reconfigure
Now that the PostgreSQL server is set up to accept remote connections, run
netstat -plnt
to make sure that PostgreSQL is listening on port5432
to the primary server's private address.-
A certificate was automatically generated when GitLab was reconfigured. This will be used automatically to protect your PostgreSQL traffic from eavesdroppers, but to protect against active ("man-in-the-middle") attackers, the secondary needs a copy of the certificate. Make a copy of the PostgreSQL
server.crt
file on the primary node by running this command:cat ~gitlab-psql/data/server.crt
Copy the output into a file on your local computer called
server.crt
. You will need it when setting up the secondary! The certificate is not sensitive data.
Step 2. Add the secondary GitLab node
To prevent the secondary geo node from trying to act as the primary once the database is replicated, the secondary geo node must be added on the primary before the database is replicated.
- Visit the primary node's Admin Area ➔ Geo Nodes
(
/admin/geo_nodes
) in your browser. - Add the secondary node by providing its full URL. Do NOT check the box 'This is a primary node'.
- Optionally, choose which namespaces should be replicated by the secondary node. Leave blank to replicate all. Read more in selective replication.
- Click the Add node button.
-
SSH into your GitLab primary server and login as root to verify the secondary is reachable:
gitlab-rake gitlab:geo:check
The new secondary geo node will have the status Unhealthy. This is expected because we have not yet configured the secondary server. This is the next step.
Step 3. Configure the secondary server
-
From your local machine, copy
server.crt
to the secondary:scp server.crt secondary.geo.example.com:
-
SSH into your GitLab secondary server and login as root:
sudo -i
-
Check TCP connectivity to the primary's PostgreSQL server:
gitlab-rake gitlab:tcp_check[1.2.3.4,5432]
If this step fails, you may be using the wrong IP address, or a firewall may be preventing access to the server. Check the IP address, paying close attention to the difference between public and private addresses and ensure that, if a firewall is present, the secondary is permitted to connect to the primary on port 5432.
-
Set up PostgreSQL TLS verification on the secondary
Install the
server.crt
file:install -D -o gitlab-psql -g gitlab-psql -m 0400 -T server.crt ~gitlab-psql/.postgresql/root.crt
PostgreSQL will now only recognize that exact certificate when verifying TLS connections. The certificate can only be replicated by someone with access to the private key, which is only present on the primary node.
-
Configure PostreSQL to enable FDW support
This step is similar to how we configured the primary instance. We need to enable this, to enable FDW support, even if using a single node.
Edit
/etc/gitlab/gitlab.rb
and add the following, replacing the IP addresses with addresses appropriate to your network configuration:# Secondary addresses # - replace '5.6.7.8' with the secondary private address postgresql['listen_address'] = '5.6.7.8' postgresql['trust_auth_cidr_addresses'] = ['127.0.0.1/32','5.6.7.8/32'] # gitlab database user's password (defined previously) gitlab_rails['db_password'] = 'mypassword' # enable fdw for the geo tracking database geo_secondary['db_fdw'] = true
-
Test that the
gitlab-psql
user can connect to the primary's database:sudo -u gitlab-psql /opt/gitlab/embedded/bin/psql --list -U gitlab_replicator -d "dbname=gitlabhq_production sslmode=verify-ca" -W -h 1.2.3.4
When prompted enter the password you set in the first step for the
gitlab_replicator
user. If all worked correctly, you should see the database prompt.A failure to connect here indicates that the TLS configuration is incorrect. Ensure that the contents of
~gitlab-psql/data/server.crt
on the primary match the contents of~gitlab-psql/.postgresql/root.crt
on the secondary. -
Edit
/etc/gitlab/gitlab.rb
and add the following:geo_secondary_role['enable'] = true
For external PostgreSQL instances, see additional instructions.
-
Reconfigure GitLab for the changes to take effect:
gitlab-ctl reconfigure
Step 4. Initiate the replication process
Below we provide a script that connects the database on the secondary node to the database on the primary node, replicates the database, and creates the needed files for streaming replication.
The directories used are the defaults that are set up in Omnibus. If you have changed any defaults or are using a source installation, configure it as you see fit replacing the directories and paths.
Warning: Make sure to run this on the secondary server as it removes all PostgreSQL's data before running
pg_basebackup
.
-
SSH into your GitLab secondary server and login as root:
sudo -i
Choose a database-friendly name to use for your secondary to use as the replication slot name. For example, if your domain is
secondary.geo.example.com
, you may usesecondary_example
as the slot name as shown in the commands below.-
Execute the command below to start a backup/restore and begin the replication (various options that can be added to these commands are listed below):
gitlab-ctl replicate-geo-database --slot-name=secondary_example --host=1.2.3.4
When prompted, enter the password you set up for the
gitlab_replicator
user in the first step.This command also takes a number of additional options. You can use
--help
to list them all, but here are a couple of tips:- If PostgreSQL is listening on a non-standard port, add
--port=
as well. - If your database is too large to be transferred in 30 minutes, you will need
to increase the timeout, e.g.,
--backup-timeout=3600
if you expect the initial replication to take under an hour. - Pass
--sslmode=disable
to skip PostgreSQL TLS authentication altogether (e.g., you know the network path is secure, or you are using a site-to-site VPN). This is not safe over the public Internet! - You can read more details about each
sslmode
in the PostgreSQL documentation; the instructions above are carefully written to ensure protection against both passive eavesdroppers and active "man-in-the-middle" attackers. - Change the
--slot-name
to the name of the replication slot to be used on the primary database. The script will attempt to create the replication slot automatically if it does not exist. - If you're repurposing an old server into a Geo secondary, you'll need to
add
--force
to the command line.
- If PostgreSQL is listening on a non-standard port, add
-
Verify that the secondary is configured correctly and that the primary is reachable:
gitlab-rake gitlab:geo:check
The replication process is now complete.
External PostgreSQL instances
For installations using external PostgreSQL instances, the geo_primary_role
and geo_secondary_role
includes configuration changes that must be applied
manually.
The geo_primary_role
makes configuration changes to pg_hba.conf
and
postgresql.conf
on the primary:
##
## GitLab Geo Primary
## - pg_hba.conf
##
host replication gitlab_replicator <trusted secondary IP>/32 md5
##
## Geo Primary Role
## - postgresql.conf
##
sql_replication_user = gitlab_replicator
wal_level = hot_standby
max_wal_senders = 10
wal_keep_segments = 50
max_replication_slots = 1 # number of secondary instances
hot_standby = on
Th geo_secondary_role
makes configuration changes to postgresql.conf
and
enables the Geo Log Cursor (geo_logcursor
) and secondary tracking database
on the secondary. The PostgreSQL settings for this database it adds to
the default settings:
##
## Geo Secondary Role
## - postgresql.conf
##
wal_level = hot_standby
max_wal_senders = 10
wal_keep_segments = 10
hot_standby = on
Geo secondary nodes use a tracking database to keep track of replication status and recover automatically from some replication issues. Follow the instructions for enabling tracking database on the secondary server.
MySQL replication
MySQL replication is not supported for GitLab Geo.
Troubleshooting
Read the troubleshooting document.