redis 4.0.11 集群搭建

  • 环境介绍:
    • IP: 10.241.0.10
      • Port: 7000
      • Port: 7001
    • IP: 10.241.0.11
      • Port: 7000
      • Port: 7001
    • IP: 10.241.0.12
      • Port: 7000
      • Port: 7001

redis集群安装

下载安装redis(所有节点)

wget http://download.redis.io/releases/redis-4.0.11.tar.gz
tar zxvf redis-4.0.11.tar.gz
cd redis-4.0.11
make
cd src/
make install 

配置redis

node1

mkdir -p /data/redis/{7000,7001}/data
mkdir -p /usr/local/redis-cluster/bin
mkdir -p /usr/local/redis-cluster/7000
cp ../redis.conf  /usr/local/redis-cluster/7000/

#修改以下配置
vim /usr/local/redis-cluster/7000/redis.conf
bind 10.241.0.10   #绑定本机IP
daemonize yes       #开启后台运行
dir /data/redis/7000/data       #数据存放路径
port 7000           #监听端口
pidfile /var/run/redis_7000.pid #pid文件
logfile /var/log/redis_7000.log #log文件
cluster-enabled yes             #打开注释
cluster-config-file nodes.conf  #打开注释
cluster-node-timeout 15000      #打开注释
appendonly yes                  #开启持久化

#7001节点配置
cp  -rf /usr/local/redis-cluster/7000/  /usr/local/redis-cluster/7001/
sed -i 's/7000/7001/g' /usr/local/redis-cluster/7001/redis.conf

#将配置文件copy到其他的节点
scp -r /usr/local/redis-cluster/{7000,7001}  root@10.241.0.11:/usr/local/redis-cluster/
scp -r /usr/local/redis-cluster/{7000,7001}  root@10.241.0.12:/usr/local/redis-cluster/

node2

mkdir -p /data/redis/{7000,7001}/data
mkdir -p /usr/local/redis-cluster/bin
cp mkreleasehdr.sh redis-benchmark redis-check-aof redis-cli redis-server redis-trib.rb /usr/local/redis-cluster/bin
sed -i 's/10.241.0.10/10.241.0.11/g' /usr/local/redis-cluster/7000/redis.conf 
sed -i 's/10.241.0.10/10.241.0.11/g' /usr/local/redis-cluster/7001/redis.conf

node3

mkdir -p /data/redis/{7000,7001}/data
mkdir -p /usr/local/redis-cluster/bin
cp mkreleasehdr.sh redis-benchmark redis-check-aof redis-cli redis-server redis-trib.rb /usr/local/redis-cluster/bin
sed -i 's/10.241.0.10/10.241.0.12/g' /usr/local/redis-cluster/7000/redis.conf 
sed -i 's/10.241.0.10/10.241.0.12/g' /usr/local/redis-cluster/7001/redis.conf

启动redis(所有节点)

/usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/7000/redis.conf
/usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/7001/redis.conf

ps -ef|grep redis
root      3079     1  0 00:26 ?        00:00:00 /usr/local/redis-cluster/bin/redis-server 10.241.0.10:7000 [cluster]
root      3243     1  0 00:28 ?        00:00:00 /usr/local/redis-cluster/bin/redis-server 10.241.0.10:7001 [cluster]

netstat -nplt|grep redis-server
tcp        0      0 10.241.0.10:17000    0.0.0.0:*               LISTEN      3243/redis-server 1 
tcp        0      0 10.241.0.10:17001    0.0.0.0:*               LISTEN      3079/redis-server 1 
tcp        0      0 10.241.0.10:7000     0.0.0.0:*               LISTEN      3243/redis-server 1 
tcp        0      0 10.241.0.10:7001     0.0.0.0:*               LISTEN      3079/redis-server 1 

iptables放行

#如开iptables,需注意,端口放行, 注意17000,17001也要放行,不然加入不到集群
-A INPUT -p tcp -m tcp --dport 7000 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 7001 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 17000 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 17001 -j ACCEPT

配置集群(只在node1操作)

安装ruby

wget https://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.7.tar.gz
tar xvf ruby-2.2.7.tar.gz
cd ruby-2.2.7
./configure --prefix=/usr/local/ruby-2.2.7
make && make install
ln -s /usr/local/ruby-2.2.7/bin/ruby /usr/bin/ruby
ln -s /usr/local/ruby-2.2.7/bin/gem /usr/bin
ruby -v
gem -v

使用gem安装redis集群依赖

gem install redis

创建集群

如集群都设置了密码,需要修该
/usr/local/ruby-2.2.7/lib/ruby/gems/2.2.0/gems/redis-4.0.1/lib/redis/client.rb 文件中的
Password 参数将密码写入到这里,
/usr/local/redis-cluster/bin/redis-trib.rb create --replicas 1 10.241.0.10:7000 10.241.0.10:7001 10.241.0.11:7000 10.241.0.11:7001  10.241.0.12:7000 10.241.0.12:7001

Using 3 masters:
10.241.0.10:7000
10.241.0.11:7000
10.241.0.12:7000
Adding replica 10.241.0.11:7001 to 10.241.0.10:7000
Adding replica 10.241.0.12:7001 to 10.241.0.11:7000
Adding replica 10.241.0.10:7001 to 10.241.0.12:7000
M: ade53fb12571212ae25a84e55c0fb0311d05a26e 10.241.0.10:7000
   slots:0-5460 (5461 slots) master
S: 8b9fde5ec861254715516f07cf84b029e8cfe6d5 10.241.0.10:7001
   replicates 407b6b0b0425e89e4e63fde2c3894616a31df869
M: 42dbdc9435bc216941db2c46f73c663812cbf8ff 10.241.0.11:7000
   slots:5461-10922 (5462 slots) master
S: 538372e405c12e3ed7d5b1da6a2736ff4f393277 10.241.0.11:7001
   replicates ade53fb12571212ae25a84e55c0fb0311d05a26e
M: 407b6b0b0425e89e4e63fde2c3894616a31df869 10.241.0.12:7000
   slots:10923-16383 (5461 slots) master
S: ff8b973753031d2d87c75eaca5530ff4f7ccb22f 10.241.0.12:7001
   replicates 42dbdc9435bc216941db2c46f73c663812cbf8ff
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 10.241.0.10:7000)
M: ade53fb12571212ae25a84e55c0fb0311d05a26e 10.241.0.10:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 538372e405c12e3ed7d5b1da6a2736ff4f393277 10.241.0.11:7001
   slots: (0 slots) slave
   replicates ade53fb12571212ae25a84e55c0fb0311d05a26e
M: 407b6b0b0425e89e4e63fde2c3894616a31df869 10.241.0.12:7000
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 42dbdc9435bc216941db2c46f73c663812cbf8ff 10.241.0.11:7000
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 8b9fde5ec861254715516f07cf84b029e8cfe6d5 10.241.0.10:7001
   slots: (0 slots) slave
   replicates 407b6b0b0425e89e4e63fde2c3894616a31df869
S: ff8b973753031d2d87c75eaca5530ff4f7ccb22f 10.241.0.12:7001
   slots: (0 slots) slave
   replicates 42dbdc9435bc216941db2c46f73c663812cbf8ff
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

验证集群

#通过客户端命令连接上,通过集群命令看一下状态和节点信息等

/usr/local/redis-cluster/bin/redis-cli  -h 10.241.0.11 -p 7001 -c
10.241.0.11:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:5
cluster_stats_messages_ping_sent:97
cluster_stats_messages_pong_sent:101
cluster_stats_messages_meet_sent:3
cluster_stats_messages_sent:201
cluster_stats_messages_ping_received:99
cluster_stats_messages_pong_received:100
cluster_stats_messages_meet_received:2
cluster_stats_messages_received:201


10.241.0.11:7001> cluster nodes
ff8b973753031d2d87c75eaca5530ff4f7ccb22f 10.241.0.12:7001@17001 slave 42dbdc9435bc216941db2c46f73c663812cbf8ff 0 1541524337000 3 connected
407b6b0b0425e89e4e63fde2c3894616a31df869 10.241.0.12:7000@17000 myself,master - 0 1541524335000 5 connected 10923-16383
ade53fb12571212ae25a84e55c0fb0311d05a26e 10.241.0.10:7000@17000 master - 0 1541524336735 1 connected 0-5460
538372e405c12e3ed7d5b1da6a2736ff4f393277 10.241.0.11:7001@17001 slave ade53fb12571212ae25a84e55c0fb0311d05a26e 0 1541524337000 4 connected
42dbdc9435bc216941db2c46f73c663812cbf8ff 10.241.0.11:7000@17000 master - 0 1541524338739 3 connected 5461-10922
8b9fde5ec861254715516f07cf84b029e8cfe6d5 10.241.0.10:7001@17001 slave 407b6b0b0425e89e4e63fde2c3894616a31df869 0 1541524338000 5 connected
    原文作者:baiyongjie
    原文地址: https://www.jianshu.com/p/abff4b70915a
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞