wKioL1bJuweDOWNoAADeueXdyuE504.jpg

1、实现目的


两台服务器,通过Nginx+Keepalived实现web服务器的负载均衡(主备模式)

负载均衡服务器IP地址:172.16.61.2 、172.16.61.3

两台服务器,通过httpd提供web服务

httpd服务器IP地址:172.16.61.1(静态资源) 、 172.16.100.123(动态资源)

VIP:172.16.61.9


2、配置nginx服务,两台nginx服务器需要配置为相同的配置选项。


user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
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;
    keepalive_timeout  65;    
    include /etc/nginx/conf.d/*.conf;
        
    upstream websrvs {             #后端静态资源服务器
         server 172.16.61.1 weight=1 max_fails=2 fail_timeout=6s;       
         ip_hash;     
    }       
    upstream phpsrvs {             #后端动态资源服务器
         server 172.16.100.123 weight=1 max_fails=2 fail_timeout=6s;
         ip_hash;
    }  
     server {
         listen 80;
         root /data/www/vhost1/;
         server_name www.tz.com;
         index index.php  index.html;
         location / {    
            proxy_pass         #静态资源统一调度到此组服务器 
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
         }      
         location ~* \.php$ {                   #动态资源统一调度到此组服务器
            proxy_pass http://phpsrvs;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
         }  
    }
}

注:另一台nginx需要同样的配置

启动nginx



3、配置keepalived


主的:

! Configuration File for keepalived

global_defs {
   notification_email {
      root@localhost
   }
   notification_email_from kaadmin@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id localhost
   vrrp_mcast_group4 224.61.0.18
}

vrrp_script chk_nginx {                 #对nginx做健康状态监测的脚本
   script "killall -0 nginx"
   interval 1
   weight -20  
}
vrrp_instance VI_1 {
    state MASTER                  #此为主
    interface eno16777736
    virtual_router_id 161
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass LAkVF4q8
    }
    virtual_ipaddress {
        172.16.61.9/16    
    }

    track_script {
        chk_nginx
    }

}

备:

! Configuration File for keepalived

global_defs {
   notification_email {
      root@localhost
   }
   notification_email_from kaadmin@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id localhost
   vrrp_mcast_group4 224.61.0.18
}

vrrp_script chk_nginx {                 #对nginx做健康状态监测的脚本
   script "killall -0 nginx"
   interval 1
   weight -20
}
vrrp_instance VI_1 {
    state BACKUP                   #此为备
    interface eno16777736
    virtual_router_id 161
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass LAkVF4q8
    }
    virtual_ipaddress {
        172.16.61.9/16    
    }

    track_script {        #追踪定义的监测脚本
        chk_nginx
    }

}

启动keepalived,并检查ip地址是否实现漂移:

2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:fe:96:33 brd ff:ff:ff:ff:ff:ff
    inet 172.16.61.2/16 brd 172.16.255.255 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet 172.16.61.9/16 scope global secondary eno16777736   #主服务器已经配置上VIP
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fefe:9633/64 scope link 
       valid_lft forever preferred_lft forever
       
[root@localhost keepalived]# systemctl stop keepalived  #关闭主服务器的keepalived

2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:70:e2:27 brd ff:ff:ff:ff:ff:ff
    inet 172.16.61.3/16 brd 172.16.255.255 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet 172.16.61.9/16 scope global secondary eno16777736    #VIP成功漂移至备用服务器
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe70:e227/64 scope link 
       valid_lft forever preferred_lft forever


3、配置后端httpd服务


安装httpd以及php

为了测试,在两台httpd服务器上配置相同的静态资源以及动态资源,但在内容上做不同的标记

[root@localhost html]# vim index.html    #静态资源httpd服务器主页内容

<h1>backend 2 server</h1>

[root@CentOS_6 ~]# vim /var/www/html/index.html  #动态资源httpd服务器主页内容

<h1>backend 3 server</h1>

[root@localhost html]# vim index.php         #静态资源服务器php资源测试页

<h1>backend2 </h1>
<?php
        phpinfo();
?>
 
[root@CentOS_6 ~]# vim /var/www/html/index.php   #定义动态资源服务器php测试页
  
<h1>backend3</h1>
<?php
     phpinfo();
?>

启动httpd服务


4、测试结果


请求172.16.61.9/index.html

wKioL1bJ0r2QOJUhAAA5OCCD7SI795.png

如图可以看出返回的资源为静态服务器提供的资源页

请求172.16.61.9/index.php

wKioL1bJ0u6DMlVOAAAyIGu2t2k240.png

如图所示请求的为动态资源时返回的结果为动态资源服务器提供的测试页


5、健康状态监测:


关闭主服务器的keepalived服务

wKiom1bJ0wnxDc32AAA5OCCD7SI252.png

如图所示依然能够正确返回请求资源,说明vip成功进行了地址漂移。


测试nginx健康状态监测:

两台keepalived服务器都开启keepalived服务


关闭主服务器的nginx服务,再次发出请求:

wKioL1bJ1CXgdm0QAAA5OCCD7SI086.png

如图所示,对nginx成功进行了健康状态监测,并实现了地址漂移。