keepalived高可用雙主配置


###########################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【配置生效】





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章