redis集群部署小脚本

#!/bin/bash
native_ip=$(ip a | awk /ens33/ |awk /inet/'{print $2}'|awk -F"/" '{print $1}')
#关闭防火墙和selinux
systemctl stop firewalld && setenforce 0
#下载所需依赖包
yum_bag="wget gcc make"
for yum in $yum_bag
do
 yum -y install $yum &>/dev/null
  if [ $? -eq 0 ];then
   echo "$yum 安装成功"
  else
   echo "$yum 安装失败,检查yum源"
   exit 1
  fi
done
#下载Redis源码包
wget http://download.redis.io/releases/redis-5.0.10.tar.gz &>/dev/null
#源码编译安装redis
tar xvzf redis-5.0.10.tar.gz -C /usr/local/ &>/dev/null
mv /usr/local/redis-5.0.10/ /usr/local/redis
cd /usr/local/redis/ && make &>/dev/null
mkdir /usr/local/redis-Cluster/{7001..7006}/data -p
path=/usr/local/redis-Cluster
mkdir_port="$(ls /usr/local/redis-Cluster)"
for i in $mkdir_port
do
cat > $path/$i/redis.conf <<EOF
port $i
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
bind $native_ip
pidfile $path/$i/redis_$i.pid
dir $path/$i/data
EOF
 if [ -f $path/$i/redis.conf ];then
  echo "$i 配置文件成功"
 else
  echo "$i 配置失败"
  exit 2
 fi
done
ln -s /usr/local/redis/src/redis-server /usr/bin/redis-server
for j in $mkdir_port
do
redis-server $path/$j/redis.conf &>/dev/null
 if [ $? -eq 0 ];then
  echo "$j 启动成功"
 else
  echo "$j 启动失败"
  exit 3
 fi
done
ln -s /usr/local/redis/src/redis-cli /usr/bin/redis-cli
redis-cli --cluster create --cluster-replicas 1 ${native_ip}:7001 ${native_ip}:7002 ${native_ip}:7003 ${native_ip}:7004 ${native_ip}:7005 ${native_ip}:7006

One thought on “redis集群部署小脚本

Comments are closed.