Redis replication and failover providing your own instance
- Tier: Free, Premium, Ultimate
- Offering: GitLab Self-Managed
如果您在云服务提供商上托管 GitLab,可以选择使用 Redis 的托管服务。 例如,AWS 提供 ElastiCache,它运行 Redis。
或者,您可以选择独立于 Linux 包来管理自己的 Redis 实例。
要求
以下是提供自己的 Redis 实例的要求:
- 在 requirements 页面中查找所需的最低 Redis 版本。
- 支持独立的 Redis 或使用 Sentinel 的 Redis 高可用性配置。Redis Cluster 不支持。
- 来自云服务提供商(如 AWS ElastiCache)的托管 Redis 工作良好。如果这些服务支持高可用性,请确保它不是Redis Cluster 类型。
记录 Redis 节点的 IP 地址或主机名、端口以及密码(如果需要)。
作为云服务提供商中的托管服务使用 Redis
-
根据 requirements 设置 Redis。
-
在您的
/etc/gitlab/gitlab.rb文件中,配置 GitLab 应用服务器,为外部 Redis 服务设置适当的连接详情:使用单个 Redis 实例时:
redis['enable'] = false gitlab_rails['redis_host'] = '<redis_instance_url>' gitlab_rails['redis_port'] = '<redis_instance_port>' # 如果在 Redis 节点上配置了 Redis 认证,则需要此设置 gitlab_rails['redis_password'] = '<redis_password>' # 如果实例使用 Redis SSL,设置为 true gitlab_rails['redis_ssl'] = true使用独立的 Redis 缓存和持久化实例时:
redis['enable'] = false # 默认 Redis 连接 gitlab_rails['redis_host'] = '<redis_persistent_instance_url>' gitlab_rails['redis_port'] = '<redis_persistent_instance_port>' gitlab_rails['redis_password'] = '<redis_persistent_password>' # 如果实例使用 Redis SSL,设置为 true gitlab_rails['redis_ssl'] = true # Redis 缓存连接 # 如果使用 SSL,将 `redis://` 替换为 `rediss://` gitlab_rails['redis_cache_instance'] = 'redis://:<redis_cache_password>@<redis_cache_instance_url>:<redis_cache_instance_port>' -
重新配置以使更改生效:
sudo gitlab-ctl reconfigure
设置驱逐策略
运行单个 Redis 实例时,应将驱逐策略设置为 noeviction。
如果您运行独立的 Redis 缓存和持久化实例,缓存应配置为 最近最少使用缓存 (LRU),使用 allkeys-lru,而持久化实例应设置为 noeviction。
此配置取决于云服务提供商或服务,但通常以下设置和值用于配置缓存:
maxmemory-policy=allkeys-lrumaxmemory-samples=5
使用自己的 Redis 服务器进行 Redis 复制和故障转移
这是关于配置可扩展 Redis 设置的文档,当您自己安装 Redis 而不使用 Linux 包中捆绑的版本时。尽管我们强烈建议使用 Linux 包,因为我们专门为 GitLab 优化了它们,并且我们会负责将 Redis 升级到最新支持的版本。
另请注意,您可以根据 Configuration Files Documentation 中概述的高级 Redis 设置,选择覆盖对 /home/git/gitlab/config/resque.yml 的所有引用。
我们无法过分强调阅读 Linux 包 Redis HA 的 replication and failover 文档的重要性,因为它为 Redis 的配置提供了一些宝贵的信息。在继续本指南之前,请先阅读它。
在设置新的 Redis 实例之前,以下是一些要求:
- 本指南中的所有 Redis 服务器都必须配置为使用 TCP 连接而不是套接字。要将 Redis 配置为使用 TCP 连接,您需要在 Redis 配置文件中定义
bind和port。您可以绑定到所有接口 (0.0.0.0) 或指定所需接口的 IP(例如,来自内部网络的接口)。 - 从 Redis 3.2 开始,您必须定义密码才能接收外部连接 (
requirepass)。 - 如果您使用 Redis 和 Sentinel,还需要在同一实例中为副本密码定义 (
masterauth) 定义相同的密码。
此外,请阅读 Redis replication and failover with the Linux package 中描述的先决条件。
步骤 1. 配置主 Redis 实例
假设主 Redis 实例的 IP 是 10.0.0.1:
-
编辑
/etc/redis/redis.conf:## 定义一个 `bind` 地址,指向您的其他机器可以访问的本地 IP。 ## 如果确实需要绑定到外部可访问的 IP,请确保添加额外的防火墙规则以防止未授权访问: bind 10.0.0.1 ## 定义一个 `port` 强制 redis 在 TCP 上监听,以便其他机器可以连接到它(默认端口是 `6379`)。 port 6379 ## 设置密码认证(所有节点使用相同的密码)。 ## 当设置 Redis 与 Sentinel 一起使用时,密码应定义为 `requirepass` 和 `masterauth` 相同。 requirepass redis-password-goes-here masterauth redis-password-goes-here -
重启 Redis 服务以使更改生效。
步骤 2. 配置副本 Redis 实例
假设副本 Redis 实例的 IP 是 10.0.0.2:
-
编辑
/etc/redis/redis.conf:## 定义一个 `bind` 地址,指向您的其他机器可以访问的本地 IP。 ## 如果确实需要绑定到外部可访问的 IP,请确保添加额外的防火墙规则以防止未授权访问: bind 10.0.0.2 ## 定义一个 `port` 强制 redis 在 TCP 上监听,以便其他机器可以连接到它(默认端口是 `6379`)。 port 6379 ## 设置密码认证(所有节点使用相同的密码)。 ## 当设置 Redis 与 Sentinel 一起使用时,密码应定义为 `requirepass` 和 `masterauth` 相同。 requirepass redis-password-goes-here masterauth redis-password-goes-here ## 定义指向主 Redis 实例的 `replicaof`,包含 IP 和端口。 replicaof 10.0.0.1 6379 -
重启 Redis 服务以使更改生效。
-
对所有其他副本节点重复这些步骤。
步骤 3. 配置 Redis Sentinel 实例
Sentinel 是一种特殊的 Redis 服务器。它继承了您可以在 redis.conf 中定义的大多数基本配置选项,以及以 sentinel 前缀开头的特定选项。
假设 Redis Sentinel 安装在 IP 为 10.0.0.1 的主 Redis 实例的同一台机器上(某些设置可能与主实例重叠):
-
编辑
/etc/redis/sentinel.conf:## 定义一个 `bind` 地址,指向您的其他机器可以访问的本地 IP。 ## 如果确实需要绑定到外部可访问的 IP,请确保添加额外的防火墙规则以防止未授权访问: bind 10.0.0.1 ## 定义一个 `port` 强制 Sentinel 在 TCP 上监听,以便其他机器可以连接到它(默认端口是 `6379`)。 port 26379 ## 设置密码认证(所有节点使用相同的密码)。 ## 当设置 Redis 与 Sentinel 一起使用时,密码应定义为 `requirepass` 和 `masterauth` 相同。 requirepass redis-password-goes-here masterauth redis-password-goes-here ## 使用 `sentinel auth-pass` 定义与主 Redis 和副本实例相同的共享密码。 sentinel auth-pass gitlab-redis redis-password-goes-here ## 使用 `sentinel monitor` 定义 Redis 主节点的 IP 和端口,以及启动故障转移所需的仲裁数。 sentinel monitor gitlab-redis 10.0.0.1 6379 2 ## 使用 `sentinel down-after-milliseconds` 定义以 `ms` 为单位的超时时间, ## 超过该时间后无响应的服务器被视为已下线。 sentinel down-after-milliseconds gitlab-redis 10000 ## 以 `ms` 为单位定义 `sentinel failover_timeout` 的值。这具有多个含义: ## ## * Sentinel 尝试对同一主节点进行故障转移后,重新启动故障转移所需的时间是故障转移超时的两倍。 ## ## * 对于根据 Sentinel 当前配置复制到错误主节点的副本,强制其复制到正确主节点所需的时间, ## 正好是故障转移超时时间(从 Sentinel 检测到错误配置的时刻开始计算)。 ## ## * 取消已进行但未产生任何配置更改(REPLICAOF NO ONE 尚未被提升的副本确认)的故障转移所需的时间。 ## ## * 进行中的故障转移等待所有副本重新配置为新副本的最大时间。然而,即使超过此时间, ## 副本仍会被 Sentinel 重新配置,但不会按照指定的并行同步进度进行。 ## sentinel failover_timeout 30000 -
重启 Redis 服务以使更改生效。
-
对所有其他 Sentinel 节点重复这些步骤。
步骤 4. 配置 GitLab 应用
您可以在新的或现有的安装中随时启用或禁用 Sentinel 支持。从 GitLab 应用角度来看,它只需要正确的 Sentinel 节点凭据。
虽然它不需要所有 Sentinel 节点的列表,但在发生故障时,它需要访问至少一个列出的节点。
以下步骤应在 GitLab 应用服务器上执行,理想情况下该服务器不应在同一台机器上运行 Redis 或 Sentinel:
-
编辑
/home/git/gitlab/config/resque.yml,遵循resque.yml.example中的示例,并取消注释 Sentinel 行,指向正确的服务器凭据:# resque.yaml production: url: redis://:redi-password-goes-here@gitlab-redis/ sentinels: - host: 10.0.0.1 port: 26379 # 指向 sentinel,而不是 redis 端口 - host: 10.0.0.2 port: 26379 # 指向 sentinel,而不是 redis 端口 - host: 10.0.0.3 port: 26379 # 指向 sentinel,而不是 redis 端口 -
重启 GitLab 以使更改生效。
1 个主节点、2 个副本节点和 3 个 Sentinel 的最小配置示例
在本示例中,我们假设所有服务器都有一个内部网络接口,IP 在 10.0.0.x 范围内,并且它们可以使用这些 IP 相互连接。
在实际使用中,您还需要设置防火墙规则以防止来自其他机器的未授权访问,并阻止来自外部的流量(Internet)。
对于此示例,Sentinel 1 配置在与 Redis Primary 相同的机器上,Sentinel 2 配置在与 Replica 1 相同的机器上,Sentinel 3 配置在与 Replica 2 相同的机器上。
以下是每个 机器 及其分配的 IP 的列表和描述:
10.0.0.1: Redis Primary + Sentinel 110.0.0.2: Redis Replica 1 + Sentinel 210.0.0.3: Redis Replica 2 + Sentinel 310.0.0.4: GitLab application
初始配置后,如果 Sentinel 节点发起故障转移,Redis 节点将被重新配置,Primary 会永久地(包括在 redis.conf 中)从一个节点更改为另一个节点,直到再次发起新的故障转移。
sentinel.conf 也会发生同样的情况,它在初始执行后被覆盖,在任何新的 Sentinel 节点开始监视 Primary,或者故障转移提升不同的 Primary 节点后。
Redis Primary 和 Sentinel 1 的示例配置
-
在
/etc/redis/redis.conf中:bind 10.0.0.1 port 6379 requirepass redis-password-goes-here masterauth redis-password-goes-here -
在
/etc/redis/sentinel.conf中:bind 10.0.0.1 port 26379 sentinel auth-pass gitlab-redis redis-password-goes-here sentinel monitor gitlab-redis 10.0.0.1 6379 2 sentinel down-after-milliseconds gitlab-redis 10000 sentinel failover_timeout 30000 -
重启 Redis 服务以使更改生效。
Redis Replica 1 和 Sentinel 2 的示例配置
-
在
/etc/redis/redis.conf中:bind 10.0.0.2 port 6379 requirepass redis-password-goes-here masterauth redis-password-goes-here replicaof 10.0.0.1 6379 -
在
/etc/redis/sentinel.conf中:bind 10.0.0.2 port 26379 sentinel auth-pass gitlab-redis redis-password-goes-here sentinel monitor gitlab-redis 10.0.0.1 6379 2 sentinel down-after-milliseconds gitlab-redis 10000 sentinel failover_timeout 30000 -
重启 Redis 服务以使更改生效。
Redis Replica 2 和 Sentinel 3 的示例配置
-
在
/etc/redis/redis.conf中:bind 10.0.0.3 port 6379 requirepass redis-password-goes-here masterauth redis-password-goes-here replicaof 10.0.0.1 6379 -
在
/etc/redis/sentinel.conf中:bind 10.0.0.3 port 26379 sentinel auth-pass gitlab-redis redis-password-goes-here sentinel monitor gitlab-redis 10.0.0.1 6379 2 sentinel down-after-milliseconds gitlab-redis 10000 sentinel failover_timeout 30000 -
重启 Redis 服务以使更改生效。
GitLab 应用的示例配置
-
编辑
/home/git/gitlab/config/resque.yml:production: url: redis://:redis-password-goes-here@gitlab-redis/ sentinels: - host: 10.0.0.1 port: 26379 # 指向 sentinel,而不是 redis 端口 - host: 10.0.0.2 port: 26379 # 指向 sentinel,而不是 redis 端口 - host: 10.0.0.3 port: 26379 # 指向 sentinel,而不是 redis 端口 -
重启 GitLab 以使更改生效。
故障排除
请参阅 Redis 故障排除指南。