溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Keepaliev+Nginx+http

發布時間:2020-07-02 04:49:58 來源:網絡 閱讀:712 作者:步荀仙 欄目:建站服務器

 

  Ngxin作為一個強大的開源軟件是可以先做為高可用集群服務的,這篇博文就介紹一下nginx+Keepalived是如何實現高性能+高可用集群服務的

    Keepaliev+Nginx+http


環境介紹:

   硬件:    4臺虛擬服務器

   系統:CentOS 7.3

   軟件:Keepalived、Apache、Nginx

IP及主機名

   Master

       主機名:shiyan1

       IP地址:172.18.17.31

   Backup

       主機名:shiyan2

       IP地址:172.18.17.32

   Httpd1

       主機名:shiyan3

       IP地址:172.18.17.33

   Httpd2

       主機名:shiyan4

       IP地址:172.18.17.34

 

四臺服務器初始化配置(四臺服務器相同的配置)

   關閉防火墻

     

    [root@shiyan~ ]# systemctl disable firewalld
    [root@shiyan~ ]# systemctl stop firewalld
    [root@shiyan~ ]# iptables –F

   

  關閉Selinux

    [root@shiyan~ ]# vim /etc/selinux/config
       SELINUX=disabled
     #保存重啟系統生效


   安裝軟件

     Master/Backup

     

    [root@shiyan~ ]# yum install keepalived httpd nginx   #(Nginx需要單獨配置EPEL源)

     Httpd1/Httpd2

      

        [root@shiyan~ ]# yum install httpd


Httpd1配置

    [root@shiyan3 ~ ]# mkdir -p /app/apache/html/  
    [root@shiyan3 ~ ]# chown -R apache.apache /app/apache/html/
    [root@shiyan3 ~ ]# echo "Apache Server 1" > /app/apache/html/index.html
    [root@shiyan3 ~ ]# vim /etc/httpd/conf/httpd.conf 
          #此處是更改httpd.conf中的內容,并非添加內容
       DocumentRoot "/app/apache/html"    
       #更改為自定義的路徑
            
       #
       # Relax access to content within /var/www.
       #
       <Directory "/app/apache">    #更改為自定義的路徑
       AllowOverride None
       # Allow open access:
       Require all granted
       </Directory>
            
       # Further relax access to the default document root:
       <Directory "/app/apache/html">     #更改為自定義的路徑.
     [root@shiyan3 ~ ]# systemctl restart httpd
     
     #測試網站是否正常運行
     [root@yum ~ ]# curl http://172.18.17.33
            Apache Server 1  #測試成功


Httpd2配置

    [root@shiyan4 ~ ]# mkdir -p /app/apache/html/  
    [root@shiyan4 ~ ]# chown -R apache.apache /app/apache/html/
    [root@shiyan4 ~ ]# echo "Apache Server 2" > /app/apache/html/index.html
    [root@shiyan4 ~ ]# vim /etc/httpd/conf/httpd.conf 
          #此處是更改httpd.conf中的內容,并非添加內容
       DocumentRoot "/app/apache/html"    
       #更改為自定義的路徑
            
       #
       # Relax access to content within /var/www.
       #
       <Directory "/app/apache">    #更改為自定義的路徑
       AllowOverride None
       # Allow open access:
       Require all granted
       </Directory>
            
       # Further relax access to the default document root:
       <Directory "/app/apache/html">     #更改為自定義的路徑.
    [root@shiyan4 ~ ]# systemctl restart httpd
          #測試網站是否正常運行
        [root@yum ~ ]# curl http://172.18.17.34
            Apache Server 2  #測試成功


Master配置

   配置Sorry-Server
    [root@shiyan1 ~ ]# mkdir -p /app/apache/html/  
    [root@shiyan1 ~ ]# chown -R apache.apache /app/apache/html/
    [root@shiyan1 ~ ]# echo "<h2>Sorry Server 1</h2>" > /app/apache/html/index.html
    [root@shiyan1 ~ ]# vim /etc/httpd/conf/httpd.conf 
       #此處是更改httpd.conf中的內容,并非添加內容
       Listen 8080
       DocumentRoot "/app/apache/html"    
       #更改為自定義的路徑
            
       #
       # Relax access to content within /var/www.
       #
       <Directory "/app/apache">    #更改為自定義的路徑
       AllowOverride None
       # Allow open access:
       Require all granted
       </Directory>
            
       # Further relax access to the default document root:
       <Directory "/app/apache/html">     #更改為自定義的路徑.
    [root@shiyan1 ~ ]# systemctl restart http
               #測試網站是否正常運行
        [root@yum ~ ]# curl http://172.18.17.31:8080
              <h2>Sorry Server 1</h2>  #測試成功
      配置Keepalived
           [root@shiyan1 ~ ]# cp /etc/keepalived/keepalived.conf{,.bak}    #備份文件
           [root@shiyan1 ~ ]# vim /etc/keepalived/keepalived.conf
              global_defs {
               notification_email {
                  root   #定義收郵件的用戶
               }
               notification_email_from Alexandre.Cassen@firewall.loc
               smtp_server 172.18.17.31   #定義郵件地址
               smtp_connect_timeout 30
               router_id node1            #定義節點名稱
            }
            
            vrrp_instance VI_1 {
                state MASTER              #定義節點為主節點模式
                interface ens33           #定義使用ens33為VIP網卡
                virtual_router_id 51      #定義節點編號
                priority 150              #定義優先級
                advert_int 1   
                authentication {
                    auth_type PASS
                    auth_pass 1111
                }
                virtual_ipaddress {
                    172.18.17.30          #定義VIP
                }
            }
~      配置Nginx服務
            [root@shiyan1 ~ ]# vim /etc/nginx/nginx.conf
                #添加nginx集群
                upstream websrvs {
                    server 172.18.17.33:80;
                    server 172.18.17.34:80;
                    server 127.0.0.1:8080 backup;
                }
                #server 部分的內容需要全部注釋掉
                
            [root@shiyan1 ~ ]# vim /etc/nginx/conf.d/default.conf 
                    server {
                    listen       80;
                    location / {
                        root  html;
                        proxy_pass http://websrvs;
                        index  index.html index.htm;
                    }
                }
            [root@shiyan1 ~ ]# systemctl restart nginx
            [root@shiyan1 ~ ]# systemctl restart keepalived
            [root@shiyan1 ~ ]# systemctl restart httpd


Backup配置

   配置Sorry-Server
    [root@shiyan2 ~ ]# mkdir -p /app/apache/html/  
    [root@shiyan2 ~ ]# chown -R apache.apache /app/apache/html/
    [root@shiyan2 ~ ]# echo "<h2>Sorry Server 2</h2>" > /app/apache/html/index.html
    [root@shiyan2 ~ ]# vim /etc/httpd/conf/httpd.conf 
       #此處是更改httpd.conf中的內容,并非添加內容
       Listen 8080
       DocumentRoot "/app/apache/html"    
       #更改為自定義的路徑
            
       #
       # Relax access to content within /var/www.
       #
       <Directory "/app/apache">    #更改為自定義的路徑
       AllowOverride None
       # Allow open access:
       Require all granted
       </Directory>
            
       # Further relax access to the default document root:
       <Directory "/app/apache/html">     #更改為自定義的路徑.
    [root@shiyan2 ~ ]# systemctl restart http
               #測試網站是否正常運行
        [root@yum ~ ]# curl http://172.18.17.31:8080
              <h2>Sorry Server 2</h2>  #測試成功
   配置Keepalived
     [root@shiyan2 ~ ]# cp /etc/keepalived/keepalived.conf{,.bak}    #備份文件
     [root@shiyan2 ~ ]# vim /etc/keepalived/keepalived.conf
              global_defs {
               notification_email {
                  root   #定義收郵件的用戶
               }
               notification_email_from Alexandre.Cassen@firewall.loc
               smtp_server 172.18.17.31   #定義郵件地址
               smtp_connect_timeout 30
               router_id node1            #定義節點名稱
            }
            
            vrrp_instance VI_1 {
                state MASTER              #定義節點為主節點模式
                interface ens33           #定義使用ens33為VIP網卡
                virtual_router_id 51      #定義節點編號
                priority 150              #定義優先級
                advert_int 1   
                authentication {
                    auth_type PASS
                    auth_pass 1111
                }
                virtual_ipaddress {
                    172.18.17.30          #定義VIP
                }
            }
~  配置Nginx服務
      [root@shiyan2 ~ ]# vim /etc/nginx/nginx.conf
         #添加nginx集群
          upstream websrvs {
           server 172.18.17.33:80;
           server 172.18.17.34:80;
           server 127.0.0.1:8080 backup;
           }
           #server 部分的內容需要全部注釋掉
                    
      [root@shiyan2 ~ ]# vim /etc/nginx/conf.d/default.conf 
           server {
           listen       80;
           location / {
           root  html;
           proxy_pass http://websrvs;
           index  index.html index.htm;
           } 
          }
       [root@shiyan2 ~ ]# systemctl restart keepalived
       [root@shiyan2 ~ ]# systemctl restart nginx
       [root@shiyan2 ~ ]# systemctl restart httpd


測試環境    

 #默認使用rr算法依次輪詢訪問后端httpd服務器
    [root@yum ~ ]# curl http://172.18.17.30    
    Apache Server 1
    [root@yum ~ ]# curl http://172.18.17.30
    Apache Server 2
 
 #關閉后端http1服務,這樣只能訪問httpd2的服務
     [root@yum ~ ]# curl http://172.18.17.30
      Apache Server 2
     [root@yum ~ ]# curl http://172.18.17.30
      Apache Server 2
      
 #關閉兩臺后端主機的httpd服務,這樣因為沒有后端服務器所以Master的sorry-server提供服務
    [root@yum ~ ]# curl http://172.18.17.30
    <h2>Sorry Server 1</h2>
 #關閉Master測試
    [root@yum ~ ]# curl http://172.18.17.30
    <h2>Sorry Server 2</h2>



向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女