环境准备:
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
globallog 127.0.0.1 local3 infomaxconn 4096uid nobody
# uid 99gid nobody
# gid 99daemonnbproc 1pidfile /run/haproxy.pid
defaultslog globalmode httpmaxconn 2048retries 3option redispatchcontimeout 5000clitimeout 50000srvtimeout 50000
#timeout connect 5000
#timeout client 50000
#timeout server 50000option abortonclosestats uri /admin?statsstats realm Private landsstats auth admin:passwordstats hide-versionfrontend http-inbind 0.0.0.0:80mode httplog globaloption httplogoption httpcloseacl html url_reg -i \.html$use_backend html-server if htmldefault_backend html-serverbackend html-servermode httpbalance roundrobinoption httpchk GET /index.htmlcookie SERVERID insert indirect nocacheserver html-A web1:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5server 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
globallog 127.0.0.1 local3 infomaxconn 4096uid nobody
# uid 99gid nobody
# gid 99daemonnbproc 1pidfile /run/haproxy.pid
defaultslog globalmode httpmaxconn 2048retries 3option redispatchcontimeout 5000clitimeout 50000srvtimeout 50000
#timeout connect 5000
#timeout client 50000
#timeout server 50000option abortonclosestats uri /admin?statsstats realm Private landsstats auth admin:passwordstats hide-versionfrontend http-inbind 0.0.0.0:80mode httplog globaloption httplogoption httpcloseacl html url_reg -i \.html$use_backend html-server if htmlacl php url_reg -i \.php$use_backend php-server if phpdefault_backend html-serverbackend html-servermode httpbalance roundrobinoption httpchk GET /index.htmlcookie SERVERID insert indirect nocacheserver html-A web1:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5server html-B web2:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5
backend php-servermode httpbalance roundrobinoption httpchk GET /index.phpcookie SERVERID insert indirect nocacheserver php-A php1:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5server 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;}