redis集群搭建

2020-10-20  本文已影响0人  牙齿不帅

1.下载redis,解压安装。

wget https://download.redis.io/releases/redis-6.0.8.tar.gz?_ga=2.61301738.718246181.1603089425-368199388.1603089425
tar -zxvf redis-6.0.8.tar.gz\?_ga\=2.61301738.718246181.1603089425-368199388.1603089425 redis-6.0.8
cd redis-6.0.8
make && make install
报错:server.c:5307:31: 错误:‘struct redisServer’没有名为‘server_xxx’的成员
参考:https://blog.csdn.net/m0_37952284/article/details/109012324
yum install glibc-kernheaders
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash
make && make install #安装成功

2.修改配置文件。

cd src/
cd ..
mkdir redis-cluster/7000
mkdir redis-cluster/7001
mkdir redis-cluster/7002
mkdir redis-cluster/7003
mkdir redis-cluster/7004
mkdir redis-cluster/7005
cp redis.conf redis-cluster/7000/
cp redis.conf redis-cluster/7001/
cp redis.conf redis-cluster/7002/
cp redis.conf redis-cluster/7003/
cp redis.conf redis-cluster/7004/
cp redis.conf redis-cluster/7005/
cd redis-cluster/7000
#替换6379的端口
sed -i 's/6379/7000/g' redis.conf
sed -i 's/# cluster-config-file nodes-7000.conf/cluster-config-file nodes-7000.conf/g' redis.conf
sed -i 's/daemonize no/daemonize yes/' redis.conf 
sed -i 's/appendonly no/appendonly yes/' redis.conf
sed -i 's/# cluster-node-timeout/cluster-node-timeout/' redis.conf
sed -i 's/# cluster-enabled yes/cluster-enabled yes/' redis.conf

分别到对应的7001/7002/7003/7004/7004目录下,也执行这样的操作,配置成各自的端口,如:redis-cluster/7000/redis.conf最后配置成如下:

port  7000                                        //端口7000,7002,7003        
bind 127.0.0.1                                     //改为其他节点机器可访问的ip 可以使用ifconfig查看一下
daemonize yes                               //redis后台运行
appendonly yes                           //aof日志开启  有需要就开启,它会每次写操作都记录一条日志
pidfile  /var/run/redis_7000.pid          //pidfile文件对应7000,7001,7002
cluster-enabled  yes                           //开启集群  
cluster-config-file  nodes_7000.conf   //集群的配置  配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout  15000                //请求超时  默认15秒,可自行修改

创建配置启动脚本startall.sh如下:

redis-server redis-cluster/7000/redis.conf
redis-server redis-cluster/7001/redis.conf
redis-server redis-cluster/7002/redis.conf
redis-server redis-cluster/7003/redis.conf
redis-server redis-cluster/7004/redis.conf
redis-server redis-cluster/7005/redis.conf

执行./startall.sh成功后如下:

ps -ef | grep redis
root     21543     1  0 10:37 ?        00:00:00 redis-server 127.0.0.1:7000 [cluster]
root     21549     1  0 10:37 ?        00:00:00 redis-server 127.0.0.1:7001 [cluster]
root     21555     1  0 10:37 ?        00:00:00 redis-server 127.0.0.1:7002 [cluster]
root     21557     1  0 10:37 ?        00:00:00 redis-server 127.0.0.1:7003 [cluster]
root     21559     1  0 10:37 ?        00:00:00 redis-server 127.0.0.1:7004 [cluster]
root     21561     1  0 10:37 ?        00:00:00 redis-server 127.0.0.1:7005 [cluster]

配置集群主从:

[root@server2 redis-6.0.8]# redis-cli --cluster create 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 127.0.0.1:7000 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7000 to 127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 7e8aeff9812bd1f02f8c0b4146da246d41021b0d 127.0.0.1:7001
   slots:[0-5460] (5461 slots) master
M: 817f825689b8855b14958c0ac646fd1e0aa44f2a 127.0.0.1:7002
   slots:[5461-10922] (5462 slots) master
M: 11a83f3b253f846f0d99ce4f7ee65e6c9a457280 127.0.0.1:7003
   slots:[10923-16383] (5461 slots) master
S: cceb9d9eed1cf10d2ea4c3a56ed09961ab2701b1 127.0.0.1:7004
   replicates 817f825689b8855b14958c0ac646fd1e0aa44f2a
S: 40746385d3ae06b4dc83a2cdbd550a882b9cf0c7 127.0.0.1:7005
   replicates 11a83f3b253f846f0d99ce4f7ee65e6c9a457280
S: 9acc427845da2e84a85aeaaf6c64c04ffad70821 127.0.0.1:7000
   replicates 7e8aeff9812bd1f02f8c0b4146da246d41021b0d
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 127.0.0.1:7001)
M: 7e8aeff9812bd1f02f8c0b4146da246d41021b0d 127.0.0.1:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 11a83f3b253f846f0d99ce4f7ee65e6c9a457280 127.0.0.1:7003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 9acc427845da2e84a85aeaaf6c64c04ffad70821 127.0.0.1:7000
   slots: (0 slots) slave
   replicates 7e8aeff9812bd1f02f8c0b4146da246d41021b0d
S: cceb9d9eed1cf10d2ea4c3a56ed09961ab2701b1 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 817f825689b8855b14958c0ac646fd1e0aa44f2a
M: 817f825689b8855b14958c0ac646fd1e0aa44f2a 127.0.0.1:7002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 40746385d3ae06b4dc83a2cdbd550a882b9cf0c7 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 11a83f3b253f846f0d99ce4f7ee65e6c9a457280
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

查看操作redis,卡槽已经散列到对应的集群机器:

[root@server2 redis-6.0.8]# redis-cli -c -p 7001
127.0.0.1:7001> keys *
(empty array)
127.0.0.1:7001> set name cong
-> Redirected to slot [5798] located at 127.0.0.1:7002
OK
127.0.0.1:7002> get name
"cong"
127.0.0.1:7002> exit
[root@server2 redis-6.0.8]# redis-cli -c -p 7000
127.0.0.1:7000> get name
-> Redirected to slot [5798] located at 127.0.0.1:7002
"cong"

参考:https://www.jianshu.com/p/813a79ddf932
参考:https://blog.csdn.net/m0_37952284/article/details/109012324
参考:https://blog.csdn.net/qq_42815754/article/details/82912130

上一篇下一篇

猜你喜欢

热点阅读