The cluster is enabled host by host: the first container becomes the PostgreSQL primary server, the others join as standby servers, then keepalived and Sentinel are enabled everywhere and the instance is pointed at the virtual IP addresses. The example addresses are from Requirements and topology: hosts 10.10.0.1, 10.10.0.2, 10.10.0.3, the frontend virtual IP address 10.10.0.10, the database virtual IP address 10.10.0.20.

Every host runs one container created as described in Requirements and topology. The configuration blocks below go into /srv/code/config/gitlab.rb on the host and are applied with docker exec code gitlab-ctl reconfigure. The full list of parameters with defaults is on the Parameters page.

Node 1: PostgreSQL primary server

Create the container on the first host and wait for the first start to finish, following the container output:

docker logs -f code

Then add the database parameters of the cluster to /srv/code/config/gitlab.rb:

postgresql['listen_address'] = '*'
postgresql['md5_auth_cidr_addresses'] = ['10.10.0.0/24', '127.0.0.1/32']
postgresql['role'] = 'primary'
postgresql['ha_peers'] = ['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>'
gitlab_rails['db_password'] = '<DB_PASSWORD>'
postgresql['sql_user_password'] = '<SQL_USER_PASSWORD_HASH>'

The database listens on all interfaces: in this mode it must accept connections to the database virtual IP address, which exists on either node. Access is restricted by the pg_hba.conf entries rendered from postgresql['md5_auth_cidr_addresses'] — keep port 5432 open to the cluster subnet only.

The db_password and sql_user_password pair is required in this block: with postgresql['listen_address'] set, the application connects to the database over TCP instead of the unix socket, and TCP connections authenticate with a password. The application sends the literal <DB_PASSWORD>. The gitlab database role takes its password from postgresql['sql_user_password'], which is the hash of <DB_PASSWORD>. Generate it on any node: run the command below, enter <DB_PASSWORD> at the prompt and copy the printed value — 32 hexadecimal characters without the md5 prefix:

docker exec -it code gitlab-ctl pg-password-md5 gitlab

Apply the change and run docker exec code gitlab-ctl reconfigure a second time. The second run must report no changes:

docker exec code gitlab-ctl reconfigure
docker exec code gitlab-ctl reconfigure

The <REPLICATION_PASSWORD>, <CHECK_PASSWORD> and <REWIND_PASSWORD> placeholders stand for the literal cluster passwords picked on Requirements and topology — together with <REDIS_PASSWORD> below. The same block on the standby server reuses them.

Check that the settings floor and the pg_hba.conf entries for the peer node are rendered:

docker exec code grep -E '^(wal_level|max_wal_senders|max_replication_slots|hot_standby|wal_log_hints)' \
  /var/opt/gitlab/postgresql/data/postgresql.conf
docker exec code grep -A4 'fork HA' /var/opt/gitlab/postgresql/data/pg_hba.conf

Node 1: replication slots

Create a physical replication slot for every standby server before it starts. A standby server refuses to stream until its own slot exists:

docker exec code gitlab-psql -c "SELECT pg_create_physical_replication_slot('gitlab_standby_2');"
docker exec code gitlab-psql -c "SELECT pg_create_physical_replication_slot('gitlab_standby_3');"

Nodes 2 and 3: PostgreSQL standby servers

Create the container on the second and third hosts and let the first start of each finish without the high availability parameters: the first start configures the database of a single-node instance, and the standby settings are added after it. Put the standby parameters into /srv/code/config/gitlab.rb:

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.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-2'
postgresql['replication_slot_name'] = 'gitlab_standby_2'
gitlab_rails['db_password'] = '<DB_PASSWORD>'
postgresql['sql_user_password'] = '<SQL_USER_PASSWORD_HASH>'

The db_password and sql_user_password pair is required here for the same reason as on node 1: the standby settings include listen_address, so the application of this node also connects over TCP.

Node 3 uses the same block with these differences: postgresql['ha_peers'] lists nodes 1 and 2, replication_application_name is gitlab-pg-3 and replication_slot_name is gitlab_standby_3. The steps below run on both nodes.

Render the configuration, stop the database and replace its data directory with a copy of the primary server:

docker exec code gitlab-ctl reconfigure
docker exec code gitlab-ctl stop postgresql
docker exec code sh -c 'rm -rf /var/opt/gitlab/postgresql/data/*'
docker exec -it -u gitlab-psql code /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

The pg_basebackup command asks for the replication password. Enter <REPLICATION_PASSWORD>, the literal value of postgresql['ha_replication_password']. The flags keep docker exec code gitlab-ctl reconfigure the single source of configuration: --no-slot leaves the permanent slot to the running standby server, and the omitted -R would have written a postgresql.auto.conf that shadows the configuration rendered from gitlab.rb.

Run docker exec code gitlab-ctl reconfigure again — with a real data directory present it creates standby.signal — and start the database:

docker exec code gitlab-ctl reconfigure
docker exec code gitlab-ctl start postgresql

Verify the replication. On the standby server:

docker exec code gitlab-psql -c 'SELECT pg_is_in_recovery();'

The answer must be t. On the primary server the standby servers show up as streaming:

docker exec code gitlab-psql -x -c "SELECT application_name, client_addr, state, sync_state FROM pg_stat_replication;"
docker exec code gitlab-psql -c "SELECT slot_name, active FROM pg_replication_slots;"

Shared secrets and host keys

Every container so far was started as an independent instance, so each generated its own Rails secrets and its own SSH host keys. Before the nodes serve one instance through the virtual IP addresses, make both identical on all of them:

  • Rails secrets: copy /srv/code/config/gitlab-secrets.json from the first host to the others, then run docker exec code gitlab-ctl reconfigure and docker restart code on each of them. With different secrets, user sessions, two-factor codes and encrypted CI variables break on the nodes that did not create them as soon as the frontend virtual IP address moves.
  • SSH host keys: copy the /srv/code/config/ssh_host_*_key and /srv/code/config/ssh_host_*_key.pub files from the first host to the others the same way. With different keys, Git clients report a changed host key on every move of the frontend virtual IP address.

keepalived on every node

Enable keepalived on every node. The frontend virtual IP address is enabled everywhere with distinct priorities. Enable the database virtual IP address on every node with the same priority — the check decides which node holds it: only the writable primary server passes it.

Node 1:

keepalived['enable'] = true
keepalived['interface'] = 'eth0'
keepalived['unicast_src_ip'] = '10.10.0.1'
keepalived['unicast_peers'] = ['10.10.0.2', '10.10.0.3']
keepalived['frontend_vip']['enable'] = true
keepalived['frontend_vip']['ip'] = '10.10.0.10'
keepalived['frontend_vip']['priority'] = 100
keepalived['db_vip']['enable'] = true
keepalived['db_vip']['ip'] = '10.10.0.20'

Nodes 2 and 3 differ from node 1 in the addresses and the frontend priority only — the db_vip lines are the same on every node:

# Node 2.
keepalived['unicast_src_ip'] = '10.10.0.2'
keepalived['unicast_peers'] = ['10.10.0.1', '10.10.0.3']
keepalived['frontend_vip']['priority'] = 95

# Node 3.
keepalived['unicast_src_ip'] = '10.10.0.3'
keepalived['unicast_peers'] = ['10.10.0.1', '10.10.0.2']
keepalived['frontend_vip']['priority'] = 90

Apply with docker exec code gitlab-ctl reconfigure on every node. After it completes, the frontend and database virtual IP addresses sit on the first host. Check it on the host:

ip addr show eth0 | grep '10.10.0.1[02]'

Redis Sentinel on every node

Redis replication with Sentinel is enabled with explicit settings, without the roles list: a role turns off the default service group, and a cluster node runs the full application stack.

On node 1, the current Redis master server:

redis['bind'] = '0.0.0.0'
redis['port'] = 6379
redis['password'] = '<REDIS_PASSWORD>'
redis['master'] = true
redis['master_name'] = 'gitlab-redis'
redis['master_ip'] = '10.10.0.1'
redis['master_port'] = 6379

sentinel['enable'] = true
sentinel['bind'] = '10.10.0.1'
sentinel['port'] = 26379
sentinel['quorum'] = 2

On nodes 2 and 3 the same block with two differences: redis['master'] = false and sentinel['bind'] set to the address of the node itself. The master_ip value is the address of the current Redis master server, the same on every node. The value is static: after an actual Redis switch, update redis['master'] and redis['master_ip'] on every node before the next run of docker exec code gitlab-ctl reconfigure — the steps are on the Operations page.

On every node, point the application at the Sentinel list:

redis['master_name'] = 'gitlab-redis'
redis['master_password'] = '<REDIS_PASSWORD>'
gitlab_rails['redis_sentinels'] = [
  { 'host' => '10.10.0.1', 'port' => 26379 },
  { 'host' => '10.10.0.2', 'port' => 26379 },
  { 'host' => '10.10.0.3', 'port' => 26379 }
]

Apply with docker exec code gitlab-ctl reconfigure on every node and check that Sentinel answers with the master server address:

docker exec code gitlab-ctl get-redis-master

Gitaly and Praefect on every node

The repositories of the cluster are stored by a Gitaly cluster: every node runs Gitaly with its own storage, and Praefect keeps three copies of every repository. The Rails and gitlab-shell of a node work with the local Praefect.

Create the Praefect database on the primary server first — before Praefect is enabled anywhere: it starts with its migrations and connects to the database at once:

docker exec code gitlab-psql -c "CREATE ROLE praefect LOGIN PASSWORD '<PRAEFECT_DB_PASSWORD>';"
docker exec code gitlab-psql -c "CREATE DATABASE praefect_production OWNER praefect;"

The connections come from the addresses of the cluster nodes and are covered by the pg_hba.conf entries of postgresql['md5_auth_cidr_addresses'].

<PRAEFECT_DB_PASSWORD> is the password of the praefect user from the command above, <GITALY_TOKEN> is the Gitaly token with which Praefect connects to the Gitaly nodes, <PRAEFECT_AUTH_TOKEN> is the Praefect token with which the application connects to Praefect.

Then add the block to /srv/code/config/gitlab.rb on every node. The block of node 1:

gitaly['configuration'] = {
  listen_addr: '10.10.0.1:8075',
  prometheus_listen_addr: 'localhost:9236',
  auth: { token: '<GITALY_TOKEN>' },
  storage: [
    { name: 'gitaly-1', path: '/var/opt/gitlab/git-data' }
  ]
}

praefect['enable'] = true
praefect['auto_migrate'] = true
praefect['configuration'] = {
  listen_addr: '10.10.0.1:2305',
  prometheus_listen_addr: 'localhost:9652',
  auth: { token: '<PRAEFECT_AUTH_TOKEN>' },
  failover: { enabled: true },
  database: {
    host: '10.10.0.20',
    port: 5432,
    user: 'praefect',
    password: '<PRAEFECT_DB_PASSWORD>',
    dbname: 'praefect_production',
    sslmode: 'disable'
  },
  virtual_storage: [
    {
      name: 'default',
      default_replication_factor: 3,
      node: [
        { storage: 'gitaly-1', address: 'tcp://10.10.0.1:8075', token: '<GITALY_TOKEN>' },
        { storage: 'gitaly-2', address: 'tcp://10.10.0.2:8075', token: '<GITALY_TOKEN>' },
        { storage: 'gitaly-3', address: 'tcp://10.10.0.3:8075', token: '<GITALY_TOKEN>' }
      ]
    }
  ]
}

gitlab_rails['repositories_storages'] = {
  'default' => {
    'gitaly_address' => 'tcp://10.10.0.1:2305',
    'gitaly_token' => '<PRAEFECT_AUTH_TOKEN>'
  }
}

Nodes 2 and 3 use the same block with these differences: the listen_addr values hold the address of the node, the storage name is gitaly-2 or gitaly-3, and the gitaly_address of gitlab_rails['repositories_storages'] points at the local Praefect. The node list is the same on every node and names every Gitaly node of the cluster, its own included.

Apply with docker exec code gitlab-ctl reconfigure on every node.

The cluster repositories live in <storage path>/@cluster/repositories/: the layout differs from the single-Gitaly layout (@hashed), so the repositories of an instance converted from a single-node installation need a migration of their own — a clean installation has no such step. The default_replication_factor value does not exceed the number of Gitaly nodes: keep 3 on three nodes, 2 on two.

Check the cluster after docker exec code gitlab-ctl reconfigure: docker exec code gitlab-ctl status praefect gitaly reports run on every node. Create a project — the repository directory appears on every Gitaly node. Stop Gitaly on one node with docker exec code gitlab-ctl stop gitaly and create another project — the creation succeeds. After docker exec code gitlab-ctl start gitaly the missing copies are backfilled by the Praefect reconciler: its default scheduling interval is 5 minutes (scheduling_interval).

The tcp:// listeners of the block above accept connections without encryption. For a cluster, set up TLS on the Gitaly and Praefect listeners.

The block with TLS and the certificates is in the “TLS configuration” section of the Gitaly page.

Pointing the instance at the virtual IP addresses

With the virtual IP addresses in place, point the application of every node at them. Until this step the nodes used their local database and Redis, which is why it comes last: the first start of a container cannot use a database virtual IP address that does not exist yet or a Sentinel list whose nodes are not up.

On every node, add to /srv/code/config/gitlab.rb:

gitlab_rails['db_host'] = '10.10.0.20'

The database password pair is already set in the blocks of every node. Apply with docker exec code gitlab-ctl reconfigure. On the node that holds the primary server docker exec code gitlab-ctl reconfigure can restart PostgreSQL, and the database virtual IP address leaves the node for the seconds the check fails — run this step and every later run of docker exec code gitlab-ctl reconfigure on a database node in a maintenance window. The application reconnects on its own.

Verifying the cluster

The HA: section of the status output gathers the cluster state on any node:

docker exec code gitlab-ctl status
HA:
  local: role=primary pg=running
  replication (this node is writable; standbys attached):
    gitlab-pg-2: state=streaming lag=0 bytes
    gitlab-pg-3: state=streaming lag=0 bytes
  db-vip: vip=10.10.0.20 on-local-interfaces=yes pg_isready=ready
  redis-master: 10.10.0.1:6379

Check the frontend entry point from a workstation:

curl -sI http://10.10.0.10/users/sign_in | head -n 1

The files of the instance — uploads, CI artifacts, LFS objects, packages, Terraform states and the rest — are stored on the disk of the node and are lost together with it when the node is lost. Move them to object storage.

The setup is described on the Object storage page.

The cluster is deployed. The day-to-day procedures — planned switchover, failover, rejoin — are on the Operations page.

Additional resources