您好,登錄后才能下訂單哦!
Ngxin作為一個(gè)強(qiáng)大的開源軟件是可以先做為高可用集群服務(wù)的,這篇博文就介紹一下nginx+Keepalived是如何實(shí)現(xiàn)高性能+高可用集群服務(wù)的
環(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 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中的內(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 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中的內(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>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。