一键发布,备注:不涉及调优。二进制文件需要官网自行下载及编译。样例目录结构
./
./
|-- 5.0.14(自行打包,并压缩)
| `-- bin
| |-- redis-benchmark
| |-- redis-check-aof
| |-- redis-check-rdb
| |-- redis-cli
| |-- redis-sentinel -> redis-server
| `-- redis-server
|-- 5.0.14.tar.gz
|-- 6.2.13.tar.gz
|-- 6.2.13_TLS.tar.gz
|-- 7.0.12.tar.gz
|-- 7.0.12_TLS.tar.gz
`-- deploy_cluster_on_one_machine.sh
使用方式
[prouser@VM-8-4-centos redis]$ sh deploy_cluster_on_one_machine.sh
1) 5.0.14.tar.gz 3) 6.2.13_TLS.tar.gz 5) 7.0.12_TLS.tar.gz
2) 6.2.13.tar.gz 4) 7.0.12.tar.gz
请选择要安装的redis安装包:1
你选择的redis版本是:[5.0.14]
输入部署目录:/home/prouser/app/redis
1) 10.0.8.4
2) 172.17.0.1
请选择要绑定的IP地址:1
你选择的绑定地址是[10.0.8.4]
输入Master端口号[默认端口号:6379]:16379
你输入的Master端口号是:[16379]
输入密码:1234abcD
你输入的密码是:[1234abcD]
输入redis名称:myredis
你输入的redis名称是:[myredis]
脚本详情
#!/bin/bash
set -e
function general_server_config_files(){
install_dir=$1
redis_version=$2
ip=$3
port=$4
passwd=$5
master_port=$6
redis_name=$7
mkdir -p ${install_dir}/${redis_version}/data/redis_${port}
cat > ${install_dir}/${redis_version}/conf/redis_${port}.conf << EOF
bind "${ip}"
port "${port}"
protected-mode yes
daemonize yes
requirepass "${passwd}"
masterauth "${passwd}"
pidfile "${install_dir}/${redis_version}/data/redis_${port}/redis.pid"
logfile "${install_dir}/${redis_version}/data/redis_${port}/redis.log"
dir "${install_dir}/${redis_version}/data/redis_${port}"
dbfilename "redis.rdb"
save 3600 1
save 300 100
save 60 10000
appendonly yes
appendfilename redis.aof
appendfsync everysec
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
EOF
if [ "${port}" != "${master_port}" ]; then
echo "replicaof ${ip} ${master_port} " >> ${install_dir}/${redis_version}/conf/redis_${port}.conf
fi
}
function general_sentinel_config_files(){
install_dir=$1
redis_version=$2
ip=$3
port=$4
passwd=$5
master_port=$6
redis_name=$7
mkdir -p ${install_dir}/${redis_version}/data/sentinel_${port}
cat > ${install_dir}/${redis_version}/conf/sentinel_${port}.conf << EOF
bind "${ip}"
port "${port}"
protected-mode yes
daemonize yes
requirepass "${passwd}"
dir "${install_dir}/${redis_version}/data/sentinel_${port}"
pidfile "${install_dir}/${redis_version}/data/sentinel_${port}/sentinel.pid"
logfile "${install_dir}/${redis_version}/data/sentinel_${port}/sentinel.log"
sentinel deny-scripts-reconfig yes
sentinel monitor ${redis_name} ${ip} ${master_port} 2
sentinel auth-pass ${redis_name} "${passwd}"
EOF
}
function general_run_scripts(){
install_dir=$1
redis_version=$2
master_port=$3
slave_port1=`expr ${PORT} + 1`
slave_port2=`expr ${PORT} + 2`
sentinel_port1=`expr ${PORT} + 10000`
sentinel_port2=`expr ${PORT} + 10001`
sentinel_port3=`expr ${PORT} + 10002`
cat > ${install_dir}/${redis_version}/start_redis.sh << EOF
#!/bin/bash
bin/redis-server conf/redis_${master_port}.conf
sleep 5
bin/redis-server conf/redis_${slave_port1}.conf
sleep 5
bin/redis-server conf/redis_${slave_port2}.conf
sleep 5
echo "redis server cluster[start...]"
EOF
cat > ${install_dir}/${redis_version}/start_sentinel.sh << EOF
#!/bin/bash
bin/redis-sentinel conf/sentinel_${sentinel_port1}.conf
sleep 5
bin/redis-sentinel conf/sentinel_${sentinel_port2}.conf
sleep 5
bin/redis-sentinel conf/sentinel_${sentinel_port3}.conf
sleep 5
echo "redis sentinel cluster[start...]"
EOF
}
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
PS3='请选择要安装的redis安装包:'
select redis_package_choice in `ls | grep gz`
do
redis_package=${redis_package_choice}
break
done
REDIS_VERSION=${redis_package/.tar.gz/}
echo "你选择的redis版本是:[$REDIS_VERSION]"
read -p "输入部署目录:" INSTALL_DIR
mkdir -p ${INSTALL_DIR}/${REDIS_VERSION}/data
mkdir -p ${INSTALL_DIR}/${REDIS_VERSION}/conf
cp $SCRIPTPATH/$redis_package ${INSTALL_DIR}/
cd ${INSTALL_DIR}/;tar -xf $redis_package;rm $redis_package
PS3='请选择要绑定的IP地址:'
select ip_choice in $(ip addr | grep -Po "(?<=inet )(\d+\.){3}\d+(?=/\d+ brd)")
do
IP=$ip_choice
break
done
echo "你选择的绑定地址是[${IP}]"
read -p "输入Master端口号[默认端口号:6379]:" iport
PORT=${iport:-6379}
echo "你输入的Master端口号是:[${PORT}]"
read -p "输入密码:" PASSWD
echo "你输入的密码是:[${PASSWD}]"
read -p "输入redis名称:" REDISNAME
echo "你输入的redis名称是:[${REDISNAME}]"
for port_offset in {0..2};
do
redis_port=`expr ${PORT} + ${port_offset}`
general_server_config_files ${INSTALL_DIR} ${REDIS_VERSION} ${IP} ${redis_port} ${PASSWD} ${PORT}
sentinel_port=`expr ${redis_port} + 10000`
general_sentinel_config_files ${INSTALL_DIR} ${REDIS_VERSION} ${IP} ${sentinel_port} ${PASSWD} ${PORT} ${REDISNAME}
done
general_run_scripts ${INSTALL_DIR} ${REDIS_VERSION} ${PORT}