Ngxin作為一個(gè)強(qiáng)大的開源軟件是可以先做為高可用集群服務(wù)的,這篇博文就介紹一下nginx+Keepalived是如何實(shí)現(xiàn)高性能+高可用集群服務(wù)的
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比大興網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式大興網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋大興地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。
環(huán)境介紹:
硬件: 4臺(tái)虛擬服務(wù)器
系統(tǒng):CentOS 7.3
軟件:Keepalived、Apache、Nginx
IP及主機(jī)名
Master
主機(jī)名:shiyan1
IP地址:172.18.17.31
Backup
主機(jī)名:shiyan2
IP地址:172.18.17.32
Httpd1
主機(jī)名:shiyan3
IP地址:172.18.17.33
Httpd2
主機(jī)名:shiyan4
IP地址:172.18.17.34
四臺(tái)服務(wù)器初始化配置(四臺(tái)服務(wù)器相同的配置)
關(guān)閉防火墻
[root@shiyan~ ]# systemctl disable firewalld [root@shiyan~ ]# systemctl stop firewalld [root@shiyan~ ]# iptables –F關(guān)閉Selinux
[root@shiyan~ ]# vim /etc/selinux/config SELINUX=disabled #保存重啟系統(tǒng)生效安裝軟件
Master/Backup
[root@shiyan~ ]# yum install keepalived httpd nginx #(Nginx需要單獨(dú)配置EPEL源)Httpd1/Httpd2
[root@shiyan~ ]# yum install httpdHttpd1配置
[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中的內(nèi)容,并非添加內(nèi)容 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 #測試網(wǎng)站是否正常運(yùn)行 [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中的內(nèi)容,并非添加內(nèi)容 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 #測試網(wǎng)站是否正常運(yùn)行 [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中的內(nèi)容,并非添加內(nèi)容 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 #測試網(wǎng)站是否正常運(yùn)行 [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 #定義節(jié)點(diǎn)名稱 } vrrp_instance VI_1 { state MASTER #定義節(jié)點(diǎn)為主節(jié)點(diǎn)模式 interface ens33 #定義使用ens33為VIP網(wǎng)卡 virtual_router_id 51 #定義節(jié)點(diǎn)編號(hào) priority 150 #定義優(yōu)先級(jí) advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.18.17.30 #定義VIP } } ~ 配置Nginx服務(wù) [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 部分的內(nèi)容需要全部注釋掉 [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 httpdBackup配置
配置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中的內(nèi)容,并非添加內(nèi)容 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 #測試網(wǎng)站是否正常運(yùn)行 [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 #定義節(jié)點(diǎn)名稱 } vrrp_instance VI_1 { state MASTER #定義節(jié)點(diǎn)為主節(jié)點(diǎn)模式 interface ens33 #定義使用ens33為VIP網(wǎng)卡 virtual_router_id 51 #定義節(jié)點(diǎn)編號(hào) priority 150 #定義優(yōu)先級(jí) advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.18.17.30 #定義VIP } } ~ 配置Nginx服務(wù) [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 部分的內(nèi)容需要全部注釋掉 [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測試環(huán)境
#默認(rèn)使用rr算法依次輪詢?cè)L問后端httpd服務(wù)器 [root@yum ~ ]# curl http://172.18.17.30 Apache Server 1 [root@yum ~ ]# curl http://172.18.17.30 Apache Server 2 #關(guān)閉后端http1服務(wù),這樣只能訪問httpd2的服務(wù) [root@yum ~ ]# curl http://172.18.17.30 Apache Server 2 [root@yum ~ ]# curl http://172.18.17.30 Apache Server 2 #關(guān)閉兩臺(tái)后端主機(jī)的httpd服務(wù),這樣因?yàn)闆]有后端服務(wù)器所以Master的sorry-server提供服務(wù) [root@yum ~ ]# curl http://172.18.17.30 <h2>Sorry Server 1</h2> #關(guān)閉Master測試 [root@yum ~ ]# curl http://172.18.17.30 <h2>Sorry Server 2</h2>另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
文章名稱:Keepaliev+Nginx+http-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://jinyejixie.com/article48/pishp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、全網(wǎng)營銷推廣、域名注冊(cè)、面包屑導(dǎo)航、企業(yè)建站、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容