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

驗證集羣
在這裏插入圖片描述
在這裏插入圖片描述
刪除單個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 [email protected].$host
expect {
  "*yes/no*" {send "yes\r"; exp_continue}
  "[email protected]*" {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 [email protected].$host
expect {
  "*yes/no*" {send "yes\r"; exp_continue}
  "[email protected]*" {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 [email protected].$host
expect {
  "*yes/no*" {send "yes\r"; exp_continue}
  "[email protected]*" {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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章