'Nginx+Keepalived實現web服務器高可用'

Nginx 軟件 OpenSSL Apache 中央處理器 黑馬程序員 2019-08-21
"

1、Nginx

業務背景

現公司需求快速搭建web服務器,對外提供給用戶web服務。

需求拆分

需要基於http協議的軟件,搭建服務實現

介紹

"

1、Nginx

業務背景

現公司需求快速搭建web服務器,對外提供給用戶web服務。

需求拆分

需要基於http協議的軟件,搭建服務實現

介紹

Nginx+Keepalived實現web服務器高可用

常見用法:

1) web服務器軟件 httpd http協議

同類的web服務器軟件:apache(老牌) nginx(俄羅斯) IIS(微軟)

2)代理服務器 反向代理

3)郵箱代理服務器 IMAP POP3 SMTP

4)負載均衡功能 LB loadblance

Nginx架構的特點:

  • ①高可靠:穩定性 master進程 管理調度請求分發到哪一個worker=> worker進程 響應請求 一master多worker②熱部署 :(1)平滑升級 (2)可以快速重載配置③高併發:可以同時響應更多的請求 事件 epoll模型 幾萬④響應快:尤其在處理靜態文件上,響應速度很快 sendfile⑤低消耗:cpu和內存 1w個請求 內存2-3MB⑥分佈式支持 :反向代理 七層負載均衡

官方網址:http://nginx.org/

1.2、安裝

常見安裝方式:

  • ①yum安裝配置,需使用Nginx官方源或者EPEL源②源碼編譯
 #添加運行用戶
shell > useradd -s/sbin/nologin -M www
#安裝依賴
shell > yum -y install pcre-devel zlib-devel openssl-devel
#編譯安裝
shell > cd /root/soft
shell > tar xvf nginx-1.14.2.tar.gz
shell > cd nginx-1.14.2
shell > ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module && make && make install

編譯參數說明

"

1、Nginx

業務背景

現公司需求快速搭建web服務器,對外提供給用戶web服務。

需求拆分

需要基於http協議的軟件,搭建服務實現

介紹

Nginx+Keepalived實現web服務器高可用

常見用法:

1) web服務器軟件 httpd http協議

同類的web服務器軟件:apache(老牌) nginx(俄羅斯) IIS(微軟)

2)代理服務器 反向代理

3)郵箱代理服務器 IMAP POP3 SMTP

4)負載均衡功能 LB loadblance

Nginx架構的特點:

  • ①高可靠:穩定性 master進程 管理調度請求分發到哪一個worker=> worker進程 響應請求 一master多worker②熱部署 :(1)平滑升級 (2)可以快速重載配置③高併發:可以同時響應更多的請求 事件 epoll模型 幾萬④響應快:尤其在處理靜態文件上,響應速度很快 sendfile⑤低消耗:cpu和內存 1w個請求 內存2-3MB⑥分佈式支持 :反向代理 七層負載均衡

官方網址:http://nginx.org/

1.2、安裝

常見安裝方式:

  • ①yum安裝配置,需使用Nginx官方源或者EPEL源②源碼編譯
 #添加運行用戶
shell > useradd -s/sbin/nologin -M www
#安裝依賴
shell > yum -y install pcre-devel zlib-devel openssl-devel
#編譯安裝
shell > cd /root/soft
shell > tar xvf nginx-1.14.2.tar.gz
shell > cd nginx-1.14.2
shell > ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module && make && make install

編譯參數說明

Nginx+Keepalived實現web服務器高可用

1.3、目錄介紹

查看安裝目錄/usr/local/nginx

"

1、Nginx

業務背景

現公司需求快速搭建web服務器,對外提供給用戶web服務。

需求拆分

需要基於http協議的軟件,搭建服務實現

介紹

Nginx+Keepalived實現web服務器高可用

常見用法:

1) web服務器軟件 httpd http協議

同類的web服務器軟件:apache(老牌) nginx(俄羅斯) IIS(微軟)

2)代理服務器 反向代理

3)郵箱代理服務器 IMAP POP3 SMTP

4)負載均衡功能 LB loadblance

Nginx架構的特點:

  • ①高可靠:穩定性 master進程 管理調度請求分發到哪一個worker=> worker進程 響應請求 一master多worker②熱部署 :(1)平滑升級 (2)可以快速重載配置③高併發:可以同時響應更多的請求 事件 epoll模型 幾萬④響應快:尤其在處理靜態文件上,響應速度很快 sendfile⑤低消耗:cpu和內存 1w個請求 內存2-3MB⑥分佈式支持 :反向代理 七層負載均衡

官方網址:http://nginx.org/

1.2、安裝

常見安裝方式:

  • ①yum安裝配置,需使用Nginx官方源或者EPEL源②源碼編譯
 #添加運行用戶
shell > useradd -s/sbin/nologin -M www
#安裝依賴
shell > yum -y install pcre-devel zlib-devel openssl-devel
#編譯安裝
shell > cd /root/soft
shell > tar xvf nginx-1.14.2.tar.gz
shell > cd nginx-1.14.2
shell > ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module && make && make install

編譯參數說明

Nginx+Keepalived實現web服務器高可用

1.3、目錄介紹

查看安裝目錄/usr/local/nginx

Nginx+Keepalived實現web服務器高可用

1.4、軟件操作參數

查看nginx的二進制可執行文件的相關參數

 shell > cd /usr/local/nginx/sbin
shell > ./nginx -h

執行後顯示

 nginx version: nginx/1.14.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
#查看幫助
-?,-h : this help
#查看版本並退出
-v : show version and exit
#查看版本和配置選項並退出
-V : show version and configure options then exit
#檢測配置文件語法並退出
-t : test configuration and exit
#檢測配置文件語法打印它並退出
-T : test configuration, dump it and exit
#在配置測試期間禁止顯示非錯誤信息
-q : suppress non-error messages during configuration testing
#發送信號給主進程 stop強制退出 quit優雅的退出 reopen重開日誌 reload重載配置
-s signal : send signal to a master process: stop, quit, reopen, reload
#設置nginx目錄 $prefix路徑
-p prefix : set prefix path (default: /usr/local/nginx/)
#指定啟動使用的配置文件
-c filename : set configuration file (default: conf/nginx.conf)
#在配置文件之外設置全局指令
-g directives : set global directives out of configuration file

一般主要使用:

  • -s參數控制管理nginx服務-V參數查看nginx開啟的模塊和編譯參數-t參數檢測配置文件是否有錯誤

2、Keepalived實現高可用

業務背景

單例web服務器能夠滿足業務基本需求,提供web服務。但是,存在單點故障的問題,即當服務器宕機後,用戶將無法獲取到服務響應。

為了能夠提高用戶體驗度,能夠持續得給用戶提供優質的服務,當web服務器不可用時,可以有備用服務器接替web服務器的工作,繼續為用戶提供響應。其中,還要解決一個問題,需要備用服務器能夠快速自動切換過來。

一般將以上業務需求,稱為實現服務的高可用HA。

需求拆分

也就是高可用的實現核心:

①冗餘服務器(備份服務器)

②自動切換 可以通過綁定虛擬IP的方式 用戶通過VIP訪問服務

"

1、Nginx

業務背景

現公司需求快速搭建web服務器,對外提供給用戶web服務。

需求拆分

需要基於http協議的軟件,搭建服務實現

介紹

Nginx+Keepalived實現web服務器高可用

常見用法:

1) web服務器軟件 httpd http協議

同類的web服務器軟件:apache(老牌) nginx(俄羅斯) IIS(微軟)

2)代理服務器 反向代理

3)郵箱代理服務器 IMAP POP3 SMTP

4)負載均衡功能 LB loadblance

Nginx架構的特點:

  • ①高可靠:穩定性 master進程 管理調度請求分發到哪一個worker=> worker進程 響應請求 一master多worker②熱部署 :(1)平滑升級 (2)可以快速重載配置③高併發:可以同時響應更多的請求 事件 epoll模型 幾萬④響應快:尤其在處理靜態文件上,響應速度很快 sendfile⑤低消耗:cpu和內存 1w個請求 內存2-3MB⑥分佈式支持 :反向代理 七層負載均衡

官方網址:http://nginx.org/

1.2、安裝

常見安裝方式:

  • ①yum安裝配置,需使用Nginx官方源或者EPEL源②源碼編譯
 #添加運行用戶
shell > useradd -s/sbin/nologin -M www
#安裝依賴
shell > yum -y install pcre-devel zlib-devel openssl-devel
#編譯安裝
shell > cd /root/soft
shell > tar xvf nginx-1.14.2.tar.gz
shell > cd nginx-1.14.2
shell > ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module && make && make install

編譯參數說明

Nginx+Keepalived實現web服務器高可用

1.3、目錄介紹

查看安裝目錄/usr/local/nginx

Nginx+Keepalived實現web服務器高可用

1.4、軟件操作參數

查看nginx的二進制可執行文件的相關參數

 shell > cd /usr/local/nginx/sbin
shell > ./nginx -h

執行後顯示

 nginx version: nginx/1.14.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
#查看幫助
-?,-h : this help
#查看版本並退出
-v : show version and exit
#查看版本和配置選項並退出
-V : show version and configure options then exit
#檢測配置文件語法並退出
-t : test configuration and exit
#檢測配置文件語法打印它並退出
-T : test configuration, dump it and exit
#在配置測試期間禁止顯示非錯誤信息
-q : suppress non-error messages during configuration testing
#發送信號給主進程 stop強制退出 quit優雅的退出 reopen重開日誌 reload重載配置
-s signal : send signal to a master process: stop, quit, reopen, reload
#設置nginx目錄 $prefix路徑
-p prefix : set prefix path (default: /usr/local/nginx/)
#指定啟動使用的配置文件
-c filename : set configuration file (default: conf/nginx.conf)
#在配置文件之外設置全局指令
-g directives : set global directives out of configuration file

一般主要使用:

  • -s參數控制管理nginx服務-V參數查看nginx開啟的模塊和編譯參數-t參數檢測配置文件是否有錯誤

2、Keepalived實現高可用

業務背景

單例web服務器能夠滿足業務基本需求,提供web服務。但是,存在單點故障的問題,即當服務器宕機後,用戶將無法獲取到服務響應。

為了能夠提高用戶體驗度,能夠持續得給用戶提供優質的服務,當web服務器不可用時,可以有備用服務器接替web服務器的工作,繼續為用戶提供響應。其中,還要解決一個問題,需要備用服務器能夠快速自動切換過來。

一般將以上業務需求,稱為實現服務的高可用HA。

需求拆分

也就是高可用的實現核心:

①冗餘服務器(備份服務器)

②自動切換 可以通過綁定虛擬IP的方式 用戶通過VIP訪問服務

Nginx+Keepalived實現web服務器高可用

2.1、介紹

Keepalived軟件起初是專為LVS負載均衡軟件設計的,用來管理並監控LVS集群系統中各個服務節點的狀態,後來又加入了可以實現高可用的VRRP功能。因此,Keepalived除了能夠管理LVS軟件外,還可以作為其他服務(例如:Nginx、Haproxy、MySQL等)的高可用解決方案軟件。

"

1、Nginx

業務背景

現公司需求快速搭建web服務器,對外提供給用戶web服務。

需求拆分

需要基於http協議的軟件,搭建服務實現

介紹

Nginx+Keepalived實現web服務器高可用

常見用法:

1) web服務器軟件 httpd http協議

同類的web服務器軟件:apache(老牌) nginx(俄羅斯) IIS(微軟)

2)代理服務器 反向代理

3)郵箱代理服務器 IMAP POP3 SMTP

4)負載均衡功能 LB loadblance

Nginx架構的特點:

  • ①高可靠:穩定性 master進程 管理調度請求分發到哪一個worker=> worker進程 響應請求 一master多worker②熱部署 :(1)平滑升級 (2)可以快速重載配置③高併發:可以同時響應更多的請求 事件 epoll模型 幾萬④響應快:尤其在處理靜態文件上,響應速度很快 sendfile⑤低消耗:cpu和內存 1w個請求 內存2-3MB⑥分佈式支持 :反向代理 七層負載均衡

官方網址:http://nginx.org/

1.2、安裝

常見安裝方式:

  • ①yum安裝配置,需使用Nginx官方源或者EPEL源②源碼編譯
 #添加運行用戶
shell > useradd -s/sbin/nologin -M www
#安裝依賴
shell > yum -y install pcre-devel zlib-devel openssl-devel
#編譯安裝
shell > cd /root/soft
shell > tar xvf nginx-1.14.2.tar.gz
shell > cd nginx-1.14.2
shell > ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module && make && make install

編譯參數說明

Nginx+Keepalived實現web服務器高可用

1.3、目錄介紹

查看安裝目錄/usr/local/nginx

Nginx+Keepalived實現web服務器高可用

1.4、軟件操作參數

查看nginx的二進制可執行文件的相關參數

 shell > cd /usr/local/nginx/sbin
shell > ./nginx -h

執行後顯示

 nginx version: nginx/1.14.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
#查看幫助
-?,-h : this help
#查看版本並退出
-v : show version and exit
#查看版本和配置選項並退出
-V : show version and configure options then exit
#檢測配置文件語法並退出
-t : test configuration and exit
#檢測配置文件語法打印它並退出
-T : test configuration, dump it and exit
#在配置測試期間禁止顯示非錯誤信息
-q : suppress non-error messages during configuration testing
#發送信號給主進程 stop強制退出 quit優雅的退出 reopen重開日誌 reload重載配置
-s signal : send signal to a master process: stop, quit, reopen, reload
#設置nginx目錄 $prefix路徑
-p prefix : set prefix path (default: /usr/local/nginx/)
#指定啟動使用的配置文件
-c filename : set configuration file (default: conf/nginx.conf)
#在配置文件之外設置全局指令
-g directives : set global directives out of configuration file

一般主要使用:

  • -s參數控制管理nginx服務-V參數查看nginx開啟的模塊和編譯參數-t參數檢測配置文件是否有錯誤

2、Keepalived實現高可用

業務背景

單例web服務器能夠滿足業務基本需求,提供web服務。但是,存在單點故障的問題,即當服務器宕機後,用戶將無法獲取到服務響應。

為了能夠提高用戶體驗度,能夠持續得給用戶提供優質的服務,當web服務器不可用時,可以有備用服務器接替web服務器的工作,繼續為用戶提供響應。其中,還要解決一個問題,需要備用服務器能夠快速自動切換過來。

一般將以上業務需求,稱為實現服務的高可用HA。

需求拆分

也就是高可用的實現核心:

①冗餘服務器(備份服務器)

②自動切換 可以通過綁定虛擬IP的方式 用戶通過VIP訪問服務

Nginx+Keepalived實現web服務器高可用

2.1、介紹

Keepalived軟件起初是專為LVS負載均衡軟件設計的,用來管理並監控LVS集群系統中各個服務節點的狀態,後來又加入了可以實現高可用的VRRP功能。因此,Keepalived除了能夠管理LVS軟件外,還可以作為其他服務(例如:Nginx、Haproxy、MySQL等)的高可用解決方案軟件。

Nginx+Keepalived實現web服務器高可用

keepalived主要使用三個模塊,分別是core、check和vrrp。

core模塊為keepalived的核心,負責主進程的啟動、維護以及全局配置文件的加載和解析。

check負責健康檢查,包括常見的各種檢查方式。

vrrp模塊是來實現VRRP協議的。

2.2、安裝

master和backup都需要進行安裝,也就是server01和server03機器

 #安裝keepalived
shell > yum -y install keepalived
"

1、Nginx

業務背景

現公司需求快速搭建web服務器,對外提供給用戶web服務。

需求拆分

需要基於http協議的軟件,搭建服務實現

介紹

Nginx+Keepalived實現web服務器高可用

常見用法:

1) web服務器軟件 httpd http協議

同類的web服務器軟件:apache(老牌) nginx(俄羅斯) IIS(微軟)

2)代理服務器 反向代理

3)郵箱代理服務器 IMAP POP3 SMTP

4)負載均衡功能 LB loadblance

Nginx架構的特點:

  • ①高可靠:穩定性 master進程 管理調度請求分發到哪一個worker=> worker進程 響應請求 一master多worker②熱部署 :(1)平滑升級 (2)可以快速重載配置③高併發:可以同時響應更多的請求 事件 epoll模型 幾萬④響應快:尤其在處理靜態文件上,響應速度很快 sendfile⑤低消耗:cpu和內存 1w個請求 內存2-3MB⑥分佈式支持 :反向代理 七層負載均衡

官方網址:http://nginx.org/

1.2、安裝

常見安裝方式:

  • ①yum安裝配置,需使用Nginx官方源或者EPEL源②源碼編譯
 #添加運行用戶
shell > useradd -s/sbin/nologin -M www
#安裝依賴
shell > yum -y install pcre-devel zlib-devel openssl-devel
#編譯安裝
shell > cd /root/soft
shell > tar xvf nginx-1.14.2.tar.gz
shell > cd nginx-1.14.2
shell > ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module && make && make install

編譯參數說明

Nginx+Keepalived實現web服務器高可用

1.3、目錄介紹

查看安裝目錄/usr/local/nginx

Nginx+Keepalived實現web服務器高可用

1.4、軟件操作參數

查看nginx的二進制可執行文件的相關參數

 shell > cd /usr/local/nginx/sbin
shell > ./nginx -h

執行後顯示

 nginx version: nginx/1.14.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
#查看幫助
-?,-h : this help
#查看版本並退出
-v : show version and exit
#查看版本和配置選項並退出
-V : show version and configure options then exit
#檢測配置文件語法並退出
-t : test configuration and exit
#檢測配置文件語法打印它並退出
-T : test configuration, dump it and exit
#在配置測試期間禁止顯示非錯誤信息
-q : suppress non-error messages during configuration testing
#發送信號給主進程 stop強制退出 quit優雅的退出 reopen重開日誌 reload重載配置
-s signal : send signal to a master process: stop, quit, reopen, reload
#設置nginx目錄 $prefix路徑
-p prefix : set prefix path (default: /usr/local/nginx/)
#指定啟動使用的配置文件
-c filename : set configuration file (default: conf/nginx.conf)
#在配置文件之外設置全局指令
-g directives : set global directives out of configuration file

一般主要使用:

  • -s參數控制管理nginx服務-V參數查看nginx開啟的模塊和編譯參數-t參數檢測配置文件是否有錯誤

2、Keepalived實現高可用

業務背景

單例web服務器能夠滿足業務基本需求,提供web服務。但是,存在單點故障的問題,即當服務器宕機後,用戶將無法獲取到服務響應。

為了能夠提高用戶體驗度,能夠持續得給用戶提供優質的服務,當web服務器不可用時,可以有備用服務器接替web服務器的工作,繼續為用戶提供響應。其中,還要解決一個問題,需要備用服務器能夠快速自動切換過來。

一般將以上業務需求,稱為實現服務的高可用HA。

需求拆分

也就是高可用的實現核心:

①冗餘服務器(備份服務器)

②自動切換 可以通過綁定虛擬IP的方式 用戶通過VIP訪問服務

Nginx+Keepalived實現web服務器高可用

2.1、介紹

Keepalived軟件起初是專為LVS負載均衡軟件設計的,用來管理並監控LVS集群系統中各個服務節點的狀態,後來又加入了可以實現高可用的VRRP功能。因此,Keepalived除了能夠管理LVS軟件外,還可以作為其他服務(例如:Nginx、Haproxy、MySQL等)的高可用解決方案軟件。

Nginx+Keepalived實現web服務器高可用

keepalived主要使用三個模塊,分別是core、check和vrrp。

core模塊為keepalived的核心,負責主進程的啟動、維護以及全局配置文件的加載和解析。

check負責健康檢查,包括常見的各種檢查方式。

vrrp模塊是來實現VRRP協議的。

2.2、安裝

master和backup都需要進行安裝,也就是server01和server03機器

 #安裝keepalived
shell > yum -y install keepalived
Nginx+Keepalived實現web服務器高可用

keepalived需要使用的目錄和文件:

"

1、Nginx

業務背景

現公司需求快速搭建web服務器,對外提供給用戶web服務。

需求拆分

需要基於http協議的軟件,搭建服務實現

介紹

Nginx+Keepalived實現web服務器高可用

常見用法:

1) web服務器軟件 httpd http協議

同類的web服務器軟件:apache(老牌) nginx(俄羅斯) IIS(微軟)

2)代理服務器 反向代理

3)郵箱代理服務器 IMAP POP3 SMTP

4)負載均衡功能 LB loadblance

Nginx架構的特點:

  • ①高可靠:穩定性 master進程 管理調度請求分發到哪一個worker=> worker進程 響應請求 一master多worker②熱部署 :(1)平滑升級 (2)可以快速重載配置③高併發:可以同時響應更多的請求 事件 epoll模型 幾萬④響應快:尤其在處理靜態文件上,響應速度很快 sendfile⑤低消耗:cpu和內存 1w個請求 內存2-3MB⑥分佈式支持 :反向代理 七層負載均衡

官方網址:http://nginx.org/

1.2、安裝

常見安裝方式:

  • ①yum安裝配置,需使用Nginx官方源或者EPEL源②源碼編譯
 #添加運行用戶
shell > useradd -s/sbin/nologin -M www
#安裝依賴
shell > yum -y install pcre-devel zlib-devel openssl-devel
#編譯安裝
shell > cd /root/soft
shell > tar xvf nginx-1.14.2.tar.gz
shell > cd nginx-1.14.2
shell > ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module && make && make install

編譯參數說明

Nginx+Keepalived實現web服務器高可用

1.3、目錄介紹

查看安裝目錄/usr/local/nginx

Nginx+Keepalived實現web服務器高可用

1.4、軟件操作參數

查看nginx的二進制可執行文件的相關參數

 shell > cd /usr/local/nginx/sbin
shell > ./nginx -h

執行後顯示

 nginx version: nginx/1.14.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
#查看幫助
-?,-h : this help
#查看版本並退出
-v : show version and exit
#查看版本和配置選項並退出
-V : show version and configure options then exit
#檢測配置文件語法並退出
-t : test configuration and exit
#檢測配置文件語法打印它並退出
-T : test configuration, dump it and exit
#在配置測試期間禁止顯示非錯誤信息
-q : suppress non-error messages during configuration testing
#發送信號給主進程 stop強制退出 quit優雅的退出 reopen重開日誌 reload重載配置
-s signal : send signal to a master process: stop, quit, reopen, reload
#設置nginx目錄 $prefix路徑
-p prefix : set prefix path (default: /usr/local/nginx/)
#指定啟動使用的配置文件
-c filename : set configuration file (default: conf/nginx.conf)
#在配置文件之外設置全局指令
-g directives : set global directives out of configuration file

一般主要使用:

  • -s參數控制管理nginx服務-V參數查看nginx開啟的模塊和編譯參數-t參數檢測配置文件是否有錯誤

2、Keepalived實現高可用

業務背景

單例web服務器能夠滿足業務基本需求,提供web服務。但是,存在單點故障的問題,即當服務器宕機後,用戶將無法獲取到服務響應。

為了能夠提高用戶體驗度,能夠持續得給用戶提供優質的服務,當web服務器不可用時,可以有備用服務器接替web服務器的工作,繼續為用戶提供響應。其中,還要解決一個問題,需要備用服務器能夠快速自動切換過來。

一般將以上業務需求,稱為實現服務的高可用HA。

需求拆分

也就是高可用的實現核心:

①冗餘服務器(備份服務器)

②自動切換 可以通過綁定虛擬IP的方式 用戶通過VIP訪問服務

Nginx+Keepalived實現web服務器高可用

2.1、介紹

Keepalived軟件起初是專為LVS負載均衡軟件設計的,用來管理並監控LVS集群系統中各個服務節點的狀態,後來又加入了可以實現高可用的VRRP功能。因此,Keepalived除了能夠管理LVS軟件外,還可以作為其他服務(例如:Nginx、Haproxy、MySQL等)的高可用解決方案軟件。

Nginx+Keepalived實現web服務器高可用

keepalived主要使用三個模塊,分別是core、check和vrrp。

core模塊為keepalived的核心,負責主進程的啟動、維護以及全局配置文件的加載和解析。

check負責健康檢查,包括常見的各種檢查方式。

vrrp模塊是來實現VRRP協議的。

2.2、安裝

master和backup都需要進行安裝,也就是server01和server03機器

 #安裝keepalived
shell > yum -y install keepalived
Nginx+Keepalived實現web服務器高可用

keepalived需要使用的目錄和文件:

Nginx+Keepalived實現web服務器高可用

2.3、配置

①備份主備服務器的配置文件

 shell > cd /etc/keepalived
shell > cp keepalived.conf keepalived.conf_bak

②分別修改主備服務器配置文件

 shell > vim keepalived.conf

示例配置文件說明

 ! 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協議的配置
vrrp_instance VI_1 {
#工作模式
state MASTER
#監聽的網卡
interface eth0
#虛擬路由id 需要和備服務器一致
virtual_router_id 51
#權重 優先級
priority 100
#vrrp包的發送週期 1s
advert_int 1
#權限驗證
authentication {
auth_type PASS
auth_pass 1111
}
#需要綁定切換的VIP
virtual_ipaddress {
192.168.200.16
192.168.200.17
192.168.200.18
}
}

主服務器

 ! 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_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
#master默認只需要修改使用VIP即可
virtual_ipaddress {
192.168.17.200
}
}

備服務器

 ! 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_instance VI_1 {
#修改工作模式為備
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
#注意修改VIP
virtual_ipaddress {
192.168.17.200
}
}

③分別按照順序啟動主服務器和備服務器的keepalived

 shell > service keepalived start

④查看主備服務器的網卡信息

 #需要通過ip a命令查看 分別在server01和server03查看
shell > ip a

2.4、模擬故障

模擬服務器故障宕機,查看是否可以切換服務到備用機器。

模擬宕機,關閉server01 master服務器,VIP自動切換到server03 backup服務器

 #抓包vrrp
shell > yum -y install tcpdump
shell > tcpdump vrrp -n
"

相關推薦

推薦中...