刚接触Redis不久,就像动手搭个集群玩玩,使用linux系统也不是很熟练,但是强大的好奇心还是推着自己去尝试,还好找度娘不是很坑,大神们写的文章很详细,用了一下午的时间总算成功了,感谢那些无私分享的人。接下来分享一下我的搭建过程。
linux系统虚拟机ubuntu 15.0.4
redis 3.2.0
终端下载redis 压缩包(默认下载到root根目录)
wget http://download.redis.io/releases/redis-3.2.0.tar.gz 也可以直接访问
http://download.redis.io/releases/ 查看有哪些版本 我下载最新版本是3.3.0 不想下载最新的有强迫症
系统一些依赖包
安装gcc:apt-get install gcc 安装zlib:yum install zib(这个我没装成功但是好像没影响)
安装ruby:apt-get install ruby 安装rubygems:yum install rubygems 安装gem
redis:(下载:http://rubygems.org/gems/redis/versions/) gem install redis
Successfully installed redis-3.3.0 1 gem installed Installing ri
documentation for redis-3.3.0… Installing RDoc documentation for
redis-3.3.0… (安装会默认下载最新版本的 想下redis-3.2.0的还没有,悲哀呀!但是不影响)
系统参数配置(完全抄别人的 我没配置以后用到在配置)
修改open files:# ulimit -n 10032 (默认1024)
添加vm.overcommit_memory=1:
vi /etc/sysctl.conf
sysctl vm.overcommit_memory=1
关闭hugepage:
echo never > /sys/kernel/mm/transparent_hugepage/enabled
修改somaxconn :
echo 511 >/proc/sys/net/core/somaxconn
关闭防火墙:# service iptables stop
关闭selinux:# vi /etc/sysconfig/selinux 改“SELINUX=disabled”
解压redis-3.2.0.tar.gz
tar -zxvf redis-3.2.0.tar.gz
/root/根目录下回默认生成文件夹redis-3.2.0
#cd /redis/redis-3.2.0
#make (编译)
#make install
(貌似我编译完了就已经安装了,在执行install这个的时候没啥用,纠结是不是有问题,结果执行了一下make test 时间很长,我就去吃了点东西,回来确实没问题,如果有类似情况的小伙伴不用test了应该没问题直接往下走)
复制命令到 /usr/local/bin/ (为了快捷执行,复制前先看一下文件bin目录下 我下边已经有前三个的命令了 就只cp了redis-trib.rb)
# cp /redis/redis-3.0.0/src/redis-trib.rb /usr/local/bin/
# cp redis-cli /usr/local/bin/
# cp redis-server /usr/local/bin/
# which redis-trib.rb
/usr/local/bin/redis-trib.rb
要新建7个配置文件 其中一个是通用的配置 其他6个是集群3个master节点 3个slave 节点 推荐配置至少是6个节点 3主3从。
首先创建一个config配置目录
mkdir /root/redis-3.2.0/config
nano /root/redis-3.2.0/config/redis-common.conf
配置信息如下:
#GENERAL
daemonize yes
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
databases 16
dir /redis/redis-3.0.0/data
slave-serve-stale-data yes
slave-read-only yes
#not use default
repl-disable-tcp-nodelay yes
slave-priority 100
appendonly yes
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
cluster-enabled yes
cluster-node-timeout 15000
cluster-migration-barrier 1
slowlog-log-slower-than 10000
slowlog-max-len 128
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
#这俩个配置是我遇到的问题 第一个是关闭保护模式如果不配置在其他机器无法用jedis客户端连接时会报错并提示你关闭或者绑定访问者的ip地址还有设置密码,我是测试选择最简单的,绑定密码或ip是靠谱的
protected-mode no
#这个不是绑定访问者的ip地址 是对自己集群绑定的ip地址默认是127.0.0.1 如果不改变客户端在配置多个集群节点时会出现Too many Cluster redirections? 异常
bind 192.168.1.181 (虚拟机的外网地址)
创建6个端口号命名的文件 只需要把文件里的所有端口号的数字替换就行
redis-6379.conf
redis-6389.conf
redis-6399.conf
redis-7379.conf
redis-7389.conf
redis-7399.conf
include /root/redis-3.2.0/config/redis-common.conf
port 7379
logfile "/root/redis-3.2.0/log/redis-7379.log"
maxmemory 100m
# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key accordingly to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys-random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just return an error on write operations
maxmemory-policy allkeys-lru
appendfilename "appendonly-7379.aof"
dbfilename dump-7379.rdb
#dir /root/redis-3.2.0/data
cluster-config-file nodes-7379.conf
auto-aof-rewrite-percentage 80-100
启动redis节点
# redis-server redis-6379.conf
# redis-server redis-6389.conf
# redis-server redis-6399.conf
# redis-server redis-7379.conf
# redis-server redis-7389.conf
# redis-server redis-7399.conf
# ps -ef | grep redis
root 4746 1261 0 21:33 ? 00:00:04 redis-server 192.168.1.181:6379 [cluster]
root 4750 1261 0 21:33 ? 00:00:04 redis-server 192.168.1.181:6389 [cluster]
root 4754 1261 0 21:33 ? 00:00:04 redis-server 192.168.1.181:6399 [cluster]
root 4758 1261 0 21:33 ? 00:00:04 redis-server 192.168.1.181:7379 [cluster]
root 4762 1261 0 21:33 ? 00:00:04 redis-server 192.168.1.181:7399 [cluster]
root 4766 1261 0 21:33 ? 00:00:04 redis-server 192.168.1.181:7389 [cluster]
通过redis-trib创建cluster
–replicas 则指定了为Redis Cluster中的每个Master节点配备几个Slave 节点
# redis-trib.rb create --replicas 1 192.168.1.181:6379 192.168.1.181:6389 192.168.1.181:6399 192.168.1.181:7379 192.168.1.181:7389 192.168.1.181:7399 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.1.181:6379 192.168.1.181:6389 192.168.1.181:6399 Adding replica 192.168.1.181:7379 to 192.168.1.181:6379 Adding replica 192.168.1.181:7389 to 192.168.1.181:6389 Adding replica 192.168.1.181:7399 to 192.168.1.181:6399 M: 05fe758161e2cbe23240697f47f1cd2c937a675b 192.168.1.181:6379 slots:0-5460 (5461 slots) master M: d1d124d35c848e9c8e726b59af669c9196557869 192.168.1.181:6389 slots:5461-10922 (5462 slots) master M: d64223d6695fcc7e1030f219f09d7488c438cf39 192.168.1.181:6399 slots:10923-16383 (5461 slots) master S: 7f77ec03e40d0cc9f343d783a293ae8aa6c6e090 192.168.1.181:7379 replicates 05fe758161e2cbe23240697f47f1cd2c937a675b S: 98dae5126228dea54d1321eeb357d8773bd2ee11 192.168.1.181:7389 replicates d1d124d35c848e9c8e726b59af669c9196557869 S: d013aee7cae8163f787cb6445778ff97bf66ce17 192.168.1.181:7399 replicates d64223d6695fcc7e1030f219f09d7488c438cf39 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.1.181:6379) M: 7191b97cf9445e6e6ebaf39a4553485115ca3b97 192.168.1.181:6379 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 9ce60bf9b8019fa55050a35cd386792e6f6bb099 192.168.1.181:7399 slots: (0 slots) slave replicates 85136d72df79e0f5dda3e0efeb8bf760eb883b61 M: 85136d72df79e0f5dda3e0efeb8bf760eb883b61 192.168.1.181:6399 slots:10923-16383 (5461 slots) master 1 additional replica(s) M: 8c063f6bfd9faa655aba2c110a5c7339f5354d2d 192.168.1.181:6389 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 5a2ddc610989980701a86e0d66a9fd69b2fd8b39 192.168.1.181:7379 slots: (0 slots) slave replicates 7191b97cf9445e6e6ebaf39a4553485115ca3b97 S: 4a6abe2e57fdf5809ae52a71908275ba6287335d 192.168.1.181:7389 slots: (0 slots) slave replicates 8c063f6bfd9faa655aba2c110a5c7339f5354d2d [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
检查集群状态
连接任意节点,执行redis-trib.rb
redis-trib.rb check 192.168.1.181:6379
>>> Performing Cluster Check (using node 192.168.1.181:6379)
M: 7191b97cf9445e6e6ebaf39a4553485115ca3b97 192.168.1.181:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 9ce60bf9b8019fa55050a35cd386792e6f6bb099 192.168.1.181:7399
slots: (0 slots) slave
replicates 85136d72df79e0f5dda3e0efeb8bf760eb883b61
M: 85136d72df79e0f5dda3e0efeb8bf760eb883b61 192.168.1.181:6399
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 8c063f6bfd9faa655aba2c110a5c7339f5354d2d 192.168.1.181:6389
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 5a2ddc610989980701a86e0d66a9fd69b2fd8b39 192.168.1.181:7379
slots: (0 slots) slave
replicates 7191b97cf9445e6e6ebaf39a4553485115ca3b97
S: 4a6abe2e57fdf5809ae52a71908275ba6287335d 192.168.1.181:7389
slots: (0 slots) slave
replicates 8c063f6bfd9faa655aba2c110a5c7339f5354d2d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
到这里集群就算配置成功了
可以启动一个本地客户端操作一下
一定要加-c 才是启动集群的配置呢 指定host -h 指定端口 -p
# redis-cli -c -h 192.168.1.181 -p 6379
192.168.1.181:6379> set key001 helloReids
-> Redirected to slot [12657] located at 192.168.1.181:6399
OK //这里默认分配槽点将此键值分配到了6399 的端口上 这个是自动切换
192.168.1.181:6399> get key001
"helloReids"
192.168.1.181:6399>
说明集群部署成功了!
接下来在Java客户端 用jedis 建立集群客户端获取key001 是否好使
这里我实在自己的主机上连接虚拟机相当于外网访问。
private static JedisCluster jc;
static {
//只给集群里一个实例就可以
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
jedisClusterNodes.add(new HostAndPort("192.168.1.181", 6379));
jedisClusterNodes.add(new HostAndPort("192.168.1.181", 6389));
jedisClusterNodes.add(new HostAndPort("192.168.1.181", 6399));
jedisClusterNodes.add(new HostAndPort("192.168.1.181", 7379));
jedisClusterNodes.add(new HostAndPort("192.168.1.181", 7389));
jedisClusterNodes.add(new HostAndPort("192.168.1.181", 7399));
jc = new JedisCluster(jedisClusterNodes);
}
@Test
public void RedisClusterDemo(){
//JedisCluster jc =RedisClient.getRedisClient().getJedisCluster();
System.out.println(jc.get("key001"));
//输出helloRedis
}
再补充一点:如何关闭集群 一个一个端口的关闭
下次再启动时依然是集群方式的启动
redis-cli -h 192.168.1.181 -p 6379 shutdown
开始的时候服务器端配置绑定的127.0.0.1 结果用本机Java jedis访问时出错了,浪费了些时间,查到解决方案后,同学来了一起出去撸串一番。回来还是迫不及待的就把它弄好。
到此就算集群初步成功了,有点小兴奋,哈哈!
有朋友,有啤酒,有喜欢的东西,有向往的追求,足以!
参考:
http://blog.sina.com.cn/s/blog_75ad98f30102w6po.html
http://www.tuicool.com/articles/36F7faJ