想跑一下Redis集群,但是没有那么多服务器,所以使用伪分布式模式,模拟一下,记录一下安装过程。
软件: redis-3.0.3.tar.gz
集群正常工作至少需要3个主节点(本示例创建6个节点,3主3从节点)
安装前提,要有ruby、rubygems环境,先安装一下:
#安装ruby yum --nogpgcheck -y install ruby yum --nogpgcheck -y install ruby-devel.x86_64 #安装rubygem yum --nogpgcheck -y install rubygems #安装Ruby的redis接口gem-redis
安装Redis软件
#进入安装目录 cd /usr/local/ rm -rf redis rm -rf redis-3.0.3 #解压 tar -zxvf redis-3.0.3.tar.gz mv redis-3.0.3 redis #进入源码目录 cd redis #编译安装 make && make install
创建集群的相关配置
#创建集群需要的目录 mkdir -p /usr/local/redis/cluster/16001/ mkdir -p /usr/local/redis/cluster/16002/ mkdir -p /usr/local/redis/cluster/16003/ mkdir -p /usr/local/redis/cluster/16004/ mkdir -p /usr/local/redis/cluster/16005/ mkdir -p /usr/local/redis/cluster/16006/ #修改配置文件redis.conf #cp /usr/local/redis/redis.conf /usr/local/redis/cluster/16001/redis.conf #cp /usr/local/redis/redis.conf /usr/local/redis/cluster/16002/redis.conf #cp /usr/local/redis/redis.conf /usr/local/redis/cluster/16003/redis.conf #cp /usr/local/redis/redis.conf /usr/local/redis/cluster/16004/redis.conf #cp /usr/local/redis/redis.conf /usr/local/redis/cluster/16005/redis.conf #cp /usr/local/redis/redis.conf /usr/local/redis/cluster/16006/redis.conf
配置文件示例:
daemonize no pidfile /var/run/redis.pid #修改端口 port 16001 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile "" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir ./ slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 #修改为yes appendonly yes appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 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 hll-sparse-max-bytes 3000 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 #=========================新增=============================== daemonize yes cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 #========================================================
启动Redis实例:
#分别启动这6个redis实例(此时,节点均以Redis Cluster的方式运行,但并没有自动构建集群,因为还处于“我不认识你,你不属于我”的状态,它们每个都是孤零零的Redis节点,或者说只包含了一个节点的集群) cd /usr/local/redis/cluster/16001 && redis-server redis.conf > redis.log 2>&1 & cd /usr/local/redis/cluster/16002 && redis-server redis.conf > redis.log 2>&1 & cd /usr/local/redis/cluster/16003 && redis-server redis.conf > redis.log 2>&1 & cd /usr/local/redis/cluster/16004 && redis-server redis.conf > redis.log 2>&1 & cd /usr/local/redis/cluster/16005 && redis-server redis.conf > redis.log 2>&1 & cd /usr/local/redis/cluster/16006 && redis-server redis.conf > redis.log 2>&1 &
##启动之后使用命令查看redis的启动情况
ps -ef|grep redis
创建集群,让上面的实例互相通讯
#创建集群,让上面的实例互相通讯(1 表示为每个master分配一个salve) /usr/local/redis/src/redis-trib.rb create --replicas 1 127.0.0.1:16001 127.0.0.1:16002 127.0.0.1:16003 127.0.0.1:16004 127.0.0.1:16005 127.0.0.1:16006
集群创建完成,可以使用下面的命令查看相关信息:
#查看集群目前状况 redis-cli -c -p 16001 #打印集群的信息 cluster info #列出集群当前已知的所有节点(node),以及这些节点的相关信息。 cluster nodes
输出的信息:
127.0.0.1:16002 master - 0 1439345771781 2 connected 5461-10922 127.0.0.1:16003 master - 0 1439345772286 3 connected 10923-16383 127.0.0.1:16001 myself,master - 0 0 1 connected 0-5460 127.0.0.1:16004 slave 2566ea486fc30c911aafaf1b71130fd24a38dba9 0 1439345772791 4 connected 127.0.0.1:16005 slave edea1e50bd224c6895b1904bce79e83fa07d6017 0 1439345772286 5 connected 127.0.0.1:16006 slave ab06e033698627ce0ecd4c8c645585a1ae70cc84 0 1439345771276 6 connected
从上面的信息可以明显看到哪些是主节点、从节点。
#将节点的配置文件保存到硬盘里
cluster saveconfig
#------------------------测试-----------------------------------
#连接端口(可以使用其他端口测试16001 -> 16006)
redis-cli -c -p 16001
127.0.0.1:16001> set key001 v001
127.0.0.1:16001> get key001
正常输出信息的话,集群建立成功,集群搭建完成。
祝好运!!
时间: 2024-10-11 05:34:53