一、环境
系统 CentOS7.0 64位最小化安装
redis1 172.16.1.46 6379,6380
redis2 172.16.1.47 6379,6380
redis3 172.16.1.47 6379,6380
二、基础软件安装
[root@redis1 ~]# yum install vim wget tree ntp net-tools lsof gcc* -y [root@redis1 ~]# yum -y install gcc openssl-devel libyaml-devel libffi-devel readline-devel zlib-devel gdbm-devel ncurses-devel gcc-c++ automake autoconf libxml2* rubygem-nokogiri
三、安装ruby
[root@redis1 ~]# yum install ruby rubygems ruby-devel -y #删除ruby的默认源 [root@redis1 ~]# gem source --remove https://rubygems.org/ https://rubygems.org/ removed from sources #添加源 [root@redis1 ~]# gem sources -a https://ruby.taobao.org https://ruby.taobao.org added to sources #安装rails [root@redis1 ~]# gem install rails #安装redis和ruby的接口 [root@redis1 ~]# gem install redis
四、安装redis
#下载redis [root@redis1 ~]# wget http://download.redis.io/releases/redis-3.0.1.tar.gz [root@redis1 ~]# tar xf redis-3.0.1.tar.gz -C /usr/local/ [root@redis1 ~]# ln -sv /usr/local/redis-3.0.1 /usr/local/redis ‘/usr/local/redis’ -> ‘/usr/local/redis-3.0.1’ [root@redis1 ~]# cd /usr/local/redis [root@redis1 redis]# make [root@redis1 redis]# make install [root@redis1 ~]# cp /usr/local/redis/redis.conf /usr/local/redis6379.conf #编辑6379的配置文件 [root@redis1 ~]# vim /usr/local/redis/redis6379.conf port 6379 pidfile /var/run/redis6379.pid dbfilename dump6379.rdb appendfilename "appendonly-6379.aof" cluster-config-file nodes-6379.conf cluster-node-timeout 15000 cluster-enabled yes appendonly yes dir /usr/local/redis/data/6379 #编辑6380的配置文件 [root@redis1 ~]# cp /usr/local/redis/redis6379.conf /usr/local/redis/redis6380.conf [root@redis1 ~]# sed -i 's#6379#6380#g' /usr/local/redis/redis6380.conf #创建相应的目录 [root@redis1 ~]# mkdir -p /usr/local/redis/data/{6379,6380} #创建6379的启动脚本 [root@redis1 ~]# cat /etc/init.d/redis6379 #!/bin/bash # chkconfig: 2345 50 30 # # description: Redis service # #Script:Redis command Redisserver=/usr/local/bin/redis-server Rediscli=/usr/local/bin/redis-cli Redisconf=/usr/local/redis/redis6379.conf function_start() { printf "start redis-server..." $Redisserver $Redisconf &>/dev/null & if [ $? -eq 0 ];then echo "runing" fi } function_stop() { printf "stop redis-server..." $Rediscli -p 6379 shutdown if [ $? -eq 0 ];then echo "stop" fi } function_restart() { function_start function_stop } function_kill() { killall redis-server } function_status() { a=`ps -A|grep "redis-server\>" -c` if [ $a -ge 1 ];then echo -e "The Redis is [\e[0;32;5m runing \e[0m]" else echo -e "The Redis is [\e[0;31;5m not run \e[0m]" fi } case "$1" in start) function_start ;; stop) function_stop ;; restart) function_stop function_start ;; kill) function_kill ;; status) function_status ;; *) echo "Usage: /etc/init.d/redis {start|stop|restart|kill|status}" esac exit #赋予执行权限 [root@redis1 ~]# chmod +x /etc/init.d/redis6379 [root@redis1 ~]# /etc/init.d/redis6379 start start redis-server...runing [root@redis1 ~]# netstat -tunlp |grep redis tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 5631/redis-server * #创建6380的启动脚本 [root@redis1 ~]# cat /etc/init.d/redis6380 #!/bin/bash # chkconfig: 2345 50 30 # # description: Redis service # #Script:Redis command Redisserver=/usr/local/bin/redis-server Rediscli=/usr/local/bin/redis-cli Redisconf=/usr/local/redis/redis6380.conf function_start() { printf "start redis-server..." $Redisserver $Redisconf &>/dev/null & if [ $? -eq 0 ];then echo "runing" fi } function_stop() { printf "stop redis-server..." $Rediscli -p 6380 shutdown if [ $? -eq 0 ];then echo "stop" fi } function_restart() { function_start function_stop } function_kill() { killall redis-server } function_status() { a=`ps -A|grep "redis-server\>" -c` if [ $a -ge 1 ];then echo -e "The Redis is [\e[0;32;5m runing \e[0m]" else echo -e "The Redis is [\e[0;31;5m not run \e[0m]" fi } case "$1" in start) function_start ;; stop) function_stop ;; restart) function_stop function_start ;; kill) function_kill ;; status) function_status ;; *) echo "Usage: /etc/init.d/redis {start|stop|restart|kill|status}" esac exit #赋予执行权限并启动6380 [root@redis1 ~]# chmod +x /etc/init.d/redis6380 [root@redis1 ~]# /etc/init.d/redis6380 start start redis-server...runing [root@redis1 ~]# netstat -tunlp |grep redis tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 5646/redis-server * tcp 0 0 0.0.0.0:6380 0.0.0.0:* LISTEN 5652/redis-server * #其余2台执行同样的操作
五、配置集群
#在任意一台机器上操作 [root@redis1 ~]# /usr/local/redis/src/redis-trib.rb create --replicas 1 172.16.1.46:6379 172.16.1.46:6380 172.16.1.47:6379 172.16.1.47:6380 172.16.1.48:6379 172.16.1.48:6380 >>> Creating cluster Connecting to node 172.16.1.46:6379: OK Connecting to node 172.16.1.46:6380: OK Connecting to node 172.16.1.47:6379: OK Connecting to node 172.16.1.47:6380: OK Connecting to node 172.16.1.48:6379: OK Connecting to node 172.16.1.48:6380: OK >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 172.16.1.46:6379 172.16.1.47:6379 172.16.1.48:6379 Adding replica 172.16.1.47:6380 to 172.16.1.46:6379 Adding replica 172.16.1.46:6380 to 172.16.1.47:6379 Adding replica 172.16.1.48:6380 to 172.16.1.48:6379 M: 3c2279ff6333b98a233ebf541e2b75981d27751f 172.16.1.46:6379 slots:0-5460 (5461 slots) master S: eb0b79b22797c14172c8f0f117a4839d6825ea1f 172.16.1.46:6380 replicates dd122d63def65b8bc62719cabbb468f7976b1b73 M: dd122d63def65b8bc62719cabbb468f7976b1b73 172.16.1.47:6379 slots:5461-10922 (5462 slots) master S: c8136c436cec8146dbbbd1ff2e5468d1dd5dd195 172.16.1.47:6380 replicates 3c2279ff6333b98a233ebf541e2b75981d27751f M: a0cf20f0ea29760dfdb3e4748417efaefc0c37f0 172.16.1.48:6379 slots:10923-16383 (5461 slots) master S: c6df03cde4424b2b09752c89d236ee1cd4834709 172.16.1.48:6380 replicates a0cf20f0ea29760dfdb3e4748417efaefc0c37f0 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 172.16.1.46:6379) M: 3c2279ff6333b98a233ebf541e2b75981d27751f 172.16.1.46:6379 slots:0-5460 (5461 slots) master M: eb0b79b22797c14172c8f0f117a4839d6825ea1f 172.16.1.46:6380 slots: (0 slots) master replicates dd122d63def65b8bc62719cabbb468f7976b1b73 M: dd122d63def65b8bc62719cabbb468f7976b1b73 172.16.1.47:6379 slots:5461-10922 (5462 slots) master M: c8136c436cec8146dbbbd1ff2e5468d1dd5dd195 172.16.1.47:6380 slots: (0 slots) master replicates 3c2279ff6333b98a233ebf541e2b75981d27751f M: a0cf20f0ea29760dfdb3e4748417efaefc0c37f0 172.16.1.48:6379 slots:10923-16383 (5461 slots) master M: c6df03cde4424b2b09752c89d236ee1cd4834709 172.16.1.48:6380 slots: (0 slots) master replicates a0cf20f0ea29760dfdb3e4748417efaefc0c37f0 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. #检查集群状态 [root@redis1 ~]# /usr/local/redis/src/redis-trib.rb check 127.0.0.1:6379 Connecting to node 127.0.0.1:6379: OK Connecting to node 172.16.1.48:6379: OK Connecting to node 172.16.1.48:6380: OK Connecting to node 172.16.1.47:6379: OK Connecting to node 172.16.1.47:6380: OK Connecting to node 172.16.1.46:6380: OK >>> Performing Cluster Check (using node 127.0.0.1:6379) M: 3c2279ff6333b98a233ebf541e2b75981d27751f 127.0.0.1:6379 slots:0-5460 (5461 slots) master 1 additional replica(s) M: a0cf20f0ea29760dfdb3e4748417efaefc0c37f0 172.16.1.48:6379 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: c6df03cde4424b2b09752c89d236ee1cd4834709 172.16.1.48:6380 slots: (0 slots) slave replicates a0cf20f0ea29760dfdb3e4748417efaefc0c37f0 M: dd122d63def65b8bc62719cabbb468f7976b1b73 172.16.1.47:6379 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: c8136c436cec8146dbbbd1ff2e5468d1dd5dd195 172.16.1.47:6380 slots: (0 slots) slave replicates 3c2279ff6333b98a233ebf541e2b75981d27751f S: eb0b79b22797c14172c8f0f117a4839d6825ea1f 172.16.1.46:6380 slots: (0 slots) slave replicates dd122d63def65b8bc62719cabbb468f7976b1b73 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. #测试集群,添加一个值 [root@redis1 ~]# /usr/local/bin/redis-cli -c -p 6379 127.0.0.1:6379> set a a -> Redirected to slot [15495] located at 172.16.1.48:6379 #可以看到值添加到了172.16.1.48:6379这个节点上 OK 172.16.1.48:6379> get a "a" #关闭172.16.1.48:6379这个节点 [root@redis3 redis]# /etc/init.d/redis6379 stop stop redis-server...stop [root@redis3 redis]# netstat -tunlp |grep 6379 #再次连接到集群上 [root@redis1 ~]# /usr/local/bin/redis-cli -c -p 6379 127.0.0.1:6379> get a -> Redirected to slot [15495] located at 172.16.1.48:6380 "a" #这里能看到之前设置的值,从172.16.1.48:6380拿到结果了 #集群配置到此结束