-----client---------haproxy-------nginx1---------nginx2------html
192.168.1.250 192.168.1.1 192.168.1.10 192.168.1.20linux
1、安裝Nginxnginx
[root@localhost ~]# yum -y install pcre-devel zlib-develweb
[root@localhost ~]# useradd -M -s /sbin/nologin nginxredis
[root@localhost ~]# tar -zxvf nginx-1.6.0.tar.gz -C /usr/src/算法
[root@localhost ~]# cd /usr/src/nginx-1.6.0/vim
[root@localhost nginx-1.6.0]# ./configure --服務器
prefix=/usr/local/nginx --user=nginx --group=nginx --with-fileaio負載均衡
--with-http_stub_status_module --with-http_gzip_static_modulesocket
--with-http_flv_module --with-http_ssl_module
[root@localhost nginx-1.6.0]# make && make install
[root@localhost ~]# ln -s /usr/local/nginx/sbin/*
/usr/local/sbin/
[root@localhost ~]# nginx -t
[root@localhost ~]# nginx
[root@localhost ~]# netstat -anpt | grep 80
[root@localhost ~]# killall -s HUP nginx //從新加載
[root@localhost ~]# killall -s QUIT nginx //關閉服務
[root@localhost ~]# nginx
驗證:
web-1:
[root@localhost ~]#echo "welcome to 192.168.1.20 web server" >
/usr/local/nginx/html/index.html
web-2:
[root@localhost ~]#echo "welcome to 192.168.1.30 web server" >
/usr/local/nginx/html/index.html
[root@localhost ~]# firefox http://localhost/ &
2、安裝haproxy
一、安裝
[root@localhost ~]# yum -y install pcre-devel zlib-devel
[root@localhost ~]# tar -zxvf haproxy-1.4.24.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/haproxy-1.4.24/
[root@localhost ~]# make TARGET=linux26
PREFIX=/usr/local/haproxy
注意:linux26 是指linux 的內核版本號。
[root@localhost ~]# make install PREFIX=/usr/local/haproxy
二、配置haproxy
[root@localhost ~]# mkdir /etc/haproxy
[root@localhost ~]# cp /usr/src/haproxy-
1.4.24/examples/haproxy.cfg /etc/haproxy/
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
修改:
global
log 127.0.0.1 local0 //配置日誌記錄,local0 爲日誌設備,默認
存放到系統日誌
log 127.0.0.1 local1 notice //notice 爲日誌級別,一般有7 個級別
#log loghost local0 info
maxconn 4096 //默認最大鏈接數,需考慮ulimit-n 限制:
可增長ulimit-n 819200 #ulimit 的數量限制
chroot /usr/share/haproxy //運行路徑
uid 99
gid 99
#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 stats
mode http
bind :6677
stats enable
stats hide-version
stats uri /haproxyadmin?stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE
listen webcluster 0.0.0.0:80 //定義集羣名、監聽地址及端口
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 //最後加
backup 表示備份借點
注意:
若是啓動時出現報錯:/haproxy.main()] Cannot
chroot(/usr/share/haproxy)
則手動建立:
[root@localhost ~]# mkdir /usr/share/haproxy
若是啓動時出現報錯:Starting proxy cacti: cannot bind socket
則執行:
[root@localhost ~]# sysctl -e net.ipv4.ip_nonlocal_bind=1
三、啓動haproxy
[root@localhost ~]# ln -s /usr/local/haproxy/sbin/* /usr/sbin/ //
注意軟連接的目錄
[root@localhost ~]# cp /usr/src/haproxy-
1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost ~]# chmod +x /etc/init.d/haproxy
[root@localhost ~]# /etc/init.d/haproxy start
[root@localhost ~]# /etc/init.d/haproxy status
[root@localhost ~]# netstat -anp | grep haproxy //佔用的也是TCP 的
80 端口
[root@localhost ~]# chkconfig --add haproxy
[root@localhost ~]# chkconfig haproxy on
http://192.168.56.200:6677/haproxyadmin?stats 查看集羣的狀態
四、驗證:
客戶端輸入:
http://192.168.1.1/
斷開其中一個節點,再訪問:
http://192.168.1.1/
五、設置haproxy 日誌
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
修改:
log 127.0.0.1 local3 //設置haproxy 日誌級別爲3
[root@localhost ~]# vim /etc/rsyslog.d/haproxy.conf
添加:
$ModLoad imudp //加載模塊
$UDPServerRun 514 //接收udp 的514 端口發送過來的日誌
local3.* /var/log/haproxy.log //定義haproxy 日誌文件
[root@localhost ~]# vim /etc/sysconfig/rsyslog
修改:
SYSLOGD_OPTIONS="-c 2 -r -m 0" //容許遠程寫入
[root@localhost ~]# /etc/init.d/rsyslog restart
[root@localhost ~]# /etc/init.d/haproxy restart
驗證:
[root@localhost ~]# tail -f /var/log/haproxy.log //查看日誌
3、驗證:
客戶端輸入:
http://192.168.1.1/index.html
不停地刷新
查看:
[root@localhost ~]# tail -f /var/log/haproxy/haproxy.log
日誌會記錄客戶端訪問信息
日誌會記錄haproxy 啓動/中止信息
haproxy+Keepalived
編譯安裝keepalived
[root@localhost keepalived-1.2.13]#./configure --prefix=/ --withkernel-
dir=/usr/src/kernels/2.6.32-431.el6.x86_64/
[root@localhost keepalived-1.2.13]# make && make install
[root@localhost ~]#chkconfig --add keepalived
[root@localhost ~]#chkconfig keepalived on
[root@localhost ~]#cp /etc/keepalived/keepalived.conf
/etc/keepalived/keepalived.conf.bak
[root@localhost ~]#vim /etc/keepalived/keepalived.conf
[root@localhost conf]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email { //通知電子郵件
}
vrrp_instance VI_1 { //VRRP 熱備
state MASTER state BACKUP //熱備狀態master 爲主,backup 爲輔
nopreempt //不搶佔,master 恢復後不會轉移
interface eth0 //承載VIP 的物理接口
virtual_router_id 51 //虛擬路由編號,每組一個
priority 100 priority 55 //優先級,越大越優先
advert_int 1 //心跳頻率,單位秒
authentication { //認證信息,每組內一致
auth_type PASS //認證類型
auth_pass 1111 //認證字符串
}
virtual_ipaddress { //漂移地址VIP。能夠有多個
192.168.56.10
}
notify_master "/etc/init.d/haproxy start" //成爲MASTER 以後執
行的動做
notify_backup "/etc/init.d/haproxy stop" //成爲BACKUP 以後執行
的動做
notify_fault "/etc/init.d/haproxy stop" //FAULT 以後執行的
動做
}
[root@localhost ~]#/etc/init.d/keepalived start
[root@localhost ~]#ip addr show
inet 192.168.56.201/24 brd 192.168.56.255 scope global eth0
inet 192.168.56.10/32 scope global eth0
[root@localhost ~]#netstat -anput | grep 80