1. 程式人生 > >Keepalived 之 雙主模式+DNS輪詢機制 實現高負載

Keepalived 之 雙主模式+DNS輪詢機制 實現高負載

location 記錄 ipa res 均衡 sta text 不一致 for

一、Keepalived雙主模式+DNS輪詢機制作用

作用:在單主模式下,備機通常會以等待狀態放著,不接受任何數據,導致所有數據請求只往主機-負載均衡發送,做成資源浪費;而雙主模式,即創造兩個VIP,兩個VIP分別放在兩臺負載均衡的機器上,同時兩臺主機均為對方的備機,以作VIP的漂移,服務接管作用,加入DNS輪詢機制,使客戶端的域名分別依次解釋到兩個VIP上,形成兩臺負載均衡主機同時對外提供服務。同時也解決了單主模式下的單機性能屏頸。

二、網絡拓撲圖

技術分享圖片

三、兩臺負載均衡主機的Keepalived 配置文件

lb01 keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
   [email protected]
}
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id lb01
}

vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 53
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 3333
    }
    virtual_ipaddress {
        10.3.150.200/24 dev eth1 label eth1:1
    }
}
vrrp_instance VI_2 {
    state BACKUP
    interface eth1
    virtual_router_id 54
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 4444
    }
    virtual_ipaddress {
        10.3.150.201/24 dev eth1 label eth1:1
    }
}

lb02 keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
   [email protected]
}
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id lb01
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth1
    virtual_router_id 53
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 3333
    }
    virtual_ipaddress {
        10.3.150.200/24 dev eth1 label eth1:1
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface eth1
    virtual_router_id 54
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 4444
    }
    virtual_ipaddress {
        10.3.150.201/24 dev eth1 label eth1:1
    }
}

四、兩臺負載均衡主機的上的nginx配置文件

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

        server 10.3.150.198:80 weight=1;
        server 10.3.150.199:80 weight=1;
        }
    server {
        listen       80;
        server_name  localhost;
        location / {
             proxy_pass http://server_pools;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

五、兩臺web主機,均為httpd,靜態內容(內容不一致)

六、把www.kang.com分別添加兩條A記錄,解釋IP為:10.3.150.200 與 10.3.150.201

七、從多個客戶端分別訪問www.kang.com 域名,同時觀察兩臺負載均衡的nginx訪問日誌,看是否正常,如正常,代表DNS設置成功。

八、在兩臺負載均衡主機上,分別關停keepalived服務,看是否出現VIP漂移到備機上,如漂移過去,代表設置成功。

Keepalived 之 雙主模式+DNS輪詢機制 實現高負載