单机多实例Redis集群部署配置

/ DatabaseLinux服务安装搭建 / 没有评论 / 1275浏览

环境

机器:Centos7 已经安装好一个redis服务

[root@pa2 src]#which redis-server
/usr/local/bin/redis-server

内网ip:172.168.1.3

我们今天使用9001,9002,9003,9004,9005,9006这些端口 为3主3从redis集群,redis自动分配,按照集群初始化实例的先后顺序来,平均分开,前面三个为主,后面三个为从

创建一个集群目录,方便管理

mkdir /usr/local/redis-cluster     
cd /usr/local/redis-cluster/
mkdir -p 9001/data 9002/data 9003/data 9004/data 9005/data 9006/data   #端口将分别为9001,9002,9003,9004,9005,9006

集群管理命令配置

#创建集群管理的命令目录
mkdir /usr/local/redis-cluster/bin -p 
#复制原来redis服务/bin目录下的相关二进制命令
cd /usr/local/redis/bin   #我原来的redis安装在/usr/local/redis
cp mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server redis-trib.rb /usr/local/redis-cluster/bin

先复制一个模板到9001端口

#由于集群的配置文件相似度较高,我们先配置一个模板,9001端口的redis

cp /usr/local/redis/* /usr/local/redis-cluster/9001

vim redis.conf #编辑该文件,就麻烦一会,这里要看准

port 9001(每个节点的端口号)
daemonize yes
bind 172.168.1.3(绑定当前机器 IP)
dir /usr/local/redis-cluster/9001/data/(数据文件存放位置)
pidfile /var/run/redis_9001.pid   #(pid 9001和port要对应)
cluster-enabled yes  #(启动集群模式)
cluster-config-file nodes9001.conf  #(9001和port要对应)
cluster-node-timeout 15000
appendonly yes

注意:集群的开启,重点是这三个 cluster-enabled yes #(启动集群模式) cluster-config-file nodes9001.conf #(9001和port要对应) cluster-node-timeout 15000

复制9001实例配置文件到其余5个redis实例

\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9002
\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9003
\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9004
\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9005
\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9006

全局替换redis.conf文件

:%s/9001/9002/g

这里需要麻烦一点,进去每个目录,更改9001为该目录对应的端口,可以使vim全局替换,但是我懒,也觉得这文件以后肯定用的到,先写个批量改

我这里用了一个sed替换

sed -i s/9001/9002/g  /usr/local/redis-cluster/9002/redis.conf 
sed -i s/9001/9003/g  /usr/local/redis-cluster/9003/redis.conf 
sed -i s/9001/9004/g  /usr/local/redis-cluster/9004/redis.conf 
sed -i s/9001/9005/g  /usr/local/redis-cluster/9005/redis.conf 
sed -i s/9001/9006/g  /usr/local/redis-cluster/9006/redis.conf 
                   

启动

/usr/local/redis/src/redis-server /usr/local/redis-cluster/9001/redis.conf 
/usr/local/redis/src/redis-server /usr/local/redis-cluster/9002/redis.conf 
/usr/local/redis/src/redis-server /usr/local/redis-cluster/9003/redis.conf 
/usr/local/redis/src/redis-server /usr/local/redis-cluster/9004/redis.conf 
/usr/local/redis/src/redis-server /usr/local/redis-cluster/9005/redis.conf 
/usr/local/redis/src/redis-server /usr/local/redis-cluster/9006/redis.conf

进去redis看看?

redis-cli -h 172.168.1.3 -p 9001   #这里的172.168.1.3指的是bind指定的地址

set name lijinghua # 回车,报错了,缺少Ruby和rubygems,不要慌

(error) CLUSTERDOWN Hash slot not served

安装Ruby和rubygems

#Ruby和rubygems


yum install ruby -y 
yum install rubygems -y 
gem install redis  

集群初始化启动

/usr/local/redis-cluster/bin/redis-trib.rb create --replicas 1 172.168.1.3:9001 172.168.1.3:9002 172.168.1.3:9003 172.168.1.3:9004 172.168.1.3:9005 172.168.1.3:9006



[root@pa2 src]#/usr/local/redis-cluster/bin/redis-trib.rb create --replicas 1 172.168.1.3:9001 172.168.1.3:9002 172.168.1.3:9003 172.168.1.3:9004 172.168.1.3:9005 172.168.1.3:9006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:     #为3主3从redis集群,redis自动分配,按照集群初始化实例的先后顺序来,平均分开,前面三个为主,后面三个为从
172.168.1.3:9001
172.168.1.3:9002
172.168.1.3:9003
Adding replica 172.168.1.3:9005 to 172.168.1.3:9001
Adding replica 172.168.1.3:9006 to 172.168.1.3:9002
Adding replica 172.168.1.3:9004 to 172.168.1.3:9003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: b33c3ba592b0280049ebc301b9f2202190a193c3 172.168.1.3:9001
   slots:0-5460 (5461 slots) master
M: 35f7e795e40183ce64dbda1b06819d4eb53da017 172.168.1.3:9002
   slots:5461-10922 (5462 slots) master
M: 762d5f76e2551ee6b9079cf9354e3c38d5eca92d 172.168.1.3:9003
   slots:10923-16383 (5461 slots) master
S: c4b0dfe713aedc0f3be777253f6ccb7d9867ad75 172.168.1.3:9004
   replicates b33c3ba592b0280049ebc301b9f2202190a193c3
S: 167f63152700fc8c62923970215a8d80f776046b 172.168.1.3:9005
   replicates 35f7e795e40183ce64dbda1b06819d4eb53da017
S: 43670c3af65049d902c22c89d12d18800cf9f2c4 172.168.1.3:9006
   replicates 762d5f76e2551ee6b9079cf9354e3c38d5eca92d
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 172.168.1.3:9001)
M: b33c3ba592b0280049ebc301b9f2202190a193c3 172.168.1.3:9001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: c4b0dfe713aedc0f3be777253f6ccb7d9867ad75 172.168.1.3:9004
   slots: (0 slots) slave
   replicates b33c3ba592b0280049ebc301b9f2202190a193c3
S: 167f63152700fc8c62923970215a8d80f776046b 172.168.1.3:9005
   slots: (0 slots) slave
   replicates 35f7e795e40183ce64dbda1b06819d4eb53da017
M: 762d5f76e2551ee6b9079cf9354e3c38d5eca92d 172.168.1.3:9003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 35f7e795e40183ce64dbda1b06819d4eb53da017 172.168.1.3:9002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 43670c3af65049d902c22c89d12d18800cf9f2c4 172.168.1.3:9006
   slots: (0 slots) slave
   replicates 762d5f76e2551ee6b9079cf9354e3c38d5eca92d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.  #出现这个绿色字体为成功

在进去redis里面测试看看?

[root@pa2 lijinghua]#redis-cli -h 127.0.0.1 -p 9001 -c
127.0.0.1:9001> set name lijinghua
-> Redirected to slot [5798] located at 127.0.0.1:9002
OK

了解一个参数

-c 启用集群模式(按照-ASK和-MOVED重定向)

End