redis集群部署和搭建
-
redis集群部署和搭建
### 安装集群插件 yum install -y ruby rubygems -y gem sources -l gem sources -a http://mirrors.aliyun.com/rubygems/ gem sources --remove http://rubygems.org/ gem install redis -v 3.3.3 gem sources -l
集群节点准备
mkdir /data/700{0..5} cd /data/7000 vim redis.conf port 7000 daemonize yes pidfile "/data/7000/redis.pid" loglevel notice logfile "/data/7000/redis.log" dbfilename "dump.rdb" dir "/data/7000" protected-mode no cluster-enabled yes ### 是否打开集群功能 cluster-config-file nodes.conf ### 集群的存放配置文件 cluster-node-timeout 5000 ### 集群的请求超时时间 appendonly yes ### 开启aof持久化功能 vim /data/7001/redis.conf port 7001 daemonize yes pidfile "/data/7001/redis.pid" loglevel notice logfile "/data/7001/redis.log" dbfilename "dump.rdb" dir "/data/7001" protected-mode no cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes vim /data/7002/redis.conf port 7002 daemonize yes pidfile "/data/7002/redis.pid" loglevel notice logfile "/data/7002/redis.log" dbfilename "dump.rdb" dir "/data/7002" protected-mode no cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes vim /data/7003/redis.conf port 7003 daemonize yes pidfile "/data/7003/redis.pid" loglevel notice logfile "/data/7003/redis.log" dbfilename "dump.rdb" dir "/data/7003" protected-mode no cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes vim /data/7004/redis.conf port 7004 daemonize yes pidfile "/data/7004/redis.pid" loglevel notice logfile "/data/7004/redis.log" dbfilename "dump.rdb" dir "/data/7004" protected-mode no cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes vim /data/7005/redis.conf port 7005 daemonize yes pidfile "/data/7005/redis.pid" loglevel notice logfile "/data/7005/redis.log" dbfilename "dump.rdb" dir "/data/7005" protected-mode no cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes
使用vim的时候可以使用如下方式进行快速替换文本中的文件
:%s/7000/7001/g #### 将文件中的所有7000替换成7001
启动redis所有节点
[root@k8s-master 7005]# redis-server /data/7000/redis.conf [root@k8s-master 7005]# redis-server /data/7002/redis.conf [root@k8s-master 7005]# redis-server /data/7001/redis.conf [root@k8s-master 7005]# redis-server /data/7003/redis.conf [root@k8s-master 7005]# redis-server /data/7004/redis.conf [root@k8s-master 7005]# redis-server /data/7005/redis.conf [root@k8s-master 7005]# ps -ef|grep redis root 22736 12989 0 14:37 pts/0 00:00:07 redis-server 127.0.0.1:6379 root 23907 1 0 14:49 ? 00:00:07 redis-server *:6382 root 25210 12989 0 15:03 pts/0 00:00:15 redis-sentinel *:26380 [sentinel] root 26279 1 0 15:14 ? 00:00:06 redis-server *:6380 root 26650 1 0 15:17 ? 00:00:06 redis-server *:6381 root 35106 1 0 16:40 ? 00:00:00 redis-server *:7000 [cluster] root 35114 1 0 16:41 ? 00:00:00 redis-server *:7002 [cluster] root 35127 1 0 16:41 ? 00:00:00 redis-server *:7001 [cluster] root 35139 1 0 16:41 ? 00:00:00 redis-server *:7003 [cluster] root 35147 1 0 16:41 ? 00:00:00 redis-server *:7004 [cluster] root 35155 1 0 16:41 ? 00:00:00 redis-server *:7005 [cluster]
-
创建集群
redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 ### create 创建 replicas 副本一个 前3个节点会被认为是主节点 后3个节点会被认为是从节点
集群状态查看
集群主节点状态 redis-cli -p 7000 cluster nodes | grep master 集群从节点状态 redis-cli -p 7000 cluster nodes | grep slave
redis集群节点管理
### 增加新的节点 mkdir /data/7006 mkdir /data/7007 vim /data/7006/redis.conf port 7006 daemonize yes pidfile "/data/7006/redis.pid" loglevel notice logfile "/data/7006/redis.log" dbfilename "dump.rdb" dir "/data/7006" protected-mode no cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes vim /data/7007/redis.conf port 7007 daemonize yes pidfile "/data/7007/redis.pid" loglevel notice logfile "/data/7007/redis.log" dbfilename "dump.rdb" dir "/data/7007" protected-mode no cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes ### redis7006-7007启动节点 [root@k8s-master 7007]# redis-server /data/7006/redis.conf [root@k8s-master 7007]# redis-server /data/7007/redis.conf ### 查看节点是否启动成功 [root@k8s-master 7007]# ps -ef |grep redis root 22736 12989 0 14:37 pts/0 00:00:09 redis-server 127.0.0.1:6379 root 23907 1 0 14:49 ? 00:00:09 redis-server *:6382 root 25210 12989 0 15:03 pts/0 00:00:20 redis-sentinel *:26380 [sentinel] root 26279 1 0 15:14 ? 00:00:08 redis-server *:6380 root 26650 1 0 15:17 ? 00:00:08 redis-server *:6381 root 35106 1 0 16:40 ? 00:00:02 redis-server *:7000 [cluster] root 35114 1 0 16:41 ? 00:00:02 redis-server *:7002 [cluster] root 35127 1 0 16:41 ? 00:00:02 redis-server *:7001 [cluster] root 35139 1 0 16:41 ? 00:00:02 redis-server *:7003 [cluster] root 35147 1 0 16:41 ? 00:00:02 redis-server *:7004 [cluster] root 35155 1 0 16:41 ? 00:00:02 redis-server *:7005 [cluster] root 38225 1 0 17:13 ? 00:00:00 redis-server *:7006 [cluster] #### 启动成功 root 38237 1 0 17:13 ? 00:00:00 redis-server *:7007 [cluster] #### 启动成功 root 38299 12989 0 17:13 pts/0 00:00:00 grep --color=auto redis
添加主节点
redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000 #### 将7006加入到集群中
转移slot(重新分片)
redis-trib.rb reshard 127.0.0.1:7000
4096
7006的id
all
添加一个从节点
###注意先获取7006的node id redis-cli -p 7000 cluster nodes | grep master redis-trib.rb add-node --slave --master-id 5daee30881a4d1b2ee3e31ae7ff04de42c8690e4 127.0.0.1:7007 127.0.0.1:7000 ###5daee30881a4d1b2ee3e31ae7ff04de42c8690e4 为7006的主节点上添加从节点 这里的node id写的是7006的id号码 后面的7007代表是从节点
删除节点
### 将需要的删除的节点slot移动走 redis-trib.rb reshard 127.0.0.1:7000 #### 删除一个节点 redis-trib.rb del-node 127.0.0.1:7005 4096 ###移动走的槽位 a60677f89e2c684549fda4f8182a55ccd10514e2 #### 7000的id号码 接受的节点 7006的id号码 ####需要移动走的id 节点 done yes [root@k8s-master 7007]# redis-cli -p 7000 cluster nodes | grep master a60677f89e2c684549fda4f8182a55ccd10514e2 127.0.0.1:7000 myself,master - 0 0 8 connected 0-6826 10923-12287 5daee30881a4d1b2ee3e31ae7ff04de42c8690e4 127.0.0.1:7006 master - 0 1550743463647 7 connected ###槽位为空 92fad7817b2093c0dfa7727cd47e773440dd2ed3 127.0.0.1:7001 master - 0 1550743463140 2 connected 6827-10922 4407a7899197370a3a81e38bef541d5264bfc96a 127.0.0.1:7002 master - 0 1550743464189 3 connected 12288-16383 ###删除一个节点 删除一个节点之前先看一下这个节点的node id号码 redis-cli -p 7000 cluster nodes 6c93febac43e385c48159ff43dee82df3857cdc7 127.0.0.1:7005 slave 4407a7899197370a3a81e38bef541d5264bfc96a 0 1550743883143 6 connected f8cccf3137eb2142618070dede315335b26abd11 127.0.0.1:7004 slave 92fad7817b2093c0dfa7727cd47e773440dd2ed3 0 1550743882129 5 connected 69961281e2e8a31e1c903767dc502e8be03e1cd6 127.0.0.1:7007 slave a60677f89e2c684549fda4f8182a55ccd10514e2 0 1550743882130 8 connected a60677f89e2c684549fda4f8182a55ccd10514e2 127.0.0.1:7000 myself,master - 0 0 8 connected 0-6826 10923-12287 5daee30881a4d1b2ee3e31ae7ff04de42c8690e4 127.0.0.1:7006 master - 0 1550743882639 7 connected 92fad7817b2093c0dfa7727cd47e773440dd2ed3 127.0.0.1:7001 master - 0 1550743883649 2 connected 6827-10922 4407a7899197370a3a81e38bef541d5264bfc96a 127.0.0.1:7002 master - 0 1550743883144 3 connected 12288-16383 c13a3baca507d933c53c880d304c679ef107ac71 127.0.0.1:7003 slave a60677f89e2c684549fda4f8182a55ccd10514e2 0 1550743884152 8 connected 69961281e2e8a31e1c903767dc502e8be03e1cd6 127.0.0.1:7007 5daee30881a4d1b2ee3e31ae7ff04de42c8690e4 127.0.0.1:7006 redis-trib.rb del-node 127.0.0.1:7007 69961281e2e8a31e1c903767dc502e8be03e1cd6 redis-trib.rb del-node 127.0.0.1:7006 5daee30881a4d1b2ee3e31ae7ff04de42c8690e4