Haproxy Nginx cluster构建
-----client---------haproxy-------nginx1---------nginx2------
192.168.1.250 192.168.1.1 192.168.1.10 192.168.1.20
一、安裝Nginx
[root@localhost ~]# yum -y install pcre-devel zlib-devel
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tar -zxvf nginx-1.6.0.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.6.0/
[root@localhost nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --
with-http_gzip_static_module --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/ &
二、安裝haproxy
1、安裝
[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
2、配置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
3、啟動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 查看集群的狀態
4、驗證:
客戶端輸入:
http://192.168.1.1/
斷開其中一個節點,再訪問:
http://192.168.1.1/
5、設置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 //查看日志
不好使:
6、設置haproxy日志
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
修改:
注釋(兩行):
#log 127.0.0.1 local0
#log 127.0.0.1 local1 notice
添加(兩行):
log /dev/log local0 info //訪問信息
log /dev/log local0 notice //啟動信息
[root@localhost ~]# /etc/init.d/haproxy stop
[root@localhost ~]# /etc/init.d/haproxy start
[root@localhost ~]# 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
& ~
三、驗證:
[root@localhost ~]# /etc/init.d/rsyslog restart
客戶端輸入:
http://192.168.1.1/index.html
http://192.168.1.1/index.html
查看:
[root@localhost ~]# tail -f /var/log/haproxy/haproxy-info.log
日志會記錄客戶端訪問信息
[root@localhost ~]# tail -f /var/log/haproxy/haproxy-notice.log
日志會記錄haproxy啟動/停止信息
haproxy+Keepalived
編譯安裝keepalived
[root@localhost keepalived-1.2.13]#./configure --prefix=/ --with-kernel-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
轉載于:https://www.cnblogs.com/luoyan01/p/9734142.html
總結
以上是生活随笔為你收集整理的Haproxy Nginx cluster构建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 离开头成语有哪些啊?
- 下一篇: 沁园春雪作者是谁啊?