1 主 2 从 3 哨兵
环境说明
操作系统版本 | Redhat 7.6 |
数据库版本 | redis-6.2.11.tar.gz |
服务IP | 角色 | 备注 |
10.100.2.31 | Master | 主 |
10.100.2.57 | Slave | 从 |
10.100.2.250 | Slave | 从 |
Sentinel 有三个定时监控任务:
每隔 10 秒向主节点和从节点发送 info 命令获取最新的拓扑。
每隔 2 秒,每个 sentinel 节点会向数据节点的sentinel:hello频道发送该 sentinel 节点对于主节点的判断以及当前 sentinel 节点信息,同时每个 sentinel 节点也会订阅该频道,来了解其他 sentinel 节点以及他们对主节点的判断。
每隔 1 秒,每个 sentinel 节点会向主节点、从节点、其他 sentinel 节点发送一条PING命令做一次心跳检测,判断节点是否存活。
部署说明
01.创建用户
groupadd -g 601 redis
useradd -u 6001 -g 601 redis
echo "redis" |passwd --stdin redis
echo "redis hard nofile 10240" >>/etc/security/limits.conf
echo "redis soft nofile 10240" >>/etc/security/limits.conf
echo "redis hard nproc 8192" >>/etc/security/limits.conf
echo "redis soft nproc 8192" >>/etc/security/limits.conf
02.创建目录
mkdir -p /data/redis
chown -R redis:redis /data/redis
mkdir -p /data/redis/log
mkdir -p /data/redis/conf
mkdir -p /data/redis/data
chown -R redis:redis /data/redis
mkdir /soft
chmod 777 -R /soft
03.安装redis
su - redis
cd /soft
tar zxvf redis-6.2.11.tar.gz
cd redis-6.2.11
make
cd src
make PREFIX=/data/redis install
04.配置环境变量
cp ../redis.conf /data/redis/conf
vi ~/.bash_profile
PATH=$PATH:$HOME/.local/bin:/data/redis/bin:$HOME/bin
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
echo "never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local
echo "net.core.somaxconn = 2048" >> /etc/sysctl.conf
sysctl -p
vm.overcommit_memory
默认值为:0
从内核文档里得知,该参数有三个值,分别是:
0:当用户空间请求更多的的内存时,内核尝试估算出剩余可用的内存。
1:当设这个参数值为1时,内核允许超量使用内存直到用完为止,主要用于科学计算
2:当设这个参数值为2时,内核会使用一个决不过量使用内存的算法,即系统整个内存地址空间不能超过swap+50%的RAM值,50%参数的设定是在overcommit_ratio中设定。
vm.overcommit_ratio
默认值为:50
这个参数值只有在vm.overcommit_memory=2的情况下,这个参数才会生效。
05.Redis 主从配置
主库:
su - redis
vi /data/redis/conf/redis.conf
daemonize yes
logfile "/data/redis/log/redis.log"
dir /data/redis/data
requirepass itpux
masterauth itpux
timeout 300
#bind 127.0.0.1
bind 10.100.2.31
从库 52:
su - redis
vi /data/redis/conf/redis.conf
daemonize yes
logfile "/data/redis/log/redis.log"
dir /data/redis/data
requirepass itpux
masterauth itpux
timeout 300
#bind 127.0.0.1
bind 10.100.2.57
slaveof 10.100.2.31 6379
# 设置当本机为 slav 服务时,设置 master 服务的 IP 地址及端口,在 Redis 启动时,它会自动从 master 进行数据同步
从库 53:
su - redis
vi /data/redis/conf/redis.conf
daemonize yes
logfile "/data/redis/log/redis.log"
dir /data/redis/data
requirepass itpux
masterauth itpux
timeout 300
#bind 127.0.0.1
bind 10.100.2.250
slaveof 10.100.2.31 6379
su - redis
redis-server /data/redis/conf/redis.conf
Redis 服务端的默认连接端口是 6379
[redis@redis1 ~]$ netstat -an |grep 6379
tcp 0 0 10.100.2.31:6379 0.0.0.0:* LISTEN
[redis@redis2 ~]$ netstat -an |grep 6379
tcp 0 0 10.100.2.57:6379 0.0.0.0:* LISTEN
[redis@redis3 ~]$ netstat -an |grep 6379
tcp 0 0 10.100.2.250:6379 0.0.0.0:* LISTEN
redis-cli -h 10.100.2.31 -p 6379 -a itpux
# Replication
role:master
connected_slaves:2
slave0:ip=10.100.2.57,port=6379,state=online,offset=546,lag=0
slave1:ip=10.100.2.250,port=6379,state=online,offset=546,lag=1
master_failover_state:no-failover
master_replid:7370ab38cb235ffcaaf21429bb28b9b6bd251da7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:546
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:546
06.Redis + 哨兵(主从切换)
10.100.2.31:
su - redis
cp /soft/redis-6.2.11/sentinel.conf /data/redis/conf/
echo "" > /data/redis/conf/sentinel.conf
vi /data/redis/conf/sentinel.conf
##sentinel 实例之间的通讯端口
sentinel monitor mymaster 10.100.2.31 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 15000
sentinel auth-pass mymaster itpux
bind 10.100.2.31
port 26379
daemonize yes
logfile /data/redis/log/sentinel.log
dir /data/redis/data
启动哨兵:
redis-sentinel /data/redis/conf/sentinel.conf
ps -ef|grep redis
[redis@pg1 soft]$ ps -ef|grep redis
root 9169 8105 0 10:55 pts/0 00:00:00 su - redis
redis 9170 9169 0 10:55 pts/0 00:00:00 -bash
redis 9216 1 0 11:03 ? 00:00:00 redis-server 10.100.2.31:6379
redis 9240 1 0 11:13 ? 00:00:00 redis-sentinel 10.100.2.31:26379 [sentinel]
redis 9244 9170 0 11:13 pts/0 00:00:00 ps -ef
redis 9245 9170 0 11:13 pts/0 00:00:00 grep --color=auto redis
10.100.2.57:
su - redis
echo "" > /data/redis/conf/sentinel.conf
vi /data/redis/conf/sentinel.conf
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 10.100.2.31 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 15000
sentinel auth-pass mymaster itpux
bind 10.100.2.57
port 26379
daemonize yes
logfile /data/redis/log/sentinel.log
dir /data/redis/data
启动哨兵:
redis-sentinel /data/redis/conf/sentinel.conf
ps -ef|grep redis
[redis@pg2 ~]$ ps -ef|grep redis
root 12532 8068 0 11:02 pts/0 00:00:00 su - redis
redis 12533 12532 0 11:02 pts/0 00:00:00 -bash
redis 12555 1 0 11:03 ? 00:00:00 redis-server 10.100.2.57:6379
redis 12571 1 0 11:15 ? 00:00:00 redis-sentinel 10.100.2.57:26379 [sentinel]
redis 12576 12533 0 11:15 pts/0 00:00:00 ps -ef
redis 12577 12533 0 11:15 pts/0 00:00:00 grep --color=auto redis
10.100.2.250:
su - redis
echo "" > /data/redis/conf/sentinel.conf
vi /data/redis/conf/sentinel.conf
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 10.100.2.31 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 15000
sentinel auth-pass mymaster itpux
bind 10.100.2.250
port 26379
daemonize yes
logfile "/data/redis/log/sentinel.log"
dir "/data/redis/data"
启动哨兵:
redis-sentinel /data/redis/conf/sentinel.conf
ps -ef|grep redis
[redis@pg3 ~]$ ps -ef|grep redis
root 12546 8086 0 11:02 pts/0 00:00:00 su - redis
redis 12547 12546 0 11:02 pts/0 00:00:00 -bash
redis 12575 1 0 11:04 ? 00:00:00 redis-server 10.100.2.250:6379
redis 12586 1 0 11:15 ? 00:00:00 redis-sentinel 10.100.2.250:26379 [sentinel]
redis 12591 12547 0 11:15 pts/0 00:00:00 ps -ef
redis 12592 12547 0 11:15 pts/0 00:00:00 grep --color=auto redis
redis-cli -p 26379 -h 10.100.2.31 info
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.100.2.31:6379,slaves=2,sentinels=3
07.故障转移测试
关闭主库:
redis-cli -p 6379 -h 10.100.2.31 -a itpux
10.100.2.31:6379> shutdown
[redis@pg1 soft]$ ps -ef | grep redis
root 9169 8105 0 10:55 pts/0 00:00:00 su - redis
redis 9170 9169 0 10:55 pts/0 00:00:00 -bash
redis 9268 1 0 11:27 ? 00:00:00 redis-sentinel 10.100.2.31:26379 [sentinel]
redis 9275 9170 0 11:27 pts/0 00:00:00 ps -ef
redis 9276 9170 0 11:27 pts/0 00:00:00 grep --color=auto redis
查看其它库:
redis-cli -p 6379 -h 10.100.2.57 -a itpux
10.100.2.57:6379> info
# Replication
role:slave
master_host:10.100.2.250
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:162438
slave_repl_offset:162438
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:e17e893de68f1cda3dc9964f96ec068a28765cfe
master_replid2:7370ab38cb235ffcaaf21429bb28b9b6bd251da7
master_repl_offset:162438
second_repl_offset:151834
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:162438
redis-cli -p 6379 -h 10.100.2.250 -a itpux
10.100.2.250:6379> info
# Replication
role:master
connected_slaves:1
slave0:ip=10.100.2.57,port=6379,state=online,offset=173713,lag=1
master_failover_state:no-failover
master_replid:e17e893de68f1cda3dc9964f96ec068a28765cfe
master_replid2:7370ab38cb235ffcaaf21429bb28b9b6bd251da7
master_repl_offset:174128
second_repl_offset:151834
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:71
repl_backlog_histlen:174058
10.100.2.250 被提升为主库