The cluster is operated with the subcommands of gitlab-ctl pg-failover: fence runs on the old primary server, promote on the standby server, rejoin on the fenced old primary server. Redis failover needs no commands. The virtual IP addresses move on their own.
All commands print the next step with the exact arguments. None of them edits /etc/gitlab/gitlab.rb: role changes are made by the administrator and applied with gitlab-ctl reconfigure.
Planned switchover
A planned switchover loses no data: the primary server flushes the write-ahead log (WAL) and prints the log position (LSN) the standby server must reach before promotion.
On the primary server, fence it with the WAL flush:
sudo gitlab-ctl pg-failover fence --switchoverThe command flushes the WAL, stops PostgreSQL and prints the end-of-WAL position together with the exact promote command. The database virtual IP address leaves the node within about 20 seconds.
On the standby server, run the printed command:
sudo gitlab-ctl pg-failover promote --switchover --expect-lsn <LSN>The command waits until the WAL is replayed up to
<LSN>(up to 30 seconds), printsloss=0 bytes, promotes the node and waits up to 60 seconds for the database virtual IP address to settle on it.Flip the roles in
/etc/gitlab/gitlab.rband apply them:On the new primary server set
postgresql['role'] = 'primary'and rungitlab-ctl reconfigure.Create the replication slots of the standby servers on the new primary server, the ones missing there. A standby server whose slot is missing on the primary server fails to stream:
sudo gitlab-psql -c "SELECT pg_create_physical_replication_slot('gitlab_standby_1');"The existing slots:
sudo gitlab-psql -c "SELECT slot_name FROM pg_replication_slots;".On the fenced node set
postgresql['role'] = 'standby',postgresql['primary_host']to the address of the new primary server and, if the slot changed,postgresql['replication_slot_name']. Then rungitlab-ctl reconfigure.On the remaining standby server — the one that was not promoted — set
postgresql['primary_host']to the address of the new primary server and rungitlab-ctl reconfigure: the database restarts and streams from the new primary server.
On the fenced node, rejoin the cluster:
sudo gitlab-ctl pg-failover rejoinThe command resynchronizes the node with
pg_rewind, clears the fence, starts the database and verifies that it streams from the new primary server.
Redis does not switch in this scenario: the fenced node keeps running, and the Redis master server stays on it.
If the primary server still answers commands but the WAL cannot be flushed, run sudo gitlab-ctl pg-failover fence without --switchover and promote the standby server with sudo gitlab-ctl pg-failover promote --expect-lsn <LSN> (zero loss when the journal is already replayed to the position) or with sudo gitlab-ctl pg-failover promote --allow-data-loss. The completion steps are the ones described above.
Failover after a primary server failure
When the primary server’s node is down, fencing is impossible, and the standby server promotes on its own state. The peer check treats an unreachable node as dead and lets the promotion proceed. The frontend virtual IP address moves to a healthy node on its own.
If an LSN of the dead primary server is known — from an earlier fence or its logs — promote against it. The refusal of the command shows exactly how much of the WAL tail the standby server is missing:
sudo gitlab-ctl pg-failover promote --expect-lsn <LSN>Without a known LSN, promote with the explicit loss acceptance:
sudo gitlab-ctl pg-failover promote --allow-data-lossThe command prints the last WAL positions received and replayed and asks for confirmation. Type yes, or set GITLAB_CTL_YES=1 in the environment for automation.
Then bring the configuration of every live node in line with the new topology. Collect both changes of a node — the PostgreSQL role and the Redis lines — in /etc/gitlab/gitlab.rb before running gitlab-ctl reconfigure: every run re-applies the static Redis topology from the file.
On the new primary server set
postgresql['role'] = 'primary'.On the other standby servers set
postgresql['primary_host']to the address of the new primary server.Find the current Redis master server — if the dead node ran it, Sentinel has already promoted a replica:
sudo gitlab-ctl get-redis-masterOn the node named by the command set
redis['master'] = trueandredis['master_ip']to its own address. On the other nodes setredis['master'] = falseandredis['master_ip']to the address of that node.
Apply the changes with gitlab-ctl reconfigure on every live node, and create the replication slots of the standby servers on the new primary server, the ones missing there. Without the Redis alignment gitlab-ctl reconfigure returns the Redis servers to the static topology of gitlab.rb — replicas of the dead node — and the cluster is left without a master server: Sentinel finds no replica to promote, and the application gets connection errors.
When the dead node returns, its PostgreSQL is a stale primary server, and its Redis lines hold the old topology:
Fence it before anything else connects to it:
sudo gitlab-ctl pg-failover fenceIn its
/etc/gitlab/gitlab.rb, set everything at once:postgresql['role'] = 'standby'andpostgresql['primary_host']to the current primary server,redis['master'] = falseandredis['master_ip']to the address of the current Redis master server. A staleredis['master'] = trueconfigures the node as a second Redis master server on the first run ofgitlab-ctl reconfigure.Run
gitlab-ctl reconfigureand rejoin the cluster:sudo gitlab-ctl pg-failover rejoin
A primary server that lost the network but stays up can keep accepting writes from the clients that reach it — return it with the same steps.
Rejoining a fenced node
A fenced node returns to the cluster as a standby server after its /etc/gitlab/gitlab.rb points at the new primary server (role = 'standby', primary_host, applied with gitlab-ctl reconfigure).
Before running gitlab-ctl reconfigure on the returning node, check its Redis lines against the current topology: a stale redis['master'] = true left in gitlab.rb configures the node as a second Redis master server. The current master server is shown by gitlab-ctl get-redis-master. Then rejoin:
sudo gitlab-ctl pg-failover rejoinThe command resynchronizes the data directory with pg_rewind under the postgresql['ha_rewind_user'] credentials, rewrites the replication connection from gitlab.rb, clears the fence and verifies streaming. It refuses to run without the fence marker: a node that was never fenced may be the current primary server, and resynchronization would destroy it.
When the WAL before the divergence is no longer kept on the new primary server, pg_rewind fails and the command prints the remediation — a full re-provision of the data directory:
sudo gitlab-ctl pg-failover rejoin --recloneThe command wipes the local data directory and re-clones it with pg_basebackup. It asks for confirmation and for the replication password, which it reads from postgresql['ha_replication_password'], from GITLAB_REPLICATION_PASSWORD in the environment, or from the prompt.
Adding a node to the cluster
The new node becomes a full member of the cluster: a PostgreSQL standby server, a Redis and Sentinel node, a Gitaly node under Praefect and a node the virtual IP addresses can move to. The example below adds a fourth node with the address 10.10.0.4.
On the existing nodes, add the new node to the shared configuration. On each of them, make the changes in
/etc/gitlab/gitlab.rband rungitlab-ctl reconfigure:- the address
10.10.0.4/32inpostgresql['ha_peers']on the database nodes; - the address
10.10.0.4inkeepalived['unicast_peers']; - the line
{ storage: 'gitaly-4', address: 'tcp://10.10.0.4:8075', token: '<GITALY_TOKEN>' }in thenodelist of thepraefect['configuration']block; { 'host' => '10.10.0.4', 'port' => 26379 }ingitlab_rails['redis_sentinels'].
The records can be added before the node exists: the peer checks treat an unreachable node as dead, Praefect routes the requests to the live nodes, and the Sentinel client moves to the next address of the list.
- the address
On the primary server, create the replication slot of the new node:
sudo gitlab-psql -c "SELECT pg_create_physical_replication_slot('gitlab_standby_4');"Deploy the new node as a regular single-node installation and let the first start finish without the high availability parameters — like nodes 2 and 3 in the deployment.
Copy
/etc/gitlab/gitlab-secrets.jsonand the SSH host key files/etc/ssh/ssh_host_*_keyand/etc/ssh/ssh_host_*_key.pubto the new node from any existing node, then rungitlab-ctl reconfigureandgitlab-ctl restartand restart the SSH service.Add the standby block to
/etc/gitlab/gitlab.rbof the new node:postgresql['listen_address'] = '*' postgresql['md5_auth_cidr_addresses'] = ['10.10.0.0/24', '127.0.0.1/32'] postgresql['role'] = 'standby' postgresql['primary_host'] = '10.10.0.1' postgresql['ha_peers'] = ['10.10.0.1/32', '10.10.0.2/32', '10.10.0.3/32'] postgresql['ha_replication_password'] = '<REPLICATION_PASSWORD>' postgresql['ha_check_user'] = 'gitlab-ha-check' postgresql['ha_check_password'] = '<CHECK_PASSWORD>' postgresql['ha_rewind_user'] = 'gitlab-ha-rewind' postgresql['ha_rewind_password'] = '<REWIND_PASSWORD>' postgresql['replication_application_name'] = 'gitlab-pg-4' postgresql['replication_slot_name'] = 'gitlab_standby_4' gitlab_rails['db_password'] = '<DB_PASSWORD>' postgresql['sql_user_password'] = '<SQL_USER_PASSWORD_HASH>'Set
primary_hostto the address of the current primary server. Then clone the database:sudo gitlab-ctl reconfigure sudo gitlab-ctl stop postgresql sudo rm -rf /var/opt/gitlab/postgresql/data/* sudo -u gitlab-psql /opt/gitlab/embedded/bin/pg_basebackup \ -h 10.10.0.1 -p 5432 -U gitlab_replicator \ -D /var/opt/gitlab/postgresql/data -Fp -Xs -P --no-slot sudo gitlab-ctl reconfigure sudo gitlab-ctl start postgresqlThe replication password the command asks for is
<REPLICATION_PASSWORD>, as in the deployment.Add the keepalived, Sentinel and Gitaly with Praefect blocks — the same as in the deployment, with the differences of the new node:
unicast_src_ip,listen_addrandsentinel['bind']with the address10.10.0.4, the next descending frontend priority (for example,85),redis['master'] = falsewith themaster_ipof the current Redis master server, the storage namegitaly-4, andgitlab_rails['repositories_storages']with the address of the local Praefect. Apply withgitlab-ctl reconfigure.Check the node.
gitlab-ctl statusshows it in theHA:section as a standby server streaming from the primary.gitlab-ctl get-redis-masteranswers with the Redis master address, and the repository copies on thegitaly-4storage are backfilled by the Praefect reconciler — as with the return of a Gitaly node.
Monitoring the cluster
The HA: section of gitlab-ctl status is the single status view. It works on every node and degrades to unknown(<reason>) instead of hiding a failed probe — treat any unknown( line as a monitoring signal:
sudo gitlab-ctl statusOn a writable node it lists the attached standby servers with the replication lag. On a standby server it shows the walreceiver state, the streaming source and the replay position. On every node it shows the holder state of the database virtual IP address and the current Redis master server.
The virtual IP address transitions are logged on every node:
sudo tail -n 20 /var/opt/gitlab/keepalived/state.logThe start guard and fencing
The start guard prevents a second writable primary server. When gitlab-ctl start, restart, up, once or force-restart would start PostgreSQL in the writable role, the command checks the other database nodes over SQL with the check user:
- A peer that answers as a writable primary server aborts the entire start: fence that peer or rejoin this node as a standby server.
- A peer whose state cannot be verified (authentication or
pg_hbafailure) aborts the start as well. - An unreachable peer is presumed dead and does not block the start, so a cold boot of the whole cluster works.
A fenced node never starts PostgreSQL: while fenced.marker exists, the service run script refuses to launch the database, and the disabled autostart survives a reboot. gitlab-ctl status shows the node as FENCED.
On a standby node the database steps of gitlab-ctl upgrade, pg-upgrade and revert-pg-upgrade abort: promote the node, fence it, or unset postgresql['role'] before upgrading it.
Leaving the high availability mode
To turn a fenced node into a standalone instance, remove the high availability settings from it:
Comment out
postgresql['role']and theha_*settings in/etc/gitlab/gitlab.rb.Run
sudo gitlab-ctl reconfigure: it removes the start-guard environment of the service.Delete the markers left by the mode:
sudo rm -f /var/opt/gitlab/postgresql/data/fenced.marker \ /opt/gitlab/sv/postgresql/down /var/opt/gitlab/postgresql/data/standby.signalStart the database:
sudo gitlab-ctl start postgresql.
The node keeps its data and serves as a single-node instance. To leave the mode on a working node, also disable keepalived['enable'] and sentinel['enable'] and point gitlab_rails['db_host'] and the Redis settings at their single-node values.
Troubleshooting
promote finished with exit code 1 although the node was promoted
Where it appears. The output of gitlab-ctl pg-failover promote ends with a boxed warning that the database virtual IP address did not settle on the node.
Cause. The promotion succeeded, but keepalived did not move the virtual IP address, so clients still reach the old node through it.
Action. Check the service on every database node: sudo gitlab-ctl status keepalived and the log in /var/log/gitlab/keepalived/. Resolve the VRRP problem. The address moves as soon as the check of the new primary server passes.
rejoin aborts: pg_rewind could not resync this node
Where it appears. The output of gitlab-ctl pg-failover rejoin after the pg_rewind failure lines.
Cause. The WAL before the divergence of the two nodes is no longer kept on the new primary server, or the rewind user lacks privileges.
Action. Re-provision the node: sudo gitlab-ctl pg-failover rejoin --reclone. The command wipes the data directory and clones it anew.
gitlab-ctl start aborts: starting this node writable would split the cluster
Where it appears. The output of gitlab-ctl start, restart, up, once or force-restart on a database node.
Cause. The start guard verified another node as a live writable primary server, or could not verify it.
Action. Fence that node (gitlab-ctl pg-failover fence on it) and rejoin this node as a standby server, or fix the check credentials and pg_hba.conf entries on the peer.
PostgreSQL does not start on a fenced node after a reboot
Where it appears. sudo gitlab-ctl status shows WARNING: node is FENCED; the service log prints refusing to exec postgres (fenced old primary).
Cause. The fence marker and the disabled autostart survive a reboot: an isolated node must not return as a writable server on its own.
Action. Return the node to the cluster with gitlab-ctl pg-failover rejoin, or leave the mode as described in Leaving the high availability mode.
promote aborts: cannot verify peer
Where it appears. The output of gitlab-ctl pg-failover promote on the standby server.
Cause. The peer node answers, but not as a database the check user can query: wrong postgresql['ha_check_user'] credentials on the peer or missing entries in its pg_hba.conf.
Action. Fix the check credentials or the postgresql['ha_peers']-rendered pg_hba.conf entries on the peer, or fence it, then retry the promotion.
After a Redis failover the sign-in page answers with a 500 error
Where it appears. After an emergency Redis failover the web interface answers with a 500 error, and the application log shows RedisClient::FailoverError.
Cause. Sentinel promoted a replica to the master server, but gitlab-ctl reconfigure returned the node to the replica role of the static gitlab.rb topology. The cluster is left without a master server: Sentinel aborts the failover (-failover-abort-no-good-slave), and the application connects to a server that answers it is a replica.
Action. Align redis['master'] and redis['master_ip'] on every node with the actual topology and run gitlab-ctl reconfigure — the steps are in the Failover after a primary server failure section.