1. Redis部署

以下以Linux系统为例

1.1 下载和编译

  1. $ wget http://download.redis.io/releases/redis-4.0.7.tar.gz
  2. $ tar xzf redis-4.0.7.tar.gz
  3. $ cd redis-4.0.7
  4. $ make

编译完成后会在src目录下生成Redis服务端程序redis-server和客户端程序redis-cli

1.2 启动服务

1、前台运行

  1. src/redis-server

该方式启动默认为前台方式运行,使用默认配置。

2、后台运行

可以修改redis.conf文件的daemonize参数为yes,指定配置文件启动,例如:

  1. vi redis.conf
  2. # By default Redis does not run as a daemon. Use 'yes' if you need it.
  3. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
  4. daemonize yes

指定配置文件启动。

  1. src/redis-server redis.conf

例如:

  1. #指定配置文件后台启动
  2. [root@kube-node-1 redis-4.0.7]# src/redis-server redis.conf
  3. 95778:C 30 Jan 00:44:37.633 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
  4. 95778:C 30 Jan 00:44:37.634 # Redis version=4.0.7, bits=64, commit=00000000, modified=0, pid=95778, just started
  5. 95778:C 30 Jan 00:44:37.634 # Configuration loaded
  6. #查看Redis进程
  7. [root@kube-node-1 redis-4.0.7]# ps aux|grep redis
  8. root 95779 0.0 0.0 145268 468 ? Ssl 00:44 0:00 src/redis-server 127.0.0.1:6379

更多启动参数如下:

  1. [root@kube-node-1 src]# ./redis-server --help
  2. Usage: ./redis-server [/path/to/redis.conf] [options]
  3. ./redis-server - (read config from stdin)
  4. ./redis-server -v or --version
  5. ./redis-server -h or --help
  6. ./redis-server --test-memory <megabytes>
  7. Examples:
  8. ./redis-server (run the server with default conf)
  9. ./redis-server /etc/redis/6379.conf
  10. ./redis-server --port 7777
  11. ./redis-server --port 7777 --slaveof 127.0.0.1 8888
  12. ./redis-server /etc/myredis.conf --loglevel verbose
  13. Sentinel mode:
  14. ./redis-server /etc/sentinel.conf --sentinel

1.3 客户端测试

  1. $ src/redis-cli
  2. redis> set foo bar
  3. OK
  4. redis> get foo
  5. "bar"

2. Redis集群部署

Redis的集群部署需要在每台集群部署的机器上安装Redis(可参考上述的[Redis安装] ),然后修改配置以集群的方式启动。

2.1 手动部署集群

2.1.1 设置配置文件及启动实例

修改配置文件redis.conf,集群模式的最小化配置文件如下:

  1. #可选操作,该项设置后台方式运行,
  2. daemonize yes
  3. port 7000
  4. cluster-enabled yes
  5. cluster-config-file nodes.conf
  6. cluster-node-timeout 5000
  7. appendonly yes

更多集群配置参数可参考默认配置文件redis.conf中Cluster模块的说明

最小集群模式需要三个master实例,一般建议起六个实例,即三主三从。因此我们创建6个以端口号命名的目录存放实例的配置文件和其他信息。

  1. mkdir cluster-test
  2. cd cluster-test
  3. mkdir 7000 7001 7002 7003 7004 7005

在对应端口号的目录中创建redis.conf的文件,配置文件的内容可参考上述的集群模式配置。每个配置文件中的端口号port参数改为对应目录的端口号。

复制redis-server的二进制文件到cluster-test目录中,通过指定配置文件的方式启动redis服务,例如:

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

如果是以前台方式运行,则会在控制台输出以下信息:

  1. [82462] 26 Nov 11:56:55.329 * No cluster configuration found, I'm 97a3a64667477371c4479320d683e4c8db5858b1

每个实例都会生成一个Node ID,类似97a3a64667477371c4479320d683e4c8db5858b1,用来作为Redis实例在集群中的唯一标识,而不是通过IP和Port,IP和Port可能会改变,该Node ID不会改变。

目录结构可参考:

  1. cluster-test/
  2. ├── 7000
  3. ├── appendonly.aof
  4. ├── dump.rdb
  5. ├── nodes.conf
  6. └── redis.conf
  7. ├── 7001
  8. ├── appendonly.aof
  9. ├── dump.rdb
  10. ├── nodes.conf
  11. └── redis.conf
  12. ├── 7002
  13. ├── appendonly.aof
  14. ├── dump.rdb
  15. ├── nodes.conf
  16. └── redis.conf
  17. ├── 7003
  18. ├── appendonly.aof
  19. ├── dump.rdb
  20. ├── nodes.conf
  21. └── redis.conf
  22. ├── 7004
  23. ├── appendonly.aof
  24. ├── dump.rdb
  25. ├── nodes.conf
  26. └── redis.conf
  27. ├── 7005
  28. ├── appendonly.aof
  29. ├── dump.rdb
  30. ├── nodes.conf
  31. └── redis.conf
  32. ├── redis-cli
  33. └── redis-server

2.1.2 redis-trib创建集群

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

1、安装Ruby

  1. yum -y install ruby rubygems

查看Ruby版本信息。

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

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

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

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

2、安装rvm

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

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

  1. [root@kube-node-1 ~]# curl -L get.rvm.io | bash -s stable
  2. % Total % Received % Xferd Average Speed Time Time Time Current
  3. Dload Upload Total Spent Left Speed
  4. 100 194 100 194 0 0 335 0 --:--:-- --:--:-- --:--:-- 335
  5. 100 24090 100 24090 0 0 17421 0 0:00:01 0:00:01 --:--:-- 44446
  6. Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz
  7. Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc
  8. gpg: 20170911 星期一 045921 CST 创建的签名,使用 RSA,钥匙号 BF04FF17
  9. gpg: 无法检查签名:没有公钥
  10. 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).
  11. 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:
  12. gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
  13. or if it fails:
  14. command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
  15. the key can be compared with:
  16. https://rvm.io/mpapis.asc
  17. https://keybase.io/mpapis
  18. 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的命令。

例如:

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

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

  1. [root@kube-node-1 ~]# curl -L get.rvm.io | bash -s stable
  2. % Total % Received % Xferd Average Speed Time Time Time Current
  3. Dload Upload Total Spent Left Speed
  4. 100 194 100 194 0 0 310 0 --:--:-- --:--:-- --:--:-- 309
  5. 100 24090 100 24090 0 0 18230 0 0:00:01 0:00:01 --:--:-- 103k
  6. Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz
  7. Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc
  8. gpg: 20170911 星期一 045921 CST 创建的签名,使用 RSA,钥匙号 BF04FF17
  9. gpg: 完好的签名,来自于“Michal Papis (RVM signing) <mpapis@gmail.com>”
  10. gpg: 亦即“Michal Papis <michal.papis@toptal.com>”
  11. gpg: 亦即“[jpeg image of size 5015]”
  12. gpg: 警告:这把密钥未经受信任的签名认证!
  13. gpg: 没有证据表明这个签名属于它所声称的持有者。
  14. 主钥指纹: 409B 6B17 96C2 7546 2A17 0311 3804 BB82 D39D C0E3
  15. 子钥指纹: 62C9 E5F4 DA30 0D94 AC36 166B E206 C29F BF04 FF17
  16. GPG verified '/usr/local/rvm/archives/rvm-1.29.3.tgz'
  17. Creating group 'rvm'
  18. Installing RVM to /usr/local/rvm/
  19. Installation of RVM in /usr/local/rvm/ is almost complete:
  20. * First you need to add all users that will be using rvm to 'rvm' group,
  21. and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.
  22. * To start using RVM you need to run `source /etc/profile.d/rvm.sh`
  23. in all your open bash windows, in rare cases you need to reopen all bash windows.

以上表示执行成功,

  1. source /usr/local/rvm/scripts/rvm

查看rvm库中已知的ruby版本

  1. rvm list known

例如:

  1. [root@kube-node-1 ~]# rvm list known
  2. # MRI Rubies
  3. [ruby-]1.8.6[-p420]
  4. [ruby-]1.8.7[-head] # security released on head
  5. [ruby-]1.9.1[-p431]
  6. [ruby-]1.9.2[-p330]
  7. [ruby-]1.9.3[-p551]
  8. [ruby-]2.0.0[-p648]
  9. [ruby-]2.1[.10]
  10. [ruby-]2.2[.7]
  11. [ruby-]2.3[.4]
  12. [ruby-]2.4[.1]
  13. ruby-head
  14. ...

3、升级Ruby

  1. #安装ruby
  2. rvm install 2.4.0
  3. #使用新版本
  4. rvm use 2.4.0
  5. #移除旧版本
  6. rvm remove 2.0.0
  7. #查看当前版本
  8. ruby --version

例如:

  1. [root@kube-node-1 ~]# rvm install 2.4.0
  2. Searching for binary rubies, this might take some time.
  3. Found remote file https://rvm_io.global.ssl.fastly.net/binaries/centos/7/x86_64/ruby-2.4.0.tar.bz2
  4. Checking requirements for centos.
  5. Installing requirements for centos.
  6. Installing required packages: autoconf, automake, bison, bzip2, gcc-c++, libffi-devel, libtool, readline-devel, sqlite-devel, zlib-devel, libyaml-devel, openssl-devel................................
  7. Requirements installation successful.
  8. ruby-2.4.0 - #configure
  9. ruby-2.4.0 - #download
  10. % Total % Received % Xferd Average Speed Time Time Time Current
  11. Dload Upload Total Spent Left Speed
  12. 100 14.0M 100 14.0M 0 0 852k 0 0:00:16 0:00:16 --:--:-- 980k
  13. No checksum for downloaded archive, recording checksum in user configuration.
  14. ruby-2.4.0 - #validate archive
  15. ruby-2.4.0 - #extract
  16. ruby-2.4.0 - #validate binary
  17. ruby-2.4.0 - #setup
  18. ruby-2.4.0 - #gemset created /usr/local/rvm/gems/ruby-2.4.0@global
  19. ruby-2.4.0 - #importing gemset /usr/local/rvm/gemsets/global.gems..............................
  20. ruby-2.4.0 - #generating global wrappers........
  21. ruby-2.4.0 - #gemset created /usr/local/rvm/gems/ruby-2.4.0
  22. ruby-2.4.0 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list
  23. ruby-2.4.0 - #generating default wrappers........
  24. [root@kube-node-1 ~]# rvm use 2.4.0
  25. Using /usr/local/rvm/gems/ruby-2.4.0
  26. [root@kube-node-1 ~]# rvm remove 2.0.0
  27. ruby-2.0.0-p648 - #already gone
  28. Using /usr/local/rvm/gems/ruby-2.4.0
  29. [root@kube-node-1 ~]# ruby --version
  30. ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]

4、安装gem

  1. gem install redis

例如:

  1. [root@kube-node-1 ~]# gem install redis
  2. Fetching: redis-4.0.1.gem (100%)
  3. Successfully installed redis-4.0.1
  4. Parsing documentation for redis-4.0.1
  5. Installing ri documentation for redis-4.0.1
  6. Done installing documentation for redis after 2 seconds
  7. 1 gem installed

5、执行redis-trib.rb命令

以上表示安装成功,可以执行redis-trib.rb命令。

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

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

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

  1. [OK] All 16384 slots covered

例如:

  1. [root@kube-node-1 src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 \
  2. > 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
  3. >>> Creating cluster
  4. >>> Performing hash slots allocation on 6 nodes...
  5. Using 3 masters:
  6. 127.0.0.1:7000
  7. 127.0.0.1:7001
  8. 127.0.0.1:7002
  9. Adding replica 127.0.0.1:7004 to 127.0.0.1:7000
  10. Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
  11. Adding replica 127.0.0.1:7003 to 127.0.0.1:7002
  12. >>> Trying to optimize slaves allocation for anti-affinity
  13. [WARNING] Some slaves are in the same host as their master
  14. M: d5a834d075fd93eefab877c6ebb86efff680650f 127.0.0.1:7000
  15. slots:0-5460 (5461 slots) master
  16. M: 13d0c397604a0b2644244c37b666fce83f29faa8 127.0.0.1:7001
  17. slots:5461-10922 (5462 slots) master
  18. M: be2718476eba4e56f696e56b75e67df720b7fc24 127.0.0.1:7002
  19. slots:10923-16383 (5461 slots) master
  20. S: 3d02f59b34047486faecc023685379de7b38076c 127.0.0.1:7003
  21. replicates 13d0c397604a0b2644244c37b666fce83f29faa8
  22. S: dedf672f0a75faf37407ac4edd5da23bc4651e25 127.0.0.1:7004
  23. replicates be2718476eba4e56f696e56b75e67df720b7fc24
  24. S: 99c07119a449a703583019f7699e15afa0e41952 127.0.0.1:7005
  25. replicates d5a834d075fd93eefab877c6ebb86efff680650f
  26. Can I set the above configuration? (type 'yes' to accept): yes
  27. >>> Nodes configuration updated
  28. >>> Assign a different config epoch to each node
  29. >>> Sending CLUSTER MEET messages to join the cluster
  30. Waiting for the cluster to join....
  31. >>> Performing Cluster Check (using node 127.0.0.1:7000)
  32. M: d5a834d075fd93eefab877c6ebb86efff680650f 127.0.0.1:7000
  33. slots:0-5460 (5461 slots) master
  34. 1 additional replica(s)
  35. M: be2718476eba4e56f696e56b75e67df720b7fc24 127.0.0.1:7002
  36. slots:10923-16383 (5461 slots) master
  37. 1 additional replica(s)
  38. M: 13d0c397604a0b2644244c37b666fce83f29faa8 127.0.0.1:7001
  39. slots:5461-10922 (5462 slots) master
  40. 1 additional replica(s)
  41. S: 3d02f59b34047486faecc023685379de7b38076c 127.0.0.1:7003
  42. slots: (0 slots) slave
  43. replicates 13d0c397604a0b2644244c37b666fce83f29faa8
  44. S: 99c07119a449a703583019f7699e15afa0e41952 127.0.0.1:7005
  45. slots: (0 slots) slave
  46. replicates d5a834d075fd93eefab877c6ebb86efff680650f
  47. S: dedf672f0a75faf37407ac4edd5da23bc4651e25 127.0.0.1:7004
  48. slots: (0 slots) slave
  49. replicates be2718476eba4e56f696e56b75e67df720b7fc24
  50. [OK] All nodes agree about slots configuration.
  51. >>> Check for open slots...
  52. >>> Check slots coverage...
  53. [OK] All 16384 slots covered.

2.1.3 部署结果验证

1、客户端访问

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

  1. $ redis-cli -c -p 7000
  2. redis 127.0.0.1:7000> set foo bar
  3. -> Redirected to slot [12182] located at 127.0.0.1:7002
  4. OK
  5. redis 127.0.0.1:7002> set hello world
  6. -> Redirected to slot [866] located at 127.0.0.1:7000
  7. OK
  8. redis 127.0.0.1:7000> get foo
  9. -> Redirected to slot [12182] located at 127.0.0.1:7002
  10. "bar"
  11. redis 127.0.0.1:7000> get hello
  12. -> Redirected to slot [866] located at 127.0.0.1:7000
  13. "world"

2、查看集群状态

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

  1. 127.0.0.1:7000> cluster info
  2. cluster_state:ok #集群状态
  3. cluster_slots_assigned:16384 #被分配的槽位数
  4. cluster_slots_ok:16384 #正确分配的槽位
  5. cluster_slots_pfail:0
  6. cluster_slots_fail:0
  7. cluster_known_nodes:6 #当前节点
  8. cluster_size:3
  9. cluster_current_epoch:6
  10. cluster_my_epoch:1
  11. cluster_stats_messages_ping_sent:48273
  12. cluster_stats_messages_pong_sent:49884
  13. cluster_stats_messages_sent:98157
  14. cluster_stats_messages_ping_received:49879
  15. cluster_stats_messages_pong_received:48273
  16. cluster_stats_messages_meet_received:5
  17. cluster_stats_messages_received:98157

3、查看节点状态

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

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

参考文章:

https://redis.io/download

https://redis.io/topics/cluster-tutorial