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)

复制代码
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)进行测试

相关推荐
wangbing11253 分钟前
迁移服务器
运维·服务器
细节控菜鸡7 分钟前
【排查实录】Web 页面能打开,服务器能通接口,客户端却访问失败?原因全在这!
运维·服务器·前端
one year.16 分钟前
Linux:库制作与原理
linux·运维·服务器
陈苏同学16 分钟前
Win11安装 Ubuntu 22.04 子系统 - WSL2 - 安装完迁移到其它盘
linux·运维·ubuntu
我命由我123451 小时前
PDFBox - PDFBox 加载 PDF 异常清单(数据为 null、数据为空、数据异常、文件为 null、文件不存在、文件异常)
java·服务器·后端·java-ee·pdf·intellij-idea·intellij idea
蓝色土耳其love1 小时前
centos 7.9 安装单机版k8s
linux·运维·服务器·kubernetes·centos
小贾要学习1 小时前
如何在Linux操作系统环境下使用git命令提交文件到远程仓库
linux·运维·git
郝学胜-神的一滴2 小时前
使用Linux系统函数递归遍历指定目录
linux·运维·服务器·开发语言·c++·软件工程
pusue_the_sun2 小时前
操作系统:进程的短程调度
运维·服务器
七度光阴;2 小时前
Docker入门手册
运维·docker·容器