Building a redis high availability environment based on haproxy

A redis master-slave builds
on 10.21.6.55 and deploys redis as master
10.21.6.56 deploys redis as slave
master configuration:

quote
daemonize yes

###If it is a slave library, it should be set to the password of the master library
#masterauth "Ifeng888"

###Login requires password authentication
requirepass "Ifeng888"
###Don't forget to change the password

pidfile /data/redis/6379/redis .pid

port 6379
tcp-backlog 511

timeout 0
tcp-keepalive 0

loglevel warning

logfile /data/logs/redis/redis.log

databases 16

save 1200 1
save 900 10000
save 300 100000

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename dump.rdb

dir /data/redis/6379

slave-serve-stale-data yes

slave-read-only no



slave configuration

quote
daemonize yes

###If it is a slave library, it should be set to the password of the master library
masterauth "Ifeng888"

###Login requires password authentication
requirepass "Ifeng888"
###Don't forget to change the password

pidfile /data/redis/6379/redis. pid

port 6379
tcp-backlog 511

timeout 0
tcp-keepalive 0

loglevel warning

logfile /data/logs/redis/redis.log

databases 16

save 1200 1
save 900 10000
save 300 100000

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename dump.rdb

dir /data/redis/6379

##Set master-slave
slaveof 10.21.6.55 6379

slave-serve-stale-data yes

slave-read-only no



Start redis

on the master slave on the master

quote
redis-cli -a Ifeng888
127.0.0.1:6379> get Ifeng777
"14"
127.0.0.1:6379> set Ifeng777 18
OK


on slave

quote
redis-cli -a Ifeng888
127.0.0.1:6379> get Ifeng888
(nil)
127.0.0.1:6379> get Ifeng777
"14"
127.0.0.1:6379> get Ifeng777
"18"

The master-slave environment was successfully built

2.
haproxy configuration 1. Install haproxy

quote
yum install haproxy



2. Modify the configuration file

quote
vim /etc/haproxy/haproxy.cfg


configuration file

quote
listen redis_cluster:6380 #Running port and hostname
        bind *:6380
        server redis-master_6.55 10.21.6.55:6379 check port 6379 inter 2000 rise 3 fall 3 maxconn 10000 weight 1
        server redis-slaver_6.56 10.21.6.56:6379 check port 6379 inter 2000 rise 3 fall 3 maxconn 10000 weight 1 backup



The configuration of haproxy is
completed Implemented:
1. Redis master-slave configuration
2. High availability of haproxy
Because the backup is configured for the slave, when the master is working normally, all redis requests are forwarded to the master, and when the master hangs, the requests are forwarded to the slave.


Write a simple java code for verification. The IP port of redisclient configured as haproxy is 6380.
When the program is halfway through, shut down the master
command: redis-cli -a Ifeng888 shutdown

test code
public class Test {

    public static void main(String[] args){
        String key = "Ifengtest1";

        RedisClient.set(key,0);
        while (true){
            Integer rv= RedisClient.get(key,Integer.class);
            System.out.println("test redis "+rv);
            if (rv!=null){
                rv++;
                RedisClient.set(key,rv);
            }

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace ();
            }

        }


    }
}



Output result:
quote
test redis 0
test redis 1
test redis 2
test redis 3
test redis 4
test redis 5
test redis 6
2017-10-31 10:05:30,154  INFO main com.ifeng.auto.we_provider.cache.redis.RedisClient - Redis
redis.clients.jedis.exceptions.JedisConnectionException: It seems like server has closed the connection.
at redis.clients.util.RedisInputStream.readLine(RedisInputStream.java:91)
at redis.clients.jedis.Protocol.processStatusCodeReply(Protocol.java:80)
at redis.clients.jedis.Protocol.process(Protocol.java:69)
at redis.clients.jedis.Protocol.read(Protocol.java:122)
at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:172)
at redis.clients.jedis.BinaryJedis.get(BinaryJedis.java:74)
at com.ifeng.auto.we_provider.cache.redis.RedisClient.get(RedisClient.java:571)
at com.ifeng.auto.we_provider.main.Test.main(Test.java:16)
test redis null
test redis 7
test redis 8
test redis 9
test redis 10
test redis 11
test redis 12
test redis 13


3. Change the password of the master and slave redis
1. Change the password of the master

quote
###Login requires password authentication
requirepass "Ifeng999"


2. Shut down the master and
use the password before changing the configuration to shut down redis
quote
redis-cli -a Ifeng888 shutdown


3. Start the master
quote
service redis start


4. The slave
needs to change the password masterauth and requirepass to the new password

quote
###If it is a slave library, it should be set to the password of the master library
masterauth "Ifeng999"

###Login requires password authentication
requirepass "Ifeng999"



5. Slave shutdown
Use the password before changing the configuration to shutdown redis
quote
redis-cli -a Ifeng888 shutdown


6. Start slave
quote
service redis start


7. Modify the redis password configuration file in each business system and restart the project.
At this point redis password change is complete.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326387871&siteId=291194637