准备工作
首先把系统打开文件最大数打开
echo "* soft nofile 65535" >> /etc/security/limits.conf
echo "* hard nofile 65535" >> /etc/security/limits.conf
下载安装redis
[root@redis ~]# wget http://download.redis.io/releases/redis-4.0.14.tar.gz
[root@redis ~]# ls
anaconda-ks.cfg redis-4.0.14.tar.gz
[root@redis ~]# tar xf redis-4.0.14.tar.gz
[root@redis ~]# ls
anaconda-ks.cfg redis-4.0.14 redis-4.0.14.tar.gz
[root@redis ~]# cd redis-4.0.14/
[root@redis redis-4.0.14]# ls
00-RELEASENOTES COPYING Makefile redis.conf runtest-sentinel tests
BUGS deps MANIFESTO runtest sentinel.conf utils
CONTRIBUTING INSTALL README.md runtest-cluster src
[root@redis redis-4.0.14]# make
cd src && make all
make[1]: 进入目录“/root/redis-4.0.14/src”
CC Makefile.dep
....................
....................
make[3]: gcc:命令未找到
make[3]: *** [net.o] 错误 127
make[3]: 离开目录“/root/redis-4.0.14/deps/hiredis”
make[2]: *** [hiredis] 错误 2
make[2]: 离开目录“/root/redis-4.0.14/deps”
make[1]: [persist-settings] 错误 2 (忽略)
CC adlist.o
/bin/sh: cc: 未找到命令
make[1]: *** [adlist.o] 错误 127
make[1]: 离开目录“/root/redis-4.0.14/src”
make: *** [all] 错误 2
缺少gcc
yum install -y gcc g++ gcc-c++
退出删掉当前redis目录,然后重新解压一份,在进入目录进行编译
新建cluster集群目录,在下面新建6个目录,我用端口号命名
[root@redis redis-cluster]# mkdir -p /redis-cluster/700{0..5}/{logs,data} && cd /redis-cluster
[root@redis redis-cluster]# tree
.
├── 7000
│ ├── data
│ ├── logs
│ └── redis.conf
├── 7001
│ ├── data
│ ├── logs
│ └── redis.conf
├── 7002
│ ├── data
│ ├── logs
│ └── redis.conf
├── 7003
│ ├── data
│ ├── logs
│ └── redis.conf
├── 7004
│ ├── data
│ ├── logs
│ └── redis.conf
├── 7005
│ ├── data
│ ├── logs
│ └── redis.conf
├── manage
├── redis-cli
├── redis-server
└── redis-trib.rb
18 directories, 10 files
执行下面脚本生成配置文件
IP=`ip a|grep 'inet' |grep -v '127.0.0.1'|grep -v 'inet6'|awk '{print $2}'|awk -F'/' '{print $1}'`
for i in {0..5}
do
cat > 700${i}/redis.conf <<EOF
daemonize yes
port 700${i}
cluster-enabled yes
cluster-config-file cluster-nodes-700${i}.conf
cluster-node-timeout 15000
appendonly yes
bind ${IP}
protected-mode no
dbfilename dump-700${i}.rdb
logfile /redis-cluster/700${i}/logs/redis.log
pidfile /redis-cluster/700${i}/data/redis.pid
dir /redis-cluster/700${i}/data
appendfilename "appendonly-700${i}.aof"
EOF
done
拷贝server与cli脚本到当前目录
[root@redis redis-cluster]# cp /root/redis-4.0.14/src/redis-server .
[root@redis redis-cluster]# cp /root/redis-4.0.14/src/redis-cli .
[root@redis redis-cluster]# cp /root/redis-4.0.14/src/redis-trib.rb .
下一步启动redis
一共六个,手动启动太麻烦,写个脚本
#!/bin/bash
# by seven
# date 2019-04-09
# redis管理脚本
# start,stop,restart,status
# 该脚本用于管理redis实例,如启动、关闭、状态等
# exp: redis.sh restart 7000
# redis.sh stop all
# # # # # # # # #
# 参数
IP=`ip a|grep 'inet' |grep -v '127.0.0.1'|grep -v 'inet6'|awk '{print $2}'|awk -F'/' '{print $1}'`
port=$2
if [ ! $# -eq 2 ];then
echo "Warning! 请输入两个参数!"
exit 1
fi
if [ -z ${port} ];then
echo "Warning! 请输入端口号"
exit 1
fi
# start函数
start(){
IS_PORT_EXISTS=$( ps -ef | grep redis| grep ${port} | grep -v grep | wc -l )
if [ $IS_PORT_EXISTS -ge 1 ];then
echo "Warning! 端口号已存在!"
exit 1
else
/redis-cluster/redis-server /redis-cluster/${port}/redis.conf
fi
}
# stop函数
stop(){
IS_PORT_EXISTS=$(ps -ef |grep redis |grep ${port} |grep -v grep |wc -l)
if [ $IS_PORT_EXISTS -lt 1 ];then
echo "Warning! 端口不存在!"
exit 1
else
/redis-cluster/redis-cli -h ${IP} -p ${port} shutdown
fi
}
# status函数
status(){
/redis-cluster/redis-trib.rb check ${IP}:${port}
}
case $1 in
start)
start
;;
stop)
stop
;;
status)
status
;;
?|help)
echo $"Usage: 'redis' {start|status|stop|help|?}"
;;
*)
echo $"Usage: 'redis' {start|status|stop|help|?}"
esac
安装集群需要ruby
[root@redis redis-cluster]# yum -y install ruby
[root@redis redis-cluster]# gem install redis
Fetching: redis-4.1.0.gem (100%)
ERROR: Error installing redis:
redis requires Ruby version >= 2.2.2.
于是,要升级Ruby版本
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh
列出Ruby版本
rvm list known
安装2.6版本
rvm install 2.6
创建集群
[root@redis redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.233.135:7000 192.168.233.135:7001 192.168.233.135:7002 192.168.233.135:7003 192.168.233.135:7004 192.168.233.135:7005
查看集群状态
[root@redis redis-cluster]# ./manage status 7002
Performing Cluster Check (using node 192.168.233.135:7002)
M: 17832c45510253a957ab1eae12c319e704b5f3e4 192.168.233.135:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: ac1382c38da3e6adedbd23c1dc6e4022778fb377 192.168.233.135:7005
slots: (0 slots) slave
replicates d8488b701db6bfa3dde4a1bda6bb3da707001adc
M: c517f80778d85b8f750b2e591cce912278f9b5f5 192.168.233.135:7004
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 8a5ea442250bebb98fb7d7c9d37e91d4d1d362a6 192.168.233.135:7000
slots: (0 slots) slave
replicates c517f80778d85b8f750b2e591cce912278f9b5f5
M: d8488b701db6bfa3dde4a1bda6bb3da707001adc 192.168.233.135:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 9981216f1988809ddd52ae9d4ec923aec75a79d5 192.168.233.135:7003
slots: (0 slots) slave
replicates 17832c45510253a957ab1eae12c319e704b5f3e4
[OK] All nodes agree about slots configuration.
Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.
添加节点
redis-trib.rb add-node 10.205.6.9:7004 10.205.6.9:7000
添加完默认是主节点,下面命令是改为从节点
redis-cli -c -p 7004 cluster replicate 8459ac5af5f427c24ccb4ffe849bde37d734a017