1、搭建一个 redis 哨兵集群
基于redis主从复制,解决master单点故障。
集群运行环境centos8, redis-5.0.3
master:10.0.0.8
node1:10.0.0.18
node2:10.0.0.28
哨兵通用配置文件
sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' /etc/redis.conf
sed -i.bak -e 's/sentinel monitor mymaster 127.0.0.1 6379 2/sentinel monitor mymaster 10.0.0.8 6379 2/' -e '/# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd/a sentinel auth-pass mymaster 123456' -e 's/sentinel down-after-milliseconds mymaster 30000/sentinel down-after-milliseconds mymaster 5000/' -e '# protected-mode no/a protected-mode no' /etc/redis-sentinel.conf
三台集群分别启动哨兵服务
systemctl enable --now redis-sentinel.service
2、实现 redis cluster 得部署
redis cluster集群环境最低要求:至少6台机器 本环境 centos8:
10.0.0.8(master)-----10.0.0.18(slave),
10.0.0.28(master)-----10.0.0.38(slave),
10.0.0.48(master)-----10.0.0.58(slave)
以下操作每个集群节点都执行,集群环境:
1、 dnf -y intall redis
2, 修改配置文件redis.conf
sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf
3、重启redis服务 systemctl restart redis
#创建集群 执行1次
redis-cli -a 123456 --cluster create 10.0.0.8:6379 10.0.0.18:6379 10.0.0.28:6379 10.0.0.38:6379 10.0.0.48:6379 10.0.0.58:6379 --cluster-replicas 1 (#注 --cluster-replicas 1 表示每个主节点对应1个从节点)
##查看集群节点信息
redis-cli cluster nodes
#主从节点详细信息
redis-cli -a 123456 -c INFO replication
3、部署一个 tomcat 服务
cat install_tomcat_for_centos.sh
. /etc/init.d/functions
DIR=`pwd`
JDK_FILE="jdk-8u251-linux-x64.tar.gz"
TOMCAT_FILE="apache-tomcat-8.5.57.tar.gz"
JDK_DIR="/usr/local"
TOMCAT_DIR="usr/local"
install_jdk(){
if ! [ -f "$DIR/$JDK_FILE" ];then
action "$JDK_FILE 文件不存在" false
exit;
elif [ -d $JDK_DIR/jdk ];then
action "JDK 已经安装" false
exit
else
[ -d "$JDK_DIR" ]||mkdir -pv $JDK_DIR
fi
tar xvf $DIR/$JDK_FILE -C $JDK_DIR
cd $JDK_DIR && ln -s jdk1.8* jdk
cat /etc/profile.d/jdk.sh<<EOF
export JAVA_HOME=$JDK_DIR/jdk
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=JAVA_HOME/lib/:$JRE_HOME/lib/
export PATH=$PATH:$JAVA_HOME/bin
EOF
. /etc/profile.d/jdk.sh
java -version && action "JDK 安装完成" || {action "JDK 安装失败" false;exit}
}
install_tomcat(){
if ! [ -f "$DIR/$TOMCAT_FILE" ];then
action "$TOMCAT_FILE 文件不存在" false
exit;
elif [ -d $TOMCAT_DIR/tomcat ];then
action "TOMCAT 已经安装" false
exit
else
[ -d "$TOMCAT_DIR" ]||mkdir -pv $TOMCAT_DIR
fi
tar xf $DIR/$TOMCAT_FILE -C $TOMCAT_DIR
cd $TOMCAT_DIR && ln -s apacha-tomcat-*/ tomcat
echo "PATH=$TOMCAT_DIR/tomcat/bin:"'$PATH'> /etc/profile.d/tomcat.sh
id tomcat & >/dev/null || useradd -r -s /sbin/nologin tomcat
cat > $TOMCAT_DIR/tomcat/conf/tomcat.conf <<EOF
JAVA_HOME=$JDK_DIR/jdk
EOF
chown -R tomcat.tomcat $TOMCAT_DIR/tomcat/
cat >/lib/systemd/system/tomcat.service <<EOF
[Unit]
Description=Tomcat
After=syslog.target.network.target
[Service]
Type=forking
EnvironmentFile=$TOMCAT_DIR/tomcat/conf/tomcat.conf
ExecStart=$TOMCAT_DIR/tomcat/bin/startup.sh
ExecStop=$TOMCAT_DIR/tomcat/bin/shutdown.sh
PrivateTmp=true
User=tomcat
Group=tomcat
[Install]
wantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now tomcat.service
systemctl is-active tomcat.service &> /dev/null && action "TOMCAT 安装完成"||{action "TOMCAT 安装失败" false ; exit; }
install_jdk
install_tomcat
}