首頁 > 軟體

永中檔案線上轉換預覽基於nginx設定部署方案

2022-06-09 14:05:27

永中檔案線上轉換預覽雙活實現方案

永中檔案線上轉換預覽服務 是永中軟體股份有限公司基於十多年核心技術積累、面向移動互聯領域推出的 一款檔案處理軟體。永中採用自主可控核心技術,具備快速技術和服務響應能力,把檔案原樣輸出為 HTML,圖片等,即點即得、無需下載、保護檔案隱私,快速高效,輕鬆實現檔案線上安全閱讀。​

實現目標

  • 通過nginx實現業務fcsserver負載均衡,後端可以動態擴充套件應用伺服器
  • nginx通過keepalived實現高可用,解決單點故障
  • 通過keepalived實現nginx雙活設定,解決nginx主備資源使用率50%問題
  • 通過dns輪詢解析域名到雙活vip,達到負載均衡效果
  • 後期nginx達到瓶頸應考慮lvs+keepalived+nginx架構,動態擴充套件nginx伺服器

環境準備

  • 伺服器可以連通外網或者有內網yum源伺服器,本次實驗伺服器可連通外網
  • nginx01和nginx02伺服器需要在同一網段的網路內
  • 保證各伺服器之前網路互通
  • 保證伺服器的防火牆和selinux關閉
  • 必須對外提供域名存取,否則只能使用其中一個vip
  • 內網部署DNS伺服器,模擬DNS輪詢解析

1、網路架構

2、實驗伺服器分佈

主機ip作業系統軟體vip
nginx01192.168.56.101Centos7.6nginx keepalived80192.168.56.200
nginx02192.168.56.106Centos7.6nginx keepalived80192.168.56.201
fcs01192.168.56.101Centos7.6tomcat8080 
fcs02192.168.56.106Centos7.6tomcat8080 
共用儲存192.168.56.108Centos7.6nfs  
快取192.168.56.108Centos7.6redis6379 
內部DNS192.168.56.108Centos7.6bind53 

3、redis

部署伺服器:192.168.56.108

3.1 設定redis

/etc/redis.conf

# 監聽地址
bind 0.0.0.0
# 認證密碼
requirepass yozosoft

3.2 啟動redis

systemctl enable redis --now && systemctl status redis

4、nfs

4.1 設定nfs

192.168.56.108(伺服器端)

# 建立儲存目錄
mkdir -p /opt/yozo/data
# 修改許可權
chown -R nfsnobody.nfsnobody /opt/yozo/data
# 修改組態檔
vim /etc/exports
	/opt/yozo/data 192.168.56.0/24(rw,sync,all_squash)

192.168.56.101/192.168.56.106(使用者端)

# 建立掛載點
mkdir -p /opt/yozo/data
# 掛載nfs共用目錄
mount -t nfs 192.168.56.108:/opt/yozo/data /opt/yozo/data

4.2 啟動nfs

systemctl enable rpcbind nfs --now && systemctl status rpcbind nfs

5、fcsserver

部署伺服器:

192.168.56.101(fcsserver01)

192.168.56.106(fcsserver02)

5.1 部署fcs

以tomcat為中介軟體,本次實驗fcs安裝目錄

/opt/yozo/fcsserver/webapps/fcsserver,/opt/yozo/fcsserver為解壓後的tomcat

部署專案包

mkdir opt/yozo/fcsserver/webapps/fcsserver -p
unzip fcscloud.war -d opt/yozo/fcsserver/webapps/fcsserver

修改fcsserver組態檔

# /opt/yozo/fcsserver/webapps/fcsserver/WEB-INF/classes/config.properties
inputDir=/opt/yozo/data/fcsdata/input	# 必須指定掛載共用儲存目錄的
outputDir=/opt/yozo/data/fcsdata/output
# 如果不提供域名,就只能設定其中一個vip;如果設定為域名,則快取後,通過vip和fcsserver的ip將不能存取轉換的快取檔案
viewDomain=http://www.fcsserver.com/fcsserver/
# /opt/yozo/fcsserver/webapps/fcsserver/WEB-INF/classes/application.yml
cache:                            #採用哪種快取資料方式
    type: redis                     #local,redis,mysql(local模式只適用於單機,叢集部署不支援)
  redis:
    enable: true                    #redis開關(預覽設定許可權時需要使用redis,並行和非同步waitting機制使用redis,必開)
 redis:
    database: 1 # Redis資料庫索引(預設為0)
    timeout: 10000 #設定使用者端超時時間,單位是毫秒,預設為2000
    password: yozosoft #密碼
    #單機版
    host: 192.168.56.108
    port: 6379
# /opt/yozo/fcsserver/webapps/fcsserver/WEB-INF/classes/
<property name="LOG_HOME">../logs</property>

5.2 設定fcs系統服務

設定fcsserver.service

# cat /usr/lib/systemd/system/fcsserver.service
[Unit]
Description=fcsserver Service.
After=network.target
[Service]
Type=forking
Environment="PATH=/opt/yozo/jdk-8u251-amd64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
WorkingDirectory=/opt/yozo/fcsserver/bin
ExecStart=/opt/yozo/fcsserver/bin/startup.sh
Restart=always
PrivateTmp=true
# 可以指定相關使用者啟動fcsserver
# User=yozo
# Group=yozo
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target

如果以普通使用者啟動,需要將tomcat目錄屬主屬組設定成相應賬戶

# 此次實驗以yozo使用者啟動
chown -R yozo. /opt/yozo/fcsserver

5.3 啟動fcs

systemctl enable fcsserver --now && systemctl status fcsserver

6、nginx

部署伺服器:

192.168.56.101(nginx01)

192.168.56.106(nginx02)

6.1 設定nginx

/etc/nginx/conf.d/fcsserver.conf設定

server {
    ....
    location ~ /fcsserver {
        add_header Cache-Control private,no-store,max-age=0,no-cache,must-revalidate,post-check=0,pre-check=0;
        proxy_redirect off;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded_Proto "http";
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_request_buffering off;
        proxy_read_timeout 7200;
        proxy_send_timeout 7200;
        proxy_pass http://fcsserver;
    }
   ....
}
upstream fcsserver {
    server 192.168.56.101:8080 fail_timeout=60s;
    server 192.168.56.106:8080 fail_timeout=60s;
    keepalive 256;
}

6.2 啟動nginx

systemctl enable nginx --now && systemctl status nginx

7、keepalived

部署伺服器:

192.168.56.101(nginx01)

192.168.56.106(nginx02)

7.1 設定keepalived

7.1.1 nginx01設定

/etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
   router_id proxy1
}
vrrp_script chk_nginx {
  script "/etc/keepalived/check_nginx.sh"
  interval 2
  weight 20
  fall 3
  rise 2
}
vrrp_instance VI_1 {
    state MASTER
    interface enp0s3
    virtual_router_id 51
    priority 100                           
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.56.200                     
    }
    track_script {
        chk_nginx
    }
}
vrrp_instance VI_2 {
    state BACKUP
    interface enp0s3
    virtual_router_id 52
    priority 90                            
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.56.201                     
    }
    track_script {
        chk_nginx
    }
}

7.1.2 nginx02設定

# 備份keepalived.conf
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.confbak

/etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
   router_id proxy2
}
vrrp_script chk_nginx {
  script "/etc/keepalived/check_nginx.sh"
  interval 2
  weight 20
  fall 3
  rise 2
}
vrrp_instance VI_1 {
    state BACKUP
    interface enp0s8
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.56.200
    }
    track_script {
        chk_nginx
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface enp0s8
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.56.201
    }
    track_script {
        chk_nginx
    }
}

雙機/etc/keepalived/check_nginx.sh

#!/bin/bash
#時間變數,用於記錄紀錄檔
d=`date --date today +%Y%m%d_%H:%M:%S`
#計算nginx程序數量
n=`ps -C nginx --no-heading|wc -l`
#如果程序為0,則啟動nginx,並且再次檢測nginx程序數量,
#如果還為0,說明nginx無法啟動,此時需要關閉keepalived
if [ $n -eq "0" ]; then
        /etc/init.d/nginx start
        n2=`ps -C nginx --no-heading|wc -l`
        if [ $n2 -eq "0"  ]; then
                echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
                systemctl stop keepalived
        fi
fi

7.2 啟動keepalived

systemctl enable keepalived --now && systemctl status keepalived

nginx01

nginx02

7.3 模擬故障

7.3.1 nginx01手動關閉nginx

nginx故障後,keepalived會自動啟動nginx

7.3.2 nginx01手動關閉keepalived

nginx01的vip將會繫結到nginx02上面,nginx02將會出現2個vip,2個vip均可以存取fcsserver

8、DNS伺服器部署

用於模擬公網DNS輪詢解析,實際使用中是在域名提供商處設定指向對映外網ip

8.1 設定dns

/etc/named.conf

zone "fcsserver.com" IN {
        type master;
        file "fcsserver.com.zone";
};
zone "56.168.192.in-addr.arpa" IN {
        type master;
        file "192.168.56.zone";
};

/var/named/fcsserver.com.zone

$TTL    86400
@               IN SOA  tom jerry (                     ; tom & jerry 這兩個引數本應是主機名和郵件地址,這裡隨便填寫,沒有問題
                                        42              ; serial (d. adams)
                                        3H              ; refresh
                                        15M             ; retry
                                        1W              ; expiry
                                        1D )            ; minimum
                IN NS           ns.fcsserver.com.            ; notice : don't forget the dot in the end
                IN MX 10        mail.fcsserver.com.
www             IN A            192.168.56.200
www             IN A            192.168.56.201
ns              IN A            192.168.56.108
mail            IN A            192.168.56.108

/var/named/192.168.56.zone

$TTL    86400
@       IN      SOA     ns.fcsserver.com. root (
                                      1997022700 ; Serial
                                      28800      ; Refresh
                                      14400      ; Retry
                                      3600000    ; Expire
                                      86400 )    ; Minimum
        IN      NS      ns.fcsserver.com.
200     IN      PTR     www.fcsserver.com.
201     IN      PTR     www.fcsserver.com.
108     IN      PTR     mail.fcsserver.com.
108     IN      PTR     ns.fcsserver.com.

修改許可權

chown named. /var/named/ -R

8.2 啟動dns服務

systemctl enable named --now && systemctl status named

9、驗證

目標:本次實驗有2個vip 192.168.56.200 和 192.168.56.201,需要驗證www.fcsserver.com分別解析到2個vip上,並確認每個vip後端服務正常可用

9.1 使用者端dns設定

測試期間,禁用其他網路卡,只留虛擬機器器網路卡

新增DNS

9.2 DNS快取清理

9.3 域名存取測試

測試之前需要確認此次DNS解析是否指向所需測試的VIP,如果不是請重新整理DNS快取

需要測試www.fcsserver.com --> 192.168.56.200和www.fcsserver.com --> 192.168.56.201

檔案轉換測試

轉換檔案存取測試

注:fcsserver的組態檔中viewDomain設定為域名,故轉換檔案預覽連線只能通過www.fcsserver.com可以正常存取,通過vip、nginx ip、fcsserver ip均不可以存取

以上就是永中檔案線上轉換預覽雙活基於nginx的實現方案的詳細內容,更多關於nginx永中檔案線上轉換預覽的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com