Haproxy是目前比較流行的一種集羣調度工具
Haproxy 與LVS、Nginx的比較
LVS性能最好,可是搭建相對複雜
Nginx的upstream模塊支持集羣功能,可是對集羣節點健康檢查功能不強,性能沒有Haproxy好
.
性能特性
單進程、事件驅動模型顯著下降了上下文切換的開銷及內存佔用。
事件檢查器(eventchecker)容許其在高併發鏈接中對任何鏈接的任何事件實現即時探測。
在任何可用的狀況下,單緩衝(singlebuffering)機制能以不復制任何數據的方式完成讀寫操做,這會節約大量的CPU時鐘週期及內存帶寬;
MRU內存分配器在固定大小的內存池中可實現即時內存分配,這可以顯著減小建立一個會話的時長;html
.
haproxy與各負載均衡器的區別node
.
能夠實現linux
負載均衡經常使用調度算法nginx
[root@centos1 /]# yum -y install pcre-devel zlib-devel [root@centos1 /]# tar zxf nginx-1.6.2.tar.gz [root@centos1 /]# cd nginx-1.6.2 [root@centos1 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install [root@centos1 nginx-1.6.2]# useradd -M -s /sbin/nologin nginx [root@centos1 nginx-1.6.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ [root@centos1 nginx-1.6.2]# echo "node_1"> /usr/local/nginx/html/index.html
. 啓動nginx服務 [root@centos3 nginx-1.6.2]# nginx . 創建防火牆規則 [root@centos3 nginx-1.6.2]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
.
(haproxy)web
掛載 [root@centos1 /]# mount /dev/cdrom /media/
.
編譯安裝haproxyredis
首先安裝兩個支持包 [root@centos1 /]# yum -y install pcre-devel bzip2-devel
.
卸載切換光盤haproxy算法
[root@centos1 /]# umount /dev/cdrom [root@centos1 /]# mount /dev/cdrom /media/ [root@centos1 /]# cd /media/ [root@centos1 /]# tar zxf haproxy-1.4.24.tar.gz -C /usr/src/ [root@centos1 /]# cd /usr/src/haproxy-1.4.24/ [root@centos1 /]# make TARGET=linux26 [root@centos1 /]# make install
.
haproxy服務器的配置vim
首先創建haproxy的配置文件 [root@centos1 /]# mkdir /etc/haproxy
.
拷貝配置文件的樣本複製到/etc/haproxy目錄下centos
[root@centos1 /]# cp examples/haproxy.cfg /etc/haproxy/ . [root@centos1 /]# vim /etc/haproxy/haproxy.cfg
.緩存
# this config needs haproxy-1.1.28 or haproxy-1.2.1 global log /dev/log local0 info //爲了把info和notice日誌分別放到不一樣的文件方便查看 log /dev/log local0 notice #log loghost local0 info maxconn 4096 //最大鏈接數 #chroot /usr/share/haproxy uid 99 //用戶uid gid 99 //用戶gid pidfile /var/run/haproxy.pid //添加pid文件的路徑以及文件名 daemon 後臺運行 #debug #quiet defaults log global //應用全局配置日誌格式 mode http //模式爲http協議 option httplog //檢查節點的失敗次數 option dontlognull retries 3 //連續達到三次則認爲節點不可達 redispatch maxconn 2000 //最大鏈接數2000 contimeout 5000 //鏈接超時5000 clitimeout 50000 //客戶端服務器差事時間都是50000 srvtimeout 50000 option httpclose //關閉客戶端請求 listen webcluster 0.0.0.0:80 //家庭地址及端口 0.0.0.0爲自動檢測 option httpchk GET /index.html //檢查index.html文件 balance roundrobin //負載均衡的調度算法採用輪詢算法 server inst1 192.168.1.20:80 check inter 2000 fall 3 //定義兩個節點的地址和端口健康檢查三次 server inst2 192.168.1.30:80 check inter 2000 fall 3
.
[root@centos1 /]# cp examples/haproxy.init /etc/init.d/haproxy [root@centos1 /]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy [root@centos1 /]# chmod +x /etc/init.d/haproxy [root@centos1 /]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
開啓haproxy服務 [root@centos1 /]# /etc/init.d/haproxy start
測試高可用
.
配置haproxy日誌
Haproxy的日誌默認是保存到系統的syslog中,查看起來不方便,因此咱們在生產環境中能夠將日誌單獨存儲到不一樣的文件中,配置以下
首先修改配置文件,主要改下面的部分
[root@centos1 /]# vim /etc/haproxy/haproxy.cfg log /dev/log local0 info log /dev/log local0 notice
.
這兩行的做用是將info和notice的日誌分別記錄到不一樣的文件中
而後修改rsyslog配置,將haproxy相關的配置獨立定義到haproxy.conf,並放到/etc/rsyslog.d下,rsyslog啓動時會自動加載此目錄下全部的配置文件。
[root@centos1 /]# touch /etc/rsyslog.d/haproxy.conf [root@centos1 /]# vim /etc/rsyslog.d/haproxy.conf
.
if ($programname == 'haproxy' and $syslogseverity-text == 'info') then -/var/log/haproxy/haproxy-info.log & ~ if ($programname == 'haproxy' and $syslogseverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log & ~
.
如下內容是將info和notice日誌放到不一樣的文件中
而後從新啓動rsyslog服務 [root@centos1 /]# service rsyslog restart
.
測試日誌信息
在客戶機訪問網站以後,可使用tail -f /var/log/haproxy/haproxy-info.log即時查看日誌 [root@centos1 /]# tail -l /var/log/haproxy/haproxy-info.log
.
實現haproxy的高可用配置keepalived
.
掛載 [root@centos1 /]# umount /dev/cdrom /media/ [root@centos1 /]# mount /dev/cdrom /media/ .
安裝相關軟件 [root@centos1 /]# yum -y install kernel-devel openssl-devel popt-devel
.
切換光盤
[root@centos1 /]# umount /dev/cdrom /media/ [root@centos1 /]# mount /dev/cdrom /media/ [root@centos1 /]# cd /media/ [root@centos1 /]# tar zxf keepalived-1.2.13.tar.gz -C /usr/src/
.
編譯安裝
[root@centos1 /]# cd /usr/src/keepalived-1.2.13/ [root@centos1 /]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/ && make && make install
.
使用keepalived服務
[root@centos1 /]# chkconfig --add keepalived [root@centos1 /]# chkconfig keepalived on
.
配置住調度器(注:若是haproxy配置指向了web節點那麼這步可不作,#表示註釋掉不生效,haproxy指向web比keepalived指向web的切換訪問速度快)
.
[root@centos1 /]# vim /etc/keepalived/keepalived.conf
.
! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id R1 } vrrp_instance VI_1 { state MASTER interface eth1 virtual_router_id 1 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.99 } } #virtual_server 192.168.1.99 80 { # delay_loop 6 # lb_algo rr # lb_kind DR # persistence_timeout 50 # protocol TCP # # real_server 192.168.1.20 80 { # weight 1 # TCP_CHECK { # connect_port 80 # connect_timeout 3 # nb_get_retry 3 # dalay_bofore_retry 3 # } # } # real_server 192.168.1.30 80 { # weight 1 # TCP_CHECK { # connect_port 80
.
重啓keepalived
[root@centos1 /]# service keepalived start [root@centos1 /]# ip addr show dev eth1
.
兩臺haproxy一樣操做,從keeplived有三個地方不一樣:
優先級,調度名稱,熱備狀態
route-id R2
state BACKUP
priority 99
.
爲了方即可用scp命令
.
如不生效重啓haproxy的命令:
[root@centos1 /]# /etc/init.d/haproxy restart
.
斷掉一臺haproxy測試,訪問依舊