当前位置: 首页 > article >正文

HAproxy,nginx实现七层负载均衡

环境准备:

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)进行测试


http://www.kler.cn/news/326201.html

相关文章:

  • [c++高阶]模版进阶
  • Golang | Leetcode Golang题解之第440题字典序的第K小数字
  • RabbitMQ的高级特性-消息确认机制
  • http请求过程 part-2
  • 【C++前缀和 动态规划 博弈】1140. 石子游戏 II|2034
  • 【已解决】Koa 异步问题导致响应信息不能正确输出,始终为 Not Found
  • redis和mysql端口修改
  • Harmony next Native API 开发工程实践
  • Python数据分析工具:Seaborn用法示例
  • UI设计师面试整理-面向用户的设计
  • k8s pv(PersistentVolume) 状态含义
  • 全自动ai生成视频MoneyPrinterTurbo源码 在线ai生成视频源码
  • 【艾思科蓝】网络安全的隐秘战场:构筑数字世界的铜墙铁壁
  • 66 使用注意力机制的seq2seq_by《李沐:动手学深度学习v2》pytorch版
  • 生产者和消费者
  • WinRAR技巧:如何给多个压缩包设置同一个密码
  • 09_React 扩展
  • 网络安全:保护您的数字世界
  • 基于单片机电容测量仪仿真设计
  • 基于大数据的高校新生数据可视化分析系统
  • 华为源NAT技术与目的NAT技术
  • unity 打包安卓 RenderTexture显示红色
  • 命令回显echo
  • SpringBoot使用@Slf4j注解实现日志输出
  • uniapp数据缓存
  • golang学习笔记20-面向对象(二):方法与结构体【重要】
  • React和Vue对比
  • Oracle数据库物理结构操作管理
  • kubeadm部署k8s集群,版本1.23.6;并设置calico网络BGP模式通信,版本v3.25--未完待续
  • C++ STL(1)迭代器