####Haproxy##########(http代理)###javascript
準備三臺虛擬機php
yum install haproxy -ycss
cd /etc/haproxy/html
vim haproxy.cfg 前端
/etc/init.d/haproxy startjava
vim haproxy.cfg web
將前端和後端的註釋vim
#---------------------------------------------------------------------後端
# main frontend which proxys to the backends瀏覽器
#---------------------------------------------------------------------
#frontend main *:5000
# acl url_static path_beg -i /static /p_w_picpaths /javascript /stylesheets
# acl url_static path_end -i .jpg .gif .png .css .js
# use_backend static if url_static
# default_backend app
#---------------------------------------------------------------------
# static backend for serving up p_w_picpaths, stylesheets and such
#---------------------------------------------------------------------
#backend static
# balance roundrobin
# server static 127.0.0.1:4331 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
#backend app
# balance roundrobin
# server app1 127.0.0.1:5001 check
# server app2 127.0.0.1:5002 check
# server app3 127.0.0.1:5003 check
# server app4 127.0.0.1:5004 check
listen lyitx *:80
balance roundrobin
server web1 172.25.50.30:80 check
server web2 172.25.50.40:80 check
/etc/init.d/haproxy start
netstat -anplt##能夠看到80端口在haproxy上
開啓sever4,server3 的httpd服務,寫個測試頁面
在真機上curl測試
[root@real Desktop]# curl 172.25.50.10
<h1>server3.example.com</h1>
[root@real Desktop]# curl 172.25.50.10
Server4.example.com
[root@real Desktop]# curl 172.25.50.10
<h1>server3.example.com</h1>
[root@real Desktop]# curl 172.25.50.10
Server4.example.com
##############監控頁面添加認證####################
listen admin *:8080
stats enable
stats uri /status
stats auth admin:lyitx##admin是登錄的用戶名lyitx是密碼
stats refresh 5s
listen lyitx *:80
balance roundrobin
server web1 172.25.50.30:80 check
server web2 172.25.50.40:80 check
/etc/init.d/haproxy reload
再在瀏覽器上;
172.25.50.10:8080/status
/////////////設置先後端//////////////
listen admin *:8080
stats enable
stats uri /status
stats auth admin:lyitx
stats refresh 5s
frontend lyitx *:80
default_backend app
backend static
balance roundrobin
server web1 172.25.50.30:80 check
backend app
balance roundrobin
server web1 172.25.50.40:80 check
再在瀏覽器上;
172.25.50.10:8080/status
//////////////////////動靜分離///////////////////////////////
vim haproxy.cfg
listen admin *:8080
stats enable
stats uri /status
stats auth admin:lyitx
stats refresh 5s
frontend lyitx *:80
acl url_static path_beg -i /p_w_picpaths
acl url_static path_end -i .jpg .gif .png
use_backend static if url_static
default_backend app
backend static
balance roundrobin
server web1 172.25.50.30:80 check
backend app
balance roundrobin
server web2 172.25.50.40:80 check
[root@server3 html]# mkdir p_w_picpaths
[root@server3 html]# ls
p_w_picpaths index.html
[root@server3 html]# cd p_w_picpaths/
[root@server3 p_w_picpaths]# ls
OSI.gif doggyt.jpg
在瀏覽器中:172.25.50.10/p_w_picpaths/doggy.jpg
###########ACL+地址轉發+重定向################
listen admin *:8080
stats enable
stats uri /status
stats auth admin:lyitx
stats refresh 5s
frontend lyitx *:80
acl url_static path_beg -i /p_w_picpaths
acl url_static path_end -i .jpg .gif .png
acl badhost src 172.25.50.250#設置禁止訪問的ip。能夠是個網段的
block if badhost
errorloc 403 http://172.25.50.10:8000#錯誤代碼403的話,將地址轉發到10主機上(在這以前將10主機的httpd打開,並將端口轉換爲8000(配置文件的136行))
redirect location http://172.25.50.10:8000 if badhost#badhost重定向
use_backend static if url_static
default_backend app
backend static
balance roundrobin
server web1 172.25.50.30:80 check
測試:172.25.50.10
////////////////////讀寫分離/////////////////////////
server2和server3都安裝php
yum install php -y
在調度器server1上;
編輯配置文件:
vim haproxy.cfg
listen admin *:8080
stats enable
stats uri /status
stats auth admin:lyitx
stats refresh 5s
frontend lyitx *:80
acl url_static path_beg -i /p_w_picpaths
acl url_static path_end -i .jpg .gif .png
acl lyitx.com hdr_beg(host) -i lyitx.com
acl badhost src 172.25.50.250
acl read method GET
acl read method HEAD
acl write method PUT
acl write method POST
# block if badhost
# errorloc 403 http://172.25.50.10:8000
# redirect location http://172.25.12.10:8000 if badhost
redirect code 301 location http://www.lyitx.com if lyitx.com
use_backend app if write
default_backend static
backend static
balance roundrobin
server web1 172.25.50.30:80 check
backend app
balance roundrobin
server web2 172.25.50.40:80 check
/etc/init.d/haproxy reload
真機上發送upload
[root@real Desktop]# scp -r upload/ 172.25.50.30:/var/www/html/
[root@real Desktop]# scp -r upload/ 172.25.50.40:/var/www/html/
在server3和server4上都進行以下操做
[root@server3 html]# ls
index.html upload
[root@server3 html]# cd upload/
[root@server3 upload]# ls
index.php upload_file.php
[root@server3 upload]# mv * ..
[root@server3 upload]# ls
[root@server3 upload]# cd ..
[root@server3 html]# ls
index.html index.php upload upload_file.php
[root@server3 html]# chmod 777 upload
[root@server3 html]# ll
total 16
-rw-r--r-- 1 root root 33 Feb 19 23:57 index.html
-rw-r--r-- 1 root root 257 Mar 18 03:36 index.php
drwxrwxrwx 2 root root 4096 Mar 18 03:44 upload
-rw-r--r-- 1 root root 927 Mar 18 03:36 upload_file.php
[root@server3 html]# vim upload_file.php
&& ($_FILES["file"]["size"] < 2000000))
[root@server3 html]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@server3 html]# ls
index.html index.php upload upload_file.php
Server4和3從新啓動httpd
在真機添加上解析後,在瀏覽器上www.lyitx.com
Keepalived+haproxy
編輯主從調度器的keepalived配置文件
把haproxy配置文件進行以下配置:
Vim /etc/haproxy/haproxy.cfg
在主調度器上:
[root@server1 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script check_haproxy {
script "/opt/check_haproxy.sh"
interval 2
weight 2
}
global_defs {
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.50.100
}
track_script {
check_haproxy
}
}
[root@server2 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script check_haproxy {
script "/opt/check_haproxy.sh"
interval 2
weight 2
}
global_defs {
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.50.100
}
track_script {
check_haproxy
}
}
編寫配置腳本文件,主從調度器都須要進行以下配置
[root@server2 ~]# cat /opt/check_haproxy.sh
#!/bin/bash
/etc/init.d/haproxy status &> /dev/null || /etc/init.d/haproxy restart &> /dev/null
if [ $? -ne 0 ];then
/etc/init.d/keepalived stop &> /dev/null
fi
[root@server2 ~]# chmod 755 /opt/check_haproxy.sh 給定權限755
配置完成後。
在真機上測試:
[root@real50 Desktop]# curl 172.25.50.100
<h2>server4.example.com</h2>
[root@real50 Desktop]# curl 172.25.50.100
<h1>server3.example.com</h1>
[root@real50 Desktop]# curl 172.25.50.100
<h2>server4.example.com</h2>
[root@real50 Desktop]# curl 172.25.50.100
<h1>server3.example.com</h1>
Vip 是在server1上的
[root@server1 ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:06:13:fa brd ff:ff:ff:ff:ff:ff
inet 172.25.50.10/24 brd 172.25.50.255 scope global eth0
inet 172.25.50.100/32 scope global eth0
inet6 fe80::5054:ff:fe06:13fa/64 scope link
測試:將server1的網卡接口關閉,
[root@server1 ~]# ip link set down eth0
負載均衡調度依然正常,此時vip出如今server2主機上
[root@real50 Desktop]# curl 172.25.50.100
<h2>server4.example.com</h2>
[root@real50 Desktop]# curl 172.25.50.100
<h1>server3.example.com</h1>
[root@real50 Desktop]# curl 172.25.50.100
<h2>server4.example.com</h2>
[root@real50 Desktop]# curl 172.25.50.100
<h1>server3.example.com</h1>
[root@server2 ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:07:bb:e5 brd ff:ff:ff:ff:ff:ff
inet 172.25.50.20/24 brd 172.25.50.255 scope global eth0
inet 172.25.50.100/32 scope global eth0
inet6 fe80::5054:ff:fe07:bbe5/64 scope link
valid_lft forever preferred_lft forever
把網卡端口打開後,serevr1繼續接管vip,server2上的vip調轉。
Realsever
測試成功!!!!!