一、下载和编译
[root@vm1 redis-4.0.8]# tar zxf redis-4.0.8.tar.gz
[root@vm1 redis-4.0.8]# cd redis-4.0.8/
[root@vm1 redis-4.0.8]# make
编译完成后会在src目录下生成Redis服务端程序redis-server和客户端程序redis-cli。
1.启动服务
#前台运行
[root@vm1 redis-4.0.8]# src/redis-server
2、后台运行
可以修改redis.conf文件的daemonize参数为yes,指定配置文件启动,例如:
[root@vm1 redis-4.0.8]# vi redis.conf
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
#指定配置文件启动。
[root@vm1 redis-4.0.8]# src/redis-server redis.conf
8917:C 19 Oct 17:27:03.575 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8917:C 19 Oct 17:27:03.575 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=8917, just started
8917:C 19 Oct 17:27:03.575 # Configuration loaded
客户端测试:
[root@vm1 redis-4.0.8]# src/redis-cli
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
二、Redis集群部署
1.设置配置文件及启动实例
更改redis.conf配置:
[root@vm1 redis]# vi redis.conf
daemonize yes
...
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
最小集群模式需要三个master实例,一般建议起六个实例,即三主三从。因此我们创建6个以端口号命名的目录存放实例的配置文件和其他信息。
mkdir cluster-test
cd cluster-test
mkdir {7000..7005}
cp redis.conf 7000/
...
[root@vm1 cluster-test]# tree
.
|-- 7000
| `-- redis.conf
|-- 7001
| `-- redis.conf
|-- 7002
| `-- redis.conf
|-- 7003
| `-- redis.conf
|-- 7004
| `-- redis.conf
`-- 7005
`-- redis.conf
[root@vm1 7000]# ln -s /mnt/redis/src/redis-server /usr/bin/redis-server
[root@vm1 cluster-test]# cat redis.sh ##脚本启动cluster节点
for i in $(seq 3 5)
do
cd 700$i
redis-server redis.conf
cd ..
done
[root@vm1 cluster-test]# tree
.
|-- 7000
| |-- appendonly.aof
| |-- nodes.conf
| `-- redis.conf
|-- 7001
| |-- appendonly.aof
| |-- nodes.conf
| `-- redis.conf
|-- 7002
| |-- appendonly.aof
| |-- nodes.conf
| `-- redis.conf
|-- 7003
| |-- appendonly.aof
| |-- nodes.conf
| `-- redis.conf
|-- 7004
| |-- appendonly.aof
| |-- nodes.conf
| `-- redis.conf
|-- 7005
| |-- appendonly.aof
| |-- nodes.conf
| `-- redis.conf
2. redis-trib创建集群
Redis的实例全部运行之后,还需要redis-trib.rb工具来完成集群的创建,redis-trib.rb二进制文件在Redis包主目录下的src目录中,运行该工具依赖Ruby环境和gem,因此需要提前安装。
2.1、安装Ruby
[root@vm1 cluster-test]# yum -y install ruby rubygems
查看Ruby版本信息。
[root@vm1 redis-4.0.8]# ruby --version
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]
由于centos系统默认支持Ruby版本为2.0.0,因此执行gem install redis命令时会报以下错误。
解决方法是先安装rvm,再升级ruby版本。
2.2、安装rvm
[root@vm1 cluster-test]# 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 237 0 --:--:-- --:--:-- --:--:-- 237
100 24361 100 24361 0 0 8482 0 0:00:02 0:00:02 --:--:-- 19709
Downloading https://github.com/rvm/rvm/archive/1.29.4.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.4/1.29.4.tar.gz.asc
gpg: Signature made 2018年07月02日 星期一 03时41分26秒 CST using RSA key ID BF04FF17
gpg: Can't check signature: No public key
GPG signature verification failed for '/usr/local/rvm/archives/rvm-1.29.4.tgz' - 'https://github.com/rvm/rvm/releases/download/1.29.4/1.29.4.tar.gz.asc'! Try to install GPG v2 and then fetch the public key:
gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
执行报错中的gpg2 --recv-keys的命令。
[root@vm1 cluster-test]# gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
gpg: requesting key D39DC0E3 from hkp server keys.gnupg.net
gpg: key D39DC0E3: "Michal Papis (RVM signing) <mpapis@gmail.com>" 2 new user IDs
gpg: key D39DC0E3: "Michal Papis (RVM signing) <mpapis@gmail.com>" 99 new signatures
gpg: key D39DC0E3: "Michal Papis (RVM signing) <mpapis@gmail.com>" 2 new subkeys
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg: new user IDs: 2
gpg: new subkeys: 2
gpg: new signatures: 99
再次执行命令curl -L get.rvm.io | bash -s stable。
[root@vm1 cluster-test]# 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 46 0 0:00:04 0:00:04 --:--:-- 47
100 24361 100 24361 0 0 3975 0 0:00:06 0:00:06 --:--:-- 22556
Downloading https://github.com/rvm/rvm/archive/1.29.4.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.4/1.29.4.tar.gz.asc
gpg: Signature made 2018年07月02日 星期一 03时41分26秒 CST using RSA key ID BF04FF17
gpg: Good signature from "Michal Papis (RVM signing) <mpapis@gmail.com>"
gpg: aka "Michal Papis <michal.papis@toptal.com>"
gpg: aka "[jpeg image of size 5015]"
gpg: WARNING: This key is not certified with a trusted signature!
gpg: There is no indication that the signature belongs to the owner.
Primary key fingerprint: 409B 6B17 96C2 7546 2A17 0311 3804 BB82 D39D C0E3
Subkey fingerprint: 62C9 E5F4 DA30 0D94 AC36 166B E206 C29F BF04 FF17
GPG verified '/usr/local/rvm/archives/rvm-1.29.4.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.
* 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!
以上表示执行成功,然后执行以下命令。
[root@vm1 cluster-test]# source /usr/local/rvm/scripts/rvm
查看rvm库中已知的ruby版本
[root@vm1 cluster-test]# rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
...
2.3、升级Ruby
#安装ruby
rvm install 2.4.0
#使用新版本
rvm use 2.4.0
#移除旧版本
rvm remove 2.0.0
#查看当前版本
ruby --version
然而,事情并没有这么简单。执行rvm install 2.4.0
时,便发生这样的错误。
查看日志,发现原因是openssl的版本不匹配。那就源码安装一遍吧。。
[root@vm1 openssl-1.0.2]# wget wget https://www.openssl.org/source/openssl-1.0.2.tar.gz
[root@vm1 openssl-1.0.2]# tar zxf openssl-1.0.2.tar.gz
[root@vm1 openssl-1.0.2]# cd openssl-1.0.2/
[root@vm1 openssl-1.0.2]# ./config -fPIC --prefix=/usr/local/openssl/ enable-shared
[root@vm1 openssl-1.0.2]# make && make install
[root@vm1 openssl-1.0.2]# ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
2.4、安装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
2.5、执行redis-trib.rb命令
[root@kube-node-1 src]# cd src
[root@kube-node-1 src]# ./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
参数create表示创建一个新的集群,–replicas 1表示为每个master创建一个slave。
三、部署结果验证
1、客户端验证
使用客户端redis-cli二进制访问某个实例,执行set和get的测试。
$ 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"
2、集群状态
使用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_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