Twemproxy 测试架构
twemproxy- nutcracker:
ip:10.207.101.101
ip:10.207.101.102
VIP:10.207.101.100
HA- keepalived
ip:10.207.101.101
ip:10.207.101.102
VIP:10.207.101.100
Redis
IP: 10.207.101.101
Port:6001/6002/6003
IP: 10.207.101.102
Port:6001/6002/6003
1、twemproxy是twitter开发的一个redis代理proxy。通过Twemproxy可以使用多台服务器来水平扩张redis服务,可以有效的避免redis单点故障问题。
使用Twemproxy 对硬件资源配置较高;在redis性能有一定的损失(twitter测试约20%)用于提高整个系统的HA;
2、twemproxy部署简单快捷;可以直接在proxy进行读写、并转发请求给后端的redis;但是不适合超大流量系统。做的时候把应用分开、使用LVS集群:实现twemproxy的负载均衡,提高proxy的可用性和可扩张能力;
3、安装部署
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
wget https://codeload.github.com/twitter/twemproxy/zip/master
yum install gcc gcc-c++ tcl ruby -y
tar -xf autoconf-2.69.tar.gz
cd autoconf-2.69/
./configure
make &&make install
unzip unzip master.zip
cd twemproxy-master
autoreconf -fvi
./configure --enable-debug=full
make
make install
4、配置示例:
# vim /etc/nutcracker.yml
alpha:
listen: 0.0.0.0:22121
hash: fnv1a_64
distribution: ketama
auto_eject_hosts: true
redis: true
server_retry_timeout: 2000
server_failure_limit: 1
servers: --两台redis服务器的地址和端口
- 10.207.141.142:6379:1
- 10.207.141.142:6379:1
5、启动Twemproxy服务
nutcracker -t nutcracker.yml
检测配置语法真确会显示OK .
# nutcracker -t twemproxy/conf/nutcracker.yml
nutcracker: configuration file ‘conf/nutcracker.yml‘ syntax is ok
6、设置启动:
cat start.sh
nutcracker -d -c /opt/twemproxy-master/conf/nutcracker.yml -p /opt/twemproxy-master/run/redisproxy.pid -o /opt/twemproxy-master/run/redisproxy.log
cat stop.sh
ps -ef | grep redis | grep -v grep | awk ‘{print $2}‘ | sed -e "s/^/kill -9 /g" | sh -
7、检测进程
ps -ef | grep nutcracker | grep -v grep
8、keepalived 部署 HA 静态路由单点故障;
yum install keepalived -y
# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_script check_twem {
# script "killall -0 redis"
script "/etc/keepalived/check_twem.sh"
interval 2
weight -3
fall 3
rise 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.27.101.100/24 dev eth0 label eth0:1
}
track_script {
check_twem
}
}
virtual_server 172.27.101.100 22121 {
delay_loop 6
protocol TCP
real_server 172.27.101.101 22121 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
real_server 172.27.101.102 22121 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
9、nutcracker 进程检测 脚本check_twem.sh
# cat /etc/keepalived/check_twem.sh
#!/bin/bash
counter=$(ps -C nutcracker --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
sh /opt/twemproxy-master/start.sh
sleep 2
counter=$(ps -C nutcracker --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
/etc/init.d/keepalived stop
fi
fi
10、Set测试:
通过twemproxy测试:
# redis-benchmark -h 10.207.101.100 -p 22121 -c 11 -t set -d 11 -l –q
SET: 38167.94 requests per second
直接对后端redis测试:
# redis-benchmark -h 10.207.101.101 -p 6001 -c 11 -t set -d 11 -l –q
直接对后端redis测试:
# redis-benchmark -h 10.207.101.102 -p 6001 -c 11 -t set -d 11 -l –q
SET: 53191.49 requests per second
Get测试:
通过twemproxy测试:
# redis-benchmark -h 0.207.101.100 -p 22121 -c 11 -t get -d 11 -l -q
GET: 37453.18 requests per second
直接对后端redis测试:
# redis-benchmark -h 0.207.101.101 -p 22121 -c 11 -t get -d 11 -l -q
GET: 62111.80 requests per second
查看键值分布:
# redis-cli info|grep db0
db0:keys=51483,expires=0,avg_ttl=0
# redis-cli info|grep db0
db0:keys=48525,expires=0,avg_ttl=0
11 、redis-cli 基本操作;
Redis 模糊搜索
keys *
select 2
删除所有以user开头的key 可以这样实现:
# redis-cli keys "user*"
1) "user1"
2) "user2"
# redis-cli keys "user*" | xargs redis-cli del
(integer) 2
# 删除成功
# 批量删除匹配通配符的key用到了Linux中的管道和xargs参数:
redis-cli keys "s*" | xargs redis-cli del
# 如果需要制定数据库,需要用到 -n 数据库编号 参数,下面是删除2数据库中s开头的键:
redis-cli -n 2 keys "s*" | xargs redis-cli -n 2 del
redis-cli keys "*" | xargs redis-cli del
# 如果redis-cli没有设置成系统变量,需要指定redis-cli的完整路径
# 如:/opt/redis/redis-cli keys "*" | xargs /opt/redis/redis-cli del
# 删除当前数据库中的所有Key
flushdb
# 删除所有数据库中的key
flushall