標籤:pass out 9.png following front glob 方式 local 主伺服器
haproxy 1的設定檔,包括 keepalived 和 haproxy 的配置,分別如下:
【haproxy 1的keepalived 設定檔】 /etc/keepalived/keepalived.conf
global_defs {
router_id NodeB
}
vrrp_instance VI_1 {
state BACKUP #設定為主伺服器
interface ens33 #監測網路介面
virtual_router_id 51 #主、備必須一樣
priority 90 #(主、備機取不同的優先順序,主機值較大,備份機值較小,值越大優先順序越高)
advert_int 1 #VRRP Multicast廣播周期秒數
authentication {
auth_type PASS #VRRP認證方式,主備必須一致
auth_pass 1111 #(密碼)
}
virtual_ipaddress {
192.168.248.200
}
【haproxy 1的haproxy.conf 設定檔】 /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the ‘-r‘ option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the ‘listen‘ and ‘backend‘ sections will
# use if not designated in their block
#---------------------------------------------------------------------
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
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend web *:80
frontend web1 192.168.248.200:80
# bind 192.168.248.200:80
mode http
use_backend app
#---------------------------------------------------------------------
# static backend for serving up images, 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 192.168.248.144:80 check
server app2 192.168.248.146:80 check
【haproxy 2的keepalived 設定檔】
global_defs {
router_id NodeB
}
vrrp_instance VI_1 {
state BACKUP #設定為主伺服器
interface ens33 #監測網路介面
virtual_router_id 51 #主、備必須一樣
priority 90 #(主、備機取不同的優先順序,主機值較大,備份機值較小,值越大優先順序越高)
advert_int 1 #VRRP Multicast廣播周期秒數
authentication {
auth_type PASS #VRRP認證方式,主備必須一致
auth_pass 1111 #(密碼)
}
virtual_ipaddress {
192.168.248.200
}
【haproxy 2的haproxy.cfg】 /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the ‘-r‘ option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the ‘listen‘ and ‘backend‘ sections will
# use if not designated in their block
#---------------------------------------------------------------------
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
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend web *:80
frontend web1 192.168.248.200:80
# bind 192.168.248.200:80
mode http
use_backend app
#---------------------------------------------------------------------
# static backend for serving up images, 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 192.168.248.144:80 check
server app2 192.168.248.146:80 check
【網頁伺服器1 的http index檔案內容】
[[email protected] zhou]# yum install -y httpd
[[email protected] zhou]# echo "Hello I am nginx-backend 1. " > /var/www/html/index.html
[[email protected] zhou]# service httpd start
Redirecting to /bin/systemctl start httpd.service
[[email protected] zhou]# firewall-cmd --permanent --add-port=80/tcp
success
[[email protected] zhou]# firewall-cmd --reload
success
[[email protected] zhou]#
【網頁伺服器2 的http index檔案內容】
[[email protected] zhou]# yum install -y httpd
[[email protected] zhou]# echo "Hello I am nginx-backend 2. " > /var/www/html/index.html
[[email protected] zhou]# service httpd start
Redirecting to /bin/systemctl start httpd.service
[[email protected] zhou]# firewall-cmd --permanent --add-port=80/tcp
success
[[email protected] zhou]# firewall-cmd --reload
success
[[email protected] zhou]#
【haproxy 1的服務啟動】 haproxy 2與之相同。
keepalived -f /etc/keepalived/keepalived.conf
haproxy -f /etc/haproxy/haproxy.cfg
firewall-cmd --permenant --add-port=80/tcp
firewall-cmd --reload
【驗證結果】
如果關閉掉一個haproxy ,則不影響系統的正常工作,web網站還是可以正常訪問,並且是輪詢的結果。
haproxy + keepalived 實現網站高可靠