Haproxy代理WEB服務

來源:互聯網
上載者:User

標籤:haproxy

-----client---------haproxy-------nginx1---------nginx2------

192.168.1.250 192.168.1.1 192.168.1.10 192.168.1.20

一、安裝Nginx

[[email protected] ~]# yum -y install pcre-devel zlib-devel

[[email protected] ~]# useradd -M -s /sbin/nologin nginx

[[email protected] ~]# tar -zxvf nginx-1.6.0.tar.gz -C /usr/src/

[[email protected] ~]# cd /usr/src/nginx-1.6.0/

[[email protected] nginx-1.6.0]# ./configure --

prefix=/usr/local/nginx --user=nginx --group=nginx --with-fileaio

--with-http_stub_status_module --with-http_gzip_static_module

--with-http_flv_module --with-http_ssl_module

[[email protected] nginx-1.6.0]# make && make install

[[email protected] ~]# ln -s /usr/local/nginx/sbin/*

/usr/local/sbin/

[[email protected] ~]# nginx -t

[[email protected] ~]# nginx

[[email protected] ~]# netstat -anpt | grep 80

[[email protected] ~]# killall -s HUP nginx //重新載入

[[email protected] ~]# killall -s QUIT nginx //關閉服務

[[email protected] ~]# nginx

驗證:

web-1:

[[email protected] ~]#echo "welcome to 192.168.1.20 web server" >

/usr/local/nginx/html/index.html

web-2:

[[email protected] ~]#echo "welcome to 192.168.1.30 web server" >

/usr/local/nginx/html/index.html

[[email protected] ~]# firefox http://localhost/ &

二、安裝haproxy

1、安裝

[[email protected] ~]# yum -y install pcre-devel zlib-devel

[[email protected] ~]# tar -zxvf haproxy-1.4.24.tar.gz -C /usr/src/

[[email protected] ~]# cd /usr/src/haproxy-1.4.24/

[[email protected] ~]# make TARGET=linux26

PREFIX=/usr/local/haproxy

注意:linux26 是指linux 的核心版本號碼。

[[email protected] ~]# make install PREFIX=/usr/local/haproxy

2、配置haproxy

[[email protected] ~]# mkdir /etc/haproxy

[[email protected] ~]# cp /usr/src/haproxy-

1.4.24/examples/haproxy.cfg /etc/haproxy/

[[email protected] ~]# 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)

則手動建立:

[[email protected] ~]# mkdir /usr/share/haproxy

如果啟動時出現報錯:Starting proxy cacti: cannot bind socket

則執行:

[[email protected] ~]# sysctl -e net.ipv4.ip_nonlocal_bind=1

3、啟動haproxy

[[email protected] ~]# ln -s /usr/local/haproxy/sbin/* /usr/sbin/ //

注意軟連結的目錄

[[email protected]calhost ~]# cp /usr/src/haproxy-

1.4.24/examples/haproxy.init /etc/init.d/haproxy

[[email protected] ~]# chmod +x /etc/init.d/haproxy

[[email protected] ~]# /etc/init.d/haproxy start

[[email protected] ~]# /etc/init.d/haproxy status

[[email protected] ~]# netstat -anp | grep haproxy //佔用的也是TCP 的

80 連接埠

[[email protected] ~]# chkconfig --add haproxy

[[email protected] ~]# chkconfig haproxy on

http://192.168.56.200:6677/haproxyadmin?stats 查看叢集的狀態

4、驗證:

用戶端輸入:

http://192.168.1.1/

斷開其中一個節點,再訪問:

http://192.168.1.1/

5、設定haproxy 日誌

[[email protected] ~]# vim /etc/haproxy/haproxy.cfg

修改:

log 127.0.0.1 local3 //設定haproxy 記錄層級為3

[[email protected] ~]# vim /etc/rsyslog.d/haproxy.conf

添加:

$ModLoad imudp //載入模組

$UDPServerRun 514 //接收udp 的514 連接埠發送過來的日誌

local3.* /var/log/haproxy.log //定義haproxy 記錄檔

[[email protected] ~]# vim /etc/sysconfig/rsyslog

修改:

SYSLOGD_OPTIONS="-c 2 -r -m 0" //允許遠程寫入

[[email protected] ~]# /etc/init.d/rsyslog restart

[[email protected] ~]# /etc/init.d/haproxy restart

驗證:

[[email protected] ~]# tail -f /var/log/haproxy.log //查看日誌

三、驗證:

用戶端輸入:

http://192.168.1.1/index.html

不停地重新整理

查看:

[[email protected] ~]# tail -f /var/log/haproxy/haproxy.log

日誌會記錄用戶端訪問資訊

日誌會記錄haproxy 啟動/停止訊息


haproxy+Keepalived

編譯安裝keepalived

[[email protected] keepalived-1.2.13]#./configure --prefix=/ --withkernel-

dir=/usr/src/kernels/2.6.32-431.el6.x86_64/

[[email protected] keepalived-1.2.13]# make && make install

[[email protected] ~]#chkconfig --add keepalived

[[email protected] ~]#chkconfig keepalived on

[[email protected] ~]#cp /etc/keepalived/keepalived.conf

/etc/keepalived/keepalived.conf.bak

[[email protected] ~]#vim /etc/keepalived/keepalived.conf

[[email protected] 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 之後執行的

動作

}

[[email protected] ~]#/etc/init.d/keepalived start

[[email protected] ~]#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

[[email protected] ~]#netstat -anput | grep 80


本文出自 “11000174” 部落格,請務必保留此出處http://11010174.blog.51cto.com/11000174/1890827

Haproxy代理WEB服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.