Large high concurrency and high availability cache architecture summary 4

A, redis installation

redis installed yum and compiled in two ways, the paper installed to compile reference documentation for the official website of the installation documentation

1.gcc installation

[root@redis-master ~]# yum -y install gcc

 

2. Download the installation package

[root@redis-master ~]# wget http://download.redis.io/redis-stable.tar.gz

 

3. Extract compilation

[root@redis-master ~]# tar xvzf redis-stable.tar.gz
[root@redis-master ~]# cd redis-stable [root@redis-master redis-stable]# make [root@redis-master redis-stable]# make install

 

4. Modify the configuration file

[root@redis-master redis-stable]# mkdir /etc/redis[root@redis-master redis-stable]# mkdir -p /var/redis/6379
[root@redis-master redis-stable]# cp redis.conf /etc/redis/6379.conf [root@redis-master redis-stable]# cd /etc/redis/ [root@redis-master redis]# cp 6379.conf 6379.conf.bak [root@redis-master redis]# grep -Ev '^$|#' 6379.conf.bak > 6379.conf

Main configuration amended as follows:

#bind 127.0.0.1
protected-mode no
daemonize yes
pidfile /var/run/redis_6379.pid
logfile /var/log/redis_6379.log
dir /var/redis/6379

 

5. Production start the service

[root@redis-master redis-stable]# cp utils/redis_init_script /etc/init.d/redis_6379

 

6. Start and join start-up service

[root@redis-master redis]# service redis_6379 start
[root@redis-master redis]# chkconfig redis_6379 on

 

7.RedisDesktopManager management tools

The tool is a graphical management tool redis

Download: https://pan.baidu.com/s/1no0IWcIEBapfehNi8dfCfw Password: 7dvc

Connection settings:

Pictures .png

After connecting effect

Pictures .png

 

Two, redis master-slave replication and persistence

1. From the Server Settings

redis master copy from very simple, only need to add a configuration from the server configuration file to

[root@redis-slave redis]# sed -i '$a slaveof 172.27.9.30 6379' /etc/redis/6379.conf

 

2. From the main test

The value written on 2.1 host

[root@redis-master ~]# redis-cli set test-key abc
OK

View from the machine redis key:

Pictures .png

Value slave write 2.2

[root@redis-slave redis]# redis-cli set test-key 123
(error) READONLY You can't write against a read only slave.

Found error, can not be written in a slave state redis.

 

3. endurance of

redis persistence has RDB and AOF in two ways, by default persisted as RDB mode, turn off the default persistence just need to comment on the default configuration line:

#save 900 1
#save 300 10
#save 60 10000

 

Three, keepalived installation configuration

1.keepalived installation

[root@redis-master ~]# yum -y install keepalived

 

2.keepalived Configuration

[root@redis-master ~]# more /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { router_id master #router_id 机器标识,通常为hostname,但不一定非得是hostname。故障发生时,邮件通知会用到。 } vrrp_script chk_redis { script "/etc/keepalived/redis-check.sh" interval 2 #健康检查周期 weight 30 #优先级变化幅度 fall 2 #尝试两次都成功才成功 rise 2 #尝试两次都失败才失败 } vrrp_instance VI_1 { #vrrp实例定义部分 state MASTER #设置lvs的状态,MASTER和BACKUP两种,必须大写 interface ens33 #设置对外服务的接口 virtual_router_id 100 #设置虚拟路由标示,这个标示是一个数字,同一个vrrp实例使用唯一标示 priority 100 #定义优先级,数字越大优先级越高,在一个vrrp——instance下,master的优先级必须大于backup advert_int 1 #设定master与backup负载均衡器之间同步检查的时间间隔,单位是秒 authentication { #设置验证类型和密码 auth_type PASS #主要有PASS和AH两种 auth_pass 1111 #验证密码,同一个vrrp_instance下MASTER和BACKUP密码必须相同 } virtual_ipaddress { #设置虚拟ip地址,可以设置多个,每行一个 172.27.9.100 } track_script { #执行脚本chk_redis chk_redis } notify_master "/etc/keepalived/redis-master.sh" #指定当切换为master时,执行的脚本 }

Compared master, slave arranged in different three points:

router_id slave
state BACKUP 
priority 90

In order to avoid switching back and forth between the master may be provided nopreemt parameters, open non-preemptive mode; however, when an abnormality occurs keepalived redis or various alarm is triggered, operation and maintenance personnel will timely intervention process.

 

3. Start and set the boot

[root@redis-master ~]# systemctl start keepalived
[root@redis-master ~]# systemctl enable keepalived

 

Four, expect to install

[root@redis-master ~]# yum -y install expect

expect is an interactive tool to provide automatic paper notify_master the script to use.

 

Fifth, high availability script

1.redis-check.sh script

[root@redis-master keepalived]# more redis-check.sh 
#!/bin/sh
rediscli="/usr/local/bin/redis-cli" logfile="/var/log/redis_6379.log" result=$($rediscli ping) echo $result echo "-------------------redis-check-------------------" >> $logfile date >> $logfile if [ $result == "PONG" ]; then : echo "Success: the result is $result" >> $logfile 2>&1 exit 0 else echo "Failed:the result is $result " >> $logfile 2>&1 exit 1 fi

The script for the examination of state redis

 

2.redis-master.sh script

[root@redis-master keepalived]# more redis-master.sh
#!/bin/bash
rediscli="/usr/local/bin/redis-cli" logfile="/var/log/redis_6379.log" sync=`$rediscli info replication|grep master_sync_in_progress|awk -F: '{print $2}'|sed 's/\r//'` #sync=`/usr/local/bin/redis-cli info replication|grep master_sync_in_progress|awk -F: '{print $2}'` echo $sync echo "-------------------change to master-------------------" >> $logfile 2>&1 date >> $logfile if [ $sync == 0 ]; then : echo "the master_sync_in_progress is 0 and excute slaveof no one" >>$logfile 2>&1 $rediscli slaveof no one elif [ $sync == 1 ]; then : sleep 10 $rediscli slaveof no one else echo "the host is master,do nothing" >>$logfile 2>&1 fi /etc/keepalived/exp.sh 172.27.9.31 monitor >>$logfile 2>&1

The key to the script for this article highly available switching.

monitor 31 for the root password; ip slave script should be replaced 172.27.9.30; the script to the script execution server when switching master, first checks before the switching from the main master synchronization is complete, and then use the tool to jump to expect another server will change its status to redis slave.

 

3.exp.sh script

[root@redis-master keepalived]# more exp.sh
#!/usr/bin/expect -f
set ip [lindex $argv 0 ] set password [lindex $argv 1 ] set timeout 30 spawn ssh root@$ip expect { "*yes/no" { send "yes\r"; exp_continue} "*password:" { send "$password\r";exp_continue } } expect "#*" send "/usr/local/bin/redis-cli slaveof 172.27.9.30 6379\r" send "exit\r" expect eof

slave script as ip 172.27.9.31; the role of the script jumps to another service execution slaveof command.

 

Six test

1. Analog redis service exception

Operation: master of redis service stopped

Expected: vip drift, redis state of the slave master switch for the master

Testing process:

1.1 Stop master of redis service

[root@redis-master keepalived]# service redis_6379 stop

View 1.2 virtual ip

图片.png

图片.png

Vip found by the drift 30-31

1.3 View redis state

[root@redis-slave keepalived]# redis-cli info replication

图片.png

redis 31 is switched to the state of the master

1.4 switchback

redis service starts 30

[root@redis-master keepalived]# service redis_6379 start

View vip status and redis

30:

图片.png

31:

图片.png

Conclusion: redis service switching occurs when abnormal, switch to master state to provide services from outside opportunities.

 

2. Analog keepalived service exception

Operation: master of keepalived service stopped

Expected: vip drift, redis state switched to the slave master, redis status of slave to master switch

Testing process:

2.1 Stop master of keepalived service

[root@redis-master keepalived]# systemctl stop keepalived

2.2 View vip status and redis

30:

图片.png

31:

图片.png

vip drift from the 30-31 and the master-slave reversed; open keepalived service will trigger the switching.

Conclusion: keepalived service switching occurs when abnormal, master-slave will reverse.

 

3. Analog master downtime

Operation: master shutdown

Expected: vip drift, redis status of slave to master switch

Testing process:

3.1master server shutdown

[root@redis-master ~]# init 0

3.2 View vip status and redis

图片.png

vip drift to 31 and the state switched to the master; 30 cut open the opportunity to return trigger.

Conclusion: The switch occurs when the master is down, switch to master state to provide services outside the machine.

 

This completes the redis availability test.

Guess you like

Origin www.cnblogs.com/xiaohaigege666/p/10992594.html