【Nginx】如何實現Nginx的高可用負載均衡?看完我也會了!!
阿新 • • 發佈:2020-07-26
## 寫在前面
> 不得不說,最近小夥伴們的學習熱情是越來越高,不斷向冰河提出新的想學習的技術。這不,又有小夥伴問我:冰河,你在【Nginx專題】寫的文章基本上都是Nginx單機版的,能不能寫一篇關於Nginx的高可用的文章呢?我:沒問題,安排上!這不,就有了這篇文章!!
## Keepalived 簡要介紹
Keepalived 是一種高效能的伺服器高可用或熱備解決方案, Keepalived 可以用來防止伺服器單點故障的發生,通過配合 Nginx 可以實現 web 前端服務的高可用。
Keepalived 以 VRRP 協議為實現基礎,用 VRRP 協議來實現高可用性(HA)。 VRRP(Virtual RouterRedundancy Protocol)協議是用於實現路由器冗餘的協議, VRRP 協議將兩臺或多臺路由器裝置虛擬成一個裝置,對外提供虛擬路由器 IP(一個或多個),而在路由器組內部,如果實際擁有這個對外 IP 的路由器如果工作正常的話就是 MASTER,或者是通過演算法選舉產生, MASTER 實現針對虛擬路由器 IP 的各種網路功能,如 ARP 請求, ICMP,以及資料的轉發等;其他裝置不擁有該虛擬 IP,狀態是 BACKUP,除了接收 MASTER 的VRRP 狀態通告資訊外,不執行對外的網路功能。
當主機失效時, BACKUP 將接管原先 MASTER 的網路功能。VRRP 協議使用多播資料來傳輸 VRRP 資料, VRRP 資料使用特殊的虛擬源 MAC 地址傳送資料而不是自身網絡卡的 MAC 地址, VRRP 執行時只有 MASTER 路由器定時傳送 VRRP 通告資訊,表示 MASTER 工作正常以及虛擬路由器 IP(組), BACKUP 只接收 VRRP 資料,不傳送資料,如果一定時間內沒有接收到 MASTER 的通告資訊,各 BACKUP 將宣告自己成為 MASTER,傳送通告資訊,重新進行 MASTER 選舉狀態。
## 方案規劃
![](https://img-blog.csdnimg.cn/20200725211553575.png#pic_center)
**作業系統與安裝軟體如下:**
* CentOS 6.8 x64
* keepalived-1.2.18.tar.gz
* nginx-1.19.1.tar.gz
## 安裝Nginx
### 1.安裝依賴環境
```bash
yum -y install wget gcc-c++ ncurses ncurses-devel cmake make perl bison openssl openssl-devel gcc* libxml2 libxml2-devel curl-devel libjpeg* libpng* freetype* autoconf automake zlib* fiex* libxml* libmcrypt* libtool-ltdl-devel* libaio libaio-devel bzr libtool
```
### 2.安裝openssl
```bash
wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz
tar -zxvf openssl-1.0.2s.tar.gz
cd /usr/local/src/openssl-1.0.2s
./config --prefix=/usr/local/openssl-1.0.2s
make
make install
```
### 3.安裝pcre
```bash
wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
tar -zxvf pcre-8.43.tar.gz
cd /usr/local/src/pcre-8.43
./configure --prefix=/usr/local/pcre-8.43
make
make install
```
### 4.安裝zlib
```bash
wget https://sourceforge.net/projects/libpng/files/zlib/1.2.11/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd /usr/local/src/zlib-1.2.11
./configure --prefix=/usr/local/zlib-1.2.11
make
make
```
### 5.下載nginx-rtmp-module
nginx-rtmp-module的官方github地址:https://github.com/arut/nginx-rtmp-module
使用命令:
```bash
git clone https://github.com/arut/nginx-rtmp-module.git
```
### 6.安裝Nginx
```bash
wget http://nginx.org/download/nginx-1.19.1.tar.gz
tar -zxvf nginx-1.19.1.tar.gz
cd /usr/local/src/nginx-1.19.1
./configure --prefix=/usr/local/nginx-1.19.1 --with-openssl=/usr/local/src/openssl-1.0.2s --with-pcre=/usr/local/src/pcre-8.43 --with-zlib=/usr/local/src/zlib-1.2.11 --add-module=/usr/local/src/nginx-rtmp-module --with-http_ssl_module
make
make install
```
**這裡需要注意的是:安裝Nginx時,指定的是openssl、pcre和zlib的原始碼解壓目錄,安裝完成後Nginx配置檔案的完整路徑為:/usr/local/nginx-1.19.1/conf/nginx.conf。**
## 配置Nginx
在命令列輸入如下命令編輯Nginx的nginx.conf檔案,如下所示。
```bash
# vim /usr/local/nginx-1.19.1/conf/nginx.conf
```
編輯後的檔案內容如下所示。
```bash
user root;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 88;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
```
修改 Nginx 歡迎首頁內容(用於後面測試, 用於區分兩個節點的 Nginx):
在binghe133伺服器上執行如下操作。
```bash
# vim /usr/local/nginx-1.19.1/html/index.html
```
在檔案title節點下新增如下程式碼。
```html