目錄
一、試驗部署
1、實驗要求
2、實驗準備
3、拓撲結構
4、網絡規劃
二、基礎設置
1、LAMP配置
2、HAProxy配置
3、Keepalived配置
三、測試
四、總結
一、試驗部署
1、實驗要求
(1) 動靜分離部署wordpress,動靜都要能實現負載均衡,要注意會話的問題;
(2) 給出設計拓撲,寫成博客;
(3)haproxy的設定要求:
(a)stats page,要求僅能通過本地訪問使用管理接口;
(b) 動靜分離;
(c) 分別考慮不同的服務器組的調度算法;
(4)haproxy高可用;
2、實驗準備
(1)實驗前關閉iptables和selinux;
(2)同步各個機器的時間;
3、拓撲結構
4、網絡規劃
由于電腦硬件問題,因此本實驗dynamic-web組與static-web組分別用一臺機器模擬;httpd虛擬主機實現;
客戶端ip:172.18.254.160;
Haproxy1(centos7-2):172.18.24.1;
Haproxy1(centos7-1):172.18.24.4;
KeepalivedVIP1:172.18.24.10;
KeepalivedVIP2:172.18.24.11;
dynamic-web1(centos6-1):172.18.24.2:80; dynamic-web2:172.18.24.2:8080;
static-web1(centos6-2):172.18.24.3:80;static-web2:172.18.24.3:8080;
Mysql(centos7-3):172.18.24.5;
二、基礎設置
1、LAMP配置
(a)配置centos7-3
yum install mariadb-server nfs-utils httpd
[root@localhost ~]#mysql –uroot –h127.0.0.1 #授權遠程用戶連接本機mysql
MariaDB [(none)]>create database wpdb;
MariaDB [(none)]> grant all privileges on wpdp.* to ‘shiyan’@’127.0.0.1’ identified by ‘shiyan’;
MariaDB [(none)]> grant all privileges on wpdp.* to ‘shiyan’@’localhost’ identified by ‘shiyan’;
MariaDB [(none)]> grant all privileges
on wpdp.* to ‘shiyan’@’172.18.24.2’ identified by ‘shiyan’;
MariaDB [(none)]> grant all privileges on wpdp.* to ‘shiyan’@’172.18.24.3’ identified by ‘shiyan’;
[root@localhost ~]#mkdir /data
[root@localhost ~]#cd /data
[root@localhost data]#
wget
ftp://172.18.0.1/pub/Sources/sources/httpd/wordpress-4.3.1-zh_CN.zip
[root@localhost data]#unzip
wordpress-4.3.1-zh_CN.zip
[root@localhost data]#chown -R mysql.mysql
/data/wordpress
[root@localhost data]#chmod -R o+w
wordpress
[root@localhost data]#vim /etc/exports #設置共享文件
/data/ 172.18.24.2 (rw)
/data/ 172.18.24.3 (rw)
[root@localhost data]#systemctl start nfs
[root@localhost data]#showmount -e
172.18.24.5
(b)配置centos6-1
[root@shiyan ~]#yum install httpd php php-mysql
[root@shiyan conf.d]#cd /etc/httpd/conf
[root@shiyan conf]#vim httpd.conf
#DocumentRoot “/var/www/html” #注銷該行
Listen 80
Listen 8080 #添加監聽端口
<VirtualHost *:8080> #創建虛擬主機
#
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /app/www/html/
ServerName www.staticweb2.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
<VirtualHost *:80>
#
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html/
ServerName www.staticweb1.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
[root@shiyan conf]#service httpd restart
[root@shiyan conf]#mkdir /app/www/html -pv
[root@shiyan ~]#mount 172.18.24.5:/data/
/app/www/html #掛載共享文件
[root@shiyan ~]#mount 172.18.24.5:/data/
/var/www/html
(c)centos6-2配置如上相同
2、HAProxy配置
(a)配置centos7-1
yum install haproxy
[root@localhost haproxy]#vim
/etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile
/var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket
/var/lib/haproxy/stats
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except
127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
frontend
eshop
bind *:80
acl phpapp path_end -i .php #動靜分離
use_backend phpsrvs if phpapp
default_backend
websrvs
compression type text/html
compression algo gzip
listen stats
bind :10080
stats enable
stats uri /haadmin?admin
stats realm ‘Haproxy\ admin’
stats auth admin:admin
stats hide-version
stats admin if TRUE
acl client src 172.18.254.160 #只允許本地訪問狀態頁面
block unless client
backend websrvs
balance source
server web1 172.18.24.2:80
check
server web2 172.18.24.3:80
check
hash-type consistent
backend phpsrvs
balance leastconn
server php1 172.18.24.2:8080
check
server php2 172.18.24.3:8080
check
hash-type consistent
[root@localhost ~]#systemctl haproxy start
3、Keepalived配置
(a)配置centos7-1
yum install keepalived
[root@localhost haproxy]#vim
/etc/keepalived/keepalived.conf
global_defs {
notification_email {
root@localhost
}
notification_email_from keeplived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id tes1t #修改為centos7-2的路由ID
vrrp_mcast_group4 224.0.100.24
}
vrrp_script ‘ngxstatus’ {
script “killall -0 nginx && exit 0 || exit 1”
interval 1
weight -5
}
vrrp_instance myr1 {
state MASTER #centos7-2 改為BACKUP
interface eth0 #centos7-2的網絡接口地址
virtual_router_id 67
priority 100 #centos7-2 優先級修改為98
advert_int 1
authentication {
auth_type PASS
auth_pass UIpYPPDI
}
track_script {
ngxstatus #執行腳本
}
virtual_ipaddress {
172.18.24.10/16 dev eth0 #虛擬IP,在centos7-2上要與之相對應的網卡
}
notify_master “/etc/keepalived/notify.sh master”
notify_backup “/etc/keepalived/notify.sh backup”
notify_fault
“/etc/keepalived/notify.sh fault”
——————————————————————————————————————————–
需要的腳本:
vim
/etc/keepalived/notify.sh
#!/bin/bash
#
contact=’root@localhost’
notify() {
mailsubject=”vrrp $(hostname) to
be $1″
mailbody=”$(hostname) to be
$1,vrrp transition, $(date).”
echo “$mailbody” | mail -s
“$mailsubject” $contact
}
case $1 in
master)
notify master
;;
backup)
notify backup
;;
fault)
notify fault
;;
*)
echo “Usage: $(basename $0)
master|backup|fault “
exit 1
;;
esac
———————————————————————————————————————-
[root@localhost keepalived]#systemctl restart keepalived
三、測試
通過網頁登錄172.18.24.10,裝載WordPress,然后對其進行增刪改查的操作。
四、總結
本實驗完成實現以下功能:
1、Haporxy主從高可用
2、Haproxy負載均衡
3、Haproxy 監控頁面訪問控制
4、動靜分離
5、會話保持
原創文章,作者:chenxu@magedu.com,如若轉載,請注明出處:http://www.www58058.com/76117