nginx+keepalived高可用及雙主模式
原文地址:http://blog.csdn.net/lexang1/article/details/52386909
高可用有2中方式。
1、Nginx+keepalived 主從配置
這種方案,使用一個vip地址,前端使用2臺機器,一臺做主,一臺做備,但同時只有一臺機器工作,另一臺備份機器在主機器不出現故障的時候,永遠處於浪費狀態,對於服務器不多的網站,該方案不經濟實惠。
2、Nginx+keepalived 雙主配置
這種方案,使用兩個vip地址,前端使用2臺機器,互為主備,同時有兩臺機器工作,當其中一臺機器出現故障,兩臺機器的請求轉移到一臺機器負擔,非常適合於當前架構環境。
1、Nginx+keepalived 主從配置
1.環境如下
lb-01:192.168.96.130 nginx+keepalived-master
lb-02:192.168.96.131 nginx+keepalived-backup
VIP:192.168.75.135
nginx+tomcat負載均衡集群參照 nginx+tomcat負載均衡集群參照
兩臺機器都要安裝nginx 配置文件相同
<span style="font-size:18px;">upstream myServer{ server 192.168.96.130:8080; server 192.168.96.131:8080; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://myServer; }</span>
nginx結合keepalived高可用
為什麽使用keepalived呢?使用keepalived就用來做高可用的,提供虛擬VIP
分別在2臺機子上安裝keepalived
# yum install keepalived -y
查看keepalived版本
keepalived -v Keepalived v1.2.13 (11/20,2015)
關於2臺keepalived配置文件/etc/keepalived
在130master keepalived配置文件內容如下
<span style="font-size:18px;">global_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_http_port { script "/usr/local/src/check_nginx_pid.sh" interval 2 #(檢測腳本執行的間隔) weight 2 } vrrp_instance VI_1 { state MASTER # 備份服務器上將 MASTER 改為 BACKUP interface eno16777736 //網卡 virtual_router_id 51 # 主、備機的virtual_router_id必須相同 priority 100 # 主、備機取不同的優先級,主機值較大,備份機值較小 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.96.138 // VRRP H虛擬地址 } }</span>
在131-backup keepalived配置文件內容如下
<span style="font-size:18px;">global_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_http_port { script "/usr/local/src/check_nginx_pid.sh" interval 2 #(檢測腳本執行的間隔) weight 2 } vrrp_instance VI_1 { state BACKUP interface eno16777736 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.96.138 } }</span>
然後分別啟動2臺keepalived服務
[root@lb-01 ~]# systemctl start keepalived.service [root@lb-02 ~]# systemctl start keepalived.service
查看虛擬VIP
130-master機器查看
從上面可以看到虛擬VIP
地址192.168.96.138
131-backup機器查看
從上面可以看到沒有虛擬VIP
地址
測試訪問虛擬VIP
打開瀏覽器訪問
此時虛擬VIP可以輪詢訪問了
模擬故障
把130-master nginx和keepalived停止查看是否還能正常提供服務
[root@lb-01 sbin]# ./nginx -s stop [root@lb-01 sbin]# systemctl stop keepalived.service
此時虛擬VIP已經不再master上了
客戶端打開瀏覽器訪問是否正常訪問
130nginx和keepalived掛了也不影響服務
查看131backup機器VIP情況
此時虛擬VIP 已經在131機器上
那麽如何實現nginx+keepalived雙主模式呢?
1.其實只是需要更改下keepalived配置文件即可,配置文件實例如下
增加新的VIP192.168.96.139
,192.168.75.138
是130機器上主虛擬VIP,192.168.96.139
是131機器上主虛擬VIP
130的keepalived配置文件內容如下
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/usr/local/src/check_nginx_pid.sh"
interval 2 #(檢測腳本執行的間隔)
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eno16777736
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.96.138
}
}
vrrp_instance VI_2 {
state BACKUP
interface eno16777736
virtual_router_id 52
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.96.139
}
}
131的keepalived配置文件內容如下
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/usr/local/src/check_nginx_pid.sh"
interval 2 #(檢測腳本執行的間隔)
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eno16777736
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.96.138
}
}
vrrp_instance VI_2 {
state MASTER
interface eno16777736
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.96.139
}
}
分別啟動2臺lb上nginx和keepalived服務
[root@lb-01 sbin]# ./nginx [root@lb-01 sbin]# systemctl start keepalived.service
130查看虛擬ip
131查看虛擬ip
客戶端測試訪問虛擬VIP
訪問虛擬VIP:192.168.96.138結果如下
訪問虛擬VIP:192.168.96.139結果如下
模擬故障
把130服務停止
查看130虛擬VIP是否存在結果如下
沒了
測試訪問虛擬VIP**
從上面結果可以看到,即使130機器發生了故障也不影響使用,這樣也利用131資源了
此時查看一下131虛擬ip結果情況如下
註:此時虛擬VIP地址都已經在lb-02機器上了.
nginx+keepalived高可用及雙主模式