学海无涯
go,go,go

redis cluster集群搭建

redis cluster(分布式集群)
高性能:
1、在多分片节点中,将16384个槽位,均匀分布到多个分片节点中
2、存数据时,将key做crc16(key),然后和16384进行取模,得出槽位值(0-16383之间)
3、根据计算得出的槽位值,找到相对应的分片节点的主节点,存储到相应槽位上
4、如果客户端当时连接的节点不是将来要存储的分片节点,分片集群会将客户端连接切换至真正存储节点进行数据存储

高可用:
在搭建集群时,会为每一个分片的主节点,对应一个从节点,实现slaveof的功能,同时当主节点down,实现类似于sentinel的自动failover的功能。

规划、搭建过程:
6个redis实例,一般会放到3台硬件服务器
注:在企业规划中,一个分片的两个分到不同的物理机,防止硬件主机宕机造成的整个分片数据丢失。
端口号:7000-7005
1、安装集群插件
EPEL源安装ruby支持
yum install ruby rubygems -y
redis cluster节点1 192.168.2.100
redis cluster节点2 192.168.2.101
redis cluster节点3 192.168.2.102

修改主机名称
hostnamectl set-hostname redis-cluster1
hostnamectl set-hostname redis-cluster2
hostnamectl set-hostname redis-cluster3

3台服务器安装redis
mkdir /nosql -p
cd /nosql
wget http://download.redis.io/releases/redis-3.2.12.tar.gz
tar xzf redis-3.2.12.tar.gz
mv redis-3.2.12 redis
cd redis
make 
配置环境变量
echo 'PATH=/application/mysql/bin/:/nosql/redis/src:$PATH' >>/etc/profile
source /etc/profile

安装集群插件,EPEL源安装ruby支持
yum install ruby rubygems -y

使用国内源
gem sources -l
gem sources -a http://mirrors.aliyun.com/rubygems/ 
gem sources  --remove http://rubygems.org/
gem install redis -v 3.3.3
gem sources -l

或者:
gem sources -a http://mirrors.aliyun.com/rubygems/  --remove http://rubygems.org/ 
redis cluster节点1
安装cluster集群
[root@redis-cluster2 ~]# gem install redis -v 3.3.3
Fetching: redis-3.3.3.gem (100%)
Successfully installed redis-3.3.3
Parsing documentation for redis-3.3.3
Installing ri documentation for redis-3.3.3
配置软件目录
mkdir /nosql/700{0..1} -p
创建主配置文件
cat >/nosql/7000/redis.conf<<'EOF'
port 7000
daemonize yes
pidfile /nosql/7000/redis.pid
loglevel notice
logfile "/nosql/7000/redis.log"
dbfilename dump.rdb
dir /nosql/7000
bind 192.168.2.100 127.0.0.1
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF
创建从配置文件
cat >/nosql/7001/redis.conf<<'EOF'
port 7001
daemonize yes
pidfile /nosql/7001/redis.pid
loglevel notice
logfile "/nosql/7001/redis.log"
dbfilename dump.rdb
dir /nosql/7001
bind 192.168.2.100 127.0.0.1
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

redis cluster节点2
安装cluster集群
[root@redis-cluster2 ~]# gem install redis -v 3.3.3
Fetching: redis-3.3.3.gem (100%)
Successfully installed redis-3.3.3
Parsing documentation for redis-3.3.3
Installing ri documentation for redis-3.3.3
1 gem installed
配置软件目录
mkdir /nosql/700{2..3} -p
创建主配置文件
cat >/nosql/7002/redis.conf<<'EOF'
port 7002
daemonize yes
pidfile /nosql/7002/redis.pid
loglevel notice
logfile "/nosql/7002/redis.log"
dbfilename dump.rdb
dir /nosql/7002
bind 192.168.2.101 127.0.0.1
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF
创建从配置文件
cat >/nosql/7003/redis.conf<<'EOF'
port 7003
daemonize yes
pidfile /nosql/7003/redis.pid
loglevel notice
logfile "/nosql/7003/redis.log"
dbfilename dump.rdb
dir /nosql/7003
bind 192.168.2.101 127.0.0.1
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

redis cluster节点3
安装redis-cluster集群
[root@redis-cluster3 ~]# gem install redis -v 3.3.3
Fetching: redis-3.3.3.gem (100%)
Successfully installed redis-3.3.3
Parsing documentation for redis-3.3.3
Installing ri documentation for redis-3.3.3
1 gem installed
配置软件目录
mkdir /nosql/700{4..5} -p
创建主配置文件
cat >/nosql/7004/redis.conf<<'EOF'
port 7004
daemonize yes
pidfile /nosql/7004/redis.pid
loglevel notice
logfile "/nosql/7004/redis.log"
dbfilename dump.rdb
dir /nosql/7004
bind 192.168.2.102 127.0.0.1
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF
创建从配置文件
cat >/nosql/7005/redis.conf<<'EOF'
port 7005
daemonize yes
pidfile /nosql/7005/redis.pid
loglevel notice
logfile "/nosql/7005/redis.log"
dbfilename dump.rdb
dir /nosql/7005
bind 192.168.2.102 127.0.0.1
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

#启动节点1
redis-server /nosql/7000/redis.conf && redis-server /nosql/7001/redis.conf 
#启动节点2
redis-server /nosql/7002/redis.conf && redis-server /nosql/7003/redis.conf
#启动节点3
redis-server /nosql/7004/redis.conf && redis-server /nosql/7005/redis.conf
节点1
[root@redis-cluster1 redis]# ps -ef |grep redis
root      16886      1  0 00:26 ?        00:00:00 redis-server 192.168.2.100:7000 [cluster]
root      16888      1  0 00:26 ?        00:00:00 redis-server 192.168.2.100:7001 [cluster]
root      16894  10535  0 00:27 pts/1    00:00:00 grep --color=auto redis
节点2
[root@redis-cluster2 redis]# ps -ef |grep redis
root      17067      1  0 00:26 ?        00:00:00 redis-server 192.168.2.101:7002 [cluster]
root      17069      1  0 00:26 ?        00:00:00 redis-server 192.168.2.101:7003 [cluster]
root      17075  10873  0 00:27 pts/0    00:00:00 grep --color=auto redis
节点3
[root@redis-cluster3 redis]# ps -ef |grep redis
root      16744      1  0 00:26 ?        00:00:00 redis-server 192.168.2.102:7004 [cluster]
root      16746      1  0 00:26 ?        00:00:00 redis-server 192.168.2.102:7005 [cluster]
root      16752  10546  0 00:27 pts/0    00:00:00 grep --color=auto redis

在节点1操作将节点加入集群管理
redis-trib.rb create --replicas 1 192.168.2.100:7000 192.168.2.100:7001 \
192.168.2.101:7002 192.168.2.101:7003 192.168.2.102:7004 192.168.2.102:7005

redis-trib.rb create --replicas 1 192.168.2.100:7000 192.168.2.100:7001 192.168.2.101:7002 192.168.2.101:7003 192.168.2.102:7004 192.168.2.102:7005

[root@redis-cluster1 ~]# redis-trib.rb create --replicas 1 192.168.2.100:7000 192.168.2.100:7001 \
> 192.168.2.101:7002 192.168.2.101:7003 192.168.2.102:7004 192.168.2.102:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.2.100:7000
192.168.2.101:7002
192.168.2.102:7004
Adding replica 192.168.2.101:7003 to 192.168.2.100:7000
Adding replica 192.168.2.100:7001 to 192.168.2.101:7002
Adding replica 192.168.2.102:7005 to 192.168.2.102:7004
M: 54e756cdd6420ba7b2cdc8b061934726dc747928 192.168.2.100:7000 节点1主节点
   slots:0-5460 (5461 slots) master
S: 7d8b3302f1a0b2e6eeb44ac3658d098bc1cb9f21 192.168.2.100:7001 节点1从节点
   replicates ea4139fff90334fd41c81812b778ae9819cd24a7
M: ea4139fff90334fd41c81812b778ae9819cd24a7 192.168.2.101:7002 节点2主节点
   slots:5461-10922 (5462 slots) master
S: fa4f8be2c57e59f8e2605d4e32dd33b577c79fca 192.168.2.101:7003 节点2从节点
   replicates 54e756cdd6420ba7b2cdc8b061934726dc747928
M: 0d026b4b20c86d736522dc52fa21ed3c0e94b75d 192.168.2.102:7004 节点3主节点
   slots:10923-16383 (5461 slots) master
S: 52ad1364931eec5ec3224f41a35346cbccd4dfff 192.168.2.102:7005 节点3从节点
   replicates 0d026b4b20c86d736522dc52fa21ed3c0e94b75d
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.2.100:7000)
M: 54e756cdd6420ba7b2cdc8b061934726dc747928 192.168.2.100:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 0d026b4b20c86d736522dc52fa21ed3c0e94b75d 192.168.2.102:7004
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 52ad1364931eec5ec3224f41a35346cbccd4dfff 192.168.2.102:7005
   slots: (0 slots) slave
   replicates 0d026b4b20c86d736522dc52fa21ed3c0e94b75d
S: fa4f8be2c57e59f8e2605d4e32dd33b577c79fca 192.168.2.101:7003
   slots: (0 slots) slave
   replicates 54e756cdd6420ba7b2cdc8b061934726dc747928
M: ea4139fff90334fd41c81812b778ae9819cd24a7 192.168.2.101:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 7d8b3302f1a0b2e6eeb44ac3658d098bc1cb9f21 192.168.2.100:7001
   slots: (0 slots) slave
   replicates ea4139fff90334fd41c81812b778ae9819cd24a7
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
安装节点配置成功
集群主节点状态
[root@redis-cluster1 ~]# redis-cli -p 7000 cluster nodes | grep master
0d026b4b20c86d736522dc52fa21ed3c0e94b75d 192.168.2.102:7004 master - 0 1565975959413 5 connected 10923-16383
54e756cdd6420ba7b2cdc8b061934726dc747928 192.168.2.100:7000 myself,master - 0 0 1 connected 0-5460
ea4139fff90334fd41c81812b778ae9819cd24a7 192.168.2.101:7002 master - 0 1565975957909 3 connected 5461-10922
集群从节点状态
[root@redis-cluster1 ~]# redis-cli -p 7000 cluster nodes | grep slave
52ad1364931eec5ec3224f41a35346cbccd4dfff 192.168.2.102:7005 slave 0d026b4b20c86d736522dc52fa21ed3c0e94b75d 0 1565975973443 6 connected
fa4f8be2c57e59f8e2605d4e32dd33b577c79fca 192.168.2.101:7003 slave 54e756cdd6420ba7b2cdc8b061934726dc747928 0 1565975973443 4 connected
7d8b3302f1a0b2e6eeb44ac3658d098bc1cb9f21 192.168.2.100:7001 slave ea4139fff90334fd41c81812b778ae9819cd24a7 0 1565975973945 3 connected

关闭节点直接使用killall
killall redis-server

节点1主
[root@web02 ~]# redis-cli -a qwe123 -h 192.168.2.100 -p 7000
192.168.2.100:7000>  info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.2.101,port=7003,state=online,offset=4131,lag=1  从为192.168.2.101:7003
master_repl_offset:4131
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:4130
192.168.2.100:7000> exit
节点2主
[root@web02 ~]# redis-cli -a qwe123 -h 192.168.2.101 -p 7002
192.168.2.101:7002>  info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.2.100,port=7001,state=online,offset=4159,lag=0  从为192.168.2.100:7001
master_repl_offset:4159 
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:4158
192.168.2.101:7002> exit
节点3主
[root@web02 ~]# redis-cli -a qwe123 -h 192.168.2.102 -p 7004
192.168.2.102:7004>  info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.2.102,port=7005,state=online,offset=4187,lag=0  从为192.168.2.102:7005
master_repl_offset:4187
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:4186
192.168.2.102:7004> exit
通过上面可以看出来从节点都不是在本机而是在不同的机器

对节点进行链接测试
[root@web02 ~]# redis-cli -a qwe123 -h 192.168.2.100 -p 7001
192.168.2.100:7001> 
[root@web02 ~]# redis-cli -a qwe123 -h 192.168.2.100 -p 7000
192.168.2.100:7000> 
[root@web02 ~]# redis-cli -a qwe123 -h 192.168.2.101 -p 7002
192.168.2.101:7002> 
[root@web02 ~]# redis-cli -a qwe123 -h 192.168.2.101 -p 7003
192.168.2.101:7003> 
[root@web02 ~]# redis-cli -a qwe123 -h 192.168.2.102 -p 7004
192.168.2.102:7004> 
[root@web02 ~]# redis-cli -a qwe123 -h 192.168.2.102 -p 7005
192.168.2.102:7005> 
赞(0) 打赏
未经允许不得转载:YYQ运维技术博客_运维的工作学习之路 » redis cluster集群搭建
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

运维devops

联系我们关于本博客

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏