高可用keepalived+haproxy:
實驗環境:
clientIP為:172.18.254.149
VIRROUTEIP為:172.18.61.5
keepalived+haproxy機器1IP為172.18.61.1
keepalived+haproxy機器2IP為172.18.61.2
server1IP為172.18.61.3
server2IP為172.18.61.4
實驗示意圖:
實驗功能:
前端2臺haproxy對后端2臺server的web服務做反向代理,并且實現動靜態頁面的分離,而對于haproxy機器,我們借用keepalived做高可用,解決前端代理服務器的單點故障,并且讓haproxy只能通過本機登陸haproxy stats的管理界面。
實驗步驟:
1.為兩臺后端服務器安裝httpd服務,172.18.0.63和172.18.0.64的80端口分別提高html靜態頁面
yum -y install httpd
2.為172.18.61.1安裝httpd服務,并將端口改為8080,將該機器設置為haproxy的backup server
3.分別為172.18.61.2和172.18.61.4安裝httpd服務。并將它們的端口8080做成虛擬主機,部署動態內容,放入以.php結尾的文件作為測試界面
4.在兩臺frontend server 上分別安裝keepalived和haproxy
yum -y install haproxy keepalived
5.2臺機器都配置keepalived,編輯/etc/keepalived/keepalived.conf
第一臺做master,配置如下:
global_defs {
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id haproxy
}
vrrp_instance haproxy {
state MASTER
interface enp0s3
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.18.61.5/16
}
}
第二臺做backup,配置如下:
vrrp_instance haproxy {
state BACKUP
interface enp0s3
virtual_router_id 51
priority 95
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.18.61.5/16
}
}
啟動并測試keepalived,看IP地址是否能夠發生漂移。
6.配置兩臺機器的haproxy服務,并實現動靜分離,編輯/etc/haproxy/haproxy.cfg
frontend main
bind *:80
#acl invalid_src src 172.18.254.149
#block if invalid_src
#errorfile 403 /etc/haproxy/403.html
reqadd Via:\ yilei-dw3
#acl curl_agent hdr_sub(User-Agent) -i curl
#use_backend curlbe if curl_agent
acl phpapp path_end -i .php
use_backend dysrvs if phpapp
default_backend websrvs
backend dysrvs
balance roundrobin
server web3 172.18.61.4:8080 check
server web5 172.18.61.2:8080 check
backend websrvs
#balance hdr(User-Agent)
balance roundrobin
#cookie websrv insert nocache
#server web1 172.18.61.3:80 check cookie web1
server web1 172.18.61.3:80 check weight 1
#server web2 172.18.61.2:80 check cookie web2
#server web2 172.18.61.2:80 check redir http://www.baidu.com weight 2
server web2 172.18.61.4:80 check weight 1
server web4 172.18.61.1:8080 backup check
listen stats
bind 127.0.0.1:10080
stats enable
stats uri /haproxyadmin?stats
stats realm ‘haproxy auth’
stats auth yilei:centos
stats admin if TRUE
7.在client上測試。
原創文章,作者:yilei-dw3,如若轉載,請注明出處:http://www.www58058.com/76104