1. 程式人生 > >lunix 集群,負載均衡,location

lunix 集群,負載均衡,location

img https inf listen 大於 檢查 轉發 哈希 too

集群介紹

技術分享圖片

為什麽要用集群

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

為什麽要用集群

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

負載均衡

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

nginx負載均衡實驗

Nginx負載均衡概述

Web服務器,直接面向用戶,往往要承載大量並發請求,單臺服務器難以負荷,我使用多臺WEB服務器組成集群,前端使用Nginx負載均衡,將請求分散的打到我們的後端服務器集群中,
實現負載的分發。那麽會大大提升系統的吞吐率、請求性能、高容災

技術分享圖片

Nginx要實現負載均衡需要用到proxy_pass代理模塊配置

Nginx負載均衡與Nginx代理不同地方在於

Nginx代理僅代理一臺服務器,而Nginx負載均衡則是將客戶端請求代理轉發至一組upstream虛擬服務池

Nginx可以配置代理多臺服務器,當一臺服務器宕機之後,仍能保持系統可用。

upstream配置

在nginx.conf > http 區域中

upstream django {
       server 10.0.0.10:8000;
       server 10.0.0.11:9000;
}

在nginx.conf > http 區域 > server區域 > location配置中

添加proxy_pass

location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://django;
}

此時初步負載均衡已經完成,upstream默認按照輪訓方式負載,每個請求按時間順序逐一分配到後端節點。

upstream分配策略

weight 權重

upstream django {
       server 10.0.0.10:8000 weight=5;
       server 10.0.0.11:9000 weight=10;#這個節點訪問比率是大於8000的
}

ip_hash

技術分享圖片
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端服務器
upstream django {
    ip_hash; server 10.0.0.10:8000; server 10.0.0.11:9000; }
技術分享圖片

backup

在非backup機器繁忙或者宕機時,請求backup機器,因此機器默認壓力最小

upstream django {
       server 10.0.0.10:8000 weight=5;
       server 10.0.0.11:9000;
       server node.oldboy.com:8080 backup;
}

負載均衡實驗環境規劃

角色            ip                    主機名
lb01        192.168.119.10        lb01    
web01        192.168.119.11        web01
web02        192.168.119.12        web02

關閉防火墻

iptables -F
sed  -i ‘s/enforcing/disabled/‘ /etc/selinux/config

systemctl stop firewalld
systemctl disable firewalld

一、web01服務器配置nginx,創建index.html

技術分享圖片
server {
        listen       80;
        server_name  192.168.119.11;
        location / {
        root /node;
            index  index.html index.htm;
        }
}

mkdir /node
echo ‘i am web01‘ > /node/index.html

#啟動NGINX
./sbgin/nginx
技術分享圖片

二、web01服務器配置nginx,創建index.html

技術分享圖片
server {
    listen       80;
    server_name  192.168.119.12;
    location / {
        root /node;
        index  index.html index.htm;
}


mkdir /node
echo ‘i am web02...‘ > /node/index.html
#啟動nginx
./sbing/nginx
技術分享圖片

三、配置lb01服務器的nginx負載均衡

1.檢查lb01的 nginx.conf

技術分享圖片
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream node {
      server 192.168.119.11:80;
      server 192.168.119.12:80;
}
    server {
        listen       80;
        server_name 192.168.119.10;
        location / {
          proxy_pass http://node;
          include proxy_params;  #需要手動創建
        }
    }
}
技術分享圖片

2.手動創建proxy_params文件,文件中存放代理的請求頭相關參數

技術分享圖片
[root@lb01 conf]# cat /opt/nginx/conf/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;

proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
技術分享圖片
啟動lb01負載均衡nginx服務

./sbin/nginx

四、訪問lb01節點nginx,反復刷新

技術分享圖片

技術分享圖片

五、nginx負載均衡調度算法

技術分享圖片
調度算法      概述
輪詢        按時間順序逐一分配到不同的後端服務器(默認)
weight       加權輪詢,weight值越大,分配到的訪問幾率越高
ip_hash      每個請求按訪問IP的hash結果分配,這樣來自同一IP的固定訪問一個後端服務器
url_hash      按照訪問URL的hash結果來分配請求,是每個URL定向到同一個後端服務器
least_conn    最少鏈接數,那個機器鏈接數少就分發
技術分享圖片

1.輪詢(不做配置,默認輪詢)

2.weight權重(優先級)

3.ip_hash配置,根據客戶端ip哈希分配,不能和weight一起用

六、nginx動靜分離負載均衡

技術分享圖片

環境準備

系統                 服務                軟件                ip地址
centos7(lb01)                負載均衡            nginx proxy        192.168.119.10
centos7(web01)                靜態資源            nginx靜態資源        192.168.119.11
centos7(web02)                動態資源            django            192.168.119.12

一、在web01機器上,配置靜態資源,圖片等

技術分享圖片
cat nginx.conf


server {
        listen       80;
        server_name  192.168.119.11;
        #定義網頁根目錄
         root /code;
        #定義了靜態資源
        index index.html;
#域名匹配,所有的png、jpg、gif請求資源,都去/root/code/images底下找
         location ~* .*\.(png|jpg|gif)$ {
                root /code/images;
        }    

#重啟nginx
./sbin/nginx
技術分享圖片 技術分享圖片
#創建目錄
mkdir -p /code/images
#準備首頁文件
[root@web01  /code]$cat index.html
static files...
#準備靜態文件,圖片
[root@web01  /code/images]$wget http://pythonav.cn/av/girlone.jpg^C
[root@web01  /code/images]$ls
girlone.jpg
技術分享圖片

二、在web02配置動態請求,準備一個flask程序和靜態資源轉發

技術分享圖片
cat  nginx.conf

#靜態資源地址
upstream static {
server 192.168.119.11:80;
}
#flask動態請求 upstream flask { server 192.168.119.12:8080; }
server { listen 80; server_name 192.168.119.12;
      #當請求到達192.168.119.12:80/時,轉發給flask的8080應用 location / { proxy_pass http://flask; include proxy_params; }
      #當判斷資源請求是 192.168.119.12/girl.jpg時候,轉發請求給static地址池的服務器192.168.119.11/ location ~ .*\.(png|jpg|gif)$ {         proxy_pass http://static; include proxy_params; }
技術分享圖片

準備flask應用,flask.py

技術分享圖片
from flask import Flask
app=Flask(__name__)
@app.route(‘/‘)
def hello():
    return "i am flask....from nginx"
if __name__=="__main__":
    app.run(host=‘0.0.0.0‘,port=8080)
技術分享圖片

後臺運行flask程序

python flask-web.py &

三、在負載均衡服務器lb01上測試訪問192.168.119.10

技術分享圖片

技術分享圖片

技術分享圖片

lunix 集群,負載均衡,location