zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

Redis Cluster 搭建手册

Redis 搭建 手册 cluster
2023-09-14 09:00:28 时间

1 下载最新版redis

wget http://download.redis.io/releases/redis-3.2.3.tar.gz


2  编译安装

#安装到/opt/redis-cluster目录


tar zxvf redis-3.2.3.tar.gz

cd redis-3.2.3/

make

make PREFIX=/opt/redis-cluster install


#创建配置目录,拷贝相关文件

mkdir /opt/redis-cluster/{log conf rdb run}


#redis目录

cp src/redis-trib.rb /opt/redis-cluster/bin/


3  安装其他依赖包

apt-get install ruby 2.3.1


gem install redis


4  创建配置文件

redis.conf

 

bind 0.0.0.0

daemonize yes

pidfile /opt/redis-cluster/run/redis-6379.pid

(pidfile /usr/local/etc/redis/run/redis.pid)

port 6379

tcp-backlog 511

timeout 0

tcp-keepalive 0

loglevel notice

logfile "/opt/redis-cluster/log/redis-6379.log"

databases 16

save 900 1

save 300 10

save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename dump-6379.rdb

dir /opt/redis-cluster/rdb

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

appendonly yes

appendfilename "redis-6379.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 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-entries 512

list-max-ziplist-value 64

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


其实大部分都是原有的配置信息,不需要做大的改动,红色文字部分都需要调整,与集群相关的配置如下:

cluster-enabled yes

cluster-config-file nodes-6379.conf

cluster-node-timeout 15000


注意其中的nodes-6379.conf这个文件不需要创建,在初始化集群的时候会自动创建的。

5    在启动redis实例前先修改一些系统级别的配置

echo never /sys/kernel/mm/transparent_hugepage/enabled
# 打开/etc/sysctl.conf,追加如下内容
vm.overcommit_memory = 1
#使配置生效
sysctl -p


6    启动所有redis实例

cd /opt/redis-cluster/bin

./redis-server /opt/redis-cluster/conf/redis-6379.conf

./redis-server /opt/redis-cluster/conf/redis-6380.conf

./redis-server /opt/redis-cluster/conf/redis-6381.conf

./redis-server /opt/redis-cluster/conf/redis-7379.conf

./redis-server /opt/redis-cluster/conf/redis-7380.conf

./redis-server /opt/redis-cluster/conf/redis-7381.conf



检查一下端口监听的状态:

netstat -tupln



7    redis-trib.rb

        redis-trib.rb是一个官方提供的用来操作cluster的ruby脚本,我们后面管理cluster会经常使用到这个脚本

Usage: redis-trib command options arguments ...


  create          host1:port1 ... hostN:portN

                  --replicas arg

  check           host:port

  fix             host:port

  reshard         host:port

                  --from arg

                  --to arg

                  --slots arg

                  --yes

  add-node        new_host:new_port existing_host:existing_port

                  --slave

                  --master-id arg

  del-node        host:port node_id

  set-timeout     host:port milliseconds

  call            host:port command arg arg .. arg

  import          host:port

                  --from arg

  help            (show this help)


For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.


8    初始化启动cluster

切记用具体IP启动

./redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:7379 127.0.0.1:7380 127.0.0.1:7381


./redis-trib.rb create --replicas 1 10.157.26.84:6379 10.157.26.85:6379 10.157.26.86:6379 10.157.26.87:6379 10.157.26.88:6379 10.157.26.89:6379


创建结果如下

Creating cluster

Performing hash slots allocation on 6 nodes...

Using 3 masters:

127.0.0.1:6379

127.0.0.1:6380

127.0.0.1:6381

Adding replica 127.0.0.1:7379 to 127.0.0.1:6379

Adding replica 127.0.0.1:7380 to 127.0.0.1:6380

Adding replica 127.0.0.1:7381 to 127.0.0.1:6381

M: e85567eff146a649f971b7e543e8421fe64c29e0 127.0.0.1:6379

   slots:0-5460 (5461 slots) master

M: b58997471998a59a4238bec506a8452a04187749 127.0.0.1:6380

   slots:5461-10922 (5462 slots) master

M: 34c068fe3182321bb53b5c3a106676095dc03bdd 127.0.0.1:6381

   slots:10923-16383 (5461 slots) master

S: bc4f4a6c25135dc9ede0428b3567d4e5a6db87bf 127.0.0.1:7379

   replicates e85567eff146a649f971b7e543e8421fe64c29e0

S: ea56cb0020e8c24a55c0301bd073aedc99da0206 127.0.0.1:7380

   replicates b58997471998a59a4238bec506a8452a04187749

S: 021ea707f99795088d474cc622b25b928e8ed73f 127.0.0.1:7381

   replicates 34c068fe3182321bb53b5c3a106676095dc03bdd

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:6379)

M: e85567eff146a649f971b7e543e8421fe64c29e0 127.0.0.1:6379

   slots:0-5460 (5461 slots) master

M: b58997471998a59a4238bec506a8452a04187749 127.0.0.1:6380

   slots:5461-10922 (5462 slots) master

M: 34c068fe3182321bb53b5c3a106676095dc03bdd 127.0.0.1:6381

   slots:10923-16383 (5461 slots) master

M: bc4f4a6c25135dc9ede0428b3567d4e5a6db87bf 127.0.0.1:7379

   slots: (0 slots) master

   replicates e85567eff146a649f971b7e543e8421fe64c29e0

M: ea56cb0020e8c24a55c0301bd073aedc99da0206 127.0.0.1:7380

   slots: (0 slots) master

   replicates b58997471998a59a4238bec506a8452a04187749

M: 021ea707f99795088d474cc622b25b928e8ed73f 127.0.0.1:7381

   slots: (0 slots) master

   replicates 34c068fe3182321bb53b5c3a106676095dc03bdd

[OK] All nodes agree about slots configuration.

Check for open slots...

Check slots coverage...

[OK] All 16384 slots covered.



  检查一下cluster的状态: ./redis-trib.rb check 127.0.0.1:6379


Performing Cluster Check (using node 127.0.0.1:6379)

M: e85567eff146a649f971b7e543e8421fe64c29e0 127.0.0.1:6379

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

S: ea56cb0020e8c24a55c0301bd073aedc99da0206 127.0.0.1:7380

   slots: (0 slots) slave

   replicates b58997471998a59a4238bec506a8452a04187749

S: bc4f4a6c25135dc9ede0428b3567d4e5a6db87bf 127.0.0.1:7379

   slots: (0 slots) slave

   replicates e85567eff146a649f971b7e543e8421fe64c29e0

M: 34c068fe3182321bb53b5c3a106676095dc03bdd 127.0.0.1:6381

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 021ea707f99795088d474cc622b25b928e8ed73f 127.0.0.1:7381

   slots: (0 slots) slave

   replicates 34c068fe3182321bb53b5c3a106676095dc03bdd

M: b58997471998a59a4238bec506a8452a04187749 127.0.0.1:6380

   slots:5461-10922 (5462 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.



9  随便接入cluster的某个节点进行测试

redis-cli -c -h 127.0.0.1 -p 6379

注意:redis-cli在接入集群模式时,要使用-c参数  

127.0.0.1:6379 keys *

(empty list or set)

127.0.0.1:6379 set test nidaye

- Redirected to slot [6918] located at 127.0.0.1:6380

OK

127.0.0.1:6380 get test

"nidaye"

127.0.0.1:6380 set nihaogepi nihaogepi

- Redirected to slot [5243] located at 127.0.0.1:6379

OK

127.0.0.1:6379



redis.conf配置文件

placeholder?type=unknown name=redis-6379


Redis数据类型选型手册 CSAPP 中的优化程序的性能章节中提到过,选择合适的算法和数据结构是优化程序的方向之一。Redis 官网中也明确提到,Redis 拥有突出的表现,不仅是因为它在内存中操作,还因为它的键值对都是按一定的数据结构来组织的,并最终对这些数据结构进行增删改查的操作。所以高效的数据结构是 Redis 拥有高性能的基石。
分布式服务器框架之搭建C#+MongoDB+Redis初步 WebAccount站点主要干的事儿是下发 服务器状态信息,这个服务器会和WorldServer建立连接,等所有的GameServer初始化完成之后会同步给WorldServer,WorldServer同步给账号服务器站点,然后账号站点等待玩家请求。
分布式服务器框架之搭建C#+MongoDB+Redis初步 Common类库主要做的是一些大家都需要用到的通用的事情,为了避免重复,所以就提取出来了一个Dll。Common库主要是实现了表格数据的加载模块、在CsRedisClient、MongoClient中间件的基础上进行二次封装。实现了数据库连接、Redis连接、以及数据库和Redis的增删改查断开操作。
redis总结万能手册,熟悉不等于精通; 日常总结:redis线程模型,多路复用原理;单体,哨兵架构,集群架构的自动化安装,解释说明,集群扩容,缩容,选举,主从自动切换策略,应用程序接入……
非常适合新手的redis cluster搭建过程 以下就是Redis Cluster科普级别的知识, 帮助大家快速全面了解Redis Cluster特性, 快速搭建Redis Cluster。 好记性不如烂笔头,倒腾一次,记录一次。
Redis 问题排查解决手册(值得收藏) 性能相关的数据指标 通过Redis-cli命令行界面访问到Redis服务器,然后使用info命令获取所有与Redis服务相关的信息。通过这些信息来分析文章后面提到的一些性能指标。