實驗:基於lvs-nat類型的負載均衡搭建WordPress
1.各軟件版本:
系統版本:CentOS Linux release 7.4.1708 (Core)
php版本:PHP 7.2
nginx版本:nginx-1.12.2
數據庫版本:MariaDB 10
WordPress版本:4.9.4
關閉防火墻與selinux
2.實驗架構及IP分配(架構圖略,後期有時間補):
調度器一臺:
安裝ipvsadm
VIP:192.168.11.128(模擬公網IP,域名www.ready.cn) DIP:172.16.142.128
realserver兩臺:
兩臺都安裝nginx+PHP,且保持配置一致
RIP1:172.16.142.11 RS2:172.16.142.22
安裝MariaDB
IP:172.16.142.33
存儲服務器一臺:
提供NFS存儲服務(略)
二、所需軟件安裝配置,並測試PHP是否正常解析
1.RS1和RS2安裝nginx+PHP:
a>安裝epel源 rpm -Uvh https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm b>yum安裝nginx yum install -y nginx c>yum安裝PHP rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm yum install -y yum install php72w-common php72w-fpm php72w-opcache php72w-gd php72w-mysqlnd php72w-mbstring php72w-pecl-redis php72w-pecl-memcached php72w-devel d>添加nginx server,創建網站目錄,將用戶請求結尾為.php的URL交給php-fpm解析,並創建phpinfo做頁面測試 vim /etc/nginx/conf.d/php.conf ###### server { listen 80; server_name www.ready.org; #因為是做試驗,所以將hosts中添加本地域名解析 location / { root html/php; #nginx配置中網站的默認根路徑為/usr/share/nginx/,這裏指定的是相對路徑 index index.html index.htm; } location ~ .*\.(php|php5)?$ { #以任意開頭以.php結尾的URI用此location做匹配 root html/php; fastcgi_pass 127.0.0.1:9000; #將請求交由fastcgi(php-fpm)處理 fastcgi_index index.php; include fastcgi.conf; } } ###### mkdir -p /usr/share/nginx/html/php/ #創建網站目錄(不是必須步驟,也可使用默認目錄) vim /usr/share/nginx/html/php/index.php ###### <?php phpinfo(); ?> ###### e>將nginx啟動並設置開機啟動 systemctl start nginx systemctl enable nginx f>開啟php-fpm並設置為開機啟動 g>瀏覽器訪問www.ready.org與www.ready.org/index.php,顯示以下界面便表示nginx與PHP已安裝成功。 然後刪除index.php文件,安全因素。 以上步驟不涉及負載均衡的使用,下面進行加入負載均衡服務器的配置測試 若是要將RS1、RS2並入負載均衡集群,則需將RS1、RS2設置為監聽本機80端口且將結尾為.php的URI交php-fpm解析 即修改/etc/nginx/nginx.conf將匹配PHP解析的location加入默認配置即可,修改如下: vim /etc/nginx/nginx.conf ######
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘ ‘$status $body_bytes_sent "$http_referer" ‘ ‘"$http_user_agent" "$http_x_forwarded_for"‘; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; server { listen 80; #server_name _; #我理解的是此負載均衡集群中沒有涉及域名解析,所以server_name可以省略 location / { root /usr/share/nginx/html/php; #我覺得root路徑盡量要用絕對路徑,最好是寫在location中,這樣便於管理,方便查閱 index index.php index.html index.htm; #不要忘了加index.php,不加會導致只輸入域名時返回403錯誤 } location ~ .*\.(php|php5)?$ { root /usr/share/nginx/html/php; index index.php; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } } } ######
2.安裝ipvsadm並配置
a>安裝ipvsadmyum install -y ipvsadm
b>配置lvs服務器,測試調度是否成功
ipvsadm -A -t 192.168.11.128:80 -s rr #添加tcp協議VIP,端口為80,調度算法為輪詢
ipvsadm -a -t 192.168.11.128:80 -r 172.16.142.11 -m #添加RS1,類型為net
ipvsadm -a -t 192.168.11.128:80 -r 172.16.142.22 -m #添加RS2,類型為net
-s 設置調度算法
rr 輪詢
wrr 加權輪詢
sh 原地址哈希調度,保證整個系統的出入口唯一
wlc 加權最小連接數調度
瀏覽器訪問192.168.11.128與192.168.11.128/index.php
若出現測試頁面即為配置成功
3.安裝並配置MariaDB
a>在172.16.142.33上yum安裝MariaDB(以下數據庫配置有很多安全隱患,待後續深入學習數據庫後會另起博文詳解數據庫的相關配置)vim /etc/yum.repos.d/mariadb.repo
######
[mariadb]
name = MariaDB
baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.2/centos7-amd64
gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1
######
yum install -y MariaDB-server
b>配置數據庫
mysql_secure_installation #初始化數據庫,設置root密碼,匿名用戶權限等
mysql -uroot -p123456 #登入數據庫進行用戶及庫的創建與配置
>GRANT ALL PRIVILEGES ON *.* TO ‘mysql‘@‘%‘IDENTIFIED BY ‘123456‘ WITH GRANT OPTION; #創建賬戶mysql,密碼為123456,並允許任意IP訪問此賬戶數據庫,權限為ALL
>CREATE DATABASE wordpress; #創建名為wordpress的庫,安裝完WordPress後會要求在數據庫創建庫,這裏事先創建好
>quit
4.安裝WordPress
a>將下載好的WordPress包解壓至已創建好的/usr/share/nginx/html/php目錄下
b>為模擬互聯網環境,將‘www.ready.cn 192.168.11.128‘hosts文件加入本地解析規則
c>瀏覽器輸入www.ready.cn,按提示步驟進行操作便可安裝完成
三、關於lvs-nat類型下ipvsadm的一些用法ipvsadm -ln --stats #狀態信息
例:
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes
-> RemoteAddress:Port
TCP 192.168.11.128:80 25 951 353 83843 2050036
-> 172.16.142.11:80 12 574 205 46157 1303372
-> 172.16.142.22:80 13 377 148 37686 746664
Conns 請求連接數
InPkts 入站的報文數
OutPkts 出站的報文數
InBytes 入站的字節數
OutBytes 出站的字節數
```
ipvsadm -ln --rate #每秒傳輸速率信息
ipvsadm -ln -c #當前站點訪問情況
例:
IPVS connection entries
pro expire state source virtual destination
TCP 14:55 ESTABLISHED 192.168.11.1:53035 192.168.11.128:80 172.16.142.22:80
TCP 14:55 ESTABLISHED 192.168.11.1:53016 192.168.11.128:80 172.16.142.11:80
TCP 01:46 TIME_WAIT 192.168.11.1:53029 192.168.11.128:80 172.16.142.11:80
TCP 01:09 TIME_WAIT 192.168.11.1:52990 192.168.11.128:80 172.16.142.11:80
這裏需要註意的是每次輸入網址訪問頁面時,並不一定只發送了一個請求,所以只訪問一次頁面時,也會出現多個RS在響應請求。另外這裏的state項可以幫助理解tcp/ip協議的“握手與揮手”
`ipvsadm -S -n #保存規則,一般是保存在/etc/sysconfig/ipvsadm`
例:ipvsadm -S -n > /etc/sysconfig/ipvsadm
也可使用systemctl stop ipvsadm.service做自動保存
`ipvsadm -C #清空規則`
四、缺陷
網站會有用戶上傳文件的需求,所以必須保證RS1和RS2上傳文件目錄保存同步或者共用一個存儲
可用inotify+rsync或者同時掛載NFS實現,這裏不再演示
實驗:基於lvs-nat類型的負載均衡搭建WordPress