Gitaly with Praefect forms a Gitaly cluster. The replication factor sets how many copies of every repository exist: with the factor of 3, Praefect keeps three copies on different Gitaly nodes. When a Gitaly node fails, Praefect serves the requests from the remaining copies, and when a node returns, the Praefect reconciler restores the missing repositories on its own. The application and gitlab-shell of every node work with the local Praefect; no separate address for Praefect is needed.

Configuration

The configuration starts with the Praefect database, then come the blocks in /etc/gitlab/gitlab.rb on every node and applying them. In the example, the Gitaly nodes have the addresses 10.10.0.1, 10.10.0.2 and 10.10.0.3 and the storages gitaly-1–gitaly-3; the Praefect database is reachable at 10.10.0.20.

Create the Praefect database first — before Praefect is enabled on any node: the service starts with migrations and connects to the database at once:

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

The connections of Praefect come over TCP from the addresses of the nodes: allow them in pg_hba.conf, for example through postgresql['md5_auth_cidr_addresses'].

<GITALY_TOKEN> is the Gitaly token, <PRAEFECT_AUTH_TOKEN> the Praefect token, <PRAEFECT_DB_PASSWORD> the password of the praefect user. Then add the block to /etc/gitlab/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 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 gitlab-ctl reconfigure: 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 gitlab-ctl stop gitaly and create another project — the creation succeeds. After gitlab-ctl start gitaly the missing copies are backfilled by the Praefect reconciler: its default scheduling interval is 5 minutes (scheduling_interval).

TLS configuration

The tcp:// listeners of the block above accept connections without encryption. The TLS mode adds a certificate to every listener: repository traffic between the nodes travels encrypted, and the nodes and the application verify the certificates against the certificate of a certificate authority (CA). The configuration starts with the certificates, then the blocks in gitlab.rb on every node get the TLS listeners and the tls:// addresses.

Certificates

Issue a certificate of your own certificate authority for every node. The SAN field of the certificate must contain the node IP address: Praefect reaches the Gitaly nodes at the addresses from the virtual_storage list, and the CN field is ignored during verification. Public certificate authorities do not issue certificates for private network addresses such as 10.10.0.1, so the certificate authority is your own.

Example: issuing the certificates

The CA certificate is issued once for the cluster, a node certificate for every node, with the node address in the SAN:

# Root CA certificate, one for the cluster.
openssl req -x509 -nodes -newkey rsa:4096 -days 3650 \
  -keyout ca.key -out ca.crt \
  -subj "/CN=Deckhouse Code cluster CA"

# Certificate of node 10.10.0.1: the node IP address goes into the SAN, the CN is ignored.
openssl req -new -nodes -newkey rsa:4096 \
  -keyout 10.10.0.1.key -out 10.10.0.1.csr \
  -subj "/CN=10.10.0.1"
openssl x509 -req -in 10.10.0.1.csr -CA ca.crt -CAkey ca.key -CAcreateserial \
  -days 825 -out 10.10.0.1.crt \
  -extfile <(printf 'subjectAltName=IP:10.10.0.1')

For nodes 2 and 3, repeat the node certificate step with their addresses in the file name and in the SAN.

Copy the certificate and key of each node to that node under /etc/gitlab/ssl/, the CA certificate under /etc/gitlab/trusted-certs/. Gitaly and Praefect read the key as the git user, so the owner of the file is git:

sudo mkdir -p /etc/gitlab/ssl /etc/gitlab/trusted-certs
sudo install -o git -m 600 10.10.0.1.key /etc/gitlab/ssl/10.10.0.1.key
sudo install -o git -m 644 10.10.0.1.crt /etc/gitlab/ssl/10.10.0.1.crt
sudo install -m 644 ca.crt /etc/gitlab/trusted-certs/ca.crt

Servers and clients on the node trust the CA certificate through the embedded certificate store, which is fed from /etc/gitlab/trusted-certs/.

Configuration on the nodes

The block of node 1 with TLS replaces the block of the configuration above:

gitaly['configuration'] = {
  tls_listen_addr: '10.10.0.1:9075',
  prometheus_listen_addr: 'localhost:9236',
  tls: {
    certificate_path: '/etc/gitlab/ssl/10.10.0.1.crt',
    key_path: '/etc/gitlab/ssl/10.10.0.1.key'
  },
  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: nil,
  tls_listen_addr: '10.10.0.1:3305',
  prometheus_listen_addr: 'localhost:9652',
  tls: {
    certificate_path: '/etc/gitlab/ssl/10.10.0.1.crt',
    key_path: '/etc/gitlab/ssl/10.10.0.1.key'
  },
  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: 'tls://10.10.0.1:9075', token: '<GITALY_TOKEN>' },
        { storage: 'gitaly-2', address: 'tls://10.10.0.2:9075', token: '<GITALY_TOKEN>' },
        { storage: 'gitaly-3', address: 'tls://10.10.0.3:9075', token: '<GITALY_TOKEN>' }
      ]
    }
  ]
}

The address Rails dials the local Praefect at is set by the gitlab_rails['repositories_storages'] parameter:

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

The tls:// addresses in the node list are the TLS listeners of the Gitaly nodes (port 9075). The address in repositories_storages is the TLS listener of the local Praefect (port 3305): Rails, Workhorse and gitlab-shell of the node dial the cluster storages through it. The Prometheus listeners of both services stay on localhost and serve no TLS. The block sets no plaintext listeners: Gitaly without listen_addr and Praefect with listen_addr: nil accept network connections over TLS only.

Nodes 2 and 3 use the same block with differences: the node address stands in the tls_listen_addr of both services, in the tls paths and in repositories_storages, together with the node’s own certificate, and the storage name is gitaly-2 or gitaly-3. The node list is the same on every node.

Applying the configuration

Run gitlab-ctl reconfigure on every node and check the cluster from one of the nodes:

sudo gitlab-ctl reconfigure
sudo gitlab-rake gitlab:gitaly:check

The gitlab:gitaly:check task verifies that Rails reaches every storage of the cluster.

A running cluster switches to TLS node by node. First add the TLS listeners and the tls:// addresses on every node and keep the former listen_addr values, so the services keep accepting unencrypted connections. Once the TLS addresses work on every node, remove listen_addr from the Gitaly block, set listen_addr: nil in the Praefect block as in the block above, and run gitlab-ctl reconfigure once more.

Certificate replacement

Replacing the certificate and key files of a node under /etc/gitlab/ssl/ without changing gitlab.rb requires a restart of the services:

sudo gitlab-ctl restart gitaly
sudo gitlab-ctl restart praefect

Replacing the CA certificate under /etc/gitlab/trusted-certs/ requires another run of gitlab-ctl reconfigure — the certificates reach the embedded store during the configuration run.

For Praefect command-line checks, set the environment variable SSL_CERT_DIR=/etc/gitlab/trusted-certs.

Additional resources