redis_2">1、下载redis
本次redis安装部署选择当前最新的稳定版本7.4.1
下载链接: https://github.com/redis/redis/archive/refs/tags/7.4.1.tar.gz
安装包名是redis-7.4.1.tar.gz
redis__9">2、安装redis集群 三主三从
redis三主三从集群机器规划
机器IP | redis端口 | redis安装目录 | 每个redis节点配置文件 | 每个redis节点的集群配置文件 |
---|---|---|---|---|
192.168.2.162 | 6379 | /usr/local/redis/ | /usr/local/redis/conf/redis-cluster-6379.conf | /data/redis7/cluster/6379/data/nodes-6379.conf |
192.168.2.162 | 6380 | /usr/local/redis/ | /usr/local/redis/conf/redis-cluster-6380.conf | /data/redis7/cluster/6380/data/nodes-6380.conf |
192.168.2.164 | 6379 | /usr/local/redis/ | /usr/local/redis/conf/redis-cluster-6379.conf | /data/redis7/cluster/6379/data/nodes-6379.conf |
192.168.2.164 | 6380 | /usr/local/redis/ | /usr/local/redis/conf/redis-cluster-6380.conf | /data/redis7/cluster/6380/data/nodes-6380.conf |
192.168.2.168 | 6379 | /usr/local/redis/ | /usr/local/redis/conf/redis-cluster-6379.conf | /data/redis7/cluster/6379/data/nodes-6379.conf |
192.168.2.168 | 6380 | /usr/local/redis/ | /usr/local/redis/conf/redis-cluster-6380.conf | /data/redis7/cluster/6380/data/nodes-6380.conf |
上传redis-7.4.1.tar.gz到三台机器上
下面是安装redis 7.4.1命令,3台redis机器都执行
cd /path/to/redis-7.4.1.tar.gz
tar -xzvf redis-7.4.1.tar.gz
cd redis-7.4.1
make
make install PREFIX=/usr/local/redis
PREFIX=/usr/local/redis 这个是指定redis的安装目录,因此要保证这个目录为空。
安装完后 会自动创建/usr/local/redis目录 内容如下 内容就是redis的可执行程序redis-cli、redis-server
配置redis环境变量
vi /etc/profile
在/etc/profile末尾处中增加如下内容:
export REDIS_HOME=/usr/local/redis
export PATH=$PATH:$REDIS_HOME/bin
之后执行source加载环境变量:
source /etc/profile
在执行如下命令。如果能输出redis版本说明环境变量配置成功:
redis-server --version
然后创建redis集群的目录、配置文件等,3台redis机器都执行
mkdir -p /data/redis7/cluster/6379
mkdir -p /data/redis7/cluster/6380cd /data/redis7/cluster/6379
mkdir logs data
cd /data/redis7/cluster/6380
mkdir logs datamkdir /usr/local/redis/conf
mkdir /usr/local/redis/pidvi /usr/local/redis/redis.conf
redis.conf中填入以下内容:
#redis访问端口(需要修改)
port 6379#设置开启守护进程
daemonize yes#关闭保护模式
protected-mode no#开启远程访问
bind 0.0.0.0# 开启AOF
appendonly yes#日志存放位置(需要修改)
logfile "/data/redis7/cluster/6379/logs/redis-cluster-6379.log"#数据保存目录(需要修改)
dir /data/redis7/cluster/6379/data/# pid文件创建位置(需要修改)
pidfile /usr/local/redis/pid/redis_6379.pid#aof 持久化文件位置(需要修改) 在dir目录下
appendfilename "6379-appendonly.aof"#修改rdb持久化文件位置(需要修改) 在dir目录下
dbfilename 6379-dump.rdb# 开启集群模式
cluster-enabled yes
# 每一个redis节点需要有一个配置文件,三主三从集群共6个节点,因此需要6份配置文件。每个节点处于集群中都需要告知其他所有节点,达到集群中节点信息交换的目的,这个文件用于存储集群模式下的集群状态等信息,这个文件是由redis自己维护,不用管。如果你要重新创建集群,那么把每个节点的这个文件删了就行
#(需要修改) 在dir目录下
cluster-config-file nodes-6379.conf
# 超时时间,超时则认为master宕机,随后主备切换单位毫秒
cluster-node-timeout 5000
#设置集群节点间访问密码
masterauth 123456
#设置连接密码
requirepass 123456
创建redis集群配置文件
cp /usr/local/redis/redis.conf /usr/local/redis/conf/redis-cluster-6379.conf
cp /usr/local/redis/redis.conf /usr/local/redis/conf/redis-cluster-6380.conf
编辑 /usr/local/redis/conf/redis-cluster-6380.conf 把其中的6379修改成6380
sed -i "s/6379/6380/g" /usr/local/redis/conf/redis-cluster-6380.conf
nohup /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-cluster-6379.conf &
nohup /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-cluster-6380.conf &
创建redis集群,在任意一台redis机器上执行就行
–cluster-replicas 1 :表示每个主节点需要1个从节点。这里随机分配主从关系,如果需要定制,则可以不加该参数,使用add-node来定制,
redis-cli --cluster create 192.168.2.162:6379 192.168.2.162:6380 192.168.2.164:6379 192.168.2.164:6380 192.168.2.168:6379 192.168.2.168:6380 --cluster-replicas 1 -a 123456
会提示是否接受上面配置,输入yes就可以了,就开始初始化集群,等待完成即可,
至此redis三主三从集群部署完成。
查看redis集群状态
redis-cli -p 6379
登入redis后 执行
auth 123456
cluster info
cluster nodes
其中cluster info 显示 cluster_state:ok 即表示集群创建成功。否则集群创建失败。
redis_174">3、配置redis开机自启动
3.1、建立启动脚本
官方启动脚本 https://github.com/redis/redis/blob/unstable/utils/redis_init_script
这是官方启动脚本内容
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.### BEGIN INIT INFO
# Provides: redis_6379
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Redis data structure server
# Description: Redis data structure server. See https://redis.io
### END INIT INFOREDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cliPIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"case "$1" instart)if [ -f $PIDFILE ]thenecho "$PIDFILE exists, process is already running or crashed"elseecho "Starting Redis server..."$EXEC $CONFfi;;stop)if [ ! -f $PIDFILE ]thenecho "$PIDFILE does not exist, process is not running"elsePID=$(cat $PIDFILE)echo "Stopping ..."$CLIEXEC -p $REDISPORT shutdownwhile [ -x /proc/${PID} ]doecho "Waiting for Redis to shutdown ..."sleep 1doneecho "Redis stopped"fi;;*)echo "Please use start or stop as first argument";;
esac
vi /etc/init.d/redisd6379 填入以下改造后内容如下(对官方启动脚本加以改造适应我们自己安装的redis程序)
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.### BEGIN INIT INFO
# Provides: redis_6379
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Redis data structure server
# Description: Redis data structure server. See https://redis.io
### END INIT INFOREDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cliPIDFILE=/usr/local/redis/pid/redis_${REDISPORT}.pid
CONF="/usr/local/redis/conf/redis-cluster-${REDISPORT}.conf"case "$1" instart)if [ -f $PIDFILE ]thenecho "$PIDFILE exists, process is already running or crashed"elseecho "Starting Redis server..."$EXEC $CONFfi;;stop)if [ ! -f $PIDFILE ]thenecho "$PIDFILE does not exist, process is not running"elsePID=$(cat $PIDFILE)echo "Stopping ..."$CLIEXEC -p $REDISPORT -a '123456' shutdownwhile [ -x /proc/${PID} ]doecho "Waiting for Redis to shutdown ..."sleep 1doneecho "Redis stopped"fi;;*)echo "Please use start or stop as first argument";;
esac
redis_286">3.2、复制多份redis启动脚本给集群使用
cd /etc/init.d
cp redisd6379 redisd6380
sed -i "s/REDISPORT=6379/REDISPORT=6380/" /etc/init.d/redisd6380
3.3、添加可执行权限
chmod +x /etc/init.d/redisd6379
chmod +x /etc/init.d/redisd6380
3.4、配置开机自启动
添加开机自启动
chkconfig --add redisd6379
chkconfig --add redisd6380
查看开机自启动配置
chkconfig --list
默认开启runlevel=2345的启动
至此redis开机自启动配置完成