[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core)
./redis-cli 127.0.0.1:6379> set ab b OK 127.0.0.1:6379> get ab "b" 127.0.0.1:6379> keys * "ab" "foo"
#!/bin/sh #chkconfig: 2345 90 10 #description: Redis is a persistent key-value database
cp /usr/local/bin/* /usr/local/redisCluster/redis01/ cp /usr/local/bin/* /usr/local/redisCluster/redis02/ cp /usr/local/bin/* /usr/local/redisCluster/redis03/ cp /usr/local/bin/* /usr/local/redisCluster/redis04/ cp /usr/local/bin/* /usr/local/redisCluster/redis05/ cp /usr/local/bin/* /usr/local/redisCluster/redis06/
vi /usr/local/redisCluster/redis01/redis.conf 第1處: port 6379 ————> 值 改爲 7001 第2處: daemonize no ————> 值 改爲 yes 第3處: pidfile /var/run/redis_6379.pid ————> 值 改爲 ./redis_7001.pid 第4處: logfile "" ————> 值 改爲 "./logs/redis_7001.log" 第5處: bind 127.0.0.1 ————> 值 改爲 本機IP(如我這裏是192.168.25.129) 第6處: protected-mode yes ————> 值 改爲 no 第7處: # cluster-enabled yes ————> 去掉註釋 第8處: # cluster-config-file nodes-6379.conf ————> 去掉註釋,並把數字改爲對應端口號,如這裏是7001 第9處: # cluster-node-timeout 15000 ————> 去掉註釋 第10處: appendonly no ————> 值 改爲 yes
touch /usr/local/redisCluster/startCluster.sh touch /usr/local/redisCluster/shutdownCluster.sh touch /usr/local/redisCluster/createCluster.sh chmod 777 /usr/local/redisCluster/*.sh
startcluster.sh 內容以下: node
/usr/local/rediscluster/redis01/redis-server /usr/local/rediscluster/redis01/7001.conf /usr/local/rediscluster/redis02/redis-server /usr/local/rediscluster/redis02/7002.conf /usr/local/rediscluster/redis03/redis-server /usr/local/rediscluster/redis03/7003.conf /usr/local/rediscluster/redis04/redis-server /usr/local/rediscluster/redis04/7004.conf /usr/local/rediscluster/redis05/redis-server /usr/local/rediscluster/redis05/7005.conf /usr/local/rediscluster/redis06/redis-server /usr/local/rediscluster/redis06/7006.conf
shutdowncluster.sh 內容以下: redis
/usr/local/rediscluster/redis01/redis-cli -c -h 10.11.1.103 -p 7001 shutdown /usr/local/rediscluster/redis02/redis-cli -c -h 10.11.1.103 -p 7002 shutdown /usr/local/rediscluster/redis03/redis-cli -c -h 10.11.1.103 -p 7003 shutdown /usr/local/rediscluster/redis04/redis-cli -c -h 10.11.1.103 -p 7004 shutdown /usr/local/rediscluster/redis05/redis-cli -c -h 10.11.1.103 -p 7005 shutdown /usr/local/rediscluster/redis06/redis-cli -c -h 10.11.1.103 -p 7006 shutdown
createcluster.sh 內容以下: ruby redis-trib.rb create --replicas 1 10.11.1.103:7001 10.11.1.103:7002 10.11.1.103:7003 10.11.1.103:7004 10.11.1.103:7005 10.11.1.103:7006
數據庫
cd /usr/local/redisCluster/ ./startCluster.sh ./createcluster.sh(輸入yes)
[root@localhost rediscluster]# ps -ef | grep redis|grep -v grep root 23716 1 0 Aug08 ? 00:10:55 /usr/local/rediscluster/redis01/redis-server *:7001 [cluster] root 23721 1 0 Aug08 ? 00:10:53 /usr/local/rediscluster/redis02/redis-server *:7002 [cluster] root 23723 1 0 Aug08 ? 00:10:51 /usr/local/rediscluster/redis03/redis-server *:7003 [cluster] root 23731 1 0 Aug08 ? 00:13:37 /usr/local/rediscluster/redis04/redis-server *:7004 [cluster] root 23736 1 0 Aug08 ? 00:10:55 /usr/local/rediscluster/redis05/redis-server *:7005 [cluster] root 23741 1 0 Aug08 ? 00:08:59 /usr/local/rediscluster/redis06/redis-server *:7006 [cluster]
cd /usr/local/redisCluster/redis01/ ./redis-cli -h 10.11.1.103 -p 7001 -c 10.11.1.103:7001> set aaaa b OK 10.11.1.103:7002> get aaaa -> Redirected to slot [1953] located at 10.11.1.103:7001 "b" 10.11.1.103:7001> cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_ping_sent:367555 cluster_stats_messages_pong_sent:342015 cluster_stats_messages_publish_sent:903835 cluster_stats_messages_sent:1613405 cluster_stats_messages_ping_received:342015 cluster_stats_messages_pong_received:367550 cluster_stats_messages_publish_received:1084495 cluster_stats_messages_received:1794060 10.11.1.103:7001>
protected-mode no port 27001 sentinel deny-scripts-reconfig yes sentinel monitor **mymaster1** 10.11.1.103 7001 2 sentinel down-after-milliseconds **mymaster1** 10000 logfile "/var/log/sentinel1.log" dir "/usr/local/rediscluster/redis01" sentinel failover-timeout **mymaster1** 15000 sentinel config-epoch **mymaster1** 0 sentinel leader-epoch **mymaster1** 0 sentinel known-slave **mymaster1 10.11.1.103 7004** sentinel current-epoch 0
/usr/local/rediscluster/redis01/redis-sentinel /usr/local/rediscluster/redis01/sentinel.conf & /usr/local/rediscluster/redis02/redis-sentinel /usr/local/rediscluster/redis02/sentinel.conf & /usr/local/rediscluster/redis03/redis-sentinel /usr/local/rediscluster/redis03/sentinel.conf &
stopsentinel.sh 腳本的內容以下: ruby
kill -9 `ps -ef | grep sentinel| grep -v grep|awk '{print $2}'`
[root@localhost rediscluster]# ps -ef | grep redis|grep -v grep root 23716 1 0 Aug08 ? 00:10:55 /usr/local/rediscluster/redis01/redis-server *:7001 [cluster] root 23721 1 0 Aug08 ? 00:10:53 /usr/local/rediscluster/redis02/redis-server *:7002 [cluster] root 23723 1 0 Aug08 ? 00:10:51 /usr/local/rediscluster/redis03/redis-server *:7003 [cluster] root 23731 1 0 Aug08 ? 00:13:37 /usr/local/rediscluster/redis04/redis-server *:7004 [cluster] root 23736 1 0 Aug08 ? 00:10:55 /usr/local/rediscluster/redis05/redis-server *:7005 [cluster] root 23741 1 0 Aug08 ? 00:08:59 /usr/local/rediscluster/redis06/redis-server *:7006 [cluster] root 23924 1 0 Aug08 ? 00:09:55 /usr/local/rediscluster/redis01/redis-sentinel *:27001 [sentinel] root 23925 1 0 Aug08 ? 00:09:38 /usr/local/rediscluster/redis02/redis-sentinel *:27002 [sentinel] root 23926 1 0 Aug08 ? 00:10:22 /usr/local/rediscluster/redis03/redis-sentinel *:27003 [sentinel]
yum -y install gcc ## make install 命令須要此環境 yum -y intall ruby ## 安裝ruby(此時安裝的可能使低版本的) curl -L get.rvm.io | bash -s stable ## 安裝rvm source /usr/local/rvm/scripts/rvm ## 使安裝的rvm當即生效 rvm install 2.4.1 ## 經過rvm來安裝ruby高級版本 gem install redis ## 經過ruby的工具包gem來安裝redis接口