- 安装Redis
cd /bea
tar -zxvf redis-5.0.8.tar.gz
cd redis-5.0.8
make all (编译)
如果没有make命令
yum -y install gcc automake autoconf libtool make
yum -y install gcc gcc-c++
cd /bea/redis-5.0.8
mkdir install
cd /bea/redis-5.0.8/src
make install PREFIX=/bea/redis-5.0.8/install (安装) 会安装到install的bin目录下、指定目录失败文件会在/uer/local/bin目录下 CP到install目录下
cd /bea/redis-5.0.8/
cp redis.conf /bea/redis-5.0.8/install/bin
cd /bea/redis-5.0.8/install/bin
./redis-server ./redis.conf (启动redis)
ps -ef |grep redis
(后台启动)
cd /bea/redis-5.0.8/install/bin/
nohup ./redis-server ./redis.conf >redisout.log 2>&1 &
- 哨兵(sentinel)+keepalived
三台设备,10.97.141.43、10.97.141.44和10.97.141.45
- master:10.97.141.45(rsgyredisjq03)
- slave:10.97.141.43/10.97.141.44(rsgyredisjq01/rsgyredisjq02)
- vip:10.97.141.52
1、master 10.97.141.45:redis.conf配置
[root@rsgyredisjq03 bin]# grep -E -v "^$|^#" redis.conf
bind 0.0.0.0(只需改这个)
protected-mode no(只需改这个)
masterauth "ylzinfo123qazxsw"(只需加这个)
requirepass "ylzinfo123qazxsw"(只需加这个)
启动命令:./redis-server ./redis.conf >out6379.log &
后台启动:nohup ./redis-server ./redis.conf > out6379.log 2>&1 &
2、master 10.97.141.45:sentinel.conf配置
[root@rsgyredisjq03 bin]# grep -E -v "^$|^#" sentinel.conf
protected-mode no(只需改这个)
sentinel monitor rsgyredisjq03 10.97.141.45 6379 1 (只需改这个) # 指定主机IP地址和端口,并且指定当有1台哨兵认为主机挂了,则对主机进行容灾切换。
sentinel auth-pass rsgyredisjq03 ylzinfo123qazxsw (只需改这个) #在redis.conf上配置了密码,则这也需要设置同样的密码 master主密码,不设置的话不能动态切换
# 在末行模式下 输入 :%s/mymaster/ rsgyredisjq03/g (只需改这个)
启动命令:./redis-sentinel ./sentinel.conf >outsen6379.log &
nohup ./redis-sentinel ./sentinel.conf >outsen6379.log 2>&1 &
3、slave 10.97.141.43:redis.conf配置
[root@rsgyredisjq01 bin]# grep -E -v "^$|^#" redis.conf
bind 0.0.0.0(只需改这个)
protected-mode no(只需改这个)
masterauth "ylzinfo123qazxsw" (只需加这个)
requirepass "ylzinfo123qazxsw"(只需加这个)
replicaof 10.97.141.45 6379 (只需加这个)
启动命令:./redis-server ./redis.conf >out6379.log &
nohup ./redis-server ./redis.conf > out6379.log 2>&1 &
4、slave 10.97.141.43:sentinel.conf配置
[root@rsgyredisjq01 bin]# grep -E -v "^$|^#" sentinel.conf
protected-mode no (只需改这个)
sentinel monitor rsgyredisjq03 10.97.141.45 6379 1 (只需改这个) # # 指定主机IP地址和端口,并且指定当有1台哨兵认为主机挂了,则对主机进行容灾切换。
sentinel auth-pass rsgyredisjq03 ylzinfo123qazxsw (只需改这个)#在redis.conf上配置了密码,则这也需要设置同样的密码。master主密码,不设置的话不能动态切换
slave的sentinel.conf 的修改monitor mymaster配置主服务IP信息等!
# 在末行模式下 输入 :%s/mymaster/rsgyredisjq03/g (只需改这个)
启动命令:./redis-sentinel ./sentinel.conf >outsen6379.log &
nohup ./redis-sentinel ./sentinel.conf >outsen6379.log 2>&1 &
——————10.97.141.44配置基本一样不做赘述
5、查看登陆redis命令
[root@rsgyredisjq03 bin]# /bea/redis-5.0.8/install/bin/redis-cli -c -h 10.97.141.45 -p 6379 -a ylzinfo123qazxsw 2>/dev/null
10.97.141.45:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.97.141.44,port=6379,state=online,offset=15202390,lag=0
slave1:ip=10.97.141.43,port=6379,state=online,offset=15202390,lag=0
master_replid:2e192a578eac0520b834ce5735de2e2548cc6fae
master_replid2:20b02a872ddff194b2566928b58186502d7eec80
master_repl_offset:15202390
second_repl_offset:205641
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:14153815
repl_backlog_histlen:1048576
6、检测redis
- 搭建完成后,需要master中set写入测试数据验证slave上能不能get获取到
- 10.97.141.45:6379> set test1 123
- 10.97.141.43:6379> get test1
"123"
- 把master 所在的redis-server ,Kill 掉,查看是否其中一台slave变为master(原来的Master在重新启动后,会自动直接变为新master的slave,不会出现双Master的情况)
- [root@rsgyredisjq03 bin]# ps -ef |grep redis
- kill -9 程序号
- 10.97.141.43:6379> info replication
- role: master
7、keepalived的配置与redis_check.sh的编写
三台服务器都安装:yum -y install keepalived
- master 10.97.141.45:keepalived.conf配置
[root@rsgyredisjq03 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id master-node
# vrrp_skip_check_adv_addr
# vrrp_strict
# vrrp_garp_interval 0
# vrrp_gna_interval 0
}
vrrp_script chk_http_port {
script "/etc/keepalived/redis_check.sh" #只有在检测失败(即exit为非0)时执行以下条件,权重减少,意思是如果在这个脚本执行的结果为exit为非0,会在vrrp实例定义的优先级减去下面的weight值,就表示期望这个节点为备用状态。
interval 20 #检测间隔时间
weight -5 #权重减少
fall 1
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface ens192 #虚拟IP所在网(在使用的网卡)
mcast_src_ip 10.97.141.45
virtual_router_id 51
priority 101 #优先级,MASTER一般比默认值大
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.97.141.52 #VIP地址,需要是没在用的
}
track_script {
chk_http_port
}
}
- master 10.97.141.45:redis_check.sh配置
[root@rsgyredisjq03 keepalived]# cat redis_check.sh
#!/bin/bash
ps -ef |grep -E -i -w "6379" | grep -v "grep"
if [ "$?" == "0" ]
then
sa=`echo "info replication" | /bea/redis-5.0.8/install/bin/redis-cli -c -h 10.97.141.45 -p 6379 -a ylzinfo123qazxsw 2>/dev/null | grep -E -i "role" | cut -f 1 | cut -d ":" -f 2`
sb=${#sa}
sh=$(($sb-1))
if [ "$sh" -eq 5 ]
then
exit 1
# touch /etc/keepalived/down
else
# rm -rf /etc/keepalived/down
exit 0
fi
else
sleep 10s
nohup /bea/redis-5.0.8/install/bin/redis-server /bea/redis-5.0.8/install/bin/redis.conf >/bea/redis-5.0.8/install/bin/out6379.log &
# rm -rf /etc/keepalived/down
exit 1
fi
#exit
- slave 10.97.141.43:keepalived.conf配置
[root@rsgyredisjq01 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id backup-node
# vrrp_skip_check_adv_addr
# vrrp_strict
# vrrp_garp_interval 0
# vrrp_gna_interval 0
}
vrrp_script chk_http_port {
script "/etc/keepalived/redis_check.sh"
interval 20
weight -5
fall 1
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
mcast_src_ip 10.97.141.43
virtual_router_id 51
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.97.141.52
}
track_script {
chk_http_port
}
}
- slave 10.97.141.43:redis_check.sh配置
[root@rsgyredisjq01 keepalived]# cat redis_check.sh
#!/bin/bash
ps -ef |grep -E -i -w "6379" | grep -v "grep"
if [ "$?" == "0" ]
then
sa=`echo "info replication" | /bea/redis-5.0.8/install/bin/redis-cli -c -h 10.97.141.43 -p 6379 -a ylzinfo123qazxsw 2>/dev/null | grep -E -i "role" | cut -f 1 | cut -d ":" -f 2`
sb=${#sa}
sh=$(($sb-1))
if [ "$sh" -eq 5 ]
then
exit 1
# touch /etc/keepalived/down
else
# rm -rf /etc/keepalived/down
exit 0
fi
else
sleep 10s
nohup /bea/redis-5.0.8/install/bin/redis-server /bea/redis-5.0.8/install/bin/redis.conf >/bea/redis-5.0.8/install/bin/out6379.log &
# rm -rf /etc/keepalived/down
exit 1
fi
#exit
8、keepalived的配置验证
- 看keepalived 运行状态:tail -f / var/log/messages
- 当master节点关闭,slaver的ip addr 看状态是否切换为虚拟IP
- ps -ef | grep redis
- kill -9 程序号