keepalived高可用使用案例

1、keepalived实现IP地址的高可用

测试环境
serverA:

eth0:192.168.10.11

eth1:192.168.170.6

serverB:

eth0:192.168.10.12

eth1:192.168.178.7

目的
实现192.168.10.11这个地址的高可用

步骤

1、安装keepalived
在主机和备用机分别安装keepalived

sudo apt-get install keepalived

2、创建监测和切换脚本
分别是chk_apache.sh、ip_master.sh和ip_backup.sh三个脚本。

ip切换脚本ip_master.sh和ip_backup.sh中的网卡配置文件请根据实际情况进行配置。
serverA:

root@ubuntu:/etc/keepalived# more scripts/chk_apache.sh
#!/bin/bash

PORT=`netstat -nap |grep "LISTEN" |grep "8088" |awk '{print $4}' |cut -d: -f2`

if [ $PORT == '8088' ]
then
echo "apache is running ..."
exit 0
else
echo "apache is down!"
/etc/init.d/keepalived stop
/sbin/ifdown eth0
exit 1
fi
root@ubuntu:/etc/keepalived# more scripts/ip_master.sh
#!/bin/bash

cp /etc/network/interfaces.master /etc/network/interfaces

/sbin/ifdown eth0
/sbin/ifup eth0
root@ubuntu:/etc/keepalived# more scripts/ip_backup.sh
#!/bin/bash

#cp /etc/network/interfaces.master /etc/network/interfaces

/sbin/ifdown eth0
#/sbin/ifup eth0

serverB:

root@ubuntu:/etc/keepalived# more scripts/chk_apache.sh
#!/bin/bash

PORT=`netstat -nap |grep "LISTEN" |grep "8088" |awk '{print $4}' |cut -d: -f2`

if [ $PORT == '8088' ]
then
echo "apache is running ..."
exit 0
else
echo "apache is down!"
/etc/init.d/keepalived stop
/sbin/ifdown eth0
exit 1
fi
root@ubuntu:/etc/keepalived# more scripts/ip_master.sh
#!/bin/bash

cp /etc/network/interfaces.master /etc/network/interfaces

/sbin/ifdown eth0
/sbin/ifup eth0
root@ubuntu:/etc/keepalived# more scripts/ip_backup.sh
#!/bin/bash

cp /etc/network/interfaces.backup /etc/network/interfaces

/sbin/ifdown eth0
/sbin/ifup eth0

3、配置keepalived.conf
分别在主机和备机上创建配置文件/etc/keepalived/keepalived.conf ,具体配置类似如下:

serverA:

root@ubuntu:/etc/keepalived# more keepalived.conf
vrrp_script chk_apache {
    
    
script "/etc/keepalived/scripts/chk_apache.sh"
interval 2
}

vrrp_instance VI_1 {
    
    
state MASTER
interface eth1
virtual_router_id 51
priority 100
advert_int 1
authentication {
    
    
auth_type PASS
auth_pass 1111
}
#virtual_ipaddress {
    
    
# 192.168.0.221/24
#}

track_script {
    
    
chk_apache
}

notify_master /etc/keepalived/scripts/ip_master.sh
notify_backup /etc/keepalived/scripts/ip_backup.sh
}

serverB:

root@ubuntu:/etc/keepalived# more keepalived.conf
vrrp_script chk_apache {
    
    
script "/etc/keepalived/scripts/chk_apache.sh"
interval 2
}

vrrp_instance VI_1 {
    
    
state BACKUP
interface eth1
virtual_router_id 51
priority 50
advert_int 1
authentication {
    
    
auth_type PASS
auth_pass 1111
}
#virtual_ipaddress {
    
    
# 192.168.0.221/24
#}

track_script {
    
    
chk_apache
}

notify_master /etc/keepalived/scripts/ip_master.sh
notify_backup /etc/keepalived/scripts/ip_backup.sh
}

4、测试
关闭serverA的keepalived进程或者关闭apache服务进行测试,serverB切换成主服务器;

扫描二维码关注公众号,回复: 12435412 查看本文章

先后开启serverA的apache服务和keepalived服务,serverA切换成主服务器,serverB切换成备服务器。

2、keepalived实现nginx高可用

环境两台Ubuntu 14.04 server 64bit

ip:192.168.1.235 master

ip:192.168.1.234 back

vip:192.168.1.233

两台机器都安装keepalived

aptitude update

aptitude  install -y keepalived
aptitude  install -y  nginx

master上面进行创建

该脚本检测ngnix的运行状态,并在nginx进程不存在时尝试重新启动ngnix,如果启动失败则停止keepalived,准备让其它机器接管。

cat  /etc/keepalived/check_nginx.sh

#!/bin/bash

counter=$(ps -C nginx --no-heading|wc -l)

if [ "${counter}" = "0" ]; then

    /etc/init.d/nginx start

    sleep 2

    counter=$(ps -C nginx --no-heading|wc -l)

    if [ "${counter}" = "0" ]; then

        /etc/init.d/keepalived stop

    fi

fi
cat  /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
    
    
    notification_email {
    
    
        no_reply@cyyun.com
        laoyw@cyyun.com
    }
    notification_email_from laoyw@cyyun.com
    smtp_server mail.cyyun.com
    smtp_connect_timeout 30
    router_id LVS_DEVEL
}

vrrp_script chk_nginx {
    
    
#    script "killall -0 nginx"
    script "/etc/keepalived/check_nginx.sh"
    interval 2
    weight -5
    fall 3  
    rise 2
}

vrrp_instance VI_1 {
    
    
    state MASTER
    interface eth0
    mcast_src_ip 192.168.1.235
    virtual_router_id 49
    priority 101
    advert_int 2
    authentication {
    
    
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    
    
        192.168.1.233
    }
    track_script {
    
    
       chk_nginx
    }
}

back 服务器上面配置

该脚本检测ngnix的运行状态,并在nginx进程不存在时尝试重新启动ngnix,如果启动失败则停止keepalived,准备让其它机器接管。

cat /etc/keepalived/check_nginx.sh

#!/bin/bash

counter=$(ps -C nginx --no-heading|wc -l)

if [ "${counter}" = "0" ]; then

    /etc/init.d/nginx start

    sleep 2

    counter=$(ps -C nginx --no-heading|wc -l)

    if [ "${counter}" = "0" ]; then

        /etc/init.d/keepalived stop

    fi

fi

cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
    
    
    notification_email {
    
    
        no_reply@cyyun.com
        laoyw@cyyun.com
    }
    notification_email_from laoyw@cyyun.com
    smtp_server mail.cyyun.com
    smtp_connect_timeout 30
    router_id LVS_DEVEL
}

vrrp_script chk_nginx {
    
    
#    script "killall -0 nginx"
    script "/etc/keepalived/check_nginx.sh"
    interval 2
    weight -5
    fall 3  
    rise 2
}

vrrp_instance VI_1 {
    
    
    state BACKUP
    interface eth0
    mcast_src_ip 192.168.1.234
    virtual_router_id 49
    priority 100
    advert_int 2
    authentication {
    
    
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    
    
        192.168.1.233
    }
    track_script {
    
    
       chk_nginx
    }
}

启动Nginx

/etc/init.d/Nginx start

启动keepalived

/etc/init.d/keepalived start

在master上面查看

ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:77:41:72 brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.235/24 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.11.233/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe77:4172/64 scope link
       valid_lft forever preferred_lft forever

在back上面查看

#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:b4:01:59 brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.234/24 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feb4:159/64 scope link
       valid_lft forever preferred_lft forever

http://192.168.11.233即可。

3、使用apache的mod_proxy模块做tomcat热备

第一步:安装apache
注意编译的时候需要加入mod_proxy模块。

第二步:配置tomcat
分别找到需要加入负载均衡或热备的tomcat的配置文件server.xml,修改engine行,添加jvmroute。例如

如果不需要session复制,其他默认保存即可。

第三步:配置ajp_proxy
不建议使用apache的http_proxy。

找到apache虚拟机主机配置文件/usr/local/apache/conf/extra/httpd-vhost.conf,添加如下内容。这个是负载均衡配置。

ProxyRequests off

Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<Proxy balancer://cyyun.com>
BalancerMember ajp://192.168.122.102:8009 route=node1 loadfactor=1
BalancerMember ajp://192.168.122.105:8009 route=node2 loadfactor=1
#ProxySet stickysession=JSESSIONID
ProxySet stickysession=ROUTEID
#byrequests(default),bytraffic,bybusyness
ProxySet lbmethod=bytraffic
ProxySet nofailover=On
</Proxy>

ProxyPass /upload !
ProxyPass /balancer !
ProxyPass / balancer://cyyun.com/
ProxyPassReverse / balancer://cyyun.com/

<Location /balancer>
SetHandler balancer-manager
Proxypass !
Order allow,deny
Allow from all
</Location>

<Proxy *>
Order allow,deny
Allow from all
</Proxy>

<Location />
Order allow,deny
Allow from all
</Location>

添加tomcat主要就是BalancerMember这行,loadfactor值越高,在负载均衡中权重就越大。负载均衡是需要考虑session的问题。

如果不需要负载均衡,使用仅仅需要热备,修改BalancerMember ajp://192.168.122.105:8009 route=node2 loadfactor=1成BalancerMember ajp://192.168.122.105:8009 route=node2 status=+H 。

参考网址:

http://httpd.apache.org/docs/2.2/mod/mod_proxy.html

http://www.ibm.com/developerworks/cn/opensource/os-lo-apache-tomcat/

猜你喜欢

转载自blog.csdn.net/qq_40907977/article/details/112176585