haproxy1.8安裝配置

1 下載最新的版本的haproxy(最新版本爲1.8),此連接須要×××才能打開
下載連接爲:http://www.haproxy.org/download/1.8/src/
簡介
目前正在維護的版本有 1.4 1.5 1.6 1.7的 和正在測試的1.8版本。haproxy與keepalived同樣,重CentOS6.4之後開始被收錄到base倉庫,隨系統反向光盤直接提供,由此,咱們能夠看到他的市場佔有率和被大衆接受的程度,同時,他的可靠性也足以被驗證;haproxy是隨base倉庫提供的,但nginx沒有,還屬於epel倉庫纔有。也就意味着,咱們未來要配置使用haproxy要比使用nginx還要容易一些。至少歷來源方面來講如此;
2 源碼安裝
2.1 安裝依賴的軟件包
yum -y install gcc systemd-devel
2.2 解壓源碼包
tar zxvf haproxy-1.8.0.tar.gz
cd haproxy-1.8.0/
2.3 注意點
若是不安裝system-devel軟件包的話,進行make編譯的時候,報以下錯誤
haproxy1.8安裝配置
2.4 編譯安裝
make TARGET=linux3100 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1
TARGET=linux3100,這個值是根據系統當前版本而定,uname -r查詢當前內核的版本
haproxy1.8安裝配置
將haproxy安裝到/usr/local/haproxy目錄
make install PREFIX=/usr/local/haproxy
2.5 將haproxy命令複製到/usr/sbin目錄下
cp -rf /usr/local/haproxy/sbin/haproxy /usr/sbin/
查看haproxy的版本爲1.8
haproxy1.8安裝配置
3 配置haproxy的配置文件
3.1 定義一個haproxy.cfg的文件
mkdir /etc/haproxy
vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
maxconn 65536
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
user haproxy
group haproxy
daemonhtml

defaults
log global
option dontlognull
retries 3
option redispatch
maxconn 65536
timeout connect 70000
timeout client 70000
timeout server 70000linux

listen admin_stats
bind 192.168.24.148:48800
stats uri /admin-status
stats auth admin:admin
mode http
option httplognginx

listen test1
bind 192.168.24.148:3307
mode tcp
balance roundrobin
server dev-haproxy00.cz 192.168.24.148:3306 check port 3306 inter 5s rise 2 fall 3
3.2 定義服務的配置文件
3.2.1 vim /lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.targetweb

[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPIDredis

[Install]
WantedBy=multi-user.target
3.2.2 執行daemon-reload
systemctl daemon-reload
3.2.3 查看haprox的狀態
systemctl status haproxy
haproxy1.8安裝配置
3.2.4 啓動haproxy
systemctl start haproxyvim

haproxy1.8安裝配置
haproxy1.8安裝配置
3.2.5 加入開機啓動
haproxy1.8安裝配置後端

4 負載httpd
4.1 定義配置文件
vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
maxconn 65536
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
user haproxy
group haproxy
daemon服務器

defaults
log global
option dontlognull
retries 3
option redispatch
maxconn 65536
timeout connect 70000
timeout client 70000
timeout server 70000負載均衡

listen admin_stats
bind 192.168.24.148:48800
stats uri /admin-status
stats auth admin:admin
mode http
option httplogfrontend

frontend web
bind 0.0.0.0:80
default_backend webser
backend webser
balance roundrobin
server web1 192.168.26.130:80 check port 80 inter 5s rise 2 fall 3
server web2 192.168.26.131:80 check port 80 inter 5s rise 2 fall 3
4.2 配置後端的兩臺web服務器
分別在192.168.26.130和192.168.26.131配置
192.168.26.130
yum -y install httpd
vim /var/www/html/index.html
<html> Backend Server: 192.168.26.130</html>

192.168.26.131
yum -y install httpd
vim /var/www/html/index.html
<html> Backend Server: 192.168.26.131</html>
4.3 查看haproxy的監控頁面
haproxy1.8安裝配置
4.4 進行驗證是否負載均衡
haproxy1.8安裝配置
能夠看到是輪詢的進行負載的
5 經常使用命令
5.1 haproxy -c -f 檢測配置語法
haproxy -c -f /etc/haproxy/haproxy.cfg
haproxy1.8安裝配置5.2 haproxy -D -f config.cfg 啓動 守護進程hproxy -D -f config.cfg -p $PIDFILE -sf $(cat $PIDFILE)

相關文章
相關標籤/搜索