負載均衡會話保持及健康檢查
阿新 • • 發佈:2020-09-01
一、回顧
1.負載均衡的作用
1.把請求平均的分配到後端伺服器上
2.後端伺服器的高可用
2.負載均衡的軟體和型別
1.nginx 支援四層負載均衡和七層負載均衡
2.LVS 只支援四層負載均衡
3.haproxy 支援四層負載均衡和七層負載均衡
3.負載均衡語法
upstream backend { server backend1.example.com weight=5; server backend2.example.com:8080; server backup1.example.com:8080 backup; server backup2.example.com:8080 backup; } server { listen 80; server_name www.test.com; location / { proxy_pass http://backend; } }
4.負載均衡排程演算法
排程演算法 | 說明 |
---|---|
輪詢 | 逐一的將請求平均的分配到後端 |
加權輪詢 | 根據設定的權重值,將請求按比例分配至後端伺服器 |
ip_hash | 根據訪問來源的IP將請求始終分配到一臺機器 |
url_hash | 根據訪問的url,將請求始終分配到一臺機器 |
least_conn | 哪一臺機器接收的請求少,就將新的請求分配到哪一臺機器 |
二、負載均衡後端狀態
狀態 | 概述 |
---|---|
down | 當前的server暫時不參與負載均衡 |
backup | 預留的備份伺服器 |
max_fails | 允許請求失敗的次數 |
fail_timeout | 經過max_fails失敗後, 服務暫停時間 |
max_conns | 限制最大的接收連線數 |
1.down狀態配置測試
[root@lb01 ~]# vim /etc/nginx/conf.d/proxy.conf
upstream web {
server 172.16.1.7:80 down;
server 172.16.1.9:80;
}
#一般在程式碼上線或維護伺服器時使用該狀態
2.backup狀態測試
[root@lb01 ~]# vim /etc/nginx/conf.d/proxy.conf upstream web { server 172.16.1.7:80; server 172.16.1.9:80; server 172.16.1.10:80 backup; server 172.16.1.11:80 backup; }
3.max_fails配置
[root@lb01 ~]# vim /etc/nginx/conf.d/proxy.conf
upstream web {
server 172.16.1.7:80 max_fails=3 fail_timeout=10s;
server 172.16.1.9:80;
}
4.測試max_conns最大TCP連線數
[root@lb01 ~]# vim /etc/nginx/conf.d/proxy.conf
upstream web {
server 172.16.1.7:80 max_conns=10;
server 172.16.1.9:80;
}
三、負載均衡結合專案
1.配置blog的負載均衡
[root@lb01 ~]# vim /etc/nginx/conf.d/blog.conf
upstream blog {
server 172.16.1.7:80;
server 172.16.1.9:80;
}
server {
listen 80;
server_name linux.blog.com;
location / {
proxy_pass http://blog;
include /etc/nginx/proxy_params;
}
}
[root@lb01 ~]# systemctl restart nginx
#配置hosts,訪問測試
2.配置wecenter的負載均衡
[root@lb01 ~]# vim /etc/nginx/conf.d/zh.conf
upstream zh {
server 172.16.1.7:80;
server 172.16.1.9:80;
}
server {
listen 80;
server_name linux.zh.com;
location / {
proxy_pass http://zh;
include /etc/nginx/proxy_params;
}
}
3.負載均衡常見錯誤
1)錯誤
如果後端伺服器返回報錯,負載均衡仍然會將請求分配到出錯的web伺服器,因為負載均衡只會根據排程演算法將請求分配到後端,不會進行判斷後端是否正常
2)解決錯誤的模組語法
Syntax: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | non_idempotent | off ...;
Default: proxy_next_upstream error timeout;
Context: http, server, location
3)配置方法
[root@lb01 ~]# vim /etc/nginx/conf.d/zh.conf
upstream zh {
server 172.16.1.7:80;
server 172.16.1.9:80;
}
server {
listen 80;
server_name linux.zh.com;
location / {
proxy_pass http://zh;
include /etc/nginx/proxy_params;
proxy_next_upstream http_502 error timeout;
}
}
四、Nginx負載均衡健康檢查
在Nginx官方模組提供的模組中,沒有對負載均衡後端節點的健康檢查模組,但可以使用第三方模組。
`nginx_upstream_check_module`來檢測後端服務的健康狀態。
1.安裝依賴包
[root@lb02 ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
2.下載nginx原始碼包以及nginx_upstream_check模組第三方模組
[root@lb02 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@lb02 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
3.解壓nginx原始碼包以及第三方模組
[root@lb02 ~]# tar xf nginx-1.14.2.tar.gz
[root@lb02 ~]# unzip master.zip
4.進入nginx目錄,打補丁(nginx的版本是1.14補丁就選擇1.14的,p1代表在nginx目錄,p0是不在nginx目錄)
[root@lb02 ~]# cd nginx-1.14.2/
[root@lb02 nginx-1.14.2]# patch -p1 <../nginx_upstream_check_module-master/check_1.14.0+.patch
[root@lb02 nginx-1.14.2]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/nginx_upstream_check_module-master --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
[root@lb02 nginx-1.14.2]# make && make install
5.在已有的負載均衡上增加健康檢查的功能
[root@lb02 /etc/nginx]# vim /etc/nginx/nginx.conf
http {
include conf.d/*.conf;
... ...
}
[root@lb02 /etc/nginx]# mkdir /etc/nginx/conf.d
[root@lb01 conf.d]# cat proxy_web.conf
upstream web {
server 172.16.1.7:80 max_fails=2 fail_timeout=10s;
server 172.16.1.8:80 max_fails=2 fail_timeout=10s;
check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
#interval 檢測間隔時間,單位為毫秒
#rise 表示請求2次正常,標記此後端的狀態為up
#fall 表示請求3次失敗,標記此後端的狀態為down
#type 型別為tcp
#timeout 超時時間,單位為毫秒
}
server {
listen 80;
server_name linux.web.com;
location / {
proxy_pass http://web;
include proxy_params;
}
location /upstream_check {
check_status;
}
}
#編輯優化檔案
[root@lb02 /etc/nginx]# vim /etc/nginx/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 8 128k;
6.建立使用者和目錄
[root@lb02 /etc/nginx]# groupadd nginx -g 666
[root@lb02 /etc/nginx]# useradd nginx -u 666 -g 666
[root@lb02 /etc/nginx]# mkdir /var/cache/nginx/
7.啟動並訪問
[root@lb02 /etc/nginx]# /usr/sbin/nginx
#配置hosts
10.0.0.5 linux.web.com
五、Nginx負載均衡會話保持
在使用負載均衡的時候會遇到會話保持的問題,可通過如下方式進行解決。
1.使用nginx的ip_hash,根據客戶端的IP,將請求分配到對應的IP上
2.基於服務端的session會話共享(file+NFS,MySQL,redis)
1.session共享的方法
1.把多臺機器的session檔案掛載到NFS
2.通過程式將session儲存到MySQL資料庫
3.通過程式將session儲存到redis
2.搭建第一臺phpmyadmin
1)上傳包
[root@web01 ~]# cd /code/
[root@web01 /code]# rz phpMyAdmin-4.9.0.1-all-languages.zip
2)解壓
[root@web01 /code]# unzip phpMyAdmin-4.9.0.1-all-languages.zip
[root@web01 /code]# mv phpMyAdmin-4.9.0.1-all-languages php
3)配置程式碼
[root@web01 /code]# cp php/config.sample.inc.php php/config.inc.php
[root@web01 /code]# vim php/config.inc.php
$cfg['Servers'][$i]['host'] = '172.16.1.51';
4)配置nginx
[root@web01 /code]# vim /etc/nginx/conf.d/linux.php.com.conf
server {
listen 80;
server_name linux.php.com;
root /code/php;
location / {
index index.php;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
5)重啟訪問
[root@web01 /code]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /code]# systemctl restart nginx
#配置hosts
10.0.0.7 linux.php.com
6)訪問頁面錯誤
#報錯
session_start(): open(SESSION_FILE, O_RDWR) failed: Permission denied (13)
session_start(): Failed to read session data: files (path: /var/lib/php/session)
#解決:
[root@web01 /code]# chown -R www.www /var/lib/php/session
7)再次訪問測試
#如果忘記資料庫遠端連線密碼
[root@db01 ~]# mysql -uroot -pLinhd@123
MariaDB [(none)]> grant all on *.* to root@'172.16.1.%' identified by '123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select user,host,password from mysql.user;
3.同步配置到第二臺伺服器
1)推送配置和站點
#推送站點目錄
[root@web01 /code]# scp -r /code/php 172.16.1.9:/code/
#推送nginx配置
[root@web01 /code]# scp /etc/nginx/conf.d/linux.php.com.conf 172.16.1.9:/etc/nginx/conf.d/
2)重啟訪問
[root@web03 ~]# systemctl restart nginx
#配置hosts
10.0.0.9 linux.php.com
3)授權目錄
#報錯
session_start(): open(SESSION_FILE, O_RDWR) failed: Permission denied (13)
session_start(): Failed to read session data: files (path: /var/lib/php/session)
#解決:
[root@web03 /code]# chown -R www.www /var/lib/php/session
4.配置負載均衡
1)配置
[root@lb01 ~]# cp /etc/nginx/conf.d/blog.conf /etc/nginx/conf.d/php.conf
[root@lb01 ~]# vim /etc/nginx/conf.d/php.conf
upstream php {
server 172.16.1.7:80;
server 172.16.1.9:80;
}
server {
listen 80;
server_name linux.php.com;
location / {
proxy_pass http://php;
include /etc/nginx/proxy_params;
}
}
2)重啟並訪問
[root@lb01 ~]# systemctl restart nginx
#配置hosts
10.0.0.4 linux.php.com
5.使用redis實現session共享
1)安裝redis
[root@db01 ~]# yum install -y redis
2)配置redis
[root@db01 ~]# vim /etc/redis.conf
bind 127.0.0.1 172.16.1.51
3)啟動redis
[root@db01 ~]# systemctl start redis
#檢查啟動
[root@db01 ~]# netstat -lntp
tcp 0 0 172.16.1.51:6379 0.0.0.0:* LISTEN 29104/redis-server
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 29104/redis-server
4)配置PHP服務將session儲存到redis
[root@web01 /code]# vim /etc/php.ini
#原配置 session.save_handler = files
session.save_handler = redis
session.save_path = "tcp://172.16.1.51:6379"
[root@web01 /code]# vim /etc/php-fpm.d/www.conf
#最下面幾行註釋
;php_value[session.save_handler] = files
;php_value[session.save_path] = /var/lib/php/session
5)重啟php
[root@web01 /code]# systemctl restart php-fpm
[root@web03 /code]# systemctl restart php-fpm
6)訪問測試
7)redis檢視seesion
[root@db01 ~]# redis-cli
127.0.0.1:6379> keys *
1) "PHPREDIS_SESSION:8b8721df0b5736149ea0c716f05773e9"
2) "PHPREDIS_SESSION:b59336d7a1a053c6d26c2550032c1609
127.0.0.1:6379> TTL PHPREDIS_SESSION:b59336d7a1a053c6d26c2550032c1609
(integer) 1199