首页 > 系统相关 >HAproxy,nginx实现负载均衡

HAproxy,nginx实现负载均衡

时间:2024-09-24 18:50:21浏览次数:3  
标签:HAproxy haproxy 负载 192.168 server nginx html 80 php

环境准备:

192.168.88.25  (client)

192.168.88.26    (HAproxy)

192.168.88.27     (web1)

192.168.88.28       (web2)

192.168.88.29     (php1)

192.168.88.30       (php2)

关闭firewalld,selinux。配置yum源,扩展源epel-release

实验1

一、web1,web2

1、下载httpd服务

yum install httpd -y

2、开启httpd服务并且设置开启自启

systemctl start httpd
systemctl enable httpd

二、haproxy主机

1、安装haproxy(192.168.88.26)

 yum install haproxy -y

2、配置HAproxy

vim /etc/haproxy/haproxy.cfg 
global
	log 127.0.0.1 local3 info
	maxconn 4096
        uid nobody
#       uid 99
        gid nobody
#       gid 99
	daemon
	nbproc 1
	pidfile /run/haproxy.pid
defaults
	log		   global
	mode	   http
	maxconn 2048
	retries 	3
	option	redispatch
	contimeout	5000
	clitimeout	    50000
	srvtimeout	    50000
#timeout connect 5000
#timeout client 50000
#timeout server 50000
    option abortonclose

    stats uri /admin?stats
    stats realm Private lands
    stats auth admin:password
    stats hide-version

frontend http-in
	bind 0.0.0.0:80
	mode http
	log global
	option httplog
	option httpclose
     acl html url_reg  -i  \.html$
     use_backend html-server if  html
     default_backend html-server

backend html-server
	mode http
	balance roundrobin
	option httpchk GET /index.html
	cookie SERVERID insert indirect nocache
	server html-A web1:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
	server html-B web2:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5

3、域名解析

vim /etc/hosts

4、开启HAproxy服务

systemctl start haproxy.service

三、客服端(client)

1、安装elinks

yum install -y elinks

2、域名解析

vim /etc/hosts

3、进行访问测试

实验2

一、192.168.88.29(php1),192.168.88.30(php2)

1、安装httpd,php

yum install httpd php -y

2、修改php服务默认的网页,方便测试观察

192.168.88.29(php1)

echo php1 > /var/www/html/index.php

192.168.88.30(php2)

echo php2 > /var/www/html/index.php

192.168.88.29(php1),192.168.88.30(php2)开启httpd服务,并且设置开启自启

systemctl start httpd.service
systemctl enable httpd.service

二、192.168.88.26(haproxy)

1、域名解释php1,php2

vim /etc/hosts

2、修改配置文件192.168.88.26(HAproxy)

vim /etc/haproxy/haproxy.cfg
global
        log 127.0.0.1 local3 info
        maxconn 4096
        uid nobody
#       uid 99
        gid nobody
#       gid 99
        daemon
        nbproc 1
        pidfile /run/haproxy.pid
defaults
        log                global
        mode       http
        maxconn 2048
        retries         3
        option  redispatch
        contimeout      5000
        clitimeout          50000
        srvtimeout          50000
#timeout connect 5000
#timeout client 50000
#timeout server 50000
    option abortonclose

    stats uri /admin?stats
    stats realm Private lands
    stats auth admin:password
    stats hide-version

frontend http-in
        bind 0.0.0.0:80
        mode http
        log global
        option httplog
        option httpclose
     acl html url_reg  -i  \.html$
     use_backend html-server if  html
     acl php url_reg  -i  \.php$
     use_backend php-server if  php
     default_backend html-server

backend html-server
        mode http
        balance roundrobin
        option httpchk GET /index.html
        cookie SERVERID insert indirect nocache
        server html-A web1:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
        server html-B web2:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5
backend php-server
        mode http
        balance roundrobin
        option httpchk GET /index.php
        cookie SERVERID insert indirect nocache
        server php-A php1:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
        server php-B php2:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5

3、重启服务haproxy

systemctl restart haproxy.service

4、192.168.88.25(client)访问192.168.88.26(haproxy)进行测试

三、nginx负载均衡

1、192.168.88.26(haproxy),把haproxy服务删除,替换成nginx进行负载均衡

yum remove -y haproxy

2、安装nginx服务

yum install -y nginx

3、编辑nginx服务配置文件

vim  /etc/nginx/nginx.conf
#http下添加
    upstream html {
        server web1:80;
        server web2:80;
        }
    upstream php {
        server php1:80;
        server php2:80;
        }
#server下添加
        location / {
         proxy_pass http://html;
        }
        location ~ \.php$ {
         proxy_pass http://php;
        }

4、192.168.88.25(client)访问192.168.88.26(nginx)进行测试

标签:HAproxy,haproxy,负载,192.168,server,nginx,html,80,php
From: https://blog.csdn.net/2201_75556224/article/details/142494226

相关文章

  • 直流负载箱的主要功能有哪些?
    直流负载箱是专门用于模拟和测试直流电源系统负载的设备,其主要功能包括以下几点:直流负载箱可以模拟各种不同类型的负载,如电阻、电感、电容等,以满足不同应用场景的需求。通过调整负载箱的参数,可以实现对直流电源系统的精确模拟,为电源系统的研发、测试和优化提供有力支持。直流负载......
  • 【linux中nginx怎么连接自己的前端项目】
    你的前端项目目录(我们用antdesign举例):此时我们没有dist文件(就是编译后的文件)所以需要编译之后你可以拿到dist文件夹了此时我们打开nginx.conf文件保存后启动nginx,你就可以看到你自己前端的页面了打开方法:......
  • D23 kubernetes 工作负载资源对象-Job与CronJob
    1、简介 Deployment和DaemonSet资源主要用于部署和管理守护进程型的应用程序,如nginx、mysql、java进程等。这类应用程序的特点是持续运行,通常在没有明确停止或下线的情况下一直保持运行状态。此外,kubernetes还提供了Job和CronJob资源,用于管理一次性任务和定时任务,如计算任务、数......
  • Linux集群负载均衡DR模式LVS搭建
    负载均衡DR模式LVS搭建1.基础准备(1)准备3台机器DR模式需要三台虚拟机,三台虚拟机只需要有“公网”IP,但在这种模式下又多了一个VIP。对应的机器和IP如下:调度器dir:192.168.147.130真实服务器rs1:192.168.147.131真实服务器rs2:192.168.147.132VIP:192.168.147.110(2)改名分发......
  • kafka负载均衡迁移(通过kafka eagle)
    在grafana监控中发现kafka的各个节点磁盘不均匀出现这样的情况是因为kafka默认是以文件数作为平衡的条件的。换句话说,kafka不会管一个副本有多大,只会看磁盘中有多少个副本文件。解决方式:1、修改策略,改为按照磁盘大小平衡数据2、手动迁移数据,将磁盘使用率高的节点数据迁移......
  • 最全nginx安装教程,小白必看。
    1、简介【Nginx是个好东西,没有它的话,这前端开发完了后(非vue开发),还得找一个web容器大概率是tomcat。咱就需要配置tomcat,tomcat的配置是比较麻烦的,nginx的配置就简单了好多。】Nginx(enginex)是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Ngi......
  • 负载均衡介绍
    你真的了解LoadBalance嘛使用LVS实现负载均衡原理及安装配置详解VIP(VirtualIP,虚拟IP地址)......
  • Nginx优化与防盗链
    目录一、隐藏Nginx版本号二、修改Nginx用户与组三、配置Nginx网页缓存时间四、实现Nginx的日志切割五、配置Nginx实现连接超时六、更改Nginx运行进程数七、配置Nginx实现网页压缩功能八、实现网页图片的大小压缩九、配置Nginx实现防盗链一、隐藏Nginx版本号隐藏N......
  • 负载均衡与Keepalived高可用部署
    负载均衡框架节点:web-7web-8,lb-5,lb-6四个节点都安装好nginx以及php将all-rpm.tgz传输到root目录下执行即可(需要先行配置yum用来安装createrepo)#!/bin/bashmvall-rpm.tgz/etc/yum.repos.d/groupaddwwwuseradd-m-gwww-s/sbin/nologinwwwcd/etc/yum.re......
  • 安装nginx-http-flv-module模块
    简介nginx-http-flv-module是什么流程注意事项详细步骤查看当前已经安装的nginx版本下载对应版本的nginx源代码下载nginx-http-flv-module模块源代码重新编译nginx验证nginx-http-flv-module是否安装好了引用简介nginx中的模块虽然就是类似插件的概念,但是它无法像VsCode那样轻松......