###########################lb01

cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived_lb01


global_defs {

#  notification_email {

#  [email protected]

#  }


   router_id LB01

}


vrrp_script check_lb {  

script "/server/scripts/check_lb.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.3/24 dev eth0 label eth0:1  

    }

    track_script {

    check_lb

    }

}   

vrrp_instance VI_2 {

    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.4/24 dev eth0 label eth0:2

    }

}

#######################lb02

 cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived_lb01


global_defs {

#  notification_email {

#  [email protected]

#  }


   router_id LB01

}


vrrp_script check_lb {  

script "/server/scripts/check_lb.sh"  

interval 2                            

weight 2

}


vrrp_instance VI_1 {

    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.3/24 dev eth0 label eth0:1  

    }

    track_script {

    check_lb

    }

}   

vrrp_instance VI_2 {

    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.4/24 dev eth0 label eth0:2

    }

}



1.1 安装

yum  insytall -y keepalived

1.2 启动

/etc/init.d/keepalived start

1.3 查看运行状态

/etc/init.d/keepalived status

1.4 修改keepalived配置文件【lb双主】

1.4.1 lb01

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived_lb01

 

global_defs {

#  notification_email {

[email protected]

#  }

 

   router_id LB01

}

 

vrrp_script check_lb { 

script "/server/scripts/check_lb.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.3/24 dev eth0 label eth0:1 

    }

    track_script {

    check_lb

    }

}  

vrrp_instance VI_2 {

    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.4/24 dev eth0 label eth0:2

    }

}

1.4.2 lb02

cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived_lb01

 

global_defs {

#  notification_email {

[email protected]

#  }

 

   router_id LB02

}

 

vrrp_script check_lb {

#/server/scripts/下的脚本 【当nginx停止时keeplived停止】

#脚本内容

cat check_lb.sh

#!/bin/bash

count=`ps -ef |grep [n]ginx |wc -l`

 

if [ $count -lt 2 ];then

       /etc/init.d/keepalived stop

   fi

script "/server/scripts/check_lb.sh" 

interval 2                           

weight 2

}

 

vrrp_instance VI_1 {

    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.3/24 dev eth0 label eth0:1 

    }

    track_script {

    check_lb

    }

}  

vrrp_instance VI_2 {

    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.4/24 dev eth0 label eth0:2

    }

}

1.5 修改nginx配置文件【lb

vim /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 web_pools {

        server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=30s;

        server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=30s;

    }

    server {

        listen       10.0.0.3:80;

        server_name  www.etiantian.org;

        location / {

                proxy_pass http://web_pools;

                proxy_set_header Host $host;

                proxy_set_header X-Forwarded-For $remote_addr;

        }

    }

       server {

        listen       10.0.0.4:80;

        server_name  blog.etiantian.org;

        location / {

                proxy_pass http://web_pools;

                proxy_set_header Host $host;

                proxy_set_header X-Forwarded-For $remote_addr;

        }

    }

}

1.6 i查看vip

ip a s eth0

1.7 修改内核参数能监听不属于本地的网卡

echo 'net.ipv4.ip_nonlocal_bind = 1' >>/etc/sysctl.conf

sysctl -p【配置生效】