redis学习redisRedis

Redis集群搭建以及Python操作集群

2018-11-17  本文已影响15人  其实我很dou

强烈推荐redis书籍 <<redis开发与运维>>

一. 概述.

1. 单机redis部署https://www.jianshu.com/p/acd3281d9074

2. redis集群介绍

Redis Cluster是Redis的分布式解决方案,在3.0版本正式推出,有效地解
决了Redis分布式方面的需求。当遇到单机内存、并发、流量等瓶颈时,可
以采用Cluster架构方案达到负载均衡的目的

3. 集群原理参照redis开发与运维, 作者讲的非常好, 一看便知

4. redis集群集群功能限制

Redis集群相对单机在功能上存在一些限制,需要开发人员提前了解,
在使用时做好规避。限制如下:

二. redis集群搭建

1. 准备节点

redis-benchmark redis-check-aof redis-check-rdb
redis-cli   redis-sentinel  redis-server    redis-trib.rb
mkdir -p  /usr/local/redis/bin
mkdir -p  /usr/local/redis/cluster
mkdir -p /usr/local/redis/cluster/7000

cd src
# 拷贝可执行文件
cp redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server redis-trib.rb /usr/local/redis/bin/

# 拷贝配置文件
cd .. && cp redis.conf /usr/local/redis/7000

#将/usr/local/redis/bin路径加入到环境变量
vim /etc/profile
PATH=/usr/local/redis/bin:$PATH
保存后执行source /etc/profile生效
cd /usr/local/redis/cluster
# 修改配置文件
vim 7000/redis.conf
# 绑定网卡(依据自己需求)
bind 0.0.0.0
# 端口
port 7000
# 守护进程运行方式开启
daemonize yes
# pid文件
pidfile /var/run/redis_700.pid
# 日志文件保存位置
logfile "/usr/local/redis/cluster/7000/redis.log"
# 目录设置
dir "/usr/local/redis/cluster/7000"
# 集群配置
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 15000
cd /usr/local/redis/cluster
cp -r 7000 7001
cp -r 7000 7002
cp -r 7000 7003
cp -r 7000 7004
cp -r 7000 7005
cp -r 7000 7006

# 使用sed修改, 将配置文件中的7000替换成各自的节点
sed -i 's/7000/7001/g' 7001/redis.conf 
sed -i 's/7000/7002/g' 7002/redis.conf 
sed -i 's/7000/7003/g' 7003/redis.conf 
sed -i 's/7000/7004/g' 7004/redis.conf 
sed -i 's/7000/7005/g' 7005/redis.conf
yum install tree -y
[root@instance-kbjefgng cluster]# tree
.
├── 7000
│   ├── redis.conf
├── 7001
│   ├── redis.conf
├── 7002
│   ├── redis.conf
├── 7003
│   ├── redis.conf
├── 7004
│   ├── redis.conf
└── 7005
    ├── redis.conf

[root@instance-kbjefgng redis]# tree bin
bin
├── redis-benchmark
├── redis-check-aof
├── redis-check-rdb
├── redis-cli
├── redis-sentinel
├── redis-server
└── redis-trib.rb
redis-server 7000/redis.conf 
redis-server 7001/redis.conf 
redis-server 7002/redis.conf 
redis-server 7003/redis.conf 
redis-server 7004/redis.conf 
redis-server 7005/redis.conf

2. 用redis-trib.rb搭建集群

redis-trib.rb是采用Ruby实现的Redis集群管理工具。内部通过Cluster相
关命令帮我们简化集群创建、检查、槽迁移和均衡等常见运维操作,使用之
前需要安装Ruby依赖环境

# ruby官网
# http://www.ruby-lang.org/en/downloads/
# 选择合适的版本, 本宝选择2.4.5
wget https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.5.tar.gz

# 常规操作
tar -zxf ruby-2.4.5.tar.gz 
cd ruby-2.4.5/
./configure --prefix=/usr/local/ruby
make && make install
vim /etc/profile
PATH=/usr/local/ruby/bin:$PATH
source 生效
# 使用ruby包管理器gem安装redis
gem install redis -v 3.2.1
[root@instance-kbjefgng redis]# redis-trib.rb help
Usage: redis-trib <command> <options> <arguments ...>

  create          host1:port1 ... hostN:portN
                  --replicas <arg>
  check           host:port

构建过程如下:

[root@instance-kbjefgng cluster]# redis-trib.rb create --replicas 1  0.0.0.0:7000 0.0.0.0:7001 0.0.0.0:7002 0.0.0.0:7003 0.0.0.0:7004 0.0.0.0:7005

# 一个副本

>>> Creating cluster
/usr/local/ruby/lib/ruby/gems/2.4.0/gems/redis-3.2.1/lib/redis/client.rb:443: warning: constant ::Fixnum is deprecated
>>> Performing hash slots allocation on 6 nodes...
# 主从情况
Using 3 masters:
0.0.0.0:7000
0.0.0.0:7001
0.0.0.0:7002
Adding replica 0.0.0.0:7003 to 0.0.0.0:7000
Adding replica 0.0.0.0:7004 to 0.0.0.0:7001
Adding replica 0.0.0.0:7005 to 0.0.0.0:7002
# 槽情况, 总共0-16383个槽, 分配到三个master节点上
M: 7477f892ff332ab393face782be606626fe1336d 0.0.0.0:7000
   slots:0-5460 (5461 slots) master
M: 97b1cef8e9ef167158a57649af82e5f5b7c518c3 0.0.0.0:7001
   slots:5461-10922 (5462 slots) master
M: 0f24de58844cb3c6ec355d4c272b122cfb601936 0.0.0.0:7002
   slots:10923-16383 (5461 slots) master
S: d67c8782e7cce986fbc300a3425642046370b830 0.0.0.0:7003
   replicates 7477f892ff332ab393face782be606626fe1336d
S: 26ff32fb6f241eab220722caf581df9e96d3ab23 0.0.0.0:7004
   replicates 97b1cef8e9ef167158a57649af82e5f5b7c518c3
S: aa7ed81beb087d265ac7a9c72dddf9f5e35da6f3 0.0.0.0:7005
   replicates 0f24de58844cb3c6ec355d4c272b122cfb601936
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 0.0.0.0:7000)
M: 7477f892ff332ab393face782be606626fe1336d 0.0.0.0:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: d67c8782e7cce986fbc300a3425642046370b830 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 7477f892ff332ab393face782be606626fe1336d
S: 26ff32fb6f241eab220722caf581df9e96d3ab23 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 97b1cef8e9ef167158a57649af82e5f5b7c518c3
S: aa7ed81beb087d265ac7a9c72dddf9f5e35da6f3 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 0f24de58844cb3c6ec355d4c272b122cfb601936
M: 97b1cef8e9ef167158a57649af82e5f5b7c518c3 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 0f24de58844cb3c6ec355d4c272b122cfb601936 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

所有槽都分配到节点

[root@instance-kbjefgng redis]# tree cluster/
cluster/
├── 7000
│   ├── dump.rdb
│   ├── nodes-7000.conf
│   ├── redis.conf
│   └── redis.log
├── 7001
│   ├── dump.rdb
│   ├── nodes-7001.conf
│   ├── redis.conf
│   └── redis.log
├── 7002
│   ├── dump.rdb
│   ├── nodes-7002.conf
│   ├── redis.conf
│   └── redis.log
├── 7003
│   ├── dump.rdb
│   ├── nodes-7003.conf
│   ├── redis.conf
│   └── redis.log
├── 7004
│   ├── dump.rdb
│   ├── nodes-7004.conf
│   ├── redis.conf
│   └── redis.log
└── 7005
    ├── dump.rdb
    ├── nodes-7005.conf
    ├── redis.conf
    └── redis.log

6 directories, 24 files

三. python操作redis集群

上一篇 下一篇

猜你喜欢

热点阅读