系统版本:ubuntu 14.04.3 LTS

服务器准备: 
lb01—> ifconfig 显示结果: 192.168.91.136 作用:安装keepalived 及 nginx 
lb02—> ifconfig 显示结果: 192.168.91.135 作用:安装keepalived 及 nginx 
web01—> ifconfig 显示结果: 192.168.91.134 作用:安装nginx 负责展示 index.html页面 
web02—> ifconfig 显示结果: 192.168.91.137 作用:安装nginx 负责展示 index.html页面

运行原理 
1 nginx 在 web01/web02 上充当的是 web服务器 
2 nginx 在 lb01 跟 lb02 上 充当的是 负载均衡的工具,使用的是 upstream 工具 
3 keepalived 在lb01 跟 lb02 上进行 虚拟vip 配置

具体安装步骤: 
1 在 lb01/lb02/web01/web02 上安装 nginx. 
apt-get install nginx -y 
2 修改 web01/web02 的默认显示页面,内容分别改成 web01 / web02 
echo ‘web02’ > /usr/share/nginx/html/index.html 
echo ‘web01’ > /usr/share/nginx/html/index.html 
3 配置 lb01 / lb02 为 负载均衡服务器。 
编辑 nginx.conf 配置文件 
nano /etc/nginx/nginx.conf

在当前默认的配置文件新增如下:
upstream backed{
    server 192.168.91.134:80 weight=5;
    server 192.168.91.137:80 weight=5;
}
server{
    listen 80;
    server_name www.jingshan.net;
    location / {
        root html;
        index index.html index.htm;
        proxy_pass http://backed;
    }
}

并且修改分别对应的/etc/hosts 下的文件,命名为 nano /etc/hosts 新增如下: 
127.0.0.1 www.jingshan.net 
重启 nginx, 
service nginx restart 
然后用curl www.jingshan.net 进行测试,当多次测试后应该发现,会分别显示 web01 或者 web02 证明配置ok.

4 安装 keepalived;命令为 apt-get install keepalived -y 
复制配置文件过去 
cp /usr/share/doc/keepalived/samples/keepalived.conf.vrrp /etc/keepalived/keepalived.conf 
编辑配置文件: 
nano /etc/keepalived/keepalived.conf 
对应的 lb01:的配置文件为: 
global_defs { 
notification_email { 
acassen 

notification_email_from Alexandre.Cassen@firewall.loc 
smtp_server 192.168.200.1 
smtp_connect_timeout 30 
router_id lb01 

vrrp_instance VI_1 { 
state MASTER 
interface eth0 
garp_master_delay 10 
smtp_alert 
virtual_router_id 51 
priority 100 
advert_int 1 
authentication { 
auth_type PASS 
auth_pass 1111 

virtual_ipaddress { 
# optional label. should be of the form “realdev:sometext” for 
# compatibility with ifconfig. 
192.168.91.188/24 label eth0:1 


lb02配置文件: 
global_defs { 
notification_email { 
acassen 

notification_email_from Alexandre.Cassen@firewall.loc 
smtp_server 192.168.200.1 
smtp_connect_timeout 30 
router_id lb02 
}

vrrp_instance VI_1 { 
state BACKUP 
interface eth0 
garp_master_delay 10 
smtp_alert 
virtual_router_id 51 
priority 40 
advert_int 1 
authentication { 
auth_type PASS 
auth_pass 1111 

virtual_ipaddress { 
# optional label. should be of the form “realdev:sometext” for 
# compatibility with ifconfig. 
192.168.91.188/24 label eth0:1 

}

分别重启对应的keepalived; 
使用ip addr 命令检索 看看vip 是否新增成功。

如果发现了: inet 192.168.188/24 scope global secondary eth0:1

然后在你的电脑本地的Host文件修改为 
192.168.91.188 www.jingshan.net

通过浏览器进行访问,如果分别显示 web01 跟 web02 就是正常的。 
当 lb01 停止的时候,也可以进行 正常的显示。