简单描述redis集群搭建
部署集群环境
1)redis集群搭建至少需要六台机器,才能组成,
ip地址规划如下:
– redisA 192.168.4.1 6341
– redisB 192.168.4.2 6342
– redisC 192.168.4.3 6343
– redisD 192.168.4.4 6344
– redisE 192.168.4.5 6345
– redisF 192.168.4.6 6346
2)创建集群环境
在六台主机做如下配置,并运行redis服务
#yum -y install gcc gcc-c++
#tar -zxvf redis-4.0.8.tar.gz
#cd redis-4.0.8/
#make
#make install
#./utils/install_server.sh
注意:以上操作必须在六台主机上都要进行
3)• 修改配置文件
#/etc/init.d/redis_6379 stop //在修改配置文件之前,先把redis服务关闭
#vim /etc/redis/redis.conf //修改redis主配置文件
bind IP 地址 // 只写物理接口 IP 地址,即网卡的真实Ip地址
daemonize yes // 守护进程方式运行
port xxxx // 端口号不要使用默认的 6379
cluster-enabled yes // 开启集群
cluster-config-file 6379.conf // 集群的配置文件不要使用默认的名称,把6379.conf改为xx.conf,不要使用默认的即可
cluster-node-timeout 5000 // 请求超时 5 秒
4)修改redis启动.停止脚本
# vim /etc/init.d/redis_6379
REDISPORT="6379" //脚本第八行,首先更改端口号,默认6379,需要改为与redis主配置文件里面对应的端口号
$CLIEXEC -p $REDISPORT shutdown //脚本第43行,因为我们对redis主配置文件的默认端口号和ip地址都进行了更改,所以这里需要增加修改后的ip地址,端口已经在上面的脚本变量修改,这里就不需要再指定
修改后:$CLIEXEC -p $REDISPORT -h 192.168.4.1 shutdown
保存退出
注意:以上操作在每一台机器上都要进行
4)创建集群
选好需要当redis主服务器的机器,并在上面执行创建集群的脚本
#yum -y install ruby rubygems
#rpm -ivh --nodeps ruby-devel-2.0.0.648-30.el7.x86_64.rpm #忽略依赖关系安装ruby-devel
#gem install redis-3.2.1.gem
# cd redis-4.0.8/
# cd src
# cp redis-trib.rb /usr/local/sbin/ #把ruby脚本拷贝到系统命令路径下
#ls /usr/local/sbin/
redis-trib.rb #脚本绿色字体表示有执行权限
#./redis-trib.rb create --replicas 1 \ //开始创建集群 --replicas 1 表示 自动为每一个 master 节点分配一个 slave 节点
192.168.4.1:6341 \
192.168.4.2:6342 \
192.168.4.3:6343 \
192.168.4.4:6344 \
192.168.4.5:6345 \
192.168.4.6:6346
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.4.1:6343
192.168.4.2:6344
192.168.4.3:6345
Adding replica 192.168.4.5:6347 to 192.168.4.3:6343
Adding replica 192.168.4.6:6348 to 192.168.4.4:6344
Adding replica 192.168.4.4:6346 to 192.168.4.5:6345
M: c834c9ae1b0330a53f35af0cffd311d7ef23c83e 192.168.4.1:6343
slots:0-5460 (5461 slots) master
M: 424e6d161b90819cc8a986e4b3143e3b82d89825 192.168.4.2:6344
slots:5461-10922 (5462 slots) master
M: 7daf622d9d59c194b9abc185679f4dcc8960a80b 192.168.4.3:6345
slots:10923-16383 (5461 slots) master
S: 1f8333b9eea0210a5c2c0c2896dfbf5b9e9259d9 192.168.4.4:6346
replicates 7daf622d9d59c194b9abc185679f4dcc8960a80b
S: 6bb3781bfb002938fa3462ed37631073f6ad6036 192.168.4.5:6347
replicates c834c9ae1b0330a53f35af0cffd311d7ef23c83e
S: d46a4985853f7430835f61a02da3375ffa7f2eec 192.168.4.6:6348
replicates 424e6d161b90819cc8a986e4b3143e3b82d89825
Can I set the above configuration? (type 'yes' to accept): 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.4.3:6343)
M: c834c9ae1b0330a53f35af0cffd311d7ef23c83e 192.168.4.3:6343
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 7daf622d9d59c194b9abc185679f4dcc8960a80b 192.168.4.5:6345
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 6bb3781bfb002938fa3462ed37631073f6ad6036 192.168.4.7:6347
slots: (0 slots) slave
replicates c834c9ae1b0330a53f35af0cffd311d7ef23c83e
M: 424e6d161b90819cc8a986e4b3143e3b82d89825 192.168.4.4:6344
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 1f8333b9eea0210a5c2c0c2896dfbf5b9e9259d9 192.168.4.6:6346
slots: (0 slots) slave
replicates 7daf622d9d59c194b9abc185679f4dcc8960a80b
S: d46a4985853f7430835f61a02da3375ffa7f2eec 192.168.4.8:6348
slots: (0 slots) slave
replicates 424e6d161b90819cc8a986e4b3143e3b82d89825
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
#redis-cli -c -h 192.168.4.1:6341 //登陆查看,-c 表示登陆集群