LAMP+Varnish的實現

来源:https://www.cnblogs.com/walk1314/archive/2018/09/19/9619447.html
-Advertisement-
Play Games

基於Keepalived+Varnish+Nginx實現的高可用LAMP架構 註意:各節點的時間需要同步(ntpdate ntp1.aliyun.com),關閉firewalld(systemctl stop firewalld.service,systemctl disable firewalld ...


 

基於Keepalived+Varnish+Nginx實現的高可用LAMP架構

註意:各節點的時間需要同步(ntpdate ntp1.aliyun.com),關閉firewalld(systemctl stop firewalld.service,systemctl disable firewalld.service),設置selinux為permissive(setenforce 0 或 vim /etc/selinux/config);同時確保DR1和DR2節點的網卡支持MULTICAST(多播)通信。通過命令ifconfig可以查看到是否開啟了MULTICAST:

       

 搭建RS1(RS1提供mariadb服務和靜態資源)

[root@RS1 Desktop]# yum -y install mariadb-server httpd
[root@RS1 Desktop]# vim /etc/my.cnf
    [mysqld]
    ...
    skip-name-resolve=ON
    innodb-file-per-table=ON
    ...
[root@RS1 Desktop]# systemctl start mariadb
[root@RS1 Desktop]# mysql_secure_installation  #進行資料庫相關安全設置
    ...
[root@RS1 Desktop]# mysql -uroot -p
    Enter password: 
    MariaDB [(none)]> create database wordpress;
    MariaDB [(none)]> grant all on wordpress.* to 'wpuser'@'10.10.0.%' identified by '123456';
    MariaDB [(none)]> flush privileges;
    MariaDB [(none)]> exit;
[root@RS1 Desktop]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[root@RS1 Desktop]# tar xf wordpress-4.9.4-zh_CN.tar.gz -C /var/www/html
[root@RS1 Desktop]# vim /var/www/html/index.html
    <h1>10.10.0.21 server</h1>
[root@RS1 Desktop]# vim /etc/httpd/conf.d/vhost.conf
    <virtualhost *:80>
        servername www.test.org
        DirectoryIndex index.html index.php
        Documentroot /var/www/html
        ProxyRequests off
        ProxyPassMatch ^/(.*\.php)$ fcgi://10.10.0.22:9000/var/www/html/$1
        ProxyPassMatch ^/(ping|status)$ fcgi://10.10.0.22:9000/$1
        <Directory />
            options FollowSymlinks
            Allowoverride none
            Require all granted
        </Directory>
    </virtualhost>
[root@RS1 Desktop]# systemctl start httpd
[root@RS1 Desktop]# /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php
[root@RS1 Desktop]# vim /var/www/html/wordpress/wp-config.php  #關聯wordpress資料庫
    define('DB_NAME', 'wordpress');
    define('DB_USER', 'wpuser');
    define('DB_PASSWORD', '123456');
    define('DB_HOST', '10.10.0.21');
[root@RS1 Desktop]# scp /var/www/html/wordpress 10.10.0.22:/var/www/html/  #複製wordpress到22的主機

搭建RS2(RS2提供動態資源)

[root@RS2 Desktop]# yum -y httpd php-fpm php-mysql php-mbstring php-mcrypt
[root@RS2 Desktop]# vim /var/www/html/index.html
    <h1>10.10.0.22 server</h1>
[root@RS2 Desktop]# vim /etc/httpd/conf.d/vhost.conf
    <virtualhost *:80>
        servername www.test.org
        DirectoryIndex index.html index.php
        Documentroot /var/www/html
        ProxyRequests off
        ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/$1
        ProxyPassMatch ^/(ping|status)$ fcgi://127.0.0.1:9000/$1
        <Directory />
            options FollowSymlinks
            Allowoverride none
            Require all granted
        </Directory>
    </virtualhost>
[root@RS2 Desktop]# systemctl start httpd
[root@RS2 Desktop]# vim /etc/php-fpm.d/www.conf
    listen = 0.0.0.0:9000
    ; listen.allowed_clients = 127.0.0.1  #註釋此句,允許其他主機遠程訪問
    pm.status_path = /status
    ping.path = /ping
    ping.response = pong
[root@RS2 Desktop]# chown apache:apache /var/lib/php/session
[root@RS2 Desktop]# systemctl start php-fpm

搭建DR1

[root@DR1 Desktop]# yum install -y nginx keepalived
[root@DR1 Desktop]# vim /etc/nginx/nginx/conf  #配置nginx反代
    http {
        ...
        upstream websrvs {
            server 10.10.0.21:80;
            server 10.10.0.22:80;
            server 127.0.0.1:80 backup;
        }
    
        server {
            listen 80;
            include /etc/nginx/default.d/*.conf;
            location / {
                proxy_pass http://websrvs;
                proxy_set_header host $http_host;
                proxy_set_header X-Forward-For $remote_addr;
            }
        ...
    }
[root@DR1 Desktop]# vim /etc/nginx/conf.d/localhost.conf  #配置nginx本地服務
    server{
        listen 127.0.0.1:80;
        root /usr/share/nginx/html;
        index index.html;
    }
[root@DR1 Desktop]# vim /usr/share/nginx/html/index.html
    <h1>Balance Server DR1</h1>
[root@DR1 Desktop]# nginx -t  #檢查nginx語法
[root@DR1 Desktop]# systemctl start nginx
[root@DR1 Desktop]# vim /etc/keepalived/keepalived.conf  #配置keepalived
    global_defs {
       notification_email {
            root@localhost
       }
       notification_email_from keepalived@localhost
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
       router_id dr1
       vrrp_skip_check_adv_addr
       vrrp_mcast_group4 224.0.0.111
    }
    
    vrrp_script chk_ngx {    #檢查此伺服器的nginx進程是否存在,如果不存在則減權
       #kill -0 PID,0信號量不發送任何信號但系統會進行錯誤檢查,經常用來檢查一個進程是否存在,存在返回0,不存在返回1
       script "killall -0 nginx 2> /dev/null && exit 0 || exit 1"
       weight -10
       interval 1
       fall 3
       rise 3
    }
    
    vrrp_instance VIP_1 {
        state MASTER
        interface eno16777736
        virtual_router_id 1
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111@#$%
        }
        track_script {
            chk_ngx
        }
        virtual_ipaddress {
            192.168.4.120/24 dev eno16777736 label eno16777736:0
        }
[root@DR1 Desktop]# systemctl start keepalived.service

搭建DR2,參考DR1自行搭建

客戶端測試

[root@client Desktop]# for i in {1..20}; do curl http://192.168.4.120; done
<h1>10.10.0.21 server</h1>
<h1>10.10.0.22 server</h1>
<h1>10.10.0.21 server</h1>
<h1>10.10.0.22 server</h1>
<h1>10.10.0.21 server</h1>
<h1>10.10.0.22 server</h1>
<h1>10.10.0.21 server</h1>
<h1>10.10.0.22 server</h1>
<h1>10.10.0.21 server</h1>
<h1>10.10.0.22 server</h1>
<h1>10.10.0.21 server</h1>
<h1>10.10.0.22 server</h1>
<h1>10.10.0.21 server</h1>
<h1>10.10.0.22 server</h1>
<h1>10.10.0.21 server</h1>
<h1>10.10.0.22 server</h1>
<h1>10.10.0.21 server</h1>
<h1>10.10.0.22 server</h1>
<h1>10.10.0.21 server</h1>
<h1>10.10.0.22 server</h1>
[root@client Desktop]# ab -c 100 -n 10000 http://192.168.4.120/wordpress  #對動態頁面進行壓測
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.4.120 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        nginx/1.12.2
Server Hostname:        192.168.4.120
Server Port:            80

Document Path:          /wordpress
Document Length:        239 bytes

Concurrency Level:      100
Time taken for tests:   4.685 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Non-2xx responses:      10000
Total transferred:      4600000 bytes
HTML transferred:       2390000 bytes
Requests per second:    2134.44 [#/sec] (mean)
Time per request:       46.851 [ms] (mean)
Time per request:       0.469 [ms] (mean, across all concurrent requests)
Transfer rate:          958.83 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   4.8      0      51
Processing:    10   45   7.4     46      67
Waiting:        1   44   7.8     46      67
Total:         12   47   6.5     46      89

Percentage of the requests served within a certain time (ms)
  50%     46
  66%     47
  75%     48
  80%     49
  90%     53
  95%     58
  98%     63
  99%     67
 100%     89 (longest request)

配置Varnish伺服器

[root@Varnish Desktop]# yum install varnish
[root@Varnish Desktop]# vim /etc/varnish/varnish.params
    RELOAD_VCL=1
    VARNISH_VCL_CONF=/etc/varnish/default.vcl
    VARNISH_LISTEN_PORT=6081
    VARNISH_ADMIN_LISTEN_ADDRESS=10.10.0.23
    VARNISH_ADMIN_LISTEN_PORT=6082
    VARNISH_SECRET_FILE=/etc/varnish/secret
    VARNISH_STORAGE="file,/data/cache,1G"  #需要先創建好/data目錄
    VARNISH_USER=varnish
    VARNISH_GROUP=varnish
[root@Varnish Desktop]# vim /etc/varnish/default.vcl  #修改配置文件,添加VCL規則
    vcl 4.0;
    import directors;
    probe web_healthchk {  #定義後端健康監測機制
        .url="/index.html";
        .interval=2s;
        .timeout=1s;
        .window=5;
        .threshold=3;
    }
    backend RS1 {  #定義後端RS1
        .host="10.10.0.21";
        .port="80";
        .probe=web_healthchk;
    }
    backend RS2 {  #定義後端RS2
        .host="10.10.0.22";
        .port="80";
        .probe=web_healthchk;
    }
    sub vcl_init {  #初始化伺服器
        new WEBGROUP=directors.round_robin();
        WEBGROUP.add_backend(RS1);
        WEBGROUP.add_backend(RS2);
    }
    acl PURGERS {  #定義可用於purge操作的ip來源
        "127.0.0.1";
        "10.10.0.0"/24;
    }
    sub vcl_recv {
        if(req.http.Authorization ||  req.http.Cookie) {  #認證及cookie不緩存
            return(pass);
        }
        if(req.method != "GET" && req.method != "HEAD") {  #除了get和head以外的請求方法不緩存
            return(pass);
        }
        if(req.url ~ "index.php") {  #動態資源不緩存
            return(pass);
        }
        if(req.method == "PURGE") {  #purge方法清理緩存
            if(client.ip ~ PURGERS) {
                return(purge);
            }
        }
        if(req.http.X-Forward-For) {  #為發往後端主機添加的請求報文添加X-Forward-For的首部
            set req.http.X-Forward-For = req.http.X-Forward-For+","+client.ip;    
        }else {
            set req.http.X-Forward-For = client.ip;
        }
        set req.backend_hint = WEBGROUP.backend();  #調用伺服器組
        return(hash);
    }
    sub vcl_hash {
        hash_data(req.url);
    }
    sub vcl_backend_response {  #自定義緩存時長
        if(bereq.url ~ "\.(jpg|jpeg|gif|png)$") {
            set beresp.ttl = 1d;
        }
        if(bereq.url ~ "\.(html|css|js)$") {
            set beresp.ttl = 12h;
        }
        if(beresp.http.Set-Cookie) {
            set beresp.grace = 600s;
            return(deliver);
        }
    }
    sub vcl_deliver {
        if(obj.hits > 0) {  #為響應報文添加X-Cache的首部,標識緩存是否命中
            set resp.http.X-Cache = "Hit from "+server.ip;
        }else {
            set resp.http.X-Cache = "Miss";
        }
    }
[root@Varnish Desktop]# systemctl start varnish.service
[root@Varnish Desktop]# ss -tan
    State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
    LISTEN     0      5      192.168.122.1:53                       *:*                  
    LISTEN     0      128          *:22                       *:*                  
    LISTEN     0      128    127.0.0.1:631                      *:*                  
    LISTEN     0      100    127.0.0.1:25                       *:*                  
    LISTEN     0      128          *:6081                     *:*                  
    LISTEN     0      10     10.10.0.23:6082                     *:*                  
    LISTEN     0      128         :::22                      :::*                  
    LISTEN     0      128        ::1:631                     :::*                  
    LISTEN     0      100        ::1:25                      :::*                  
    LISTEN     0      128         :::6081                    :::*   

修改DR1和DR2定義的代理伺服器組(DR2的修改參考DR1)

[root@DR1 Desktop]# vim /etc/nginx/nginx.conf
...
    upstream websrvs {
        #server 10.10.0.21:80;
        #server 10.10.0.22:80;
        server 10.10.0.23:6081;  #轉發到Varnish伺服器
        server 127.0.0.1:80 backup;
    }
...
[root@DR2 Desktop]# systemctl reload nginx.service

客戶端再次測試


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • VPN英文全稱是“Virtual Private Network”,就是“虛擬專用網路”。可以遠程幫助用戶、分公司、商業伙伴及供應商同公司的內部網建立可信的安全連接,用於經濟有效地連接到商業伙伴和用戶的安全外聯網虛擬專用網。 搭建環境: 伺服器系統:Windows server 2008 R2 客戶 ...
  • 環境:Ubuntu 16.04 由於很久不用我的Ubuntu系統導緻密碼忘記,就想著應該有什麼辦法可以強制修改root密碼,就上百度找了一下,果然Ubuntu有辦法強制修改root密碼。 在這裡要謝謝這位博主啦。 第一步 重啟 Ubuntu ,並長按shift鍵,進入grub菜單,上下鍵選擇Ubun ...
  • windows server的伺服器遠程桌面預設埠號是3389,在工作中經常使用遠程桌面連接伺服器,但是這也是常常被黑客利用的埠號,但是如何修改掉預設埠,預防被黑客利用呢? 可以如下操作配置:很多人在使用windows操作系統的時候,由於修改埠的方法錯誤,導致自己不能遠程操作伺服器,給自己帶 ...
  • 雖然有kali linux這樣集合了很多安全工具的操作系統,但是kali的軟體源相對老舊,沒有ubuntu等主流debian系統豐富,kali預設使用su許可權進入圖形化界面也是違背linux許可權機制的體現之一,故使用主流debian系linux系統安裝安全工具集比kali更具可用性,但是許多安全工具 ...
  •     在awk中常用的內置函數大概分為:數值函數、字元函數、時間函數、二進位操作函數、數組函數、自定義函數等。 數值函數   常用的數值函數主要有int、rand、srand、sqrt等。詳細如下所示: | 函數 | 說明 | | | | | int(expr) | ...
  • 最近由於在學習STM32看到別人用寄存器編程式控制制跑馬燈,於是自己也想試一試。可是試了好久終究弄不出來。回頭看了下庫函數的調用關係才搞明白。首先通過查看GPIOA的設置函數發現設置如下: 改機構體根據結構體成員函數的大小自己形成了針對首地址的偏移量。從而與需要操作的寄存器地址偏移一一對應。通過查看設置 ...
  • 一、什麼是SSH? 簡單說,SSH是一種網路協議,用於電腦之間的加密登錄。 如果一個用戶從本地電腦,使用SSH協議登錄另一臺遠程電腦,我們就可以認為,這種登錄是安全的,即使被中途截獲,密碼也不會泄露。 最早的時候,互聯網通信都是明文通信,一旦被截獲,內容就暴露無疑。1995年,芬蘭學者Tatu ...
  • 一. 概述 linux安全系統的核心是用戶賬戶。 創建用戶時會分配用戶ID(UID)。 UID是唯一的,但在登錄系統時不是用UID,而是用登錄名。在講文件許可權之之前,先瞭解下linux是怎樣處理用戶賬戶的。以及用戶賬戶需要的文件和工具,這樣處理文件許可權問題時,就知道如何使用它們了。 1.1 /etc ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...