redis数据库之--rediscluster集群搭建

一、环境
三台CentOS7.5服务器(每台服务器两个实例,以一台服务器为例,另外两台服务器配置相同,安装相同)
192.168.10.176
192.168.10.177
192.168.10.178
二、集群模式(cluster)
1、下载redis、ruby、rubygems二进制包,创建包存放目录,解压目录。

mkdir /tools
mkdir /depend
cd /tools
wget http://download.redis.io/releases/redis-3.2.12.tar.gz
wget http://ftp.ruby-lang.org/pub/ruby/2.4/ruby-2.4.4.tar.gz
wget https://rubygems.org/rubygems/rubygems-2.7.7.tgz
wget https://rubygems.org/downloads/redis-3.2.0.gem
tar xzf redis-3.2.12.tar.gz -C /depend/
tar xzf ruby-2.4.4.tar.gz -C /depend/
tar xzf rubygems-2.7.7.tgz -C /depend/

2、安装redis和ruby同时加入环境变量

cd /depend/redis-3.2.12/
make -j 8 && make install PREFIX=/opt/redis
cd ../ruby-2.4.4/
./configure --prefix=/opt/ruby
make -j 8 && make install
echo "# redis" >> /etc/profile
echo "export HOME=/opt/redis/bin" >> /etc/profile
echo "export PATH=$PATH:$HOME" >> /etc/profile
echo "# ruby" >> /etc/profile
echo "export HOME=/opt/ruby/bin" >> /etc/profile
echo "export PATH=$PATH:$HOME" >> /etc/profile
source /etc/profile

3、安装rubygems

[root@localhost ruby-2.4.4]# cd /tools
[root@localhost tools]# gem install redis-3.2.0.gem  
ERROR:  Loading command: install (LoadError)
	cannot load such file -- zlib
ERROR:  While executing gem ... (NoMethodError)
    undefined method `invoke_with_build_args' for nil:NilClass

4、安装失败,这是需要依赖zlib工具才能安装,下载zlib工具,解压安装。

[root@localhost rubygems-2.7.7]# cd /tools/
[root@localhost tools]# wget http://www.zlib.net/zlib-1.2.11.tar.gz
[root@localhost tools]# tar xzf zlib-1.2.11.tar.gz -C /depend/
[root@localhost tools]# cd /depend/zlib-1.2.11/

# 编译和安装过程省略
[root@localhost zlib-1.2.11]# ./configure --prefix=/usr/local/zlib
[root@localhost zlib-1.2.11]# make -j 8 && make install

5、安装完成后执行以下命令,安装zlib

[root@localhost ruby-2.4.4]# cd /depend/ruby-2.4.4/ext/zlib/
[root@localhost zlib]# ruby extconf.rb --with-zlib-include=/usr/local/zlib/include/ --with-zlib-lib=/usr/local/zlib/lib
[root@localhost zlib]# make -j 8 && make install
make: *** No rule to make target `/include/ruby.h', needed by `zlib.o'.  Stop.

6、安装zlib报错,修改Makefile文件中:zlib.o: $(top_srcdir)/include/ruby.h 改成:zlib.o: …/…/include/ruby.h

vi Makefile
zlib.o: ../../include/ruby.h

7、继续执行make -j 8 && make install

[root@localhost zlib]# make -j 8 && make install
compiling zlib.c
linking shared-object zlib.so
/usr/bin/install -c -m 0755 zlib.so /opt/ruby/lib/ruby/site_ruby/2.4.0/x86_64-linux

8、回到gem目录下,继续安装redis-3.2.0.gem

[root@localhost tools]# gem install redis-3.2.
[root@localhost tools]# gem install redis-3.2.0.gem 
ERROR:  While executing gem ... (Gem::Exception)
    Unable to require openssl, install OpenSSL and rebuild Ruby (preferred) or use non-HTTPS sources

9、安装报错,需要依赖OpenSSL,继续下载openssl安装。

wget https://www.openssl.org/source/old/1.0.2/openssl-1.0.2l.tar.gz
tar xzf openssl-1.0.2l.tar.gz-C /depend
cd /depend/openssl-1.0.2l
./config -fPIC --prefix=/usr/local/openssl enable-shared
Operating system: x86_64-whatever-linux2
You need Perl 5

10、编译失败,需要perl工具,下载安装perl工具,在安装OpenSSL。

cd /tools
wget https://www.cpan.org/src/5.0/perl-5.28.0.tar.gz
tar xzf perl-5.28.0.tar.gz -C /depend/
cd /depend/perl-5.28.0/
./Configure -des -Dprefix=/usr/local/localperl
make -j 8 && make install
cd /depend/openssl-1.0.2l
./config -fPIC --prefix=/usr/local/openssl enable-shared
./config -t
make -j 8 && make install

11、安装完成后跟zlib一样执行以下命令

cd ../ruby-2.4.4/ext/openssl/
ruby extconf.rb --with-openssl-include=/usr/local/openssl/include/ --with-openssl-lib=/usr/local/openssl/lib

[root@localhost openssl]# make -j 8 && make install
compiling openssl_missing.c
make: *** No rule to make target `/include/ruby.h', needed by `ossl.o'.  Stop.
make: *** Waiting for unfinished jobs....

12、安装报错,跟zlib一样的类似错误,修改Makefile文件中:将全部$(top_srcdir)全部替换成…/…

vi Makefile
# 执行以下命令替换所有
:%s/\$(top_srcdir)/\.\.\/\.\./g

13、继续执行make -j 8 && make install

make -j 8 && make install

14、回到gem目录下,继续安装redis-3.2.0.gem

[root@localhost openssl]# cd /tools/
[root@localhost tools]# gem install redis-3.2.0.gem 
Successfully installed redis-3.2.0
Parsing documentation for redis-3.2.0
Installing ri documentation for redis-3.2.0
Done installing documentation for redis after 1 seconds
1 gem installed

15、安装成功,创建目录拷贝配置文件。

[root@localhost conf]# mkdir -pv /opt/redis/data/6379
[root@localhost conf]# mkdir -pv /opt/redis/data/6479
[root@localhost conf]# mkdir /opt/redis/logs
[root@localhost conf]# mkdir /opt/redis/conf
[root@localhost conf]# mkdir /opt/redis/pid
[root@localhost conf]# mkdir /opt/redis/scripts

# 回到解压目录拷贝redis.conf配置文件
[root@localhost redis-3.2.12]# cp redis.conf /opt/redis/conf/
[root@localhost redis-3.2.12]# cd /opt/redis/conf/
[root@localhost conf]# mv redis.conf redis-6379.conf
[root@localhost conf]# cp redis-6379.conf redis-6479.conf
[root@localhost conf]# cat redis-6379.conf 
protected-mode yes
bind 192.168.10.177 127.0.0.1
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/opt/redis/pid/redis_6379.pid"
loglevel notice
logfile "/opt/redis/logs/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.rdb"
dir "/opt/redis/data/6379"
appendonly yes
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 "/opt/redis/conf/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
maxmemory 16gb
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
# Generated by CONFIG REWRITE
#masterauth "test@2019"
#requirepass "test@2019"
maxclients 4064

16、启动并查看redis

nohup redis-server conf/redis-6379.conf
nohup redis-server conf/redis-6479.conf
ps aux | grep redis
root     10476  0.0  0.0 141072  7576 ?        Ssl  14:56   0:01 redis-server 192.168.10.177:6379 [cluster]
root     10525  0.0  0.0 136976  7556 ?        Ssl  14:57   0:01 redis-server 192.168.10.177:6479 [cluster]
root     11483  0.0  0.0 112708   984 pts/0    S+   15:16   0:00 grep --color=auto redis

17、启动集群设置密码及验证集群

redis-trib.rb create --replicas 1 192.168.10.176:6379 192.168.10.176:6479 192.168.10.177:6379 192.168.10.177:6479 192.168.10.178:6379 192.168.10.178:6479

启动成功后设置密码

redis-cli -c -h 192.168.10.176 -p 6379
config set masterauth test@123
config set requirepass test@123
auth test@123
config rewrite

验证集群
chisongzi
chisongzi
删除单个url换成

redis-cli -c -h 192.168.10.176 -p 6379
auth passwd
get portal:perm:url:cache:10013
del portal:perm:url:cache:10013

查看所有key值:keys *
删除指定索引的值:del key
清空整个 Redis 服务器的数据:flushall
清空当前库中的所有 key:flushdb
18、启动和关闭redis脚本
在这里插入图片描述
启动脚本

[root@localhost scripts]# cat start.sh 
#!/bin/bash
read -p "plese [6379|6479|all]: " redis
if [ $redis = 6379 ];then
  nohup redis-server /opt/redis/conf/redis-6379.conf
fi 
if [ $redis = 6479 ];then
  nohup redis-server /opt/redis/conf/redis-6479.conf
fi
if [ $redis = all ];then
  nohup redis-server /opt/redis/conf/redis-6379.conf
  nohup redis-server /opt/redis/conf/redis-6479.conf
fi

关闭脚本

[root@localhost scripts]# cat 6379_stop.exp 
#!/usr/bin/expect
set user "root"
set host [lindex $argv 0]
set timeout 1000
set password "test@123"
set db_pass "test@123"
spawn ssh $user@192.168.10.$host
expect {
  "*yes/no*" {send "yes\r"; exp_continue}
  "root@192.168*" {send "$password\r"; exp_continue}
  "*]*" {send "redis-cli -c -h 192.168.10.$host -p 6379\r"}
}
expect "192.168*>" {send "auth $db_pass\r"}
expect "192.168*>" {send "shutdown\r"}
expect "not*" {send "quit\r"}
expect eof

[root@localhost scripts]# cat 6479_stop.exp 
#!/usr/bin/expect
set user "root"
set host [lindex $argv 0]
set timeout 1000
set password "test@123"
set db_pass "test@123"
spawn ssh $user@192.168.10.$host
expect {
  "*yes/no*" {send "yes\r"; exp_continue}
  "root@192.168*" {send "$password\r"; exp_continue}
  "*]*" {send "redis-cli -c -h 192.168.10.$host -p 6479\r"}
}
expect "192.168*>" {send "auth $db_pass\r"}
expect "192.168*>" {send "shutdown\r"}
expect "not*" {send "quit\r"}
expect eof

[root@localhost scripts]# cat all_stop.exp 
#!/usr/bin/expect
set user "root"
set host [lindex $argv 0]
set port1 [lindex $argv 1]
set port2 [lindex $argv 2]
set timeout 1000
set password "test@123"
set db_pass "test@123"
spawn ssh $user@192.168.10.$host
expect {
  "*yes/no*" {send "yes\r"; exp_continue}
  "root@192.168*" {send "$password\r"; exp_continue}
  "*]*" {send "redis-cli -c -h 192.168.10.$host -p $port1\r"; exp_continue}
  "192.168*>" {send "auth $db_pass\r"}
  #"192.168*>" {send "shutdown\r"; exp_continue}
  #"not*" {send "quit\r"}
}
expect "192.168*>" {send "shutdown\r"}
expect "not*" {send "quit\r"}
expect { 
  "*]*" {send "redis-cli -c -h 192.168.10.$host -p $port2\r"; exp_continue}
  "192.168.10*>" {send "auth $db_pass\r"}
  #"192.168.10.$host:6479>" { send "auth $db_pass\r"}
#  "192.168.10.$host:6479>" { send "shutdown\r"; exp_continue}
#  "not*" {send "quit\r"}
}
expect "192.168.10.$host:6479>" { send "shutdown\r"}
expect "not*" {send "quit\r"}
expect eof

[root@localhost scripts]# cat shutdown.sh 
#!/bin/bash
read -p "plese [6379|6479|all]: " redis
if [ $redis = 6379 ];then
  ./6379_stop.exp 176
fi 
if [ $redis = 6479 ];then
  ./6479_stop.exp 176
fi
if [ $redis = all ];then
  ./all_stop.exp 176 6379 6479
fi
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值