我是靠谱客的博主 坦率可乐,最近开发中收集的这篇文章主要介绍部署haproxy,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

使用3台主机。lvs安装haproxy,RS1 .RS2安装apache

      LVs192.168.80.40haproxy
RS1192.168.80.20apache
RS2192.168.80.30apache
[root@lvs ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@lvs ~]# vim /etc/selinux/config 
[root@lvs ~]# setenforce o

//RS1  RS2也需要关闭

 

[root@RS1 ~]# yum -y install httpd
[root@RS2 ~]# yum -y install httpd
//安装apache

[root@RS1 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@RS1 ~]# ss -antl
State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     Process     
LISTEN     0          128                   0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0          128                         *:80                        *:*                    
LISTEN     0          128                      [::]:22                     [::]:*   

[root@RS2 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@RS2 ~]# ss -antl
State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     Process     
LISTEN     0          128                   0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0          128                         *:80                        *:*                    
LISTEN     0          128                      [::]:22                     [::]:*  

  
[root@RS1 ~]#echo 'RS1' > /var/www/html/index.html
[root@RS2 ~]#echo 'RS2' > /var/www/html/index.html

访问

 接下来在lvs主机安装haproxy

[root@lvs ~]# ls
anaconda-ks.cfg  haproxy-2.6.0.tar.gz
//提前把包下下来

https://github.com/haproxy/haproxy/tags
//下载链接

[root@lvs ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
//安装依赖包

[root@node2 ~]# useradd -r -M -s /sbin/nologin haproxy

 

[root@lvs ~]# tar xf haproxy-2.6.0.tar.gz 
[root@lvs ~]# ls
anaconda-ks.cfg  haproxy-2.6.0
[root@lvs ~]# cd haproxy-2.6.0
[root@lvs haproxy-2.6.0]# make clean
[root@lvs haproxy-2.6.0]# make -j $(grep 'processor' /proc/cpuinfo |wc -l)  
> TARGET=linux-glibc  
> USE_OPENSSL=1  
> USE_ZLIB=1  
> USE_PCRE=1  
> USE_SYSTEMD=1

[root@lvs haproxy-2.6.0]# make install PREFIX=/usr/local/haproxy
//编译安装

[root@lvs haproxy]# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/
[root@lvs haproxy]# which haproxy
/usr/sbin/haproxy

配置lvs

[root@lvs ~]# vi /etc/sysctl.conf 

# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1

提供配置文件

[root@lvs ~]# vim /etc/rsyslog.conf
# Save boot messages also to boot.log
local0.*                                                /var/log/haproxy.log
local7.*                                                /var/log/boot.log
[root@lvs ~]# systemctl enable --now rsyslog

mkdir /etc/haproxy

cat > /etc/haproxy/haproxy.cfg <<EOF
#--------------全局配置----------------
global
    log 127.0.0.1 local0  info
    #log loghost local0 info
    maxconn 20480
#chroot /usr/local/haproxy
    pidfile /var/run/haproxy.pid
    #maxconn 4000
    user haproxy
    group haproxy
    daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode http
    log global
    option dontlognull
    option httpclose
    option httplog
    #option forwardfor
    option redispatch
    balance roundrobin
    timeout connect 10s
    timeout client 10s
    timeout server 10s
    timeout check 10s
    maxconn 60000
    retries 3
#--------------统计页面配置------------------
listen admin_stats
    bind 0.0.0.0:8189
    stats enable
    mode http
    log global
    stats uri /haproxy_stats
    stats realm Haproxy Statistics
    stats auth admin:admin
    #stats hide-version
    stats admin if TRUE
    stats refresh 30s
#---------------web设置-----------------------
listen webcluster
    bind 0.0.0.0:80
    mode http
    #option httpchk GET /index.html
    log global
    maxconn 3000
    balance roundrobin
    cookie SESSION_COOKIE insert indirect nocache
    server web01 192.168.80.20:80 check inter 2000 fall 5
    server web01 192.168.80.30:80 check inter 2000 fall 5
    #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
EOF

开机自启

cat > /usr/lib/systemd/system/haproxy.service <<EOF
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

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

[Install]
WantedBy=multi-user.target
EOF

[root@node2 ~]# ss -antl
State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     Process     
LISTEN     0          128                   0.0.0.0:80                  0.0.0.0:*                    
LISTEN     0          128                   0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0          128                   0.0.0.0:8189                0.0.0.0:*                    
LISTEN     0          128                      [::]:22                     [::]:*   

接下来用lvs192.168.80.40/访问其他2个主机的apache

 

 

最后

以上就是坦率可乐为你收集整理的部署haproxy的全部内容,希望文章能够帮你解决部署haproxy所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部