配置nginx   keepalived雙主模式(雙機互為主備)

前言

此前已經寫過一篇使用keepalived實現nginx的高可用,這種方式有一臺機器一直作backup使用,有50%的資源被浪費。

下面來配置nginx keepalived的雙主機雙機熱備,這種配置下有兩個Virtual IP,兩個機器互為主備,最後我們把域名DNS伺服器解析至兩個Virtual IP即可。


環境介紹

兩臺伺服器

Server1:192.168.30.61
Server2:192.168.30.62

要設定的兩個虛擬IP

Virtual IP1:192.168.30.51
Virtual IP2:192.168.30.52

將DNS域名解析至兩個虛擬IP: 192.168.30.51、192.168.30.52


網路撲拓圖

這裡寫圖片描述


配置第一臺伺服器


第一臺伺服器ip為192.168.30.61

vi /etc/keepalived/keepalived.conf

配置內容如下:

global_defs
{
notification_email
{
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
stmp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script Monitor_Nginx {
script "/usr/local/keepalived/scripts/monitor_nginx.sh"
interval 2
weight 2
}
# 虛擬IP1, 本機作為Master
vrrp_instance VI_1 {
state MASTER
interface enp0s3
virtual_router_id 51
priority 100
advert_int 1
track_interface {
enp0s3
}
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.30.51
}i
track_script {
Monitor_Nginx
}
}
# 虛擬IP2, 本機作為Backup
vrrp_instance VI_2 {
state BACKUP
interface enp0s3
virtual_router_id 52
priority 100
advert_int 1
track_interface {
enp0s3
}
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.30.52
}
track_script {
Monitor_Nginx
}
}

配置第二臺伺服器


第二臺伺服器ip為192.168.30.62

vi /etc/keepalived/keepalived.conf

配置內容如下:

global_defs
{
notification_email
{
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
stmp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script Monitor_Nginx {
script "/usr/local/keepalived/scripts/monitor_nginx.sh"
interval 2
weight 2
}
# 虛擬IP1, 本機作為BACKUP
vrrp_instance VI_1 {
state BACKUP
interface enp0s3
virtual_router_id 51
priority 100
advert_int 1
track_interface {
enp0s3
}
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.30.51
}
track_script {
Monitor_Nginx
}
}
# 虛擬IP2, 本機作為Master
vrrp_instance VI_2 {
state MASTER
interface enp0s3
virtual_router_id 52
priority 100
advert_int 1
track_interface {
enp0s3
}
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.30.52
}
track_script {
Monitor_Nginx
}
}

監控指令碼


和之前的監控指令碼一樣

# 監控nginx程序,若nginx主程序不存在則啟動nginx
# 若5s後nginx程序還是不存在的話kill掉keepalived程序,防止nginx沒執行該主機的keepalived還接管虛擬IP
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
/usr/local/nginx/sbin/nginx
sleep 5
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
killall keepalived
fi
fi

測試


修改nginx預設訪問頁nginx的預設頁面

# nginx 預設訪問頁面
$NGINX_HOME/html/index.html

修改server1的頁,加入IP: 192.168.30.61

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx! 192.168.30.61 </h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

server2同理


兩個機器都啟動keepalived和nginx

訪問虛擬ip1

在瀏覽器輸入192.168.30.51進入訪問頁面
這裡寫圖片描述

訪問虛擬ip2

在瀏覽器輸入192.168.30.52進入訪問頁面
這裡寫圖片描述

證明兩個VIP都起效果了


Kill掉server2的keepalived

pkill keepalived
  • 訪問ip2

這裡寫圖片描述

證明了server1為server2的備機,server2 down掉後server1接管server1的nginx訪問


Kill掉server1機器的keepalived,啟動server2機器的keepalived

  • 訪問ip1

這裡寫圖片描述

證明了server2為server1的備機,server1 down掉後server1接管server1的nginx訪問


有關nginx和keepalived的安裝可參考