Redis主从+KeepAlived实现高可用(2)

vim /etc/redis/redis.conf
    bind 0.0.0.0
    port 6379
    daemonize yes
    requirepass 123456
    slave-serve-stale-data yes
    slave-read-only no

Slave-141配置文件

vim /etc/redis/redis.conf
    bind 0.0.0.0
    port 6379
    daemonize yes
    slaveof 172.16.81.140 6379
    masterauth 123456
    slave-serve-stale-data yes
    slave-read-only no

2、配置完成后重启redis服务!验证主从是否正常。

主节点140终端登录测试:

[root@localhost ~]# redis-cli -a 123456
127.0.0.1:6379> INFO
.
.
.
# Replication
role:master
connected_slaves:1
slave0:ip=172.16.81.141,port=6379,state=online,offset=105768,lag=1
master_replid:f83fcc3c98614d770f2205831fef1e877fa3f482
master_replid2:1f25604997a4ad3eb8344e8155990e78acd93312
master_repl_offset:105768
second_repl_offset:447
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:447
repl_backlog_histlen:105322

从节点141终端登录测试:

[root@localhost ~]# redis-cli -a 123456
127.0.0.1:6379> info
.
.
.
# Replication
role:slave
master_host:172.16.81.140
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:105992
slave_priority:100
slave_read_only:0
connected_slaves:0
master_replid:f83fcc3c98614d770f2205831fef1e877fa3f482
master_replid2:1f25604997a4ad3eb8344e8155990e78acd93312
master_repl_offset:105992
second_repl_offset:447
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:239
repl_backlog_histlen:105754
3、同步测试

主节点140

Redis主从+KeepAlived实现高可用

从节点141

Redis主从+KeepAlived实现高可用

到此redis的主从已经完成!

KeepAlived配置实现双机热备

使用Keepalived实现VIP,并且通过notify_master、notify_backup、notify_fault、notify_stop来实现容灾。

1、配置Keepalived配置文件

主Keepalived配置文件

vim /etc/keepalived/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 192.168.200.1
  smtp_connect_timeout 30
  router_id redis01
}

vrrp_script chk_redis {
    script "/etc/keepalived/script/redis_check.sh"
    interval 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eno16777984
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

track_script {
        chk_redis
    }
    virtual_ipaddress {
        172.16.81.139
    }
   
    notify_master /etc/keepalived/script/redis_master.sh
    notify_backup /etc/keepalived/script/redis_backup.sh
    notify_fault  /etc/keepalived/script/redis_fault.sh 
    notify_stop  /etc/keepalived/script/redis_stop.sh
}

备用Keepalived配置文件

vim /etc/keepalived/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 192.168.200.1
  smtp_connect_timeout 30
  router_id redis02
}

vrrp_script chk_redis {
    script "/etc/keepalived/script/redis_check.sh"
    interval 2
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/d8312496d08b0fb3e873eb97ec125ea5.html