web3=192.168.158.118php
img2=192.168.158.120html
安裝配置過程linux
一、安裝配置過程,(兩臺Haproxy的安裝配置步驟同樣) haproxy下載地址:http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz #cd /usr/local/src #wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz #tar xf haproxy-1.4.24.tar.gz #cd haproxy-1.4.24 #make TARGET=linux26 ARCH=x86_64 #編譯參數參考README,其中TARGET是指定內核版本,ARCH指定CPU架構, #make install 二、安裝完畢後,建立配置文件和啓動文件。 #mkdir /etc/haproxy #cp examples/haproxy.cfg /etc/haproxy #cp examples/haproxy.init /etc/init.d/haproxy #chmod +x /etc/init.d/haproxy #ln -s /usr/local/sbin/haproxy /usr/sbin/
三、編輯配置文件(兩臺Haproxy配置文件相同)web
# vim /etc/haproxy/haproxy.cfg # this config needs haproxy-1.1.28 or haproxy-1.2.1 global log 127.0.0.1 local0 #日誌輸出配置,全部日誌都記錄在本機,經過local0輸出 log 127.0.0.1 local1 notice #log loghost local0 info maxconn 65535 #最大鏈接數 chroot /usr/local/ #改變當前工做目錄。 uid 99 #所屬用戶的uid gid 99 #所屬運行的gid daemon #之後臺形式運行haproxy defaults log global mode http #默認的模式mode { tcp|http|health },tcp是4層,http是7層,health只會返回OK option dontlognull option httplog retries 3 #兩次鏈接失敗就認爲是服務器不可用 option redispatch #當serverId對應的服務器掛掉後,強制定向到其餘健康的服務器 option abortonclose #當服務器負載很高的時候,自動結束掉當前隊列處理比較久的連接 maxconn 65535 #默認的最大鏈接數 #timeout http-keep-alive 10s # timeout queue 1m contimeout 5000 #鏈接超時 clitimeout 50000 #客戶端超時 srvtimeout 50000 #服務器超時 timeout check 5s #心跳檢測超時 stats refresh 30s #統計頁面自動刷新時間 stats uri /stats #統計頁面url stats realm baison-test-Haproxy #統計頁面密碼框上提示文本 stats auth admin:admin123 #統計頁面用戶名和密碼設置 stats hide-version #隱藏統計頁面上HAProxy的版本信息 frontend www bind :80 #這裏建議使用bind *:80的方式,要否則作集羣高可用的時候有問題,vip切換到其餘機器就不能訪問了。 acl web hdr(host) -i www.baison.com.cn #acl後面是規則名稱,-i是要訪問的域名,若是訪問www.baison.com這個域名就分發到下面的webserver 的做用域。 acl img hdr(host) -i img.baison.com.cn #若是訪問img.baison.com.cn就分發到imgserver這個做用域。 use_backend webserver if web use_backend imgserver if img backend webserver #webserver做用域 mode http balance roundrobin #banlance roundrobin 輪詢,balance source 保存session值,支持static-rr,leastconn,first,uri等參數 option httpchk /index.html #檢測文件,若是分發到後臺index.html訪問不到就再也不分發給它。 server web01 192.168.158.116:80 check inter 2000 fall 3 weight 20 server web02 192.168.158.117:80 check inter 2000 fall 3 weight 10 server web03 192.168.158.118:80 check inter 2000 fall 3 weight 10 backend imgserver mode http option httpchk /index.php balance roundrobin server img01 192.168.149.119:80 check inter 2000 fall 3 server img02 192.168.149.120:80 check inter 2000 fall 3
四、啓動Haproxy服務,查看狀態。redis
#service haproxy start
#tar zxvf -1.2.7.tar.gz #cd keepalived-1.2.7 #./configure --prefix=/usr/local/keepalived #make #make install #cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ #cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ #cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ #mkdir /etc/keepalived #cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
#vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id LVS_DEVEL } vrrp_script chk_haproxy { script "/usr/local/keepalived/check_haproxy.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 5555 } track_script { chk_haproxy #監測haproxy進程狀態 } virtual_ipaddress { 192.168.150.222 } } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 52 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.150.223 } }
#vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id LVS_DEVEL } vrrp_script chk_haproxy { script "/usr/local/keepalived/check_haproxy.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 5555 } track_script { chk_haproxy #監測haproxy進程狀態 } virtual_ipaddress { 192.168.150.222 } } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.150.223 } }
#vim /usr/local/keepalived/check_haproxy.sh #!/bin/bash if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then /etc/init.d/haproxy start fi sleep 2 if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then /etc/init.d/keepalived stop fi
#servcie haproxy start #service keepalived start
#ip addr
echo 1024 60999 > /proc/sys/net/ipv4/ip_local_port_range echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout echo 4096 > /proc/sys/net/ipv4/tcp_max_syn_backlog echo 262144 > /proc/sys/net/ipv4/tcp_max_tw_buckets echo 262144 > /proc/sys/net/ipv4/tcp_max_orphans echo 300 > /proc/sys/net/ipv4/tcp_keepalive_time echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle echo 0 > /proc/sys/net/ipv4/tcp_timestamps echo 0 > /proc/sys/net/ipv4/tcp_ecn echo 1 > /proc/sys/net/ipv4/tcp_sack echo 0 > /proc/sys/net/ipv4/tcp_dsack