Haproxy+Keepalived高可用双机单活
编程技术  /  houtizong 发布于 3年前   309
10.75.201.2:虚拟IP(VIP),与master和slave在同一子网;以此IP对外暴露服务10.75.201.67(master):安装MyApp+ Haproxy + Keepalived10.75.201.66(slave):安装MyApp+ Haproxy + Keepalived
10.75.201.2:8000 <-> 10.75.201.67:900010.75.201.2:8000 <-> 10.75.201.66:9000
tar -xvf haproxy-1.4.24.tar.gzcd haproxy-1.4.24make TARGET=linux26 ARCH=x86_64make install
global log 127.0.0.1 alert log 127.0.0.1 alert debugdefaults log global mode http option dontlognull option redispatch retries 3 contimeout 10800000 clitimeout 10800000 srvtimeout 10800000listen MyApp 10.75.201.2:8000 mode tcp balance roundrobin option tcpka server MyApp01 10.75.201.67:9000 check inter 5000 downinter 500 server MyApp02 10.75.201.66:9000 check inter 5000 backup
rpm -Uvh keyutils-libs-devel-1.4-4.el6.x86_64.rpmrpm -Uvh libcom_err-devel-1.41.12-14.el6.x86_64.rpmrpm -Uvh libsepol-devel-2.0.41-4.el6.x86_64.rpmrpm -Uvh libselinux-devel-2.0.94-5.3.el6.x86_64.rpmrpm -Uvh krb5-devel-1.10.3-10.el6.x86_64.rpmrpm -Uvh zlib-devel-1.2.3-29.el6.x86_64.rpmrpm -Uvh openssl-devel-1.0.0-27.el6.x86_64.rpm
./configuremakemake install
master:vrrp_script check_MyApp { script "/app/risk/MyApp/shell/check_MyApp.sh" # verify the port is listening interval 2 # check every 2 seconds}vrrp_instance VI_1 { interface eth1 # interface to monitor state MASTER virtual_router_id 11 # Assign one ID for this route priority 101 # 101 on master, 100 on backup virtual_ipaddress { 10.75.201.2 # the virtual IP } track_script { check_MyApp } notify_stop "/app/risk/MyApp/shell/stopAll.sh"}slave:vrrp_script check_MyApp { script "killall -0 haproxy" # verify the haproxy is running interval 2 # check every 2 seconds}vrrp_instance VI_1 { interface eth1 # interface to monitor state BACKUP virtual_router_id 11 # Assign one ID for this route priority 100 # 101 on master, 100 on backup virtual_ipaddress { 10.75.201.2 # the virtual IP } track_script { check_MyApp } notify "/app/risk/MyApp/shell/keepalivednotify.sh"}
#!/bin/bash#if MyApp is down, stop keepalivedPORT=9000nc -z 127.0.0.1 $PORT 1>/dev/null 2>&1; result=$?;if [ $result -eq 0 ]; then exit 0else service keepalived stop . ./log.sh "keepalived stopped" exit 1fi
#!/bin/bashLOCAL=10.75.201.66REMOTE=10.75.201.67PORT=9000NOW=`date "+%Y-%m-%d %H:%M:%S"`startLocal(){ nc -z $LOCAL $PORT 1>/dev/null 2>&1; result=$?; if [ $result -eq 0 ]; then echo $NOW-$LOCAL:$PORT 'is listening! No need to restart!' else . /app/risk/MyApp/shell/startAll.sh echo $NOW-'start at' $LOCAL:$PORT 'ok' fi}#startRemote(){# nc -z $REMOTE $PORT 1>/dev/null 2>&1; result=$?;# if [ $result -eq 0 ]; then# echo $NOW-$REMOTE:$PORT 'is listening! No need to restart!'# else# ssh root@$REMOTE "sh /app/risk/MyApp/shell/startAll.sh"# echo $NOW-'start at' $REMOTE:$PORT 'ok'# fi#}stopRemote(){ nc -z $REMOTE $PORT 1>/dev/null 2>&1; result=$?; if [ $result -eq 0 ]; then ssh root@$REMOTE "sh /app/risk/MyApp/shell/stopAll.sh" fi}stopLocal(){ nc -z $LOCAL $PORT 1>/dev/null 2>&1; result=$?; if [ $result -eq 0 ]; then . /app/risk/MyApp/shell/stopAll.sh fi}TYPE=$1NAME=$2STATE=$3case $STATE in "MASTER") startLocal stopRemote exit 0 ;; "BACKUP") stopLocal exit 0 ;; "FAULT") stopLocal exit 0 ;; *) echo "unknown state" exit 1 ;;esac
cd /etc/sysconfigln -s /usr/local/etc/sysconfig/keepalived .cd /etc/rc3.d/ln -s /usr/local/etc/rc.d/init.d/keepalived S100keepalivedcd /etc/init.d/ln -s /usr/local/etc/rc.d/init.d/keepalived .
echo "net.ipv4.ip_nonlocal_bind = 1" >> /etc/sysctl.confsysctl -p
#!/bin/sh## Startup script for the Keepalived daemon## processname: keepalived# pidfile: /var/run/keepalived.pid# config: /etc/keepalived/keepalived.conf# chkconfig: - 21 79# description: Start and stop Keepalived# Source function library. /etc/rc.d/init.d/functions# Source configuration file (we set KEEPALIVED_OPTIONS there). /etc/sysconfig/keepalived......
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接