部署準備
部署使用5個centos 7 的虛擬主機分別作為Router、Director、RS1、RS2及提供相關應用的server。 按照拓撲規劃部署相關的應用服務,實現mnp應用服務器能夠提供mysql、php-fpm、nfs等應用服務給RS1、RS2使用;RS1,RS2分別利用nginx部署wordpress和phpMyadmin;在Director上啟用lvs虛擬服務,分別分別負載wordpress和phpmyadmin到兩臺RS上;Router模擬網關,提供SNAT服務,并將內網的lvs虛擬服務DNAT映射到外網。基礎的網絡搭建如拓撲所示,本文不再列出。
另在配置前需要注意各服務器的時間是否同步,如時區不一致,可能會導致應用服務運行異常:
#更改時區命令
[root@director ~]# tzselect
......
You can make this change permanent for yourself by appending the line
TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.
Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai
#然后TZ='Asia/Shanghai'; export TZ添加到/etc/profile文件中,然后重新登錄登出即可
#向網絡ntp服務器同步時間
[root@director ~]# ntpdate ntp1.aliyun.com
配置router
配置提供內網的SNAT上網:
#清楚防火墻策略
[root@router ~]# iptables -F
#配置SNAT規則
[root@router ~]# iptables -t nat -I POSTROUTING -s 10.10.10.0/24 -j SNAT --to-source 192.168.0.81
#開啟路由轉發功能
[root@router ~]# sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1
配置DNAT將外網訪問192.168.0.81的80、8080 端口映射到內網的Lvs虛擬服務:
[root@router ~]# iptables -t nat -I PREROUTING -d 192.168.0.81 -p tcp --dport 80 -j DNAT --to-destination 10.10.10.10:80
[root@router ~]# iptables -t nat -I PREROUTING -d 192.168.0.81 -p tcp --dport 8080 -j DNAT --to-destination 10.10.10.10:8080
配置mpn server
關閉mpn server的firewalld和和設置selinux為permissive:
[root@mpn ~]# systemctl stop firewalld
[root@mpn ~]# setenforce 0
- 搭建mysql
安裝mariadb-server:
[root@mpn ~]# yum install -y mariadb-server
......
作為依賴被升級:
mariadb-libs.x86_64 1:5.5.56-2.el7
完畢!
編輯/etc/my.cnf配置文件:
[root@mpn ~]# vim /etc/my.cnf
skip-name-resolve=ON
innodb-file-per-table=ON
啟動mariadb server并對配置mysql root 的密碼:
[root@mpn ~]# systemctl start mariadb
[root@mpn ~]# mysql_secure_installation
......
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
.....
Thanks for using MariaDB
授權wordpress用戶:
[root@mpn ~]# mysql -uroot -p123456
MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> grant all on wordpress.* to 'wordpress'@'10.10.10.%' identified by 'magedeu';
Query OK, 0 rows affected (0.04 sec)
- 搭建php-fpm
安裝php程序包:
[root@mpn ~]# yum install epel-release -y
[root@mpn ~]# yum install php-fpm php-mysql php-mcrypt php-mbstring -y
編輯/etc/php-fpm.d/www.conf :
[root@mpn ~]# vim /etc/php-fpm.d/www.conf
#找到相應的參數并修改為如下配置
listen = 0.0.0.0:9000
listen.allowed_clients = 10.10.10.11 10.10.10.12
pm.status_path = /status
ping.path = /ping
php_value[session.save_path] = /var/lib/php/session
創建會話目錄:
[root@mpn ~]# mkdir -pv /var/lib/php/session
mkdir: 已創建目錄 "/var/lib/php/session"
[root@mpn ~]# chown apache:apache /var/lib/php/session
啟動php-fpm服務:
[root@mpn ~]# systemctl start php-fpm
- 搭建nfs服務
安裝nfs服務:
[root@mpn ~]# yum install -y nfs-utils
創建nfs共享目錄:
[root@mpn ~]# mkdir -pv /data/nginx/html
mkdir: 已創建目錄 "/data"
mkdir: 已創建目錄 "/data/nginx"
mkdir: 已創建目錄 "/data/nginx/html"
上傳并解壓縮wordpress和phpMyadmin到共享目錄:
[root@mpn ~]# ll /data/nginx/html/
總用量 8
drwxr-xr-x. 9 root root 4096 3月 29 2017 phpMyAdmin-4.0.10.20-all-languages
drwxr-xr-x. 5 root root 4096 2月 8 12:53 wordpress
創建對應的軟連接:
[root@mpn html]# ln -sv phpMyAdmin-4.0.10.20-all-languages/ pma
"pma" -> "phpMyAdmin-4.0.10.20-all-languages/"
[root@mpn html]# ln -sv wordpress/ blog
"blog" -> "wordpress/"
編輯/etc/exports:
[root@mpn ~]# vim /etc/exports
/data 10.10.10.*(ro,sync,root_squash)
分別啟動rpcbind和nfs-server服務:
[root@mpn ~]# systemctl start rpcbind
[root@mpn ~]# systemctl start nfs-server
搭建配置RS1、RS2
創建目錄/data:
[root@RS1 ~]# mkdir /data
掛載mpn的nfs共享目錄到/data:
[root@RS1 ~]# mount -t nfs 10.10.10.13:/data /data
#若掛載報錯說沒有mount.nfs程序,需要按照nfs-utils
[root@RS1 ~]# yum install -y nfs-utils
安裝nginx程序:
[root@RS1 ~]# yum install -y nginx
修改/etc/nginx/nginx.conf文件和編輯生成/etc/nginx/conf.d/services.conf:
[root@RS1 ~]# vim /etc/nginx/nginx.conf
#在server配置段中將這下面兩行注釋掉;
# listen 80 default_server;
# listen [::]:80 default_server;
[root@RS1 ~]# vim /etc/nginx/conf.d/services.conf
server {
listen 80;
server_name www.ilinux.io;
index index.html index.php;
location / {
root /data/nginx/html;
}
location ~* \.php$ {
fastcgi_pass 10.10.10.13:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /data/nginx/html/$fastcgi_script_name;
}
}
關閉firewalld,設置selinux為permissive:
[root@RS1 ~]# systemctl stop firewalld
[root@RS1 ~]# setenforce 0
啟動nginx服務:
[root@RS1 ~]# systemctl start nginx
隨后創建lvs-dr配置腳本,內容如下:
#!/bin/bash
#
vip=10.10.10.10.
mask='255.255.255.255'
case $1 in
start)
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig lo:0 $vip netmask $mask broadcast $vip up
route add -host $vip dev lo:0
;;
stop)
ifconfig lo:0 down
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
;;
*)
echo "Usage $(basename $0) start|stop"
exit 1
;;
esac
執行腳本:
[root@RS1 ~]# bash -x rs.sh start
+ vip=10.10.10.10
+ mask=255.255.255.255
+ case $1 in
+ echo 1
+ echo 1
+ echo 2
+ echo 2
+ ifconfig lo:0 10.10.10.10 netmask 255.255.255.255 broadcast 10.10.10.10 up
+ route add -host 10.10.10.10 dev lo:0
配置director
安裝ipvsadm命令工具:
[root@director ~]# yum install -y ipvsadm
創建lvs-dr配置腳本:
[root@director ~]# vim director.sh
#!/bin/bash
#
vip='10.10.10.10.'
iface='ens33:0'
mask='255.255.255.255'
port='80'
rs1='10.10.10.11'
rs2='10.10.10.12'
scheduler='rr'
type='-g'
case $1 in
start)
ifconfig $iface $vip netmask $mask broadcast $vip up
iptables -F
ipvsadm -A -t ${vip}:${port} -s $scheduler
ipvsadm -a -t ${vip}:${port} -r ${rs1} $type -w 1
ipvsadm -a -t ${vip}:${port} -r ${rs2} $type -w 1
;;
stop)
ipvsadm -C
ifconfig $iface down
;;
*)
echo "Usage $(basename $0) start|stop"
exit 1
;;
esac
執行腳本:
[root@director ~]# bash -x director.sh start
+ vip=10.10.10.10
+ iface=ens33:0
+ mask=255.255.255.255
+ port=80
+ rs1=10.10.10.11
+ rs2=10.10.10.12
+ scheduler=rr
+ type=-g
+ case $1 in
+ ifconfig ens33:0 10.10.10.10 netmask 255.255.255.255 broadcast 10.10.10.10 up
+ iptables -F
+ ipvsadm -A -t 10.10.10.10:80 -s rr
+ ipvsadm -a -t 10.10.10.10:80 -r 10.10.10.11 -g -w 1
+ ipvsadm -a -t 10.10.10.10:80 -r 10.10.10.12 -g -w 1
關閉firewalld:
[root@director ~]# systemctl stop firewalld
7、測試訪問
通過外網訪問192.168.0.81來訪問相關應用:
在RS2上停用nginx服務,測試訪問效果:
在client上測試訪問:
[root@client ~]# for i in {1..10};do curl http://192.168.0.81/index.html;done<h1>This is RS Server</h1>
curl: (7) Failed connect to 192.168.0.81:80; Connection refused
<h1>This is RS Server</h1>
curl: (7) Failed connect to 192.168.0.81:80; Connection refused
<h1>This is RS Server</h1>
curl: (7) Failed connect to 192.168.0.81:80; Connection refused
<h1>This is RS Server</h1>
curl: (7) Failed connect to 192.168.0.81:80; Connection refused
<h1>This is RS Server</h1>
curl: (7) Failed connect to 192.168.0.81:80; Connection refused
如上述結果,負載到正常運行的RS上的請求依舊能正常訪問,但是負載到異常的服務器上的連接會請求失敗。為了讓director能夠檢測后端提供的應用服務是否存活,并根據其存活的狀態自動增減ipvsadm的RS服務器,可在director上安裝ldirectord來實現此需求。具體配置如下:
在director上安裝ldirectord
[root@director ~]# yum install ldirectord-3.9.5-3.1.x86_64.rpm
復制示例配置文件到指定目錄:
[root@director ~]# cp /usr/share/doc/ldirectord-3.9.5/ldirectord.cf /etc/ha.d/
編輯/etc/ha.d/ldirectord-cf文件:
[root@director ~]# vim /etc/ha.d/ldirectord.cf
virtual=10.10.10.10:80
real=10.10.10.11:80 gate
real=10.10.10.12:80 gate
fallback=127.0.0.1:80 gate #可在director配置一個提示頁面作為維護頁面提示
service=http
scheduler=rr
#persistent=600
#netmask=255.255.255.255
protocol=tcp
checktype=negotiate
checkport=80
request="index.html"
啟動ldirectord服務:
[root@director ~]# systemctl status ldirectord
此時在R1或RS2上停用nginx服務,director會自動檢測其對應的應用服務是否存活,如果檢測失敗,則刪除在ipvsadm中的real server條目;當檢測恢復成功時,ldirectord會將對應的real server自動增加到ipvsadmn條目中。