服务器:
节点1:10.10.175.55 端口:6379/7379
节点2:10.10.175.56 端口:6379/7379
节点3:10.10.175.57 端口:6379/7379
以下操作均需在每台服务器上执行
- 安装依赖关系
yum install make zlib openssl* ImageMagick-devel gcc* rubygems -y
2、创建节点目录
mkdir -p /usr/local/redis-cluster/redis6379
mkdir -p /usr/local/redis-cluster/redis7379
3、下载redis-4.0.12版本并解压
cd /usr/local/redis-cluster
wget http://download.redis.io/releases/redis-4.0.12.tar.gz
tar -zxvf redis-4.0.12.tar.gz
4、安装redis
cd redis-4.0.12
make #编译
make install PREFIX=/usr/local/redis #编译安装并将redis放在/usr/local/redis下,这样就可以直接使用redis
5、配置集群文件
将配置文件复制到安装目录
cp redis.conf /usr/local/redis-cluster/redis6379/redis-6379.conf
将配置安装文件复制到集群节点
cp /usr/local/redis/bin/* /usr/local/redis-cluster/redis6379/
cp /usr/local/redis/bin/* /usr/local/redis-cluster/redis7379/
内核调优(可无)
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo "vm.overcommit_memory = 1" >> /usr/local/sysctl.conf
6、编辑配置文件
vim /usr/local/redis-cluster/redis6379/redis-6379.conf
更改以下配置:
port 6379 #端口
cluster-enabled yes #启用集群模式
cluster-node-timeout 5000 #超时时间
protected-mode no
appendonly yes #开启持久化模式
daemonize yes #后台运行
bind 10.10.175.55 #127.0.0.0配置为本机IP
cluster-config-file nodes-6379.conf #nodes配置为节点名称,不输入的话就是默认port端口
pidfile /var/run/redis_6379.pid #防止启动多个进程副本,只启动7376
requirepass Redispass01! #在最后一行加入密码
更改完成后复制redis-6379.conf至redis-7379并修改端口、pid和nodes文件名称
cp /usr/local/redis-cluster/redis6379/redis-6379.conf /usr/local/redis-cluster/redis7379/redis-7379.conf
vim /usr/local/redis-cluster/redis7379/redis-7379.conf
6379和7379更改完成后拷贝至另外两台服务器,并修改pid IP地址信息
7、安装ruby(在10.10.175.55主节点安装就行)
上传ruby-3.0.1.tar.gz安装包
tar zxvf ruby-3.0.1.tar.gz
cd ruby-3.0.1
./configure --prefix=/usr/local/ruby-3.0.1
make && make install
rm -f /usr/bin/ruby
rm -f /usr/bin/gem
ln -s /usr/local/ruby-3.0.1/bin/ruby /usr/bin/ruby
ln -s /usr/local/ruby-3.0.1/bin/gem /usr/bin/gem
ruby --version
gem --version
8、安装redis集群依赖包
wget http://rubygems.org/downloads/redis-4.0.0.gem
上传redis-4.0.0.gem包
gem install -l ./redis-4.0.0.gem
9、更改ruby调用的redis密码
find / -name client.rb
vim /usr/local/ruby-3.0.1/lib/ruby/gems/3.0.0/gems/redis-4.0.0/lib/redis/client.rb
password => "Redispass01!"
10、启动redis
每台服务器上编辑startall.sh脚本
cd /usr/local/redis-cluster
/bin/bash
DIR=/usr/local/redis-cluster
cd $DIR/redis6379
./redis-server redis-6379.conf
cd $DIR/redis7379
./redis-server redis-7379.conf
ps -ef |grep redis|grep -v grep
启动redis
sh startall.sh
10、启动redis集群(主节点)
cd /usr/local/redis-cluster/src/
./redis-trib.rb create --replicas 1 10.10.175.55:6379 10.10.175.55:7379 10.10.175.56:6379 10.10.175.56:7379 10.10.175.57:6379 10.10.175.57:7379
输入yes
11、测试集群
[root@bjtn-5g183-151 src]# ./redis-cli -c -h 10.10.183.151 -p 6379 -a Redispass01! 必须输入-c参数
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.10.183.151:6379> get *
(nil)
10.10.183.151:6379> keys *
(empty list or set)
10.10.183.151:6379> set name lee
-> Redirected to slot [5798] located at 10.10.183.152:6379
OK
10.10.183.152:6379> get name
"lee"
10.10.183.152:6379>
[root@bjtn-5g183-151 src]# redis-cli -c -h 10.10.183.152 -p 6379 -a Redispass01! get name
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"lee"
[root@bjtn-5g183-151 src]# redis-cli -c -h 10.10.183.153 -p 6379 -a Redispass01! get name
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"lee"
[root@bjtn-5g183-151 src]# redis-cli -c -h 10.10.183.153 -p 7379 -a Redispasswd01! get name
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"lee"
[root@bjtn-5g183-151 src]# redis-cli -c -h 10.10.183.152 -p 7379 -a Redispasswd01! get name
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"lee"
[root@bjtn-5g183-151 src]# redis-cli -c -h 10.10.183.151 -p 7379 -a Redispasswd01! get name
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"lee"
12、停止集群
每台服务器上编辑stopall.sh脚本
cd /usr/local/redis-cluster
/bin/bash
PID1=ps -ef |grep 6379|grep -v grep|awk '{print $2}'
PID2=ps -ef |grep 7379|grep -v grep|awk '{print $2}'
kill -9 $PID1 $PID2
ps -ef |grep 'redis'
13、再次启动集群
注意再次启动需要删除每个节点下生成的文件,示例:
cd /usr/local/redis-cluster/redis6379
rm -rf appendonly.aof nodes-6379.conf dump.rdb
cd /usr/local/redis-cluster/redis7379
rm -rf appendonly.aof nodes-7379.conf dump.rdb