跳到主要内容

搭建redis集群

基础环境规划:

-|IP地址 | 端口| 角色|Redis版本 ---|---|---|--- 192.168.192.161|6379|redis-master,sentinel|6.2.14 192.168.192.162|6379|redis-slave1,sentinel|6.2.14 192.168.192.163|6379|redis-slave2,sentinel|6.2.14

官网下载网页链接:https://redis.io/download

Redis安装包下载

cd /usr/local/
wget https://download.redis.io/releases/redis-6.2.14.tar.gz
tar -zxvf redis-6.2.14.tar.gz
mv redis-6.2.14 redis

mkdir /usr/local/redis/{logs,data,sentinel_logs,sentinel_data} -p

安装gcc环境

Redis是C语言编写,需要安装C环境。

#centos/oracle
yum install gcc gcc-c++ make -y

# ubuntu/debian
sudo apt-get install gcc -y
sudo apt-get install g++ -y
sudo apt-get install make -y
#进入目录: 
cd /usr/local/redis
#编译:
make
#安装Redis:
make PREFIX=/usr/local/redis install

groupadd redis
useradd -r -g redis redis
mkdir /usr/local/redis/conf/ & vim /usr/local/redis/conf/redis.conf

master节点配置

# 绑定的主机地址
bind 0.0.0.0
# 是否开启保护模式,默认开启。要是配置里没有指定bind和密码。开启该参数后,
# redis只会本地进行访问,拒绝外部访问。要是开启了密码和指定bind,可以开启。
# 否则最好关闭设置为no。
protected-mode no
# redis进程的端口号
port 6379
# 客户端闲置多长时间后关闭连接,默认此参数为0即关闭此功能
timeout 0
tcp-keepalive 300
# redis进程是否以守护进程的方式运行,yes为是,no为否。默认为no(不以守护进程的方式运行会占用
# 一个终端)。
daemonize yes
supervised no
# 指定redis进程的PID文件存放位置
pidfile /usr/local/redis/redis_6379.pid
# redis日志级别,可用的级别有debug.verbose.notice.warning
loglevel notice
# log文件输出位置,如果进程以守护进程的方式运行,此处又将输出文件设置为stdout的话,
# 就会将日志信息输出到/dev/null里面去了
logfile "/usr/local/redis/logs/redis.log"
# 设置数据库的数量,默认为0可以使用select <dbid>命令在连接上指定数据库id
databases 16
always-show-logo yes
# 指定在多少时间内刷新次数达到多少的时候会将数据同步到数据文件
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
# 指定存储至本地数据库时是否压缩文件,默认为yes即启用存储
rdbcompression yes
rdbchecksum yes
# 指定本地数据库文件名
dbfilename dump.rdb
# 指定本地数据问就按存放位置
dir /usr/local/redis/data/
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
# 设置redis连接密码,如果配置了连接密码,客户端在连接redis是需要通过AUTH<password>命令提供密码,默认关闭
requirepass redis@123456
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
# 指定是否在每次更新操作后进行日志记录,Redis在默认情况下是异步的把数据写入磁盘,如果不开启,可能会在断电时导致一段时间内的数据丢失。因为redis本身同步数据文件是按上面save条件来同步的,所以有的数据会在一段时间内只存在于内存中。默认为no。
appendonly no
# 指定跟新日志文件名默认为appendonly.aof
appendfilename "appendonly.aof"
# 指定更新日志的条件,有三个可选参数 - no:表示等操作系统进行数据缓存同步到磁盘(快),always:表示每次更新操作后手动调用fsync()将数据写到磁盘(慢,安全), everysec:表示每秒同步一次(折衷,默认值);
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
# 当master设置了密码保护时,slave服务连接master的密码
masterauth "redis@123456"
#slave-read-only no
vim /usr/local/redis/conf/sentinel.conf
# 修改Sentinel监听端口
port 26379
# 允许Sentinel后台运行
daemonize yes
# 设置Sentinel日志存放路径
logfile "/usr/local/redis/sentinel_logs/redis-sentinel.log"
# 工作目录,需要创建好目录,可自定义
dir "/usr/local/redis/sentinel_data"
pidfile "/usr/local/redis/redis-sentinel.pid"
sentinel myid b525892799239ffe20ea5e6ef860f918f1ec3122
sentinel deny-scripts-reconfig yes
# redis01:master名称可自定义
# 192.168.192.161 6379 :redis主节点IP和端口
# 2 :表示多少个Sentinel认为redis主节点失效时,才算真正失效
# Sentinel监听redis主节点
sentinel monitor redis01 192.168.192.161 6379 2
# 若sentinel在该配置值内未能完成master/slave自动切换,则认为本次failover失败。
sentinel failover-timeout redis01 15000
# 设置连接master和slave时的密码,注意的是sentinel不能分别为master和slave设置不同的密码,因此master和slave的密码应该设置相同
sentinel auth-pass redis01 redis@123456
# 配置失效时间,master会被这个sentinel主观地认为是不可用的,单位毫秒
sentinel down-after-milliseconds redis01 10000
# 在发生failover主备切换时最多可以有多少个slave同时对新的master进行同步。
sentinel parallel-syncs redis01 2
maxclients 4064
protected-mode no

slave节点1配置

mkdir /usr/local/redis/conf/ & vim /usr/local/redis/conf/redis.conf
# 绑定的主机地址
bind 0.0.0.0
# 是否开启保护模式,默认开启。要是配置里没有指定bind和密码。开启该参数后,
# redis只会本地进行访问,拒绝外部访问。要是开启了密码和指定bind,可以开启。
# 否则最好关闭设置为no。
protected-mode no
# redis进程的端口号
port 6379
# 客户端闲置多长时间后关闭连接,默认此参数为0即关闭此功能
timeout 0
tcp-keepalive 300
# redis进程是否以守护进程的方式运行,yes为是,no为否。默认为no(不以守护进程的方式运行会占用
# 一个终端)。
daemonize yes
supervised no
# 指定redis进程的PID文件存放位置
pidfile /usr/local/redis/redis_6379.pid
# redis日志级别,可用的级别有debug.verbose.notice.warning
loglevel notice
# log文件输出位置,如果进程以守护进程的方式运行,此处又将输出文件设置为stdout的话,
# 就会将日志信息输出到/dev/null里面去了
logfile "/usr/local/redis/logs/redis.log"
# 设置数据库的数量,默认为0可以使用select <dbid>命令在连接上指定数据库id
databases 16
always-show-logo yes
# 指定在多少时间内刷新次数达到多少的时候会将数据同步到数据文件
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
# 指定存储至本地数据库时是否压缩文件,默认为yes即启用存储
rdbcompression yes
rdbchecksum yes
# 指定本地数据库文件名
dbfilename dump.rdb
# 指定本地数据问就按存放位置
dir /usr/local/redis/data/
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
# 设置redis连接密码,如果配置了连接密码,客户端在连接redis是需要通过AUTH<password>命令提供密码,默认关闭
requirepass redis@123456
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
# 指定是否在每次更新操作后进行日志记录,Redis在默认情况下是异步的把数据写入磁盘,如果不开启,可能会在断电时导致一段时间内的数据丢失。因为redis本身同步数据文件是按上面save条件来同步的,所以有的数据会在一段时间内只存在于内存中。默认为no。
appendonly no
# 指定跟新日志文件名默认为appendonly.aof
appendfilename "appendonly.aof"
# 指定更新日志的条件,有三个可选参数 - no:表示等操作系统进行数据缓存同步到磁盘(快),always:表示每次更新操作后手动调用fsync()将数据写到磁盘(慢,安全), everysec:表示每秒同步一次(折衷,默认值);
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
# 从机的配置和主机相似,不同的地方是需要使用replicaof指定主机(master)的IP地址和端口,需要注意的是老版本使用的是 slaveof,而本次使用的redis-6.2.14版本要使用 replicaof
replicaof 192.168.192.161 6379
# 当master设置了密码保护时,slave服务连接master的密码
masterauth "redis@123456"
#slave-read-only no
vim /usr/local/redis/conf/sentinel.conf
# 修改Sentinel监听端口
port 26379
# 允许Sentinel后台运行
daemonize yes
# 设置Sentinel日志存放路径
logfile "/usr/local/redis/sentinel_logs/redis-sentinel.log"
pidfile "/usr/local/redis/redis-sentinel.pid"
# 工作目录,需要创建好目录,可自定义
dir "/usr/local/redis/sentinel_data"
sentinel myid c7e0fb1ffe01de5d4ba4350f4075a75ac061d713
sentinel deny-scripts-reconfig yes
# redis01:master名称可自定义
# 192.168.192.161 6379 :redis主节点IP和端口
# 2 :表示多少个Sentinel认为redis主节点失效时,才算真正失效
# Sentinel监听redis主节点
sentinel monitor redis01 192.168.192.161 6379 2
# 若sentinel在该配置值内未能完成master/slave自动切换,则认为本次failover失败。
sentinel failover-timeout redis01 15000
# 设置连接master和slave时的密码,注意的是sentinel不能分别为master和slave设置不同的密码,因此master和slave的密码应该设置相同
sentinel auth-pass redis01 redis@123456
# 配置失效时间,master会被这个sentinel主观地认为是不可用的,单位毫秒
sentinel down-after-milliseconds redis01 10000
# 在发生failover主备切换时最多可以有多少个slave同时对新的master进行同步。
sentinel parallel-syncs redis01 2
maxclients 4064
protected-mode no

slave节点2配置

mkdir /usr/local/redis/conf/ & vim /usr/local/redis/conf/redis.conf
# 绑定的主机地址
bind 0.0.0.0
# 是否开启保护模式,默认开启。要是配置里没有指定bind和密码。开启该参数后,
# redis只会本地进行访问,拒绝外部访问。要是开启了密码和指定bind,可以开启。
# 否则最好关闭设置为no。
protected-mode no
# redis进程的端口号
port 6379
# 客户端闲置多长时间后关闭连接,默认此参数为0即关闭此功能
timeout 0
tcp-keepalive 300
# redis进程是否以守护进程的方式运行,yes为是,no为否。默认为no(不以守护进程的方式运行会占用
# 一个终端)。
daemonize yes
supervised no
# 指定redis进程的PID文件存放位置
pidfile /usr/local/redis/redis_6379.pid
# redis日志级别,可用的级别有debug.verbose.notice.warning
loglevel notice
# log文件输出位置,如果进程以守护进程的方式运行,此处又将输出文件设置为stdout的话,
# 就会将日志信息输出到/dev/null里面去了
logfile "/usr/local/redis/logs/redis.log"
# 设置数据库的数量,默认为0可以使用select <dbid>命令在连接上指定数据库id
databases 16
always-show-logo yes
# 指定在多少时间内刷新次数达到多少的时候会将数据同步到数据文件
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
# 指定存储至本地数据库时是否压缩文件,默认为yes即启用存储
rdbcompression yes
rdbchecksum yes
# 指定本地数据库文件名
dbfilename dump.rdb
# 指定本地数据问就按存放位置
dir /usr/local/redis/data/
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
# 设置redis连接密码,如果配置了连接密码,客户端在连接redis是需要通过AUTH<password>命令提供密码,默认关闭
requirepass redis@123456
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
# 指定是否在每次更新操作后进行日志记录,Redis在默认情况下是异步的把数据写入磁盘,如果不开启,可能会在断电时导致一段时间内的数据丢失。因为redis本身同步数据文件是按上面save条件来同步的,所以有的数据会在一段时间内只存在于内存中。默认为no。
appendonly no
# 指定跟新日志文件名默认为appendonly.aof
appendfilename "appendonly.aof"
# 指定更新日志的条件,有三个可选参数 - no:表示等操作系统进行数据缓存同步到磁盘(快),always:表示每次更新操作后手动调用fsync()将数据写到磁盘(慢,安全), everysec:表示每秒同步一次(折衷,默认值);
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
# 从机的配置和主机相似,不同的地方是需要使用replicaof指定主机(master)的IP地址和端口,需要注意的是老版本使用的是 slaveof,而本次使用的redis-6.2.14版本要使用 replicaof
replicaof 192.168.192.161 6379
# 当master设置了密码保护时,slave服务连接master的密码
masterauth "redis@123456"
#slave-read-only no
vim /usr/local/redis/conf/sentinel.conf
# 修改Sentinel监听端口
port 26379
# 允许Sentinel后台运行
daemonize yes
# 设置Sentinel日志存放路径
logfile "/usr/local/redis/sentinel_logs/redis-sentinel.log"
pidfile "/usr/local/redis/redis-sentinel.pid"
# 工作目录,需要创建好目录,可自定义
dir "/usr/local/redis/sentinel_data"
sentinel myid 309718c934d48507fc39ba2d394ca9c7585565fd
sentinel deny-scripts-reconfig yes
# redis01:master名称可自定义
# 192.168.192.161 6379 :redis主节点IP和端口
# 2 :表示多少个Sentinel认为redis主节点失效时,才算真正失效
# Sentinel监听redis主节点
sentinel monitor redis01 192.168.192.161 6379 2
# 若sentinel在该配置值内未能完成master/slave自动切换,则认为本次failover失败。
sentinel failover-timeout redis01 15000
# 设置连接master和slave时的密码,注意的是sentinel不能分别为master和slave设置不同的密码,因此master和slave的密码应该设置相同
sentinel auth-pass redis01 redis@123456
# 配置失效时间,master会被这个sentinel主观地认为是不可用的,单位毫秒
sentinel down-after-milliseconds redis01 10000
# 在发生failover主备切换时最多可以有多少个slave同时对新的master进行同步。
sentinel parallel-syncs redis01 2
maxclients 4064
protected-mode no

开启防火墙

#centos/oracle
# redis实例端口
firewall-cmd --add-port=6379/tcp --permanent --zone=public
# 哨兵实例端口
firewall-cmd --add-port=26379/tcp --permanent --zone=public
#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload

# ubuntu/debian
ufw allow 6379/tcp
ufw allow 26379/tcp
ufw reload

启动redis,sentinel

chown redis . /usr/local/redis -R
# 切换redis用户
su redis
# 启动redis服务
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
# 启动哨兵服务
/usr/local/redis/bin/redis-sentinel /usr/local/redis/conf/sentinel.conf

设置自动启动

cat <<EOF | sudo tee /etc/systemd/system/redis.service
[Unit]
Description=redis-server
After=network.target

[Service]
User=redis
#Group=redis
Type=forking
# 这行配置内容要根据redis的安装目录自定义路径
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
cat <<EOF | sudo tee /etc/systemd/system/redis-sentinel.service
[Unit]
Description=redis-sentinel-server
After=network.target

[Service]
User=redis
#Group=redis
Type=forking
# 这行配置内容要根据redis的安装目录自定义路径
ExecStart=/usr/local/redis/bin/redis-sentinel /usr/local/redis/conf/sentinel.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
# 重载系统服务
systemctl daemon-reload
# 实现开机自启
systemctl enable redis
systemctl enable redis-sentinel

重启服务器验证

reboot
#查看端口 
netstat -tnap
#重新连接服务器,查看redis 服务的状态:
systemctl status redis
#查看Redis启动情况
ps aux|grep redis
#重新连接服务器,查看redis 服务的状态:
systemctl status redis-sentinel
#查看Redis启动情况
ps aux|grep redis-sentinel

注意问题

查看cat /usr/local/redis/logs/redis.log 出现:

WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

说明

0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。1,表示内核允许分配所有的物理内存,而不管当前的内存状态如何。2, 表示内核允许分配超过所有物理内存和交换空间总和的内存

解决方式

临时启用内存过度分配

如果你只想立即启用内存过度分配,可以运行以下命令:

sudo sysctl vm.overcommit_memory=1

这会使内核立即启用内存过度分配,但这个更改在系统重启后会失效。如果你希望永久启用这个设置,并且避免每次重启系统后重新设置,请按照以下步骤操作。

永久启用内存过度分配

要永久启用内存过度分配,你需要修改系统的 sysctl 配置文件 /etc/sysctl.conf

步骤 1:编辑 /etc/sysctl.conf

vim /etc/sysctl.conf

步骤 2:添加配置项 在文件的末尾添加以下一行:

vm.overcommit_memory = 1

这将指示内核在每次系统启动时都启用内存过度分配。

步骤 3:使更改立即生效 为了立即应用更改,可以运行以下命令:

sudo sysctl -p

这个命令会重新加载 /etc/sysctl.conf 文件中的所有配置项,使你的更改立即生效。

验证设置 你可以通过以下命令验证是否成功启用了内存过度分配:

sysctl vm.overcommit_memory

如果输出为 vm.overcommit_memory = 1,说明设置已成功应用。