########环境######### centos 7.2 ,
gcch 环境
ruby 2.0.0 redis 3.2.8 redis-3.3.3gem
公司要求搭建redis集群, 本来觉得挺好搞的,没想到弄到现在....
1, 环境准备
gcc , ruby 等环境准备
yum -y install gcc ruby ruby-devel rubygems rpm-build zlib
redis-ruby接口安装, 我直接yum安装的,
gem install redis
你也可以下载 redis.3.3.3.gem 然后执行命令
gem install redis-3.3.3.gem
2, 再说下单机redis的搭建方法
1, 下载redis 3.2.8 的安装包:
wget http://download.redis.io/releases/redis-3.2.8.tar.gz
2, 解压
tar -zxvf redis-3.2.8
3, 进入解压目录下
make
然后安装
make install PREFIX=/opt/wenbronk_soft/redis/redis1
安装完成后, 可在redis1 目录下看到一个bin目录, 对, 就是他了.
4, 此时已经可以访问了, 但需要更改下配置文件, 才可以使用外部ip访问
redis.conf 在redis的解压目录下
cp /opt/wenbronk_soft/redis/redis-3.2.8/redis.conf /opt/wenbronk_soft/redis/redis1/bin
vim
bind 192.168.xx.202 (当前主机的ip)
5, 启动
./redis-server ./redis.conf
现在已经可以通过本地的 ./redis-cli -h localhost -p 6379 或者通过外部ip进行访问了.
3, 接下来是集群的安装
安装集群之前, 首先要保证单个的redis是空的, 也就是没有dump.rb文件没有密码设置, 就是 redis.conf中 masterauth masterauth 没有设置
这儿搭建一个台机器上的伪集群, 分布式集群搭建和这个类似, 只是使用ruby创建集群时ip:port有所区别
注意: 至少需要 3 master+ 2 slave, 官方建议是 3 + 3 , 这儿使用 3 mater + 3 slave
1, 修改 redis.conf
添加守护进程: daemonize
port 7000 //端口7000,7001,7002,7003,7004,7005,7005 bind 本机ip //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群 daemonize yes //redis后台运行, # pidfile /var/run/redis_7000.pid //pidfile文件对应7000,7001,7002 cluster-enabled yes //开启集群 把注释#去掉 cluster-config-file nodes_7000.conf //集群的配置 配置文件首次启动自动生成 7000,7001,7002 cluster-node-timeout 15000 //请求超时 默认15秒,可自行设置 appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
2, 复制解压目录下的 redis-trib.rb 到 安装 目录下 (/opt/wenbronk_soft/redis/redis/bin)
cp /opt/wenbronk_soft/redis/redis-3.2.8/src/redis-trib.rb /opt/wenbronk_soft/redis/redis/bin
此时bin下目录结构为:
3, 复制bin为另外6个实例
4, 进入到每个目录下更改 port设置
从 7000 - 7005
5, 使用启动脚本启动6个实例
cd bin ./redis-server ./redis.conf cd .. cd bin1 ./redis-server ./redis.conf cd .. cd bin2 ./redis-server ./redis.conf cd .. cd bin3 ./redis-server ./redis.conf cd .. cd bin4 ./redis-server ./redis.conf cd .. cd bin5 ./redis-server ./redis.conf cd ..
修改脚本权限
chmod u+x start.sh
6, 查看redis实例额运行状态
ps -ef | grep redis
netstat -tnlp | grep redis
7, 创建集群
./redis-trib.rb create --replicas 1 192.168.50.202:7000 192.168.50.202:7001 192.168.50.202:7002 192.168.50.202:7003 192.168.50.202:7004 192.168.50.202:7005
[wenbronk@localhost bin]$ ./redis-trib.rb create --replicas 1 192.168.50.202:7000 192.168.50.202:7001 192 .168.50.202:7002 192.168.50.202:7003 192.168.50.202:7004 192.168.50.202:7005>>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.50.202:7000 192.168.50.202:7001 192.168.50.202:7002 Adding replica 192.168.50.202:7003 to 192.168.50.202:7000 Adding replica 192.168.50.202:7004 to 192.168.50.202:7001 Adding replica 192.168.50.202:7005 to 192.168.50.202:7002 M: b06c9670ad6e16cf41c9eeefb8260ad29dbb52e8 192.168.50.202:7000slots:0-5460 (5461 slots) master M: 048291cbc8e9ccf81de57f02150447340d310fe0 192.168.50.202:7001slots:5461-10922 (5462 slots) master M: 740557eca4a8fa31d67fe6a7f6b34814f3f96094 192.168.50.202:7002slots:10923-16383 (5461 slots) master S: 63a9d243b8da03169dee24ec9403e50f36ab7cb0 192.168.50.202:7003replicates b06c9670ad6e16cf41c9eeefb8260ad29dbb52e8 S: 824b5d23e012cd7082701e3737ce8ea21eb8abe3 192.168.50.202:7004replicates 048291cbc8e9ccf81de57f02150447340d310fe0 S: f00948f8f9df4c10cba340cab3f0767d98f00b77 192.168.50.202:7005replicates 740557eca4a8fa31d67fe6a7f6b34814f3f96094 Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join... >>> Performing Cluster Check (using node 192.168.50.202:7000) M: b06c9670ad6e16cf41c9eeefb8260ad29dbb52e8 192.168.50.202:7000slots:0-5460 (5461 slots) master1 additional replica(s) M: 740557eca4a8fa31d67fe6a7f6b34814f3f96094 192.168.50.202:7002slots:10923-16383 (5461 slots) master1 additional replica(s) S: 824b5d23e012cd7082701e3737ce8ea21eb8abe3 192.168.50.202:7004slots: (0 slots) slavereplicates 048291cbc8e9ccf81de57f02150447340d310fe0 S: f00948f8f9df4c10cba340cab3f0767d98f00b77 192.168.50.202:7005slots: (0 slots) slavereplicates 740557eca4a8fa31d67fe6a7f6b34814f3f96094 M: 048291cbc8e9ccf81de57f02150447340d310fe0 192.168.50.202:7001slots:5461-10922 (5462 slots) master1 additional replica(s) S: 63a9d243b8da03169dee24ec9403e50f36ab7cb0 192.168.50.202:7003slots: (0 slots) slavereplicates b06c9670ad6e16cf41c9eeefb8260ad29dbb52e8 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
ok, 集群搭建完成
深坑, redis 集群之间的通讯端口为basePort + 1w
4 链接集群
1, redis客户端连接
-c 连接集群
-p 如果bind 绑定的是本机地址, 那么必须加
./redis-cli -h 192.168.50.202 -p 7006 -c
2, 简单测试
set abc 1234 get abc
3, java连接
Jedis Cluster 会自动去发现集群中的节点,所以JedisClusterNodes只需要 add一个实例
ps -ef | grep redis | awk '{print $2}' | xargs kill -9
明天单独写吧.. 东西很多..
原创地址, http://www.cnblogs.com/wenbronk/p/6864390.html 转载请注明出处, 谢谢