##臨時關閉selinux~]# setenforce 0
圖片服務器centos 1:192.168.150.149 (須要單獨安裝一個lrzsz來導入圖片使用)文檔服務器centos 3:192.168.150.152
centos 2:內網:192.168.150.154外網:172.16.251.68centos 4:內網:192.168.150.155外網:172.16.251.162
~]#cd /var/www/html(拖一個圖片文件到這個目錄)##查看目錄下內容文件已經存在~l]# lsup.png##啓動服務~]# systemctl start httpd##查看80端口已經啓用~]# ss -tanlState Recv-Q Send-Q Local Address:Port Peer Address:PortLISTEN 0 128 *:22 *:*LISTEN 0 100 127.0.0.1:25 *:*LISTEN 0 128 :::80 :::*LISTEN 0 128 :::22 :::*LISTEN 0 100 ::1:25 :::*
##這裏使用默認目錄以及默認文件。vim /var/www/html/index.html<h1>centos7.3</h1>##啓動服務~]# systemctl start httpd##查看80端口已經啓用~]# ss -tanlState Recv-Q Send-Q Local Address:Port Peer Address:PortLISTEN 0 128 *:22 *:*LISTEN 0 100 127.0.0.1:25 *:*ISTEN 0 128 :::80 :::*LISTEN 0 128 :::22 :::*LISTEN 0 100 ::1:25 :::*
##文件是本身建立的,沒有修改原配置文件,可是建立的文件必須在conf.d目錄下以.conf結尾,由於在nginx.conf文件中有設定;vim /etc/nginx/conf.d/ap.conf##給服務器配置緩存功能;三級目錄,定義緩存關鍵字以及大小,磁盤上用於緩存數據的上限;proxy_cache_path /data/web levels=1:1:1 keys_zone=pxycache:20m max_size=1g;##配置一個server;server {##監聽在本機全部IP 的80端口;listen 80;##給反代的web服務器配置的域名;server_name www.myue.com;##設置網站根目錄;root /var/www/html;##指明要調用的緩存;proxy_cache pxycache;##設置緩存的key;proxy_cache_key $request_uri;##定義狀態碼20三、30二、304緩存多久;proxy_cache_valid 200 302 304 1h;##定義除了以上的狀態碼另外的狀態緩存多久;proxy_cache_valid any 1m;##文檔訪問IP ;location / {proxy_pass http://192.168.150.149:80;}##圖片訪問IP,用正則匹配。由於我只配置了一個圖片因此只匹配.png的圖片,能夠根據本身須要進行配置;location ~* \.png$ {proxy_pass http://192.168.150.152:80;}}##手動建立緩存目錄;~]#mkdir /data/web -pv##測試配置文件語法;~]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successfulhtml##注意keepalived重啓不穩定,若是須要重啓須要stop 而後在start,雙主模式下只能啓動不能中止;~]# systemctl stop keepalived~]# systemctl start keepalived
~]# vim /etc/keepalived/keepalived.conf##默認起始行! Configuration File for keepalived##全局配置;global_defs {##故障出現是發送的郵箱地址,這裏設置爲本主機;notification_email {root@localhost}##表示發送郵件的源地址;notification_email_from keepalived@localhost##發送有限的smtp服務器地址;smtp_server 127.0.0.1##超時時長smtp_connect_timeout 30##組廣播地址vrrp_mcast_group4 224.1.100.100}##keepalived從服務器定義;vrrp_instance VI_1 {##定義爲從,可選項MASTER|BACKUP;state BACKUP##使用的網卡;interface ens33##定義組ID;virtual_router_id 51##定義優先級,從服務器的優先級要比主服務器低;priority 96##多久在組裏廣播一下本身的心跳信息,默認單位爲秒s;advert_int 1##通訊時認證;authentication {##使用簡單字符串方式,不能超過8個字符,超過部分不予理會;auth_type PASS##主從兩個服務器的字符串要設定爲相同的;auth_pass YN9jd93d}##檢測網卡狀態;track_interface {ens33}##給keepalived配置的虛擬IP;virtual_ipaddress {172.16.100.100/16}}vrrp_instance VI_2 {##主服務器,由於是雙主模式,因此一個服務器能夠即爲主又爲從,可在網站PV量較大的時候 兩個服務器同時工做提升效率;state MASTERinterface ens33##組ID和上面配置不能相同,可是和須要相同組內的ID要保持一致;virtual_router_id 41##優先級定義100,本組內的從服務器優先級要低於這個數字;priority 100advert_int 1authentication {auth_type PASS##這裏要和本組內的字符串保持一致;auth_pass YN98d93d}track_interface {ens33}virtual_ipaddress {172.16.100.101/16}}##啓動nginx 以及keepalived;~]# systemctl start nginx~]# systemctl stop keepalived~]# systemctl start keepalived
##大體配置相同,只有幾處地方不一樣,上文已經指出,而且下面已經用紅顏色標註,這裏不過多介紹;! Configuration File for keepalivedglobal_defs {notification_email {root@localhost}notification_email_from keepalived@localhostsmtp_server 127.0.0.1smtp_connect_timeout 30vrrp_mcast_group4 224.1.100.100}vrrp_instance VI_1 {state MASTERinterface ens33virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass YN9jd93d}track_interface {ens33}virtual_ipaddress {172.16.100.100/16}}vrrp_instance VI_2 {state BACKUPinterface ens33virtual_router_id 41priority 96advert_int 1authentication {auth_type PASSauth_pass YN98d93d}track_interface {ens33}virtual_ipaddress {172.16.100.101/16}}##啓動nginx 以及keepalived~]# systemctl start nginx~]# systemctl stop keepalived~]# systemctl start keepalived
##抓包組IP試一下看到兩個服務器都爲主而且都每隔1s在組內發送一下本身的心跳~]# tcpdump -i ens33 -nn host 224.1.100.100tcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on ens33, link-type EN10MB (Ethernet), capture size 65535 bytes10:17:27.566889 IP 172.16.251.68 > 224.1.100.100: VRRPv2, Advertisement, vrid 41, prio 100, authtype simple, intvl 1s, length 2010:17:27.951391 IP 172.27.35.8 > 224.1.100.100: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 2010:17:28.567182 IP 172.16.251.68 > 224.1.100.100: VRRPv2, Advertisement, vrid 41, prio 100, authtype simple, intvl 1s, length 2010:17:28.952235 IP 172.27.35.8 > 224.1.100.100: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20^C13 packets captured13 packets received by filter0 packets dropped by kernel
C:\Windows\System32\drivers\etc\HOSTS ##文件最下面添加兩行解析linux
##把兩個IP解析爲相同域名,實現簡單的輪詢效果, 保存退出nginx
172.16.100.100 www.myue.comweb
172.16.100.101 www.myue.comvim
##中止一個nginx 或者中止一個keepalived一樣能夠訪問centos
~]# systemctl stop nginx 緩存
~]# systemctl stop keepalived服務器
##而且緩存目錄中已經有內容,而且三級目錄。負載均衡
~]# ls /data/web/9/d/7/6666cd76f96956469e7be39d750cc7d9
/data/web/9/d/7/6666cd76f96956469e7be39d750cc7d框架