Redis集群部署

Posted 西风h

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis集群部署相关的知识,希望对你有一定的参考价值。

采用三主三从,6节点集群模式,版本5.0.9

安装脚本如下,脚本是采用三主机,6节点模式,每台主机运行主从两个redis节点,端口分别是7001,7002(默认7001是主,7002是从)

#!/bin/bash
echo "本地yum源是否配置完毕,是请忽略,否请按CTRL+C退出!"
sleep 3
echo "自动化安装redis-5.0.9版本3台服务器集群:"
read -p "请根据实际数据挂载盘输入redis-cluster安装的主目录(譬如/wls或是/data):" maindir
echo "---使用3台机6节点的交叉部署模式,3台机均需执行该脚本---"
if [ -f /root/redis-5.0.9.tar.gz ];then

echo "系统内核参数优化!"
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo "net.core.somaxconn=551" >> /etc/sysctl.conf
echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
sysctl -p

dir=$maindir/redis-cluster
mkdir -p $dir/bin 

yum -y install zlib-devel openssl-devel gcc-c++
echo "redis编译安装!"
cd /root
tar xf redis-5.0.9.tar.gz
cd redis-5.0.9
make
cd src
cp redis-server redis-cli redis-check-aof redis-check-rdb redis-sentinel redis-benchmark $dir/bin
echo "如有异常,请按CTRL+C退出!如正常,将继续执行下一步!"
sleep 2

ip addr
echo "redis5集群配置:"
read -p "请输入本地内网IP地址(参考上面输出信息,非127.0.0.1):" ip
read -p "请输入节点端口1(3台机一致):" port1
read -p "请输入节点端口2(3台机一致):" port2
read -p "请输入加密密码(3台机一致):" password
echo "redis节点端口为:${port1}和${port2}"
mkdir $dir/{$port1,$port2}

cat >$dir/$port1/redis.conf <<EOF
port $port1
daemonize yes
bind $ip 127.0.0.1
dir $dir/$port1
pidfile $dir/$port1/redis.pid
logfile $dir/$port1/redis.log
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
appendonly yes
appendfsync everysec
masterauth $password
requirepass $password
EOF

cat >$dir/$port2/redis.conf <<EOF
port $port2
daemonize yes
bind $ip 127.0.0.1
dir $dir/$port2
pidfile $dir/$port2/redis.pid
logfile $dir/$port2/redis.log
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
appendonly yes
appendfsync everysec
masterauth $password
requirepass $password
EOF

echo "如有异常,请按CTRL+C退出!如正常,将继续执行下一步!"
sleep 2

$dir/bin/redis-server $dir/$port1/redis.conf &
$dir/bin/redis-server $dir/$port2/redis.conf &
sleep 2
ps -ef|grep redis

read -p "3台机6个redis节点是否已经全部安装并启动(是请输入"y"或者"Y",否请输入"n"或者"N"):" answer
  if [ "$answer" = y -o "$answer" = Y ];then
    read -p "是否创建redis集群?只需要在3台机中的任1台执行即可(是请输入"y"或者"Y",否请输入"n"或者"N"):" answer1
    if [ "$answer1" = y -o "$answer1" = Y ];then
	read -p "请输入集群第1台机的IP:" ip1
	read -p "请输入集群第2台机的IP:" ip2
	read -p "请输入集群第3台机的IP:" ip3
	echo "以下若出现交互,请输入yes并回车"
	sleep 2
    $maindir/redis-cluster/bin/redis-cli -a $password --cluster create $ip1:$port1 $ip2:$port1 $ip3:$port1 --cluster-replicas 0
    string1=`$maindir/redis-cluster/bin/redis-cli -a $password --cluster check $ip1:$port1|grep " $ip1:$port1" 2>/dev/null|grep -v Performing |awk -F ' ' '{print $2}'`
	string2=`$maindir/redis-cluster/bin/redis-cli -a $password --cluster check $ip1:$port1|grep " $ip2:$port1" 2>/dev/null|grep -v Performing |awk -F ' ' '{print $2}'`
	string3=`$maindir/redis-cluster/bin/redis-cli -a $password --cluster check $ip1:$port1|grep " $ip3:$port1" 2>/dev/null|grep -v Performing |awk -F ' ' '{print $2}'`
    $maindir/redis-cluster/bin/redis-cli -a $password --cluster add-node $ip2:$port2 $ip1:$port1 --cluster-slave --cluster-master-id $string1
    $maindir/redis-cluster/bin/redis-cli -a $password --cluster add-node $ip3:$port2 $ip2:$port1 --cluster-slave --cluster-master-id $string2
    $maindir/redis-cluster/bin/redis-cli -a $password --cluster add-node $ip1:$port2 $ip3:$port1 --cluster-slave --cluster-master-id $string3
	echo "以下为集群创建后的最终状态信息:"
	$maindir/redis-cluster/bin/redis-cli -a $password --cluster check $ip1:$port1
	echo "###已完成3台机6个redis节点的集群模式部署###"
    echo "注意:如果6个节点交叉分布在3台机(主主和主从节点均不在同1台出现),这样的话,挂掉其中任何1台服务器将不会影响集群使用,但是需要在机器恢复后,须把主节点还原回去,办法就是:停止新的主节点,此时新从节点会变成原先旧主节点,再开启刚刚关闭的节点,让其变成从节点。"
   	else
	echo "退出集群创建!"
	fi
  else
  echo "请继续安装redis节点并启动!"
  exit 0
  fi 

else
echo "请把相应的包上传到/root目录,再进行下一步!"
exit 0
fi

 

以上是关于Redis集群部署的主要内容,如果未能解决你的问题,请参考以下文章

02.使用ansible自动化部署redis集群

在Kubernetes上部署Redis集群

Redis集群演变和集群部署

Redis3.0.7集群部署完整版

docker部署redis集群(实战)

部署redis集群