haproxy搭建httpd


haproxy部署

关闭防火墙和selinux

[root@LB ~]# systemctl  stop firewalld
[root@LB ~]# setenforce 0

[root@RS1 ~]# systemctl  stop firewalld
[root@RS1 ~]# setenforce 0

[root@RS2 ~]# systemctl  stop firewalld
[root@RS2 ~]# setenforce 0

RS1配置

[root@RS1 ~]# dnf -y install httpd

[root@RS1 ~]# echo liu > /var/www/html/index.html
[root@RS1 ~]# systemctl restart httpd
[root@RS1 ~]# systemctl enable httpd

RS2配置

[root@RS2 ~]# dnf -y install httpd

[root@RS2 ~]# echo "yang" > /var/www/html/index.html
[root@RS2 ~]# systemctl  restart httpd
[root@RS2 ~]# systemctl  enable httpd

LB设置

修改LB得内核

[root@LB ~]# cat /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1

修改haproxy配置文件

[root@LB ~]# cat /etc/haproxy/haproxy.cfg
global
    daemon
    maxconn 256

defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend http-in
    bind *:80
    default_backend servers

backend servers
    server web01 192.168.124.135:80
    server web02 192.168.124.138:80

启动haproxy服务

[root@LB ~]# haproxy -f /etc/haproxy/haproxy.cfg -c
[root@LB ~]# systemctl  restart haproxy

客户端验证

[root@Clinet ~]# curl http://192.168.124.128
liu
[root@Clinet ~]# curl http://192.168.124.128
yang
[root@Clinet ~]# curl http://192.168.124.128
liu
[root@Clinet ~]# curl http://192.168.124.128
yang

使用web网页访问测试

[root@LB ~]# cat /etc/haproxy/haproxy.cfg
global
    log 127.0.0.1 local0  info
    maxconn 20480
    pidfile /var/run/haproxy.pid
    user haproxy
    group haproxy
    daemon
defaults
    mode http
    log global
    option dontlognull
    option httpclose
    option httplog
    option redispatch
    balance roundrobin
    timeout connect 10s
    timeout client 10s
    timeout server 10s
    timeout check 10s
    maxconn 60000
    retries 5
listen admin_stats
    bind 0.0.0.0:8189
    stats enable
    mode http
    log global
    stats uri /haproxy_stats            //访问网页后缀URL
    stats realm Haproxy\ Statistics
    stats auth liu:yang              //用户名和密码
    stats admin if TRUE
    stats refresh 30s
listen webcluster
    bind 0.0.0.0:80
    mode http
    log global
    maxconn 3000
    balance roundrobin
    cookie SESSION_COOKIE insert indirect nocache
    server web01 192.168.124.135:80 check inter 2000 fall 5
    server web02 192.168.124.138:80 check inter 2000 fall 5

重启服务

[root@LB ~]# systemctl  restart haproxy

[root@LB ~]# 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               [::]:*   

网页测试

密码是配置文件中得用户名和密码我此处是liu,密码是yang

posted @ 2022-09-28 00:20  Tqing  阅读(21)  评论(0编辑  收藏  举报