這種方案,使用兩個vip位址,前端使用2台機器,互為主備,同時有兩台機器工作,當其中一台機器出現故障,兩台機器的請求轉移到一台機器負擔,非常適合於當前架構環境,故本次採用此方案對網站進行高可用架構。
下面我們以第二種配置演示實現雙主高可用負載平衡。
第一組
VIP1:192.168.36.100036.10005313236383836.100.36.1005236.3396.
備nginx:192.168.36.86
第二組
VIP2:192.168.36.100
主nginx:192.168.36.86%
web服務:
web1:
192.168.36.215
web2:
192.168.36.80一、nginx配置(安裝省略). 6.86的nginx。檢查訪問是否正常.
web1
http://192.168.36.86:88/
web2
http://192.168.36.86:99/
二、安裝、設定keepalived(安裝省略)
1)192.168.36.86配置vi /etc/
keepalived/keepD. 2)192.168.36.99配置
vi /etc/keepalived
/keepalived.conf
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; upstream web1 { server 192.168.36.215:80 max_fails=3 fail_timeout=3s; } upstream web2 { server 192.168.36.80:80 max_fails=3 fail_timeout=3s; } server { listen 88; server_name localhost; location / { root html; index index.html index.htm; proxy_pass http://web1; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } server { listen 99; server_name localhost; location / { root html; index index.html index.htm; proxy_pass http://web2; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }
! Configuration File for keepalived
global_defs {
notification_email {
monitor@3evip.cn
#failover@firewall.loc
}
notification_email_from tianwei7518@163.com
smtp_server smtp.163.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/chk_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 100
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.36.100
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 200
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.36.200
}
}
service keepalived start
service keepalived start
http://192.168.36.200:88/
web2http://192.1682 2.168.36.200:99 /
可以正常存取。
我們可以試一下,關閉一個keepalived。關閉192.168.36.86上的keepalived
service keepalived stop
tail -f /var/log/messages
192.168.36.86日誌:
! Configuration File for keepalived
global_defs {
notification_email {
monitor@3evip.cn
#failover@firewall.loc
}
notification_email_from tianwei7518@163.com
smtp_server smtp.163.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/chk_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 100
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.36.100
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 200
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.36.200
}
}
#!/bin/sh #description: # 如果启动失败,则停止keepalived status=$( ps -C nginx --no-heading| wc -l) if [ "${status}" = "0" ]; then /usr/local/nginx/sbin/nginx status2=$( ps -C nginx --no-heading| wc -l) if [ "${status2}" = "0" ]; then service keepalived stop fi fi
再開啟192.168.36.86上的
86日誌:
Feb 7 00:39:05 slave-d Keepalived[5738]: Stopping Keepalived v1.2.15 (02/07,2015) Feb 7 00:39:05 slave-d Keepalived_vrrp[5741]: VRRP_Instance(VI_2) sending 0 priority Feb 7 00:39:05 slave-d Keepalived_vrrp[5741]: VRRP_Instance(VI_2) removing protocol VIPs. Feb 7 00:39:05 slave-d Keepalived_healthcheckers[5740]: Netlink reflector reports IP 192.168.36.200 removed Feb 7 00:39:05 slave-d avahi-daemon[1823]: Withdrawing address record for 192.168.36.200 on eth0.
Feb 7 00:39:11 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Transition to MASTER STATE Feb 7 00:39:12 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Entering MASTER STATE Feb 7 00:39:12 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) setting protocol VIPs. Feb 7 00:39:12 slave-c Keepalived_healthcheckers[25102]: Netlink reflector reports IP 192.168.36.200 added Feb 7 00:39:12 slave-c avahi-daemon[1832]: Registering new address record for 192.168.36.200 on eth0.IPv4. Feb 7 00:39:12 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.36.200 Feb 7 00:39:17 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.36.200
參考文章:CentOS6.5 keepalived詳解及實作Nginx服務的高可用性
用NginX+用NginX+可用的可用負載和雙主節點負載平衡
nginx+keepalived實現nginx雙主高可用的負載平衡
以上就介紹了Keepalived+nginx實現雙主高可用負載平衡,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。