centos环境redis 3.x集群搭建 超详细步骤

一 、什么是Redis

Redis是由意大利人Salvatore Sanfilippo(网名:antirez)开发的一款内存高速缓存数据库。Redis全称为:Remote Dictionary Server(远程数据服务),该软件使用C语言编写,Redis是一个key-value存储系统,它支持丰富的数据类型,如:string、list、set、zset(sorted set)、hash。

二、 Redis特点

Redis以内存作为数据存储介质,所以读写数据的效率极高,远远超过数据库。以设置和获取一个256字节字符串为例,它的读取速度可高达110000次/s,写速度高达81000次/s。
Redis跟memcache不同的是,储存在Redis中的数据是持久化的,断电或重启后,数据也不会丢失。因为Redis的存储分为内存存储、磁盘存储和log文件三部分,重启后,Redis可以从磁盘重新将数据加载到内存中,这些可以通过配置文件对其进行配置,正因为这样,Redis才能实现持久化。
Redis支持主从模式,可以配置集群,这样更利于支撑起大型的项目,这也是Redis的一大亮点。

三、 Redis应用场景,它能做什么

众多语言都支持Redis,因为Redis交换数据快,所以在服务器中常用来存储一些需要频繁调取的数据,这样可以大大节省系统直接读取磁盘来获得数据的I/O开销,更重要的是可以极大提升速度。
拿大型网站来举个例子,比如a网站首页一天有100万人访问,其中有一个板块为推荐新闻。要是直接从数据库查询,那么一天就要多消耗100万次数据库请求。上面已经说过,Redis支持丰富的数据类型,所以这完全可以用Redis来完成,将这种热点数据存到Redis(内存)中,要用的时候,直接从内存取,极大的提高了速度和节约了服务器的开销。

四、启动redis

  0、准备机器 系统: centos7.4

redis-1 ip:192.168.10.207 1核2G 100G硬盘
redis-2 ip:192.168.10.208 1核2G 100G硬盘
redis-3 ip:192.168.10.203 1核2G 100G硬盘
redis-4 ip:192.168.10.205 1核2G 100G硬盘
redis-5 ip:192.168.10.206 1核2G 100G硬盘
redis-6 ip:192.168.10.204 1核2G 100G硬盘

  1、下载

redis客户端 密码:b4t1 || 或者官网下载传送门
建议选择第一个 以下教程是根据第一种配置的 如果使用第二种可能有一点点变化
上传到/usr/local/src

  2、解压

 tar -vxzf redis-3.2.11.tgz

  3、配置

cd /usr/local/src/redis-3.2.11/etc
vi redis.conf

配置图 基本配置都配置好了 修改bind 修改自己的IP就可以了。 参数不做讲解自行百度

bind 192.168.10.204
protected-mode no
port 6379
tcp-backlog 128

timeout 0
tcp-keepalive 300

daemonize yes
supervised no
pidfile /usr/local/src/redis-3.2.11/run/redis.pid
logfile /usr/local/src/redis-3.2.11/logs/redis.log

#snapshot default enable
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /usr/local/src/redis-3.2.11/data

slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100

#masterauth foobared
#requirepass foobared


# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
# rename-command CONFIG ""

maxclients 10000
maxmemory 500mb
maxmemory-policy volatile-lru
maxmemory-samples 5

#append only default disable
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes

lua-time-limit 5000

cluster-enabled yes
cluster-config-file /usr/local/src/redis-3.2.11/etc/nodes-6379.conf
cluster-node-timeout 15000

slowlog-log-slower-than 10000
slowlog-max-len 128

latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

4、启动redis 注意redis.conf是上面修改的(启动完成后检查下)

sudo ./redis-server ../etc/redis.conf

五、安装集群必要工具

安装gcc:

yum install gcc-c++

ruby 和 Rubygems 安装 这里不建议使用(yum install ruby)安装有很大几率会进坑的。 别问我怎么知道的 (emmmm 当然踩过的)
建议使用 rvm来安装 传送门 作者这里安装的是2.4.4版本

[admin@iZbp11jguofhwhavcrbmmvZ etc]$ ruby -v
ruby 2.4.4p296 (2018-03-28 revision 63013) [x86_64-linux]
[admin@iZbp11jguofhwhavcrbmmvZ etc]$ gem -v
2.6.14.1

安装好后再需要下载redis

gem install redis

最后通过redis-trib.rb 来启动集群 (/usr/local/src/redis-3.2.11/bin)目录下执行

./redis-trib.rb create --replicas 1 192.168.10.207:6379 192.168.10.208:6379 192.168.10.203:6379 192.168.10.205:6379 192.168.10.206:6379 192.168.10.204:6379

错误1 :出现如下错误 解决方案 传送门

[admin@iZbp11jguofhwhavcrbmmsZ bin]$ ./redis-trib.rb create --replicas 1 192.168.10.207:6379 192.168.10.208:6379 192.168.10.203:6379 192.168.10.205:6379 192.168.10.206:6379 192.168.10.204:6379
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.10.207:6379
192.168.10.208:6379
192.168.10.203:6379
Adding replica 192.168.10.205:6379 to 192.168.10.207:6379
Adding replica 192.168.10.206:6379 to 192.168.10.208:6379
Adding replica 192.168.10.204:6379 to 192.168.10.203:6379
M: c18a7905132e35b39ab9c30b35383a011421cb0d 192.168.10.207:6379
   slots:0-5460 (5461 slots) master
M: add83ab5baf1028c82ac46ea22ea2199be2d07b1 192.168.10.208:6379
   slots:5461-10922 (5462 slots) master
M: b4c09c212755f76f8017c3b743bc948dc23561b2 192.168.10.203:6379
   slots:10923-16383 (5461 slots) master
S: 1a0494af9e47316204b0b556dc666fa29f6bd3da 192.168.10.205:6379
   replicates c18a7905132e35b39ab9c30b35383a011421cb0d
S: a18e7dbf7126b8b85fa04d78e07725d8339cef82 192.168.10.206:6379
   replicates add83ab5baf1028c82ac46ea22ea2199be2d07b1
S: 3dcfe849bbbebc751c438872907bfa35ff197266 192.168.10.204:6379
   replicates b4c09c212755f76f8017c3b743bc948dc23561b2
Can I set the above configuration? (type 'yes' to accept): yes
/home/admin/.rvm/gems/ruby-2.4.4/gems/redis-4.0.1/lib/redis/client.rb:119:in `call': ERR Slot 1382 is already busy (Redis::CommandError)
    from /home/admin/.rvm/gems/ruby-2.4.4/gems/redis-4.0.1/lib/redis.rb:2764:in `block in method_missing'
    from /home/admin/.rvm/gems/ruby-2.4.4/gems/redis-4.0.1/lib/redis.rb:45:in `block in synchronize'
    from /home/admin/.rvm/rubies/ruby-2.4.4/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
    from /home/admin/.rvm/gems/ruby-2.4.4/gems/redis-4.0.1/lib/redis.rb:45:in `synchronize'
    from /home/admin/.rvm/gems/ruby-2.4.4/gems/redis-4.0.1/lib/redis.rb:2763:in `method_missing'
    from ./redis-trib.rb:212:in `flush_node_config'
    from ./redis-trib.rb:776:in `block in flush_nodes_config'
    from ./redis-trib.rb:775:in `each'
    from ./redis-trib.rb:775:in `flush_nodes_config'
    from ./redis-trib.rb:1296:in `create_cluster_cmd'
    from ./redis-trib.rb:1701:in `
' [admin@iZbp11jguofhwhavcrbmmsZ bin]$ ./redis-cli -h 192.168.10.207 -p 6379

错误2:

[ERR] Node 192.168.10.204:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

解决方案: https://www.jianshu.com/p/7720c922dd80

正确姿势

[admin@iZbp11jguofhwhavcrbmmsZ bin]$ ./redis-trib.rb create --replicas 1 192.168.10.207:6379 192.168.10.208:6379 192.168.10.203:6379 192.168.10.205:6379 192.168.10.206:6379 192.168.10.204:6379
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.10.207:6379
192.168.10.208:6379
192.168.10.203:6379
Adding replica 192.168.10.205:6379 to 192.168.10.207:6379
Adding replica 192.168.10.206:6379 to 192.168.10.208:6379
Adding replica 192.168.10.204:6379 to 192.168.10.203:6379
M: c18a7905132e35b39ab9c30b35383a011421cb0d 192.168.10.207:6379
   slots:0-5460 (5461 slots) master
M: add83ab5baf1028c82ac46ea22ea2199be2d07b1 192.168.10.208:6379
   slots:5461-10922 (5462 slots) master
M: b4c09c212755f76f8017c3b743bc948dc23561b2 192.168.10.203:6379
   slots:10923-16383 (5461 slots) master
S: 1a0494af9e47316204b0b556dc666fa29f6bd3da 192.168.10.205:6379
   replicates c18a7905132e35b39ab9c30b35383a011421cb0d
S: a18e7dbf7126b8b85fa04d78e07725d8339cef82 192.168.10.206:6379
   replicates add83ab5baf1028c82ac46ea22ea2199be2d07b1
S: 3dcfe849bbbebc751c438872907bfa35ff197266 192.168.10.204:6379
   replicates b4c09c212755f76f8017c3b743bc948dc23561b2
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.10.207:6379)
M: c18a7905132e35b39ab9c30b35383a011421cb0d 192.168.10.207:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: a18e7dbf7126b8b85fa04d78e07725d8339cef82 192.168.10.206:6379
   slots: (0 slots) slave
   replicates add83ab5baf1028c82ac46ea22ea2199be2d07b1
M: add83ab5baf1028c82ac46ea22ea2199be2d07b1 192.168.10.208:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: b4c09c212755f76f8017c3b743bc948dc23561b2 192.168.10.203:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 3dcfe849bbbebc751c438872907bfa35ff197266 192.168.10.204:6379
   slots: (0 slots) slave
   replicates b4c09c212755f76f8017c3b743bc948dc23561b2
S: 1a0494af9e47316204b0b556dc666fa29f6bd3da 192.168.10.205:6379
   slots: (0 slots) slave
   replicates c18a7905132e35b39ab9c30b35383a011421cb0d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

你可能感兴趣的:(centos环境redis 3.x集群搭建 超详细步骤)