Redis集群 (1) 配置 ruby安装 rvm

1. Redis位置 /usr/local/redis

2. 其下目录: redis-3.2.8 为redis解压目录;

3. 安装后redis执行文件在 /usr/local/bin

cd /usr/local/redis
mkdir redis-cluster
cd redis-cluster
mkdir redis{6380,6381,6382,6384}

将 /usr/local/bin下的 redis文件拷到 redis-cluster/redis{6380,6381,6382,6383}下

cd /usr/local/bin
cp redis-* /usr/local/redis/redis-cluster/redis{6380,6381,6382,6383}

再将redis.conf也拷贝各个cluster redis下;

cd /usr/local/redis/redis-3.2.8
cp redis.conf /usr/local/redis/redis-cluster/redis{6380,6381,6382,6383}

修改各个redis.conf,主要要为端口,后台运行,dump文件,pid, cluster支持等;

redis6380/redis.conf

port 6380
daemonize yes
pidfile /var/run/redis_6380.pid
dbfilename dump-6380.rdb
requirepass 123456
cluster-enabled yes

制作启动脚本;

cd /usr/local/redis/redis-cluster
vim startup.sh

为以下内容的脚本:

cd redis6380/
./redis-server redis.conf
cd ..
cd redis6381/
./redis-server redis.conf
cd ..
cd redis6382/
./redis-server redis.conf
cd ..
cd redis6383/
./redis-server redis.conf

执行时,先给权限;

./startup.sh
chmod u+x 777 startup.sh

ps -ef | grep redis
root      94834      1  0 7月05 ?       00:00:25 ./redis-server *:6380 [cluster]
root      94838      1  0 7月05 ?       00:00:25 ./redis-server *:6381 [cluster]
root      94842      1  0 7月05 ?       00:00:25 ./redis-server *:6382 [cluster]
root      94846      1  0 7月05 ?       00:00:25 ./redis-server *:6383 [cluster]

安全配置 ruby,采用RVM管理ruby

参考: https://rvm.io/rvm/install

安装依赖

 sudo yum install curl gpg gcc gcc-c++ make patch, autoconf, automake, bison, libffi-devel, libtool, patch, readline-devel, sqlite-devel, zlib-devel, openssl-devel

安装KEY,密钥相关

[root@localhost local]# gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
gpg: 下载密钥‘D39DC0E3’,从 hkp 服务器 pool.sks-keyservers.net
gpg: 下载密钥‘39499BDB’,从 hkp 服务器 pool.sks-keyservers.net
gpg: 密钥 D39DC0E3:公钥“Michal Papis (RVM signing) <mpapis@gmail.com>”已导入
gpg: 密钥 39499BDB:“Piotr Kuczynski <piotr.kuczynski@gmail.com>”未改变
gpg: 没有找到任何绝对信任的密钥
gpg: 合计被处理的数量:2
gpg:           已导入:1  (RSA: 1)
gpg:           未改变:1
[root@localhost local]# \curl -sSL https://get.rvm.io | bash -s stable
Downloading https://github.com/rvm/rvm/archive/1.29.8.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.8/1.29.8.tar.gz.asc
gpg: 于 2019年05月08日 星期三 22时14分49秒 CST 创建的签名,使用 RSA,钥匙号 39499BDB
gpg: 完好的签名,来自于“Piotr Kuczynski <piotr.kuczynski@gmail.com>”
gpg: 警告:这把密钥未经受信任的签名认证!
gpg:       没有证据表明这个签名属于它所声称的持有者。
主钥指纹: 7D2B AF1C F37B 13E2 069D  6956 105B D0E7 3949 9BDB
GPG verified '/usr/local/rvm/scripts/archives/rvm-1.29.8.tgz'
Installing RVM to /usr/local/rvm/scripts/
Installation of RVM in /usr/local/rvm/scripts/ is almost complete:

  * First you need to add all users that will be using rvm to 'rvm' group,
    and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`
    in all your open shell windows, in rare cases you need to reopen all shell windows.
  * Please do NOT forget to add your users to the rvm group.
     The installer no longer auto-adds root or users to the rvm group. Admins must do this.
     Also, please note that group memberships are ONLY evaluated at login time.
     This means that users must log out then back in before group membership takes effect!
Thanks for installing RVM 🙏
Please consider donating to our open collective to help us maintain RVM.

👉  Donate: https://opencollective.com/rvm/donate

查看 $rvm_path

[root@localhost local]# echo $rvm_path
/usr/local/rvm/scripts

按照上面的提示执行:

[root@localhost local]# source /etc/profile.d/rvm.sh

查看rvm版本

[root@localhost local]# rvm -v
rvm 1.29.8 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]

安装ruby 2.5.1

[root@localhost local]# rvm install 2.5.1
Searching for binary rubies, this might take some time.
No binary rubies available for: centos/7/x86_64/ruby-2.5.1.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for centos.
Installing requirements for centos.
Installing required packages: patch, autoconf, automake, bison, libffi-devel, libtool, patch, readline-devel, ruby, sqlite-devel................
Requirements installation successful.
Installing Ruby from source to: /usr/local/rvm/scripts/rubies/ruby-2.5.1, this may take a while depending on your cpu(s)...
ruby-2.5.1 - #downloading ruby-2.5.1, this may take a while depending on your connection...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed to connect to 2a04:4e42:1a::434: 网络不可达
There was an error(7).
Checking fallback: https://ftp.ruby-lang.org/pub/ruby/2.5/ruby-2.5.1.tar.bz2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 13.3M  100 13.3M    0     0  16239      0  0:14:22  0:14:22 --:--:-- 32914
ruby-2.5.1 - #extracting ruby-2.5.1 to /usr/local/rvm/scripts/src/ruby-2.5.1.....
ruby-2.5.1 - #applying patch /usr/local/rvm/scripts/patches/ruby/2.5.1/libressl_2_7.patch.
ruby-2.5.1 - #configuring...................................................................
ruby-2.5.1 - #post-configuration..
ruby-2.5.1 - #compiling....................................................................................
ruby-2.5.1 - #installing.............................
ruby-2.5.1 - #making binaries executable..
ruby-2.5.1 - #downloading rubygems-3.0.4
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  862k  100  862k    0     0   1258      0  0:11:42  0:11:41  0:00:01  190k
No checksum for downloaded archive, recording checksum in user configuration.
ruby-2.5.1 - #extracting rubygems-3.0.4.....
ruby-2.5.1 - #removing old rubygems........
ruby-2.5.1 - #installing rubygems-3.0.4.........................................
ruby-2.5.1 - #gemset created /usr/local/rvm/scripts/gems/ruby-2.5.1@global
ruby-2.5.1 - #importing gemset /usr/local/rvm/scripts/gemsets/global.gems................................................................
ruby-2.5.1 - #generating global wrappers.......
ruby-2.5.1 - #gemset created /usr/local/rvm/scripts/gems/ruby-2.5.1
ruby-2.5.1 - #importing gemsetfile /usr/local/rvm/scripts/gemsets/default.gems evaluated to empty gem list
ruby-2.5.1 - #generating default wrappers.......
ruby-2.5.1 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.5.1 - #complete 
Please be aware that you just installed a ruby that requires 1 patches just to be compiled on an up to date linux system.
This may have known and unaccounted for security vulnerabilities.
Please consider upgrading to ruby-2.6.3 which will have all of the latest security patches.
Ruby was built without documentation, to build it run: rvm docs generate-ri

查看 ruby, gem 版本

[root@localhost local]# ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
[root@localhost local]# gem -v
3.0.4

安装支持redis 支持脚本:

[root@localhost redis-cluster]# gem install redis
Fetching redis-4.1.2.gem
Successfully installed redis-4.1.2
Parsing documentation for redis-4.1.2
Installing ri documentation for redis-4.1.2
Done installing documentation for redis after 2 seconds
1 gem installed

执行创建集群错误

[root@localhost redis-cluster]# ./redis-trib.rb create --replicas 192.168.1.141:6380 192.168.1.141:6381 192.168.1.141:6382 192.168.1.141:6383
>>> Creating cluster
[ERR] Sorry, can't connect to node 192.168.1.141:6381

以上原因有两个:

protected-mode no 原为yes

有些旧版本注释requirepass

重新执行;

[root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.1.141:6380 192.168.1.141:6381 192.168.1.141:6382 192.168.1.141:6383
>>> Creating cluster
*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 4 nodes and 1 replicas per node.
*** At least 6 nodes are required.

以上提示,集群最少要6个节点;

添加两个新节点后,重新执行;

[root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.1.141:6380 192.168.1.141:6381 192.168.1.141:6382 192.168.1.141:6383 192.168.1.141:6384 192.168.1.141:6385
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.141:6380
192.168.1.141:6381
192.168.1.141:6382
Adding replica 192.168.1.141:6383 to 192.168.1.141:6380
Adding replica 192.168.1.141:6384 to 192.168.1.141:6381
Adding replica 192.168.1.141:6385 to 192.168.1.141:6382
M: 89bea82985c707e28416cc441592f6cc4d7db06c 192.168.1.141:6380
   slots:0-5460 (5461 slots) master
M: caf21fb5c3d3c2b35b3e634893bab40551501540 192.168.1.141:6381
   slots:5461-10922 (5462 slots) master
M: 46b36e9e1854110d05c6ced0104202fbda631277 192.168.1.141:6382
   slots:10923-16383 (5461 slots) master
S: e07962ae7b1daf440115d480eb8d3f662cac0489 192.168.1.141:6383
   replicates 89bea82985c707e28416cc441592f6cc4d7db06c
S: 89bea82985c707e28416cc441592f6cc4d7db06c 192.168.1.141:6384
   replicates caf21fb5c3d3c2b35b3e634893bab40551501540
S: 89bea82985c707e28416cc441592f6cc4d7db06c 192.168.1.141:6385
   replicates 46b36e9e1854110d05c6ced0104202fbda631277
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

此时会一直卡在 Waiting for the cuslter to join 上;

这是因为redis.conf 里 bind 配置不对造成:

原是注释掉;

要打开,并配置为

bind 192.168.1.141 127.0.0.1

这样就可以; 后面的 127.0.0.1 可以;

再次执行 创建集群,会失败:

Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0;

首先关闭所有的节点; 采用 shutdown.sh

cd redis6380/
./redis-cli -p 6380 shutdown
cd ..

cd redis6381/
./redis-cli -p 6381 shutdown
cd ..

cd redis6382/
./redis-cli -p 6382 shutdown
cd ..

cd redis6383/
./redis-cli -p 6383 shutdown

cd ..
cd redis6384/
./redis-cli -p 6384 shutdown

cd ..
cd redis6385/
./redis-cli -p 6385 shutdown

再把各个节点下 执行 :

redis-cli -c -p xxxx
flushall

再把dump-x.rdb, nodes.conf, 删除;

再启动各个节点redis; 采用 startup.sh

再次创建 集群,成功;

redis-trib.rb create –replicas 1: 这个最后的 “1” 表示为每个主节点配一个 从节点;

欢迎您的到来,感谢您的支持!

为您推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注