Redis安装+ 哨兵(sentinel)+keepalived + vip漂移搭建高可用集群实战

  • 安装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

  1. master:10.97.141.45(rsgyredisjq03)
  2. slave:10.97.141.43/10.97.141.44(rsgyredisjq01/rsgyredisjq02)
  3. 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

  1. 搭建完成后,需要master中set写入测试数据验证slave上能不能get获取到
    1. 10.97.141.45:6379> set test1 123
    2. 10.97.141.43:6379> get test1

"123"

  1. 把master 所在的redis-server ,Kill 掉,查看是否其中一台slave变为master(原来的Master在重新启动后,会自动直接变为新master的slave,不会出现双Master的情况)
    1. [root@rsgyredisjq03 bin]# ps -ef |grep redis
    2. kill -9 程序号
    3. 10.97.141.43:6379> info replication
      1. role: master

7、keepalived的配置与redis_check.sh的编写

三台服务器都安装:yum -y install keepalived

  1. 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                    

   }

}

  1. 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

  1. 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                    

   }

}

  1. 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的配置验证

  1. 看keepalived 运行状态:tail -f / var/log/messages
  2. 当master节点关闭,slaver的ip addr 看状态是否切换为虚拟IP
  1. ps -ef | grep redis
  2. kill -9 程序号

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yo-ke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值