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 /srv/code/config/gitlab.rb on every node and applying them. In the example, the Gitaly hosts 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:
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 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 /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).
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.
Copy the certificate and key of each node to its host under /srv/code/config/ssl/, the CA certificate under /srv/code/config/trusted-certs/; inside the container these are /etc/gitlab/ssl/ and /etc/gitlab/trusted-certs/. Gitaly and Praefect read the key as the git user, so its owner inside the container is git:
sudo mkdir -p /srv/code/config/ssl /srv/code/config/trusted-certs
sudo install -m 600 10.10.0.1.key /srv/code/config/ssl/10.10.0.1.key
sudo install -m 644 10.10.0.1.crt /srv/code/config/ssl/10.10.0.1.crt
sudo install -m 644 ca.crt /srv/code/config/trusted-certs/ca.crt
docker exec code chown git:git /etc/gitlab/ssl/10.10.0.1.keyServers 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 docker exec code gitlab-ctl reconfigure on every node and check the cluster from one of the nodes:
docker exec code gitlab-ctl reconfigure
docker exec code gitlab-rake gitlab:gitaly:checkThe 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 docker exec code gitlab-ctl reconfigure once more.
Certificate replacement
Replacing the certificate and key files of a node under /srv/code/config/ssl/ without changing gitlab.rb requires a restart of the services:
docker exec code gitlab-ctl restart gitaly
docker exec code gitlab-ctl restart praefectReplacing the CA certificate under /srv/code/config/trusted-certs/ requires another run of docker exec code 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.