地址和端口规划
应用 | ip | 端口 |
---|---|---|
Redis 主节点 | 172.16.219.138 | 6400 |
Redis 从从节点 | 172.16.219.139 | 6400 |
VIP | 172.16.219.199 | |
sentinel 本地节点 | 172.16.219.138 | 26400 |
sentinel 本地节点 | 172.16.219.139 | 26400 |
sentinel 仲裁节点 | 172.16.219.140 | 26400 |
redis高可用方案:master-slave+sentinel+VIP漂移配置地址和端口规划实现思路:1.VIP配置2.redis安装及配置(138、139、140)3.redis-sentinel配置(138、139、140)4.启动服务查看主从信息及sentinel信息5.测试
实现思路:
1.VIP配置
-
vip实现方案:keepalived
-
172.16.219.138/139都安装keepalived
#更新系统时间 root@localhost#:ntpdate time2.aliyun.com root@localhost#:date #安装keepalived root@localhost#:yum install keepalived #修改配置文件 root@localhost#:vim etc/keepalived/keepalived.conf #需要修改配置的地方 global_defs { notification_email { //定义邮件服务的 root@localhost //定义收件人 } notification_email_from kaadmin@localhost //定义发件人, smtp_server 127.0.0.1 //定义邮件服务器,一定不能使用外部地址 smtp_connect_timeout 30 //超时时间 router_id node1 //此处从节点需要修改 } vrrp_instance VI_1 { //定义虚拟路由,VI_1为虚拟路由的标示符,自己定义名称 state BACKUP interface eth33 //所有的通告等信息都从ens33这个接口出去 virtual_router_id 7 //虚拟路由的ID,不能大于255,且这个ID一定不能有冲突 priority 100 //初始优先级,主100,从98 advert_int 1 //通告的个数 authentication { //认证机制 auth_type PASS //认证类型 auth_pass 691216 //密码,应该为随机的字符串 } virtual_ipaddress { //虚拟地址,即VIP 172.16.219.199 } track_script{ chk_redis } } vrrp_script chk_redis{ script "redis-cli -a 691216 -p 6400 info | grep role:master >/dev/null 2>&1" #间隔 interval 1 #超时 timeout 2 #判定服务异常的检查次数。 fall 2 #判定服务正常的检查次数。 rise 1 #即如果未设置weight时,weight默认值为0,此时当vrrp_script连续检测失败时,vrrp实例进入FAULT状态。会导致VIP转移,原来如此!! #weight 成功或失败权重增加值 } }
配置文件中从节点需要修改的地方:
# 主从不同的字段 主/从 router_id node1/node2 priority 100/98
主从节点启动服务(138、139)
# 启动keepalived服务 root@localhost#:service keepalived start root@localhost#:ip addr show
-
-
VIP指向_____________ 172.16.219.138(master)
|____ 172.16.219.139(slave)
2.redis安装及配置(138、139、140)
-
三台服务均安装redis(138、139、140)
root@localhost#:yum install redis
-
创建redis日志及数据文件存储目录,添加读写权限(138、139、140)
root@localhost#:cd /usr/local/soft mkdir logs mkdir db mkdir sentinel chmod 755 logs/db/sentinel
-
修改配置文件redis.conf(138、139上)
-
root@localhost#:vim /etc/redis.conf ###redis-conf #守护模式 daemonize yes pidfile "/var/run/redis_6400.pid" #端口 port 6400 tcp-backlog 65535 bind 0.0.0.0 notify-keyspace-events "" #保护模式 protected-mode no #密码登录 requirepass "691216" #日志文件路径 logfile "/usr/local/soft/redis/logs/redis_6400.log" #数据文件路径 dir "/usr/local/soft/redis/db" maxmemory 8gb maxmemory-policy allkeys-lru #mater密码 masterauth "691216" timeout 0 tcp-keepalive 0 loglevel notice databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump.rdb" slave-serve-stale-data yes slave-read-only yes repl-disable-tcp-nodelay no #优先级数字小的salve会优先考虑提升为master slave-priority 100 appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 #主从关系,master不设置 slaveof 172.16.219.138 6400
3.redis-sentinel配置(138、139、140)
-
修改配置文件redis-sentinel.conf
root@localhost#:vim /etc/redis-sentinel.conf ###redis-sentinel-conf #后台模式 daemonize yes #保护模式,允许外部访问 protected-mode no #端口 port 26400 #数据目录 dir "/usr/local/soft/redis/redis_sentinels" pidfile "/var/run/redis/sentinel6400.pid" #日志文件 logfile "/usr/local/soft/redis/redis_sentinels/sentinel6400.log" ##监控的 master 的名字叫做 master6400(自定义),地址为172.16.219.138:6400 ##行尾最后的一个 2 代表在 sentinel 集群中,多少个 sentinel 认为 masters 死了,才能真正认为该 ##master 不可用了。 sentinel monitor master6400 172.16.219.139 6400 2 # failover 过期时间,当 failover 开始后,在此时间内仍然没有触发任何 failover 操作, # 当前 sentinel 将会认为此次 failoer 失败。默认180秒,即3分钟。 sentinel failover-timeout master6400 18000 # sentinel 会向 master 发送心跳 PING 来确认 master 是否存活 # 如果 master 在「一定时间范围内」 不回应 PONG 或者是回复了一个错误消息 # 那么这个 sentinel 会认为这个 master 已经不可用了 (subjectively down, 也简称为SDOWN)。 # 而这个 down-after-milliseconds 就是用来指定这个「一定时间范围内」的,单位是毫秒,默认30秒。 sentinel down-after-milliseconds master6400 6000 # sentinel 连接设置了密码的主和从 sentinel auth-pass master6400 691216
4.启动服务查看主从信息及sentinel信息
-
启动redis-server(138,139)
-
root@localhost#: redis-server /etc/redis.conf
-
添加主从信息,初始化主从(139)
root@localhost#: redis-cli -a 691216 -p 6400 -h 172.16.219.139 >mysql: slaveof 172.16.219.138 6400 >mysql: ok >mysql: info Replication 127.0.0.1:6400> info Replication # Replication role:master connected_slaves:1 slave0:ip=172.16.219.139,port=6400,state=online,offset=2332399,lag=0 master_repl_offset:2332544 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1304347 repl_backlog_histlen:1028198 root@localhost#:
-
启动redis-sentinel(138、139、140)
[root@centos138]#: redis-server /etc/redis-sentinel.conf --sentinel [root@centos138 redis]# redis-cli -a 691216 -p 26400 -h 172.16.219.138 172.16.219.138:26400> info Sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=master6400,status=ok,address=172.16.219.139:6400,slaves=1,sentinels=3
-
启动keepalived
-
查看ip信息(我的网卡名称是ens33,根据自己的查询)
[root@centos138 redis]# ip a |grep ens33 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 inet 172.16.219.138/24 brd 172.16.219.255 scope global noprefixroute ens33 inet 172.16.219.199/32 scope global secondary ens33
-
测试主从数据是否同步
[root@centos138 redis]# redis-cli -a 691216 -p 6400 -h 172.16.219.138 172.16.219.138:6400> set myname lilei OK 172.16.219.138:6400>
主库写入,从库读取
[root@centos139 redis]# redis-cli -a 691216 -p 6400 -h 172.16.219.139 172.16.219.139:6400> get myname "lilei" 172.16.219.139:6400> set yourmname hanmeimei (error) READONLY You can't write against a read only slave. 172.16.219.139:6400>
从库读取成功,写入失败(主库正常的时候,从库只有读权限)
5.测试
-
把主库停掉
[root@centos138 redis]# redis-cli -a 691216 -p 6400 -h 172.16.219.138 172.16.219.138:6400> shutdown not connected> quit;
-
查看从库是否提升为主库
-
[root@centos138 redis]# redis-cli -a 691216 -p 6400 -h 172.16.219.139 172.16.219.139:6400> info Replication # Replication role:master connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:265804 repl_backlog_histlen:1048576 #测试数据是否正常 172.16.219.139:6400> get myname "lilei" 172.16.219.139:6400> set yourname hanmeimei OK 172.16.219.139:6400> #可以看到从库slave(139)已经提升为mater角色,已经可以写入数据,且之前的数据并未丢失。
-
查看VIP是否漂移到172.16.219.139(从库所在ip)上
-
[root@centos139 redis]# ip a |grep ens33 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 inet 172.16.219.139/24 brd 172.16.219.255 scope global noprefixroute ens33 inet 172.16.219.199/32 scope global ens33 #此时vip已经指向139服务器(即从库ip)
-
查看Sentinel的监控状态
[root@centos139 redis]# redis-cli -a 691216 -p 26400 -h 172.16.219.139 172.16.219.139:26400> info Sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=master6400,status=ok,address=172.16.219.139:6400,slaves=1,sentinels=3
-
重启138的redis服务后,查看状态
[root@centos138 ~]# redis-server /etc/redis.conf [root@centos138 ~]# redis-cli -a 691216 -p 6400 127.0.0.1:6400> info Replication # Replication,此时恢复138节点的redis,发现变成从节点 ##同时,vip也没有漂移回来,还是指向139(新的master) role:slave master_host:172.16.219.139 master_port:6400 master_link_status:up master_last_io_seconds_ago:2 master_sync_in_progress:0 slave_repl_offset:465 slave_priority:90 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 127.0.0.1:6400> quit [root@centos138 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:50:56:21:00:9c brd ff:ff:ff:ff:ff:ff inet 172.16.219.138/24 brd 172.16.219.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet6 fe80::5eeb:2c7a:7ac5:955d/64 scope link noprefixroute valid_lft forever preferred_lft forever