在两台服务器redis实现集群:
1、cluseter服务需要rub支持。首先安装rub相关服务
yum -y install ruby rubygems
rpm -ivh http://yum.puppetlabs.com/el/5/products/x86_64/puppetlabs-release-5-6.noarch.rpm
yum install ruby ruby-devel rubygems rpm-build
gem source -l
gem install redis --version 3.0.5
如果上面gem install失败了就执行下面语句来切换gem源
gem source --remove http://rubygems.org/
gem sources -a http://ruby.taobao.org/
—– A、B服务上都执行一次以上命令
2、 安装好rub、redis后,配置redis的配置文件。
vim /etc/redis/6379.conf
/*按照以下内容修改。*/
port 6379
pidfile /var/run/redis-6379.pid
dbfilename dump-6379.rdb
appendfilename "appendonly-6379.aof"
cluster-config-file nodes-6379.conf
cluster-enabled yes
cluster-node-timeout 5000
appendonly yes
3、 复制文件配置文件。
–(1)A服务器:
cp 6379.conf 6380.conf
cp 6379.conf 6381.conf
scp 6379.conf root@192.168.4.125:/etc/redis/6382.conf
scp 6379.conf root@192.168.4.125:/etc/redis/6383.conf
scp 6379.conf root@192.168.4.125:/etc/redis/6384.conf
sed -i "s/6379/6380/g" /etc/redis/6380.conf
sed -i "s/6379/6381/g" /etc/redis/6381.conf
B服务器:
sed -i "s/6379/6382/g" /etc/redis/6382.conf
sed -i "s/6379/6383/g" /etc/redis/6383.conf
sed -i "s/6379/6384/g" /etc/redis/6384.conf
A服务器:
cat 6380.conf |awk '{if($0 !~ /^$/ && $0 !~ /#/) {print $0}}' |grep 6380
mkdir /var/log/redis
redis-server /etc/redis/6379.conf > /var/log/redis/redis-6379.log 2>&1 &
redis-server /etc/redis/6380.conf > /var/log/redis/redis-6380.log 2>&1 &
redis-server /etc/redis/6381.conf > /var/log/redis/redis-6381.log 2>&1 &
B服务器:
pkill - 9 redis
mkdir /var/log/redis
redis-server /etc/redis/6382.conf > /var/log/redis/redis-6382.log 2>&1 &
redis-server /etc/redis/6383.conf > /var/log/redis/redis-6383.log 2>&1 &
redis-server /etc/redis/6384.conf > /var/log/redis/redis-6384.log 2>&1 &
A服务器:
cd /usr/local/redis/
netstat -tpnl |grep redis
cp /usr/local/src/redis-3.0.5/src/redis-trib.rb redis-trib.rb
./redis-trib.rb create --replicas 1 192.168.4.254:6379 192.168.4.254:6380 192.168.4.254:6381 192.168.4.125:6382 192.168.4.125:6383 192.168.4.125:6384
#检查集群状态
–(1)A服务器:
redis-trib.rb check 192.168.10.219:6379
4、 测试
(1)A服务器
redis-cli -c -p 6379 -h 192.168.4.254
set tank tank1
(2)B服务器
redis-cli -c -p 6382 -h 192.168.4.125
get tank
###缺点:
重启的时候或者冲突的时候很难保证同步性。重启比较麻烦为此我写了两个脚本。
(1)redis.sh #用于重启redis多个端口的服务 (A服务器和B服务器都需要)
ps -fe|grep redis |grep -v grep
if [ $? -ne 0 ]
then
echo “start process…..”
else
redis-server /etc/redis/6379.conf > /var/log/redis/redis-6379.log 2>&1 &
redis-server /etc/redis/6380.conf > /var/log/redis/redis-6380.log 2>&1 &
redis-server /etc/redis/6381.conf > /var/log/redis/redis-6381.log 2>&1 &
fi
(2)redisCluster.sh#启动集群服务。(仅A服务器即可)
/usr/local/redis/bin/redis-trib.rb create –replicas 1 192.168.4.254:6379 192.168.4.254:6380 192.168.4.254:6381 192.168.4.125:6382 192.168.4.125:6383 192.168.4.125:6384
“`
参考:
http://blog.51yip.com/nosql/1725.html