One, install redis
The installation of the cluster requires Ruby commands, so we need to install Ruby, just find one to execute it in the cluster machine
- yum install ruby //安装ruby
- yum install rubygems //install rubygems, the latest version will be installed automatically
- gem install redis
After the execution is complete, you will find an error
,
The minimum Ruby version required by redis is 2.2.2, but the version of ruby in the CentOS7 yum library supports up to 2.0.0,
View Ruby version command: ruby --version
But gem installation redis needs at least 2.2.2, use rvm to update ruby:
- Install RVM
- curl -L get.rvm.io | bash -s stable
- find / -name rvm -print (There may be a problem at this time)
- If the above information does not appear, execute the command as shown in the figure below, and execute the command as shown in the figure above.
- After the above command is executed, continue to execute find / -name rvm -print, you can see the correct screenshot above
- Make the newly installed rvm effective immediately
- source /usr/local/rvm/scripts/rvm
- View the ruby version in the rvm library
- rvm list known
- Install a ruby version
-
rvm install 2.4.1
-
- Use a ruby version
-
rvm use 2.4.1
-
- Set default version
-
rvm use 2.4.1 --default
-
- Uninstall a known version
-
rvm remove 版本号 比如原来的ruby版本如下:
那么删除命令为:rvm remove 2.0.0p648
-
- View ruby version
-
ruby --version
-
- So far, the new version of ruby is installed, let me continue to install redis execution command
gem install redis
2. Download the redis source code package and unzip it
wget http://download.redis.io/releases/redis-3.2.4.tar.gz
tar -zxvf redis-3.2.4.tar.gz
cd redis-3.2.4
3. Install redis
make
make install PREFIX=/usr/local/redis
cp redis.conf /usr/local/redis/bin/redis.conf
chmod 777 /usr/local/redis/bin/redis.conf
4. Start redis (this is a stand-alone test, it can be skipped only to build a cluster)
./redis-server redis.conf
5. Verification (this is a stand-alone test, it can be skipped only to build a cluster)
[root@host-172-16-80-177 bin]# ./redis-cli -p 6379
127.0.0.1:6379> get
(error) ERR wrong number of arguments for 'get' command
127.0.0.1:6379> set aa hzb
OK
127.0.0.1:6379> get aa
"hzb"
2. Build a redis cluster (cluster mode)
1. Create a redis-cluster directory under /usr/local/
mkdir -p /usr/local/redis-cluster
2. Copy the /usr/local/redis/bin directory to redis-cluster and rename it to redis1
cd /usr/local
cp -r redis/bin redis-cluster/redis1
3. Copy 3 copies of redis1 in the same directory and name them redis2, redis3, redis4, redis5, redis6 (you must have more than 6 nodes to create a cluster)
cd /usr/local/redis-cluster
[root@host-172-16-80-177 redis-cluster]# pwd
/usr/local/redis-cluster
[root@host-172-16-80-177 redis-cluster]# cp -r redis1 redis2
[root@host-172-16-80-177 redis-cluster]# cp -r redis1 redis3
[root@host-172-16-80-177 redis-cluster]# cp -r redis1 redis4
[root@host-172-16-80-177 redis-cluster]# cp -r redis1 redis5
[root@host-172-16-80-177 redis-cluster]# cp -r redis1 redis6
Respectively modify to redis-7001.conf, redis-7002.conf followed by redis-7006.conf (it is better to download to the local, modify and upload)
Take redis-7001 as an example:
daemonize yes
bind 192.168.65.130 #(外部可访问IP)
Port 7001
logfile "./redis-7001.log"
databases 1
protected-mode no
pidfile /var/run/redis_7001.pid
cluster-enabled yes
4. Copy redis-trib.rb to the redis-cluster directory
First find the location of redis-trib.rb, knowing the location can save this step
[root@bslsvrnexusp1 bin]# find / -name redis-trib.rb
/usr/local/Redis/redis-3.2.4/src/redis-trib.rb
然后进入redis-cluster文件夹,将redis-trib.rb复制到当前目录中
[root@host-172-16-80-177 redis-cluster]# cp /usr/local/Redis/redis-3.2.4/src/redis-trib.rb .
[root@host-172-16-80-177 redis-cluster]# ll
total 76
drwxr-xr-x 2 root root 4096 Oct 19 22:37 redis1
drwxr-xr-x 2 root root 4096 Oct 19 22:48 redis2
drwxr-xr-x 2 root root 4096 Oct 19 22:49 redis3
drwxr-xr-x 2 root root 4096 Oct 19 22:49 redis4
drwxr-xr-x 2 root root 4096 Oct 19 22:49 redis5
drwxr-xr-x 2 root root 4096 Oct 19 22:49 redis6
-rwxr-xr-x 1 root root 60852 Oct 19 22:52 redis-trib.rb
5. Enter redis1, redis2, redis3, redis4, redis5, redis6 respectively to execute (must follow: enter separately )
./redis-server redis-7001.conf
......
Start 6 nodes
[root@host-172-16-80-177 redis4]# ps -ef|grep redis
root 3167 1 0 02:49 ? 00:00:00 ./redis-server 127.0.0.1:7001 [cluster]
root 3176 1 0 02:50 ? 00:00:00 ./redis-server 127.0.0.1:7002 [cluster]
root 3187 1 0 02:51 ? 00:00:00 ./redis-server 127.0.0.1:7003 [cluster]
root 3191 1 0 02:52 ? 00:00:00 ./redis-server 127.0.0.1:7004 [cluster]
root 3191 1 0 02:52 ? 00:00:00 ./redis-server 127.0.0.1:7005 [cluster]
root 3191 1 0 02:52 ? 00:00:00 ./redis-server 127.0.0.1:7006 [cluster]
root 3195 1852 0 02:52 pts/0 00:00:00 grep redis
6. Build a cluster with redis-trib.rb
cd /usr/local/redis-cluster/
[root@host-172-16-80-177 redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.65.130:7001 192.168.65.130:7002 192.168.65.130:7003 192.168.65.130:7004 192.168.65.130:7005 192.168.65.130:7006
7. Verify that the cluster is successful
[root@host-172-16-80-177 redis2]# ./redis-cli -h 192.168.65.130 -c -p 7002
192.168.65.130:7002> set hello helloword
-> Redirected to slot [866] located at 172.16.80.177:7001
OK
172.16.80.177:7006> get hello
-> Redirected to slot [866] located at 172.16.80.177:7001
"helloword"
Remarks:
Password setting: Modify the redis-700X.conf file in all Redis clusters. Add:
Add the following two lines in the next line: [port 700X]
masterauth passwd123
requirepass passwd123
After setting a password if you need to use a variety of command redis-trib.rb
such as: ./ redis-trib.rb check 127.0.0.1:7000, it will error ERR] Sorry, can not connect to node 127.0.0.1:7000
solve Method: vim /usr/local/rvm/gems/ruby-2.3.3/gems/redis-4.0.0/lib/redis/client.rb, then modify passord
class Client
DEFAULTS = {
:url => lambda { ENV["REDIS_URL"] },
:scheme => "redis",
:host => "127.0.0.1",
:port => 6379,
:path => nil,
:timeout => 5.0,
:password => "passwd123",
:db => 0,
:driver => nil,
:id => nil,
:tcp_keepalive => 0,
:reconnect_attempts => 1,
:inherit_socket => false
}