redis集群配置
一、redis主从对应关系:
master: 192.168.2.160:7000
slave: 192.168.2.169:7001
master: 192.168.2.170:7000
slave: 192.168.2.160:7001
master: 192.168.2.169:7000
slave: 192.168.2.170:7001
二、创建工作目录
mkdir -pv /usr/local/redis-5.0.12/redis7000
mkdir -pv /usr/local/redis-5.0.12/redis7001
三、redis配置文件redis.conf
bind 192.168.2.160 protected-mode yes port 7000 tcp-backlog 128 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile /usr/local/redis-5.0.12/redis7000/redis_7000.pid loglevel notice logfile "/usr/local/redis-5.0.12/redis7000/redis.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression no rdbchecksum no dbfilename dump.rdb dir /usr/local/redis-5.0.12/redis7000/ masterauth 123.com slave-serve-stale-data yes slave-read-only yes repl-diskless-sync yes repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 cluster-enabled yes cluster-config-file /usr/local/redis-5.0.12/redis7000/nodes-7000.conf requirepass 123.com appendonly no 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-size -2 list-compress-depth 0 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 30 aof-rewrite-incremental-fsync no
三、启动redis服务(三台服务器均执行)
/usr/local/redis-5.0.12/bin/redis-server /app/redis-5.0.1/redis7000/redis.conf
/usr/local/redis-5.0.12/bin/redis-server /app/redis-5.0.1/redis7001/redis.conf
四、检查redis服务是否启动(三台服务器均执行)
ps aux |grep redis
[root@master ~]# ps aux |grep redis root 9553 0.2 0.8 156556 8196 ? Rsl Apr14 7:03 /usr/local/redis-5.0.12/bin/redis-server 192.168.2.160:7000 [cluster] root 9558 0.2 1.0 162700 10252 ? Rsl Apr14 7:02 /usr/local/redis-5.0.12/bin/redis-server 192.168.2.160:7001 [cluster]
五、创建集群(在3台主机的任意一台设备执行集群命令即可)
1、创建主节点: /usr/local/redis-5.0.12/bin/redis-cli -a 123.com --cluster create --cluster-replicas 0 30.1.2.160:7000 30.1.2.169:7000 30.1.2.170:7000 2、记录创建主节点时打印的master id: M: 13b6d3b4a47ed00e12cc09b261f7addae7212365 192.168.2.169:7000 slots:[0-5460] (5461 slots) master M: 0ac92d48da91c14fb7f8ec00d17d898ec02fa003 192.168.2.170:7000 slots:[10923-16383] (5461 slots) master M: cc5054b3b51ea9a49adc569809ec942d12d2810f 192.168.2.160:7000 slots:[5461-10922] (5462 slots) master 3、为主节点添加从节点: /usr/local/redis-5.0.1/bin/redis-cli -a 123.com --cluster add-node 30.1.2.169:7001 30.1.2.160:7000 --cluster-slave --cluster-master-id 13b6d3b4a47ed00e12cc09b261f7addae7212365 /usr/local/redis-5.0.1/bin/redis-cli -a 123.com --cluster add-node 30.1.2.160:7001 30.1.2.170:7000 --cluster-slave --cluster-master-id 0ac92d48da91c14fb7f8ec00d17d898ec02fa003 /usr/local/redis-5.0.1/bin/redis-cli -a 123.com --cluster add-node 30.1.2.170:7001 30.1.2.169:7000 --cluster-slave --cluster-master-id cc5054b3b51ea9a49adc569809ec942d12d2810f
六、验证集群
/usr/local/redis-5.0.12/bin/redis-cli -h 192.168.2.160 -p 7000 -a 123.com info Replication
/usr/local/redis-5.0.12/bin/redis-cli -h 192.168.2.169 -p 7000 -a 123.com info Replication
/usr/local/redis-5.0.12/bin/redis-cli -h 192.168.2.170 -p 7000 -a 123.com info Replication
/usr/local/redis-5.0.12/bin/redis-cli --cluster check 192.168.2.160:7000 -a 123.com --cluster-search-multiple-owners
七、参数介绍
redis-cli --cluster help Cluster Manager Commands: create host1:port1 ... hostN:portN #创建集群 --cluster-replicas <arg> #从节点个数 check host:port #检查集群 --cluster-search-multiple-owners #检查是否有槽同时被分配给了多个节点 info host:port #查看集群状态 fix host:port #修复集群 --cluster-search-multiple-owners #修复槽的重复分配问题 reshard host:port #指定集群的任意一节点进行迁移slot,重新分slots --cluster-from <arg> #需要从哪些源节点上迁移slot,可从多个源节点完成迁移,以逗号隔开,传递的是节点的node id,还可以直接传递--from all,这样源节点就是集群的所有节点,不传递该参数的话,则会在迁移过程中提示用户输入 --cluster-to <arg> #slot需要迁移的目的节点的node id,目的节点只能填写一个,不传递该参数的话,则会在迁移过程中提示用户输入 --cluster-slots <arg> #需要迁移的slot数量,不传递该参数的话,则会在迁移过程中提示用户输入。 --cluster-yes #指定迁移时的确认输入 --cluster-timeout <arg> #设置migrate命令的超时时间 --cluster-pipeline <arg> #定义cluster getkeysinslot命令一次取出的key数量,不传的话使用默认值为10 --cluster-replace #是否直接replace到目标节点 rebalance host:port #指定集群的任意一节点进行平衡集群节点slot数量 --cluster-weight <node1=w1...nodeN=wN> #指定集群节点的权重 --cluster-use-empty-masters #设置可以让没有分配slot的主节点参与,默认不允许 --cluster-timeout <arg> #设置migrate命令的超时时间 --cluster-simulate #模拟rebalance操作,不会真正执行迁移操作 --cluster-pipeline <arg> #定义cluster getkeysinslot命令一次取出的key数量,默认值为10 --cluster-threshold <arg> #迁移的slot阈值超过threshold,执行rebalance操作 --cluster-replace #是否直接replace到目标节点 add-node new_host:new_port existing_host:existing_port #添加节点,把新节点加入到指定的集群,默认添加主节点 --cluster-slave #新节点作为从节点,默认随机一个主节点 --cluster-master-id <arg> #给新节点指定主节点 del-node host:port node_id #删除给定的一个节点,成功后关闭该节点服务 call host:port command arg arg .. arg #在集群的所有节点执行相关命令 set-timeout host:port milliseconds #设置cluster-node-timeout import host:port #将外部redis数据导入集群 --cluster-from <arg> #将指定实例的数据导入到集群 --cluster-copy #migrate时指定copy --cluster-replace #migrate时指定replace