Redis主从部署及sentinel配置详细教程
Posted 江湖有缘
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis主从部署及sentinel配置详细教程相关的知识,希望对你有一定的参考价值。
Redis主从部署及sentinel配置详细教程
- 一、环境介绍
- 二、安装redis
- 三、redis初始化环境配置
- 四、启动redis
- 五、将redis作为服务运行
- 六、redis-sentinel介绍
- 七、配置redis主从
- 八、master节点配置redis-sentinel
- 九、从节点启动sentinel
- 十、所有节点修改redis配置文件
- 十一、测试master节点故障
一、环境介绍
1.三个redis节点的IP规划
node-1 主redis 192.168.3.71
node-2 从redis 192.168.3.72
node-3 从redis 192.168.3.73
2.节点的系统版本
[root@node03 ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
二、安装redis
1.下载Redis源码包
[root@node01 soft]# wget https://download.redis.io/releases/redis-6.2.6.tar.gz
--2022-06-01 18:23:58-- https://download.redis.io/releases/redis-6.2.6.tar.gz
Resolving download.redis.io (download.redis.io)... 45.60.125.1
Connecting to download.redis.io (download.redis.io)|45.60.125.1|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2476542 (2.4M) [application/octet-stream]
Saving to: ‘redis-6.2.6.tar.gz’
100%[================================================================================================================================================>] 2,476,542 5.31MB/s in 0.4s
2022-06-01 18:23:59 (5.31 MB/s) - ‘redis-6.2.6.tar.gz’ saved [2476542/2476542]
[root@node01 soft]# ll
total 2420
-rw-r--r-- 1 root root 2476542 Oct 4 2021 redis-6.2.6.tar.gz
2.解压软件包
[root@node01 soft]# tar -xzf redis-6.2.6.tar.gz
[root@node01 soft]# ls
redis-6.2.6 redis-6.2.6.tar.gz
3.安装gcc相关模块
yum -y install gcc automake autoconf libtool make
4.编译软件目录deps/下内容
make lua hiredis linenoise hdr_histogram jemalloc
5.编译redis
make PREFIX=/usr/local/redis MALLOC=libc install
三、redis初始化环境配置
1.配置redis环境变量
[root@node01 redis-6.2.6]# cat /etc/profile.d/redis.sh
export PATH=$PATH:/usr/local/redis/bin
[root@node01 redis-6.2.6]# source !$
source /etc/profile.d/redis.sh
2.检查redis的安装版本
[root@node01 redis-6.2.6]# redis-server --version
Redis server v=6.2.6 sha=00000000:0 malloc=libc bits=64 build=220bd54e8ce44ec8
3.创建redis相关目录
mkdir -p /usr/local/redis/etc,logs,data
4.编辑redis配置文件
[root@node01 redis-6.2.6]# cat /usr/local/redis/etc/redis.conf
daemonize yes
supervised systemd
pidfile /var/run/redis.pid
port 6379
logfile /usr/local/redis/logs/redis.log
dbfilename dump.rdb
dir /usr/local/redis/data
maxmemory 1G
bind 192.168.3.71 127.0.0.1
timeout 300
loglevel notice
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
maxclients 10000
appendonly yes
appendfilename appendonly.aof
appendfsync everysec
四、启动redis
1.启动redis
[root@node01 redis-6.2.6]# redis-server /usr/local/redis/etc/redis.conf
2.检查redis状态
[root@node01 redis-6.2.6]# ps -ef |grep redis
root 20551 1 0 18:52 ? 00:00:00 redis-server 192.168.3.71:6379
root 20694 9002 0 18:53 pts/0 00:00:00 grep --color=auto redis
3.客户端连接
[root@node01 redis-6.2.6]# redis-cli -h 192.168.3.71 -p 6379
192.168.3.71:6379> info
# Server
redis_version:6.2.6
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:220bd54e8ce44ec8
redis_mode:standalone
os:Linux 3.10.0-957.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:20551
process_supervised:no
run_id:1e6d5087e0b847c6a9cd9b77a6dd4acc2aa3c68b
tcp_port:6379
server_time_usec:1654081019193038
uptime_in_seconds:277
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:9913851
executable:/redis/soft/redis-6.2.6/redis-server
config_file:/usr/local/redis/etc/redis.conf
io_threads_active:0
五、将redis作为服务运行
1.编辑服务
vi /usr/lib/systemd/system/redis.service
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
~
2.启动redis服务
systemctl start redis
3.查看redis运行状态
[root@node01 ~]# systemctl status redis
● redis.service - redis-server
Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2022-06-01 19:04:57 CST; 2min 16s ago
Process: 7706 ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf (code=exited, status=0/SUCCESS)
Main PID: 7736 (redis-server)
Tasks: 4
CGroup: /system.slice/redis.service
└─7736 /usr/local/redis/bin/redis-server 192.168.3.71:6379
Jun 01 19:04:57 node01 systemd[1]: Starting redis-server...
Jun 01 19:04:57 node01 systemd[1]: Started redis-server.
4.将node1、node2、node3三个节点全部安装redis
安装步骤如上一样!
六、redis-sentinel介绍
redis-sentinel:Sentinel(哨兵)是用于监控redis集群中Master状态的工具,其已经被集成在redis2.4+的版本中
七、配置redis主从
1.两个从节点修改配置文件
[root@node02 ~]# cat /usr/local/redis/etc/redis.conf
daemonize yes
supervised systemd
pidfile /var/run/redis.pid
port 6379
logfile /usr/local/redis/logs/redis.log
dbfilename dump.rdb
dir /usr/local/redis/data
maxmemory 1G
bind 192.168.3.72 127.0.0.1
slaveof 192.168.3.71 6379
slave-read-only yes
timeout 300
loglevel notice
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
maxclients 10000
appendonly yes
appendfilename appendonly.aof
appendfsync everysec
2.重启redis服务
systemctl restart redis.service
3.master节点检查redis状态
[root@node01 ~]# redis-cli -h 192.168.3.71 -p 6379
192.168.3.71:6379> info
# Server
redis_version:6.2.6
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:220bd54e8ce44ec8
redis_mode:standalone
os:Linux 3.10.0-957.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:20437
process_supervised:no
run_id:7d8b9feabe6a279c507b1571b62aeddfc19366e6
tcp_port:6379
server_time_usec:1654085058098552
uptime_in_seconds:5
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:9917890
executable:/usr/local/redis/bin/redis-server
config_file:/usr/local/redis/etc/redis.conf
io_threads_active:0
# Clients
connected_clients:1
cluster_connections:0
maxclients:10000
client_recent_max_input_buffer:48
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
clients_in_timeout_table:0
# Memory
used_memory:2023976
used_memory_human:1.93M
used_memory_rss:2465792
used_memory_rss_human:2.35M
used_memory_peak:2078792
used_memory_peak_human:1.98M
used_memory_peak_perc:97.36%
used_memory_overhead:1987904
used_memory_startup:884128
used_memory_dataset:36072
used_memory_dataset_perc:3.16%
allocator_allocated:1989560
allocator_active:2427904
allocator_resident:2427904
total_system_memory:3933761536
total_system_memory_human:3.66G
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:1000000000
maxmemory_human:953.67M
maxmemory_policy:noeviction
allocator_frag_ratio:1.22
allocator_frag_bytes:438344
allocator_rss_ratio:1.00
allocator_rss_bytes:0
rss_overhead_ratio:1.02
rss_overhead_bytes:37888
mem_fragmentation_ratio:1.24
mem_fragmentation_bytes:476232
mem_not_counted_for_evict:20
mem_replication_backlog:1052656
mem_clients_slaves:34064
mem_clients_normal:17032
mem_aof_buffer:24
mem_allocator:libc
active_defrag_running:0
lazyfree_pending_objects:0
lazyfreed_objects:0
# Persistence
loading:0
current_cow_size:0
current_cow_size_age:0
current_fork_perc:0.00
current_save_keys_processed:0
current_save_keys_total:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1654085053
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:192512
aof_enabled:1
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0
module_fork_in_progress:0
module_fork_last_cow_size:0
aof_current_size:0
aof_base_size:0
aof_pending_rewrite:0
aof_buffer_length:0
aof_rewrite_buffer_length:0
aof_pending_bio_fsync:0
aof_delayed_fsync:0
# Stats
total_connections_received:3
total_commands_processed:17
instantaneous_ops_per_sec:2
total_net_input_bytes:689
total_net_output_bytes:20720
instantaneous_input_kbps:0.08
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:2
sync_partial_ok:0
sync_partial_err:2
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
expire_cycle_cpu_milliseconds:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:109
total_forks:2
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
tracking_total_keys:0
tracking_total_items:0
tracking_total_prefixes:0
unexpected_error_replies:0
total_error_replies:0
dump_payload_sanitizations:0
total_reads_processed:18
total_writes_processed:9
io_threaded_reads_processed:0
io_threaded_writes_processed:0
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.3.73,port=6379,state=online,offset=0,lag=1
slave1:ip=192.168.3.72,port=6379,state=online,offset=0,lag=1
master_failover_state:no-failover
master_replid:a46f10699f2ca9171d53e490ae150b9c9c2a929d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:0
八、master节点配置redis-sentinel
1.编辑sentinel文件
[root@node01 ~]# cat /usr/local/redis/etc/redis_sentinel.conf
port 26379
logfile /usr/local/redis/logs/redis-sentinel.log
daemonize yes
dir /tmp
sentinel monitor mymaster 192.168.3.71 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 30000
sentinel client-reconfig-script mymaster /usr/local/redis/bin/redis_failover.sh
2.编辑VIP切换脚本
[root@node01 ~]# cat /usr/local/redis/bin/redis_failover.sh
#!/bin/sh
_DEBUG="on"
DEBUGFILE=/usr/local/redis/logs/sentinel_failover.log
VIP='192.168.3.100'
MASTERIP=$6
MASK='24'
IFACE='eth0'
MYIP=$(ip -4 -o addr show dev $IFACE| grep -v secondary| awk 'split($4,a,"/");print a[1]')
DEBUG ()
if [ "$_DEBUG" = "on" ]; then
$@
fi
rm -rf $DEBUGFILE
set -e
DEBUG date >> $DEBUGFILE
DEBUG echo $@ >> $DEBUGFILE
DEBUG echo "Master: $MASTERIP My IP: $MYIP" >> $DEBUGFILE
if [ $MASTERIP == $MYIP ]; then
if [ $(ip addr show $IFACE | grep $VIP | wc -l) -eq 0 ]; then
ip addr add $VIP/$MASK dev $IFACE
DEBUG echo "ip addr add $VIP/$MASK dev $IFACE" >> $DEBUGFILE
arping -q -c 3 -A $VIP -I $INTERFACE
fi
exit 0
else
if [ $(ip addr show $IFACE | grep $VIP | wc -l) -ne 0 ]; then
ip addr del $VIP/$MASK dev $IFACE
DEBUG echo "ip addr del $VIP/$MASK dev $IFACE" >> $DEBUGFILE
fi
exit 0
fi
exit 1
3.给VIP切换脚本执行权限
[root@node01 ~]# chmod +x /usr/local/redis/bin/redis_failover.sh
4.启动redis-sentinel
redis-sentinel /usr/local/redis/etc/redis_sentinel.conf
5.检查sentinel启动状态
[root@node01 ~]# ps -ef |grep sentinel
root 48708 1 0 22:19 ? 00:00:00 redis-sentinel *:26379 [sentinel]
root 48784 9195 0 22:20 pts/0 00:00:以上是关于Redis主从部署及sentinel配置详细教程的主要内容,如果未能解决你的问题,请参考以下文章
Redis哨兵模式(sentinel)学习总结及部署记录(主从复制读写分离主从切换)
Redis哨兵模式(sentinel)学习总结及部署记录(主从复制读写分离主从切换)