haproxy cfg for redis sentinel

Standard
frontend redis-cluster
        mode tcp
        option tcplog
        bind *:6379
        # If at least 3 sentinels agree with the redis host that it is master, use it.
        use_backend redis-node1 if { srv_is_up(redis-node1/redis-1:10.25.0.2:6379) } { nbsrv(check_master_redis-1) ge 3 }            
        use_backend redis-node2 if { srv_is_up(redis-node2/redis-2:10.25.0.3:6379) } { nbsrv(check_master_redis-2) ge 3 }
        # If sentinel cant tell us, well, fall back to master detection
        default_backend redis-cluster
 
backend redis-node1
        mode tcp
        balance first
        option tcp-check
        tcp-check send AUTH\ password\r\n
        tcp-check expect string +OK
        tcp-check send info\ replication\r\n
        tcp-check expect string role:master
        server redis-1:10.25.0.2:6379 10.25.0.2:6379 maxconn 5000 check inter 1s
 
backend redis-node2
        mode tcp
        balance first
        option tcp-check
        tcp-check send AUTH\ password\r\n
        tcp-check expect string +OK
        tcp-check send info\ replication\r\n
        tcp-check expect string role:master
        server redis-2:10.25.0.3:6379 10.25.0.3:6379 maxconn 5000 check inter 1s
 
backend redis-cluster
        mode tcp
        balance first
        option tcp-check
        tcp-check send AUTH\ password\r\n
        tcp-check expect string +OK
        tcp-check send info\ replication\r\n
        tcp-check expect string role:master
        tcp-check send info\ persistence\r\n           
        tcp-check expect string loading:0
        server redis-1:10.25.0.2:6379 10.25.0.2:6379 maxconn 5000 check inter 1s
        server redis-2:10.25.0.3:6379 10.25.0.3:6379 maxconn 5000 check inter 1s
 
## Check 4 sentinels to see if they think redis-1 (10.25.0.2) is master
backend check_master_redis-1
        mode tcp
        option tcp-check
        tcp-check send PING\r\n
        tcp-check expect string +PONG
        tcp-check send SENTINEL\ master\ redis\r\n
        tcp-check expect string 10.25.0.2
        tcp-check send QUIT\r\n
        tcp-check expect string +OK
 
        server redis-1:10.25.0.2:26379 10.25.0.2:26379 check inter 2s
        server redis-2:10.25.0.3:26379 10.25.0.3:26379 check inter 2s
        server redis-sentinel1:10.25.0.4:26379 10.25.0.4:26379 check inter 2s
        server redis-sentinel2:10.25.0.5:26379 10.25.0.5:26379 check inter 2s
 
## Check 4 sentinels to see if they think redis-2 (10.25.0.3) is master
backend check_master_redis-2
        mode tcp
        option tcp-check
        tcp-check send PING\r\n
        tcp-check expect string +PONG
        tcp-check send SENTINEL\ master\ redis\r\n
        tcp-check expect string 10.25.0.3
        tcp-check send QUIT\r\n
        tcp-check expect string +OK
 
        server redis-1:10.25.0.2:26379 10.25.0.2:26379 check inter 2s
        server redis-2:10.25.0.3:26379 10.25.0.3:26379 check inter 2s
        server redis-sentinel1:10.25.0.4:26379 10.25.0.4:26379 check inter 2s
        server redis-sentinel2:10.25.0.5:26379 10.25.0.5:26379 check inter 2s

Redis安装配置操作

Standard

Redis和Memcached类似,但其断电后数据仍然能得到保存,功能也更丰富.

#安装
cd /usr/local/
wget http://redis.googlecode.com/files/redis-1.2.6.tar.gz
tar zxvf redis-1.2.6.tar.gz 
mv redis-1.2.6 redis
cd redis
./make
#make命令执行完成后,会在当前目录下生成本个可执行文件,如下:
#redis-server:Redis服务器的daemon启动程序
#redis-cli:Redis命令行操作工具。当然,你也可以用telnet根据其纯文本协议来操作
#redis-benchmark:Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能
#redis-stat:Redis状态检测工具,可以检测Redis当前状态参数及延迟状况 
 
#修改配置
vi redis.conf
daemonize yes #默认是no,改为yes让其后台运行.
 
#配置的一些基本说明
daemonize:#是否以后台daemon方式运行
pidfile:#pid文件位置
port:#监听的端口号
timeout:#请求超时时间
loglevel:#log信息级别
logfile:#log文件位置
databases:#开启数据库的数量
save * *#保存快照的频率,第一个*表示多长时间,第三个*表示执行多少次写操作。在一定时间内执行一定数量的写操作时,自动保存快照。可设置多个条件。
rdbcompression:#是否使用压缩
dbfilename:#数据快照文件名(只是文件名,不包括目录)
dir#数据快照的保存目录(这个是目录)
appendonly:#是否开启appendonlylog,开启的话每次写操作会记一条log,这会提高数据抗风险能力,但影响效率。
appendfsync:#appendonlylog如何同步到磁盘(三个选项,分别是每次写都强制调用fsync、每秒启用一次fsync、不调用fsync等待系统自己同步)
 
#启动
./redis-server redis.conf
 
#操作
./redis-cli 
redis> set key1 value1
OK
redis> get key1
value1
redis> set site yemaosheng.com
OK
redis> get site
yemaosheng.com
redis> lpush city SH
OK
redis> lpush city BJ
OK
redis> lpush city SZ
OK
redis> lpush city HZ
OK
redis> lpush city GZ
OK
redis> lrange city 0 2
1. GZ
2. HZ
3. SZ
redis> lrange city 1 2
1. HZ
2. SZ
redis> lpop city
GZ
redis> lpop city
HZ
redis> lpop city
SZ
redis> lpop city
BJ
redis> lpop city
SH
redis> lpop city
(nil)

转载请注明出处 http://yemaosheng.com