負載均衡排程器Haproxy
- Web叢集排程器
- Haproxy
- Haproxy排程算法
- Haproxy部署
Web叢集排程器
目前常見的Web叢集排程器為軟體和硬體,軟體通常使用開源的LVS、Haproxy、Nginx,硬體一般使用比較多的是F5,也有很多人使用國内的一些鏟平,比如綠盟等。
Haproxy
LVS在企業中抗負載能力很強,但是存在不足,它不支援正則處理,不能實作動靜分離并且對于大型網站而言,LVS的實施配置很複雜,維護成本比較高,但是Haproxy可提供高可用、負載均衡以及基于TCP和HTTP應用的代理軟體,适用于負載打的Web站點,運作在硬體上可支援大量的連接配接請求。
Haproxy排程算法
- RR(Round Robin):輪詢排程,輪詢配置設定各節點使用者通路,可以實作負載均衡
- LC(Least Connections):最小連接配接數算法,更具後端的節點連接配接數大小動态配置設定前端請求
- SH(Source Hashing):基于通路排程算法,用于一些在伺服器端由Session會話記錄時,可以基于來源的ip、Cookie等做叢集排程,可以實作會話保持,但當IP通路量非常大時會引起負載不均衡,部分節點通路量大,影響業務
Haproxy部署
- Haproxy
[[email protected] opt]# yum install -y pcre-devel bzip2-devel gcc gcc-c++ make //安裝編譯環境
[[email protected] opt]# cd haproxy-1.5.19/
[[email protected] haproxy-1.5.19]# make TARGET=linux2628 ARCH=x86_64
[[email protected] haproxy-1.5.19]# make install
install -d "/usr/local/sbin"
install haproxy "/usr/local/sbin"
install -d "/usr/local/share/man"/man1
install -m 644 doc/haproxy.1 "/usr/local/share/man"/man1
install -d "/usr/local/doc/haproxy"
for x in configuration architecture haproxy-en haproxy-fr; do \
install -m 644 doc/$x.txt "/usr/local/doc/haproxy" ; \
done
[[email protected] haproxy-1.5.19]# mkdir /etc/haproxy
[[email protected] haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/
[[email protected] haproxy-1.5.19]# cd /etc/haproxy/
[[email protected] haproxy]# vim haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
log 127.0.0.1 local0 info
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096 //最大連接配接數
#chroot /usr/share/haproxy
uid 99 //使用者UID
gid 99 //使用者GID
daemon //守護程序模式
#debug
#quiet
defaults
log global //定義日志為global配置中的日志
mode http //模式為http
option httplog //采用http日志格式記錄
option dontlognull //不記錄健康檢查日志資訊
retries 3 //檢查節點伺服器失敗次數,連續失敗了三次,則認為不可用
redispatch //伺服器負載很高時,自動結束目前隊列處理比較久的連接配接
maxconn 2000 //最大連接配接數
contimeout 5000 //連接配接逾時時間
clitimeout 50000 //用戶端逾時時間
srvtimeout 50000 //伺服器逾時時間
listen webcluster 0.0.0.0:80
option httpchk GET /test.html //檢查伺服器html檔案
balance roundrobin //負載均衡排程算法使用輪詢
server inst1 192.168.20.22:80 cookie app1inst1 check inter 2000 fall 3 //定義節點
server inst1 192.168.20.33:80 cookie app1inst2 check inter 2000 fall 3
[[email protected] haproxy]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
[[email protected] haproxy]# cd /etc/init.d/
[[email protected] init.d]# chmod +x haproxy
[[email protected] init.d]# chkconfig --add /etc/init.d/haproxy
[[email protected] init.d]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[[email protected] ~]# service haproxy start //啟動
Starting haproxy (via systemctl): [ 确定 ]
- Nginx1
[[email protected] opt]# yum install -y pcre-devel zlib-devel gcc gcc-c++ make
[[email protected] opt]# useradd -M -s /sbin/nologin nginx
[[email protected] opt]# tar zxvf nginx-1.12.2.tar.gz -C /opt/
[[email protected] opt]# cd nginx-1.12.2/
[[email protected] nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[[email protected] nginx-1.12.2]# make && make install
[[email protected] nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[[email protected] nginx-1.12.2]# nginx
[[email protected] nginx-1.12.2]# echo "this is tom web" > /usr/local/nginx/html/test.html
- Nginx2
[[email protected] opt]# yum install -y pcre-devel zlib-devel gcc gcc-c++ make
[[email protected] opt]# useradd -M -s /sbin/nologin nginx
[[email protected] opt]# tar zxvf nginx-1.12.2.tar.gz -C /opt/
[[email protected] opt]# cd nginx-1.12.2/
[[email protected] nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[[email protected] nginx-1.12.2]# make && make install
[[email protected] nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[[email protected] nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[[email protected] nginx-1.12.2]# nginx
[[email protected] nginx-1.12.2]# echo "this is jerry web" > /usr/local/nginx/html/test.html