一:测试环境 服务器DELL-R720 系统版本:CentOS6.4 64bit 虚拟化 KVM 虚拟机系统CentOS6.4 64bit 虚拟机4台: Nginx01,nginx02,web01,web02 网络环境: Nginx01:192.168.40.30 Nginx02:192.168.40.31 Web01:192.168.40.86 Web02:192.168.40.87 Vip:192.168.40.32
二:服务配置 1>Nginx01 [iyunv@nginx01~]#wget http://nginx.org/packages/centos ... .el6.ngx.noarch.rpm [iyunv@nginx01~]#rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm [iyunv@nginx01~]#yum list |grep nginx nginx-release-centos.noarch 6-0.el6.ngx installed nginx.x86_64 1.2.8-1.el6.ngx nginx nginx-debug.x86_64 1.2.8-1.el6.ngx nginx [iyunv@nginx01~]#yum -y install nginx [iyunv@nginx01~]#vi /etc/nginx/nginx.conf user nginx; worker_processes 1;
error_log /var/log/nginx/error.log; #error_log /var/log/nginx/error.log notice; #error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;
events { worker_connections 1024; }
http { include /etc/nginx/mime.types; default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on; #tcp_nopush on;
#keepalive_timeout 0; keepalive_timeout 65;
#gzip on;
# Load config files from the /etc/nginx/conf.d directory # The default server is in conf.d/default.conf include /etc/nginx/conf.d/*.conf;
upstream test { server 192.168.40.86; server 192.168.40.87; }
server{ listen 80; server_name 192.168.40.32; location / { proxy_pass http://test; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
}
>Keepalived 配置信息 [iyunv@nginx01~]#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL }
vrrp_script chk_http_port { script "/opt/nginx_pid.sh" #nginx 服务检测脚本 interval 2 weight 2 }
vrrp_instance VI_1 { state MASTER #nginx02修改为BACKUP interface eth0 virtual_router_id 55 priority 100 #nginx02 修改为 80 低于MASTER即可 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { #指定一下检测的脚本http_port http_port } virtual_ipaddress { 192.168.40.32 } }
2>付上nginx服务检测脚本 [iyunv@nginx01~]# vi /opt/nginx_pid.sh #!/bin/bash A=`ps -C nginx --no-header |wc -l` if [ $A -eq 0 ];then /etc/init.d/nginx start sleep 3 if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then killall keepalived fi fi
3>nginx02配置与01相同,keepalived稍作修改
4>启动nginx01 02 上的nginx 和keepalived服务
三:测试 1>负载均衡测试
刷新
Down掉40.86这台web再测试只有87这台web响应。
2>HA 测试 Ip a 结果显示vip在nginx01上 [iyunv@nginx01 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo 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 qlen 1000 link/ether 52:54:00:57:3b:80 brd ff:ff:ff:ff:ff:ff inet 192.168.40.30/24 brd 192.168.40.255 scope global eth0 inet 192.168.40.32/32 scope global eth0 inet6 fe80::5054:ff:fe57:3b80/64 scope link valid_lft forever preferred_lft forever
移动01这台nginx的配置文件然后down掉nginx服务,使其nginx服务不能启动 [iyunv@nginx01 ~]#/etc/init.d/nginx status nginx is stopped
然后ip a发现VIP地址已经飘到nginx02上去了 [iyunv@nginx02 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo 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 qlen 1000 link/ether 52:54:00:95:45:5f brd ff:ff:ff:ff:ff:ff inet 192.168.40.31/24 brd 192.168.40.255 scope global eth0 inet 192.168.40.32/32 scope global eth0 inet6 fe80::5054:ff:fe95:455f/64 scope link valid_lft forever preferred_lft forever
|