目录
一、准备
[root@localhost ~] yum install epel-release -y #安装epel源
[root@localhost ~] yum -y install redis #安装redis
[root@localhost ~] systemctl stop firewalld #关闭防火墙
[root@localhost ~] setenforce 0
二、搭建redis高可用集群(同IP)
[root@localhost ~] mkdir /root/redis #创建一个目录用来存放服务器
[root@localhost ~] cd /root/redis/
[root@localhost redis] cp /etc/redis.conf .
[root@localhost redis] cp redis.conf redis_6379.conf #以redis_6379.conf为主服务器
[root@localhost redis] cp redis.conf redis_6380.conf #以redis_6380.conf为slave1
[root@localhost redis] cp redis.conf redis_6381.conf #以redis_6381.conf为slave2
[root@localhost redis] vim redis_6379.conf #修改配置文件
port 6379
pidfile /var/run/redis_6379.pid
daemonize yes
[root@localhost redis] vim redis_6380.conf
port 6380
pidfile /var/run/redis_6380.pid #监听主服务器端口
daemonize yes
slaveof 127.0.0.1 6379
[root@localhost redis] vim redis_6381.conf
port 6381
pidfile /var/run/redis_6381.pid #监听主服务器端口
daemonize yes
slaveof 127.0.0.1 6379
[root@localhost redis] redis-server redis_6379.conf #启动服务
[root@localhost redis] redis-server redis_6381.conf
[root@localhost redis] redis-server redis_6380.conf
[root@localhost redis] ps -ef|grep redis
root 8849 1 0 19:19 ? 00:00:00 redis-server 127.0.0.1:6379
root 8857 1 0 19:20 ? 00:00:00 redis-server 127.0.0.1:6380
root 8864 1 0 19:20 ? 00:00:00 redis-server 127.0.0.1:6381
root 8881 8528 0 19:26 pts/0 00:00:00 grep --color=auto redis
[root@localhost redis]# redis-cli -p 6379 #进入redis测试
127.0.0.1:6379> role
1) "master"
2) (integer) 4355
3) 1) 1) "127.0.0.1"
2) "6380"
3) "4355"
2) 1) "127.0.0.1"
2) "6381"
3) "4355"
127.0.0.1:6379> set name skl
OK
127.0.0.1:6379> exit
[root@localhost redis]# redis-cli -p 6380
127.0.0.1:6380> get name
"skl"
127.0.0.1:6380> role
1) "slave"
2) "127.0.0.1"
3) (integer) 6379
4) "connected"
5) (integer) 4452
127.0.0.1:6380> exit
[root@localhost redis]# redis-cli -p 6381 #三台redis主从搭建完成
127.0.0.1:6381> get name
"skl"
搭建redis哨兵
[root@localhost redis] cp /etc/redis-sentinel.conf .
[root@localhost redis] cp redis-sentinel.conf redis-sentinel_26379.conf
[root@localhost redis] cp redis-sentinel.conf redis-sentinel_26380.conf
[root@localhost redis] cp redis-sentinel.conf redis-sentinel_26381.conf
[root@localhost redis] vim redis-sentinel_26379.conf
daemonize yes
protected-mode no
[root@localhost redis] ps -ef|grep redis
root 8967 1 0 20:28 ? 00:00:00 redis-sentinel *:26379 [sentinel]
[root@localhost redis] vim redis-sentinel_26380.conf
port 26380
daemonize yes
protected-mode no
[root@localhost redis] vim redis-sentinel_26381.conf
port 26381
daemonize yes
protected-mode no
[root@localhost redis] redis-sentinel redis-sentinel_26380.conf
[root@localhost redis] redis-sentinel redis-sentinel_26381.conf
[root@localhost redis] ps -ef|grep redis
root 8849 1 0 19:19 ? 00:00:03 redis-server 127.0.0.1:6379
root 8857 1 0 19:20 ? 00:00:03 redis-server 127.0.0.1:6380
root 8864 1 0 19:20 ? 00:00:03 redis-server 127.0.0.1:6381
root 8967 1 0 20:28 ? 00:00:00 redis-sentinel *:26379 [sentinel]
root 8976 1 0 20:32 ? 00:00:00 redis-sentinel *:26380 [sentinel]
root 8983 1 0 20:33 ? 00:00:00 redis-sentinel *:26381 [sentinel]
root 8987 8528 0 20:33 pts/0 00:00:00 grep --color=auto redis
#都成功启动
[root@localhost redis] redis-cli -p 26379
127.0.0.1:26379> 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=127.0.0.1:6379,slaves=2,sentinels=3
#哨兵监听成功
测试
[root@localhost redis] redis-cli -p 6379
127.0.0.1:6379> shutdown #关闭master(6379)服务器
not connected>
[root@localhost redis] !ps
ps -ef|grep redis
root 8857 1 0 19:20 ? 00:00:03 redis-server 127.0.0.1:6380
root 8864 1 0 19:20 ? 00:00:03 redis-server 127.0.0.1:6381
root 8967 1 0 20:28 ? 00:00:01 redis-sentinel *:26379 [sentinel]
root 8976 1 0 20:32 ? 00:00:00 redis-sentinel *:26380 [sentinel]
root 8983 1 0 20:33 ? 00:00:00 redis-sentinel *:26381 [sentinel]
root 8994 8528 0 20:40 pts/0 00:00:00 grep --color=auto redis
[root@localhost redis] redis-cli -p 6380
127.0.0.1:6380> role
1) "master" #6380变成了master
2) (integer) 3230
3) 1) 1) "127.0.0.1"
2) "6381"
3) "3230"
127.0.0.1:6380> exit
三、不同IP地址搭建redis高可用哨兵集群
主master和从slave1、slave2 下载redis并更改etc/redis.conf文件
[root@localhost ~] yum -y install redis #3台服务器下载redis
[root@localhost ~] vim /etc/redis.conf #3台服务器配置文件修改如下
bind 0.0.0.0
daemonize yes
[root@localhost ~] systemctl start redis #启动服务
[root@localhost ~] ps -ef|grep redis #成功启动
redis 1615 1 0 08:42 ? 00:00:00 /usr/bin/redis-server 0.0.0.0:6379
root 1621 1485 0 08:50 pts/0 00:00:00 grep --color=auto redis
slave1 slave2 添加slaveof 192.168.1.12 6379 变成从服务器
[root@localhost ~] vim /etc/redis.conf
slaveof 192.168.1.12 6379
修改哨兵配置文件
[root@localhost ~] vim /etc/redis-sentinel.conf
sentinel monitor mymaster 192.169.1.12 6379 2 #都监听主master IP
daemonize yes
[root@localhost ~] systemctl start redis-sentinel.service
测试主从
[root@localhost ~] redis-cli -p 6379
127.0.0.1:6379> set name master1 #主master创建key
OK
127.0.0.1:6379> get name #从1从2服务器 同步复制
"master1"
127.0.0.1:6379> get name
"master1"
测试哨兵集群
关闭master服务器redis服务器
slave 2创建key 成功(变成了master)
slave 1 创建key 失败 ,可以复制 slave 2 日志, 哨兵已经自动切换master服务器