blog.etiantian.org 10.0.0.17 lb01 主 10.0.0.5
bbs.etiantian.org 10.0.0.18 lb02主 10.0.0.6
说明:后端的web服务器一个是apacheweb服务,一个nginx服务
测试,,选用静态的html,不涉及nfs以及mysql
1.A1-nginx 负载服务器 01-10.0.0.5-lb01
①查看keepalive的配置文件
[root@lb01 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
notification_email {
}
notification_email_from 31333741@qq.com
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_17 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.17/24 dev eth0 label eth0:1
}
}
vrrp_instance VI_18 {
state BACKUP
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.18/24 dev eth0 label eth0:2
}
}
[root@lb01 ~]#
②查看nginx的配置文件
[root@lb01 ~]# cat /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream server_pools {
server 172.16.1.7:80 weight=1;
server 172.16.1.8:80 weight=1;
}
server {
listen 10.0.0.17:80;
server_name blog.etiantian.org;
location / {
proxy_pass http://server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 10.0.0.18:80;
server_name bbs.etiantian.org;
location / {
proxy_pass http://server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
[root@lb01 ~]#
A2-nginx 负载服务器 02 -10.0.0.6-lb02
①查看keepalive的配置文件
[root@lb02 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
notification_email {
}
notification_email_from 31333741@qq.com
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL1
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.17/24 dev eth0 label eth0:1
}
}
vrrp_instance VI_18 {
state MASTER
interface eth0
virtual_router_id 52
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.18/24 dev eth0 label eth0:2
}
}
[root@lb02 ~]#
②查看nginx的配置文件
[root@lb02 ~]# cat /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream server_pools {
server 172.16.1.7:80 weight=1;
server 172.16.1.8:80 weight=1;
}
server {
listen 80;
server_name blog.etiantian.org;
location / {
proxy_pass http://server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 10.0.0.18:80;
server_name bbs.etiantian.org;
location / {
proxy_pass http://server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
[root@lb02 ~]#
测试blog和bbs的站点:
宕掉一台负载均衡器再接着测试
宕机以后查看IP:可以看出IP全部飘过来了
[root@lb01 ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:90:A5:31
inet addr:10.0.0.5 Bcast:10.0.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe90:a531/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:299420 errors:0 dropped:0 overruns:0 frame:0
TX packets:319616 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:26869861 (25.6 MiB) TX bytes:58054361 (55.3 MiB)
eth0:1 Link encap:Ethernet HWaddr 00:0C:29:90:A5:31
inet addr:10.0.0.17 Bcast:0.0.0.0 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
eth0:2 Link encap:Ethernet HWaddr 00:0C:29:90:A5:31
inet addr:10.0.0.18 Bcast:0.0.0.0 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
eth1 Link encap:Ethernet HWaddr 00:0C:29:90:A5:3B
inet addr:172.16.1.5 Bcast:172.16.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe90:a53b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1971 errors:0 dropped:0 overruns:0 frame:0
TX packets:356 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:334109 (326.2 KiB) TX bytes:47217 (46.1 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:293602 errors:0 dropped:0 overruns:0 frame:0
TX packets:293602 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:17072153 (16.2 MiB) TX bytes:17072153 (16.2 MiB)
[root@lb01 ~]#
web测试(依然OK):
keepalive和 drbd 都是基于硬件方面的切换
例如:服务器宕机或者keepalive服务停掉,ip就会自动切换
但是有一个问题就是 nginx服务没有正常开启或者宕掉。但是这个时候
keepalive的虚拟IP不会自动漂移,以下是解决方案。
思路:监控nginx服务,如果不正常,就停掉keepalived或者关机
方法1.守护进程的方式
说明:可以在每个负载均衡器上让这个脚本在后台执行
[root@lb02 scripts]# cat check_w_proxy.sh
#!/bin/bash
while true
do
nginxpid=`ps -C nginx --no-header|wc -l`
if [ $nginxpid -eq 0 ];then
/application/nginx/sbin/nginx
sleep 5
nginxpid=`ps -C nginx --no-header|wc -l`
if [ $nginxpid -eq 0 ];then
/etc/init.d/keepalived stop
exit 1
fi
fi
sleep 5
done
[root@lb02 scripts]#
[root@lb02 scripts]# sh check_w_proxy.sh &
[1] 3046
方法2.keepalive内置触发配置
在每个负载均衡器的虚拟Ip为master内配置,然后调用,
主节点挂了,备节点来接管
[root@lb02 scripts]# cat /etc/keepalived/keepalived.conf
global_defs {
notification_email {
}
notification_email_from 31333741@qq.com
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL1
}
vrrp_script check_nginx_proxy {
script "/server/scripts/check_w_proxy.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.17/24 dev eth0 label eth0:1
}
}
vrrp_instance VI_18 {
state MASTER
interface eth0
virtual_router_id 52
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.18/24 dev eth0 label eth0:2
}
track_script {
check_nginx_proxy
}
}
[root@lb02 scripts]#