redis创建集群cluster模式

准备好压缩包

一、编译,解压编译过后,在src目录下执行 make install命令

二、在安装目录下创建一个cluster目录,以及7000 7001 7002端口,如果是多台创建的,就在多台上面创建对应的目录

三、把上面编译好的src目录下面的redis-server拷贝到cluster目录下

四、修改redis.conf文件,内容如下

#可选操作,该项设置后台方式运行,
daemonize yes

bind 192.168.2.247(需要不同服务器的节点连通,就不能设置为 127.0.0.1,如果是单台的服务器,写为127.0.0.1就行)
protected-mode no(需要不同服务器的节点连通,这个就要设置为 no)
daemonize yes(设置后台运行redis)
cluster-enabled yes
cluster-node-timeout 5000
appendonly yes

 修改对应的端口就行,修改完过后,在把对应端口的文件放入到上面的对应目录下面去

五、进入对应的目录,执行下面命令

cd 7000
../redis-server ./redis.conf

  每个端口都执行对应命令,出现以下表示成功

 或者执行披萨-ef|grep redis ,也可以查看是否启动成功

Redis的实例全部运行之后,还需要redis-trib.rb工具来完成集群的创建,redis-trib.rb二进制文件在Redis包主目录下的src目录中,运行该工具依赖Ruby环境和gem,因此需要提前安装。

六、安装Ruby

yum -y install ruby rubygems

  先查看查看Ruby版本信息

[root@kube-node-1 src]# ruby --version
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]

  由于centos系统默认支持Ruby版本为2.0.0,因此执行gem install redis命令时会报以下错误。

[root@kube-node-1 src]# gem install redis
Fetching: redis-4.0.1.gem (100%)
ERROR:  Error installing redis:
	redis requires Ruby version >= 2.2.2.

  解决方法是先安装rvm,再升级ruby版本。

七、安装rvm


curl -L get.rvm.io | bash -s stable

如果遇到以下报错,则执行报错中的gpg2 --recv-keys的命令。

[root@kube-node-1 ~]# curl -L get.rvm.io | bash -s stable
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   194  100   194    0     0    335      0 --:--:-- --:--:-- --:--:--   335
100 24090  100 24090    0     0  17421      0  0:00:01  0:00:01 --:--:-- 44446
Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc
gpg: 于 2017年09月11日 星期一 04时59分21秒 CST 创建的签名,使用 RSA,钥匙号 BF04FF17
gpg: 无法检查签名:没有公钥
Warning, RVM 1.26.0 introduces signed releases and automated check of signatures when GPG software found. Assuming you trust Michal Papis import the mpapis public key (downloading the signatures).

GPG signature verification failed for '/usr/local/rvm/archives/rvm-1.29.3.tgz' - 'https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc'! Try to install GPG v2 and then fetch the public key:

    gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

or if it fails:

    command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -

the key can be compared with:

    https://rvm.io/mpapis.asc
    https://keybase.io/mpapis

NOTE: GPG version 2.1.17 have a bug which cause failures during fetching keys from remote server. Please downgrade or upgrade to newer version (if available) or use the second method described above.

  执行报错中的gpg2 --recv-keys的命令。

[root@kube-node-1 ~]# gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
gpg: 钥匙环‘/root/.gnupg/secring.gpg’已建立
gpg: 下载密钥‘D39DC0E3’,从 hkp 服务器 keys.gnupg.net
gpg: /root/.gnupg/trustdb.gpg:建立了信任度数据库
gpg: 密钥 D39DC0E3:公钥“Michal Papis (RVM signing) <[email protected]>”已导入
gpg: 没有找到任何绝对信任的密钥
gpg: 合计被处理的数量:1
gpg:           已导入:1  (RSA: 1)

  再次执行命令curl -L get.rvm.io | bash -s stable报错。例如

Failed to connect to 2600:3c03::f03c:91ff:fedb:6957: Network is unreachable

  请换成如下命令执行

curl -L https://raw.githubusercontent.com/wayneeseguin/rvm/master/binscripts/rvm-installer | bash -s stable

  

[root@kube-node-1 ~]# curl -L get.rvm.io | bash -s stable
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   194  100   194    0     0    310      0 --:--:-- --:--:-- --:--:--   309
100 24090  100 24090    0     0  18230      0  0:00:01  0:00:01 --:--:--  103k
Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc
gpg: 于 2017年09月11日 星期一 04时59分21秒 CST 创建的签名,使用 RSA,钥匙号 BF04FF17
gpg: 完好的签名,来自于“Michal Papis (RVM signing) <[email protected]>”
gpg:               亦即“Michal Papis <[email protected]>”
gpg:               亦即“[jpeg image of size 5015]”
gpg: 警告:这把密钥未经受信任的签名认证!
gpg:       没有证据表明这个签名属于它所声称的持有者。
主钥指纹: 409B 6B17 96C2 7546 2A17  0311 3804 BB82 D39D C0E3
子钥指纹: 62C9 E5F4 DA30 0D94 AC36  166B E206 C29F BF04 FF17
GPG verified '/usr/local/rvm/archives/rvm-1.29.3.tgz'
Creating group 'rvm'

Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ 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.

  以上表示执行成功,

source /usr/local/rvm/scripts/rvm  

  curl -L get.rvm.io | bash -s stable执行这个命令是报如下错误

查看rvm库中已知的ruby版本

rvm list known

  八、升级Ruby

安装ruby
rvm install  2.4.0
#使用新版本
rvm use  2.4.0
#移除旧版本
rvm remove 2.0.0
#查看当前版本
ruby --version

  例如

[root@kube-node-1 ~]# rvm install  2.4.0
Searching for binary rubies, this might take some time.
Found remote file https://rvm_io.global.ssl.fastly.net/binaries/centos/7/x86_64/ruby-2.4.0.tar.bz2
Checking requirements for centos.
Installing requirements for centos.
Installing required packages: autoconf, automake, bison, bzip2, gcc-c++, libffi-devel, libtool, readline-devel, sqlite-devel, zlib-devel, libyaml-devel, openssl-devel................................
Requirements installation successful.
ruby-2.4.0 - #configure
ruby-2.4.0 - #download
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 14.0M  100 14.0M    0     0   852k      0  0:00:16  0:00:16 --:--:--  980k
No checksum for downloaded archive, recording checksum in user configuration.
ruby-2.4.0 - #validate archive
ruby-2.4.0 - #extract
ruby-2.4.0 - #validate binary
ruby-2.4.0 - #setup
ruby-2.4.0 - #gemset created /usr/local/rvm/gems/ruby-2.4.0@global
ruby-2.4.0 - #importing gemset /usr/local/rvm/gemsets/global.gems..............................
ruby-2.4.0 - #generating global wrappers........
ruby-2.4.0 - #gemset created /usr/local/rvm/gems/ruby-2.4.0
ruby-2.4.0 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.4.0 - #generating default wrappers........

[root@kube-node-1 ~]# rvm use  2.4.0
Using /usr/local/rvm/gems/ruby-2.4.0

[root@kube-node-1 ~]# rvm remove 2.0.0
ruby-2.0.0-p648 - #already gone
Using /usr/local/rvm/gems/ruby-2.4.0

[root@kube-node-1 ~]# ruby --version
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]

  九、安装gem

[root@kube-node-1 ~]# gem install redis

Fetching: redis-4.0.1.gem (100%)
Successfully installed redis-4.0.1
Parsing documentation for redis-4.0.1
Installing ri documentation for redis-4.0.1
Done installing documentation for redis after 2 seconds
1 gem installed

  十、执行redis-trib.rb命令

cd src 
#执行redis-trib.rb命令
./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 \
> 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

  

redis-trib.rb命令在之前编译的redis下面的src目录下面,

参数create表示创建一个新的集群,--replicas 1表示为每个master创建一个slave。

如果创建成功会显示以下信息

十一、部署结果验证

使用客户端redis-cli二进制访问某个实例,执行setget的测试。

$ redis-cli -c -p 7000
redis 127.0.0.1:7000> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:7002
OK
redis 127.0.0.1:7002> set hello world
-> Redirected to slot [866] located at 127.0.0.1:7000
OK
redis 127.0.0.1:7000> get foo
-> Redirected to slot [12182] located at 127.0.0.1:7002
"bar"
redis 127.0.0.1:7000> get hello
-> Redirected to slot [866] located at 127.0.0.1:7000
"world"

  使用cluster info命令查看集群状态。

127.0.0.1:7000> cluster info
cluster_state:ok                       #集群状态
cluster_slots_assigned:16384           #被分配的槽位数
cluster_slots_ok:16384                 #正确分配的槽位
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6                  #当前节点
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:48273
cluster_stats_messages_pong_sent:49884
cluster_stats_messages_sent:98157
cluster_stats_messages_ping_received:49879
cluster_stats_messages_pong_received:48273
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:98157

  使用cluster nodes命令查看节点状态。

127.0.0.1:7000> cluster nodes
be2718476eba4e56f696e56b75e67df720b7fc24 127.0.0.1:7002@17002 master - 0 1517303607000 3 connected 10923-16383
13d0c397604a0b2644244c37b666fce83f29faa8 127.0.0.1:7001@17001 master - 0 1517303606000 2 connected 5461-10922
3d02f59b34047486faecc023685379de7b38076c 127.0.0.1:7003@17003 slave 13d0c397604a0b2644244c37b666fce83f29faa8 0 1517303606030 4 connected
d5a834d075fd93eefab877c6ebb86efff680650f 127.0.0.1:7000@17000 myself,master - 0 1517303604000 1 connected 0-5460
99c07119a449a703583019f7699e15afa0e41952 127.0.0.1:7005@17005 slave d5a834d075fd93eefab877c6ebb86efff680650f 0 1517303607060 6 connected
dedf672f0a75faf37407ac4edd5da23bc4651e25 127.0.0.1:7004@17004 slave be2718476eba4e56f696e56b75e67df720b7fc24 0 1517303608082 5 connected

  

文章参考于https://www.huweihuang.com/article/redis/redis-cluster/,

本次笔记只是个人工作遇见的一些问题的记录,如有不对的,请联系我,共同学习

 

猜你喜欢

转载自www.cnblogs.com/LMDclg/p/12283488.html