2.redis集群搭建

集群模式下开启服务端

start-redis.sh

集群模式下开启客户端
redis-cli -c -h 192.168.116.101 -p 7000
集群模式下关闭服务端
stop-redis.sh
 
 
主从模式
master -- slave
key: 互为副本存储
        hash  相当于分桶机制
 
1.在 /soft/redis  目录  下
su
    root 用户 下
 
2.mkdir conf 
    cd conf
 
 
3.修改redis.conf文件
################################## NETWORK #####################################
#绑定地址
bind 192.168.116.101
 
#关闭保护模式
protected-mode no
 
#监听端口
port 7000
 
################################# GENERAL #####################################
#守护进程
daemonize yes
 
#pid文件
pidfile /var/run/pid_7000.pid
 
#数据库数量
databases 16
 
################################ SNAPSHOTTING  ################################
#数据库文件名
dbfilename dump.rdb
 
#存放目录
dir ./
 
################################ REDIS CLUSTER  ###############################
#启用集群
cluster-enabled yes
 
#集群生成的配置文件名
cluster-config-file nodes-7000.conf
 
4.在conf目录下
1)创建六个文件夹
for x in 0 1 2 3 4 5 ; do mkdir 700$x ; done
2)复制上级目录的内容到文件夹
for x in 0 1 2 3 4 5 ; do cp ../redis.conf  700$x/ ; done
3)替换文本中内容
for x in 0 1 2 3 4 5 ; do sed -i  s/6379/700$x/g  700$x/redis.conf  ; done
for x in 0 1 2 3 4 5 ; do sed -i  s/bind 0.0.0.0/bind 192.168.116.101/g  700$x/redis.conf  ; done
 
5.安装gem
安装gem软件包
sudo yum install -y gem
删除旧的gem源
sudo gem sources --remove https://rubygems.com/
添加新的gem源
sudo gem sources -a https://gems.ruby-china.com/
使用gem安装redis 3.0.0 版本
sudo gem install redis --version 3.0.0
 
6.redis脚本管理
启动集群脚本
 
#!/bin/bash 
cd /soft/redis/conf/7000 
redis-server redis.conf & 
cd ../7001 
redis-server redis.conf & 
cd ../7002 
redis-server redis.conf & 
cd ../7003 
redis-server redis.conf & 
cd ../7004
redis-server redis.conf & 
cd ../7005
redis-server redis.conf &
 
停止集群脚本
#!/bin/bash 
netstat -anop | grep 700 | grep LIST | grep redis-server | awk  '{print $7}' | awk -F / '{print $1}' | kill -9 `xargs`
 
查看集群进程脚本
#!/bin/bash
netstat -anop | grep 700 | grep LIST | grep redis-server
 
 
7.启动集群后 将各主机上的redis进程加入集群部落
执行redis源代码目录下的redis-trib.rb脚本,切记是该文件在源代码目录中,如下图所示:
 
/home/centos/redis-3.2.8/src   目录下
执行如下命令,将各节点加入集群部落:
./redis-trib.rb create --replicas 1 192.168.116.101:7000  192.168.116.101:7001  192.168.116.101:7002  192.168.116.101:7003  192.168.116.101:7004  192.168.116.101:7005
 
 
执行结果如下
 
 
7.启用客户端 连接集群
#-c表示启用集群,-h连接主机 -p连接端口
 $>redis-cli -c -h 192.168.116.101 -p 7000
 
 
8.执行key操作,考察集群重定向
对key操作时,会对key进行hash计算,重定向到相应的redis服务器进行存储
 

redis.conf完整配置
################################## INCLUDES ###################################
 
################################## NETWORK #####################################
bind 192.168.116.101
protected-mode no
port 7000
tcp-backlog 511
timeout 0
tcp-keepalive 300
 
################################# GENERAL #####################################
daemonize yes
supervised no
pidfile /var/run/redis_7000.pid
loglevel notice
logfile ""
databases 16
 
################################ SNAPSHOTTING  ################################
 
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump_7000.rdb
dir ./
 
################################# REPLICATION #################################
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
################################## SECURITY ###################################
################################### LIMITS ####################################
############################## APPEND ONLY MODE ###############################
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 SCRIPTING  ###############################
lua-time-limit 5000
 
################################ REDIS CLUSTER  ###############################
cluster-enabled yes
cluster-config-file nodes-7000.conf
################################## SLOW LOG ###################################
slowlog-log-slower-than 10000
slowlog-max-len 128
 
################################ LATENCY MONITOR ##############################
latency-monitor-threshold 0
 
############################# EVENT NOTIFICATION ##############################
notify-keyspace-events ""
 
############################### ADVANCED CONFIG ###############################
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
 
 
 
 
 
 
 
 
 
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/star521/p/9863245.html