项目中要用到redis集群,我在测试环境上搭建了redis 的集群。
我这里用的是redis-4.0.11.tar.gz 的下载地址: https://redis.io/download
执行下面的命令下载即可
关闭防火墙 节点之前需要开放指定端口,为了方便,生产不要禁用
centos 6.x
$ service iptables stop # 关闭命令:
centos 7.x
$ systemctl stop firewalld.service # 停止firewall(一)
$ systemctl stop firewalld ##关闭防火墙 (二)
$ firewall-cmd --state ##查看防火墙状态 running
not running(看到这个表示禁用了)
10.xx.xx.111 、10.xx.xx.112 两台机器
角色 |
IP&port |
master |
10.xx,xx.111:7000 |
slave |
10.xx.xx.111:7001 |
slave |
10.xx.xx.111:7002 |
master |
10.xx.xx.112:7003 |
slave |
10.xx.xx.112:7004 |
slave |
10.xx.xx.112:7005 |
下载,解压,编译:
$ wget http://download.redis.io/releases/redis-4.0.10.tar.gz
$ tar xzf redis-4.0.10.tar.gz
$ cd redis-4.0.10
$ make
先执行上面的操作,如果出现报gcc错误,则需要安裝 GCC 编译工具 不然会有编译不过的问题
yum install -y gcc
1.首先在 10.xx.xx.111机器上 /opt/redis-4.0.1目录下创建 redis-cluster 目录
$ mkdir /opt/redis-4.0.1/redis-cluster
2.在 redis-cluster 目录下,创建名为7000、7001、7002的目录
$ cd /opt/redis-4.0.1/redis-cluster
$ mkdir 7000 7001 7002
将/opt/redis-4.0.1/redis.conf文件拷贝到 新建的这三个文件夹中
$ cd /opt/redis-4.0.1/
$ cp redis.conf /opt/redis-4.0.1/redis_cluster/7000
$ cp redis.conf /opt/redis-4.0.1/redis_cluster/7001
$ cp redis.conf /opt/redis-4.0.1/redis_cluster/7002
3.分别修改这三个配置文件,把如下redis.conf 配置内容粘贴进去
$ vi 7000/redis.conf
$ vi 7001/redis.conf
$ vi 7002/redis.conf
以上三个 需要修改 redis.conf 配置 如下
port 7000
bind 10.xx.xx.111 (在112上需要修改绑定的ip)
daemonize yes
pidfile /var/run/redis_7000.pid(这个也要改)
cluster-enabled yes
cluster-config-file nodes_7000.conf
cluster-node-timeout 15000
appendonly yes
第一台机器上做好了配置远程拷贝到另一台机器上
执行 命令 scp -rp /opt/redis-4.0.1 [email protected]:/opt/
接着在另外台机器上(10.xx.xx.112)重复以上三步,只是把目录改为7003、7004、7005对应的配置文件也按照这个规则修改即。注意(上面标记黄色的部分是需要修改的)
##第一台机器111上执行
$ /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis_cluster/7000/redis.conf
$ /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis_cluster/7001/redis.conf
$ /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis_cluster/7002/redis.conf
##第二台机器112上执行
$ /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis_cluster/7003/redis.conf
$ /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis_cluster/7004/redis.conf
$ /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis_cluster/7005/redis.conf
检查各 Redis 启动情况
##第一台机器
$ ps -ef | grep redis
//redis是否启动成功
$ netstat -tnlp | grep redis //监听redis端口
2.随便找一个节点测试试
连接成功了,但好像报错了阿???(error) CLUSTERDOWN Hash slot not served(不提供集群的散列槽)
,这是什么鬼?
这是因为虽然我们配置并启动了 Redis 集群服务,但是他们暂时还并不在一个集群中,互相直接发现不了,而且还没有可存储的位置,就是所谓的slot(槽)。
$ yum -y install ruby ruby-devel rubygems rpm-build
$ gem install redis
注:创建集群时需要安装 Ruby 运行redis-trib.rb
创建集群
Redis 官方提供了 redis-trib.rb 这个工具,就在解压目录的 src 目录中
注意:在任意一台上运行 不要在每台机器上都运行,一台就够了
$ /opt/redis-4.0.1/src/redis-trib.rb create --replicas 1 10.xx.xx.111:7000 10.xx.xx.111:7001 10.xx.xx.111:7002 10.xx.xx.112:7003 10.xx.xx.112:7004 10.xx.xx.112:7005
前三个 ip:port 为第一台机器的节点,后三个为第二台机器,大概类似下面的意思
[root@localhost redis-cluster]# /opt/redis-4.0.1/src/redis-trib.rb create --replicas 1 192.168.252.101:7000 192.168.252.101:7001 192.168.252.101:7002 192.168.252.102:7003 192.168.252.102:7004 192.168.252.102:7005 192.168.252.103:7006 192.168.252.103:7007 192.168.252.103:7008 >>> Creating cluster >>> Performing hash slots allocation on 9 nodes... Using 4 masters: 192.168.252.101:7000 192.168.252.102:7003 192.168.252.103:7006 192.168.252.101:7001 Adding replica 192.168.252.102:7004 to 192.168.252.101:7000 Adding replica 192.168.252.103:7007 to 192.168.252.102:7003 Adding replica 192.168.252.101:7002 to 192.168.252.103:7006 Adding replica 192.168.252.102:7005 to 192.168.252.101:7001 Adding replica 192.168.252.103:7008 to 192.168.252.101:7000 M: 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf 192.168.252.101:7000 slots:0-4095 (4096 slots) master M: 44c81c15b01d992cb9ede4ad35477ec853d70723 192.168.252.101:7001 slots:12288-16383 (4096 slots) master S: 38f03c27af39723e1828eb62d1775c4b6e2c3638 192.168.252.101:7002 replicates f1abb62a8c9b448ea14db421bdfe3f1d8075189c M: 987965baf505a9aa43e50e46c76189c51a8f17ec 192.168.252.102:7003 slots:4096-8191 (4096 slots) master S: 6555292fed9c5d52fcf5b983c441aff6f96923d5 192.168.252.102:7004 replicates 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf S: 2b5ba254a0405d4efde4c459867b15176f79244a 192.168.252.102:7005 replicates 44c81c15b01d992cb9ede4ad35477ec853d70723 M: f1abb62a8c9b448ea14db421bdfe3f1d8075189c 192.168.252.103:7006 slots:8192-12287 (4096 slots) master S: eb4067373d36d8a8df07951f92794e67a6aac022 192.168.252.103:7007 replicates 987965baf505a9aa43e50e46c76189c51a8f17ec S: 2919e041dd3d1daf176d6800dcd262f4e727f366 192.168.252.103:7008 replicates 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf Can I set the above configuration? (type 'yes' to accept): yes |
输入 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 192.168.252.101:7000) M: 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf 192.168.252.101:7000 slots:0-4095 (4096 slots) master 2 additional replica(s) S: 6555292fed9c5d52fcf5b983c441aff6f96923d5 192.168.252.102:7004 slots: (0 slots) slave replicates 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf M: 44c81c15b01d992cb9ede4ad35477ec853d70723 192.168.252.101:7001 slots:12288-16383 (4096 slots) master 1 additional replica(s) S: 2919e041dd3d1daf176d6800dcd262f4e727f366 192.168.252.103:7008 slots: (0 slots) slave replicates 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf M: f1abb62a8c9b448ea14db421bdfe3f1d8075189c 192.168.252.103:7006 slots:8192-12287 (4096 slots) master 1 additional replica(s) S: eb4067373d36d8a8df07951f92794e67a6aac022 192.168.252.103:7007 slots: (0 slots) slave replicates 987965baf505a9aa43e50e46c76189c51a8f17ec S: 38f03c27af39723e1828eb62d1775c4b6e2c3638 192.168.252.101:7002 slots: (0 slots) slave replicates f1abb62a8c9b448ea14db421bdfe3f1d8075189c S: 2b5ba254a0405d4efde4c459867b15176f79244a 192.168.252.102:7005 slots: (0 slots) slave replicates 44c81c15b01d992cb9ede4ad35477ec853d70723 M: 987965baf505a9aa43e50e46c76189c51a8f17ec 192.168.252.102:7003 slots:4096-8191 (4096 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. |
这样也可以,推荐 $ pkill redis
循环节点逐个关闭
$ for((i=0;i<=2;i++)); do /opt/redis-4.0.1/src/redis-cli -c -h 10.xx.xx.111 -p 700$i shutdown; done
$ for((i=3;i<=5;i++)); do /opt/redis-4.0.1/src/redis-cli -c -h 10.xx.xx.112 -p 700$i shutdown; done
在第一台机器上连接集群的7000节点,在另外一台连接7004节点,连接方式为:
##加参数 -C 可连接到集群,因为 redis.conf 将 bind 改为了ip地址,所以 -h 参数不可以省略,-p 参数为端口号 我们在10.xx.xx.111机器redis 7000 的节点set 一个key
111机器上
112上
$ /opt/redis-4.0.1/src/redis-trib.rb check 10.xx.xx.111:7011
以下是我们测试环境上的一台112机器上模拟部署的三个从节点
列出集群当前已知的所有节点(node),以及这些节点的相关信息
先登录一个redis的客户端
比如:./redis-cli -h 10.xx.xx.112 -c -p 7024 进入后在命令行输入cluster nodes查看当前进群节点有哪些,哪些是master,哪些是slave
cluster nodes列出集群当前已知的所有节点( node),以及这些节点的相关信息。