以下步骤为个人实验环境
简单原理:客户端访问对外入口lvs+keepalived(四层),lvs将请求负载至后端两台haproxy(七层),haproxy再将请求负载至后端服务器。
如下拓扑图:
一、准备工作
1、9台虚拟机
主机 | IP | 作用 |
---|---|---|
192.168.137.100 | 虚拟IP | |
lvs-keepalived-01 | 192.168.137.145 | LVS将请求负载至haproxy中的一台 |
lvs-keepalived-02 | 192.168.137.146 | LVS-KEEPALIVED的备份 |
haproxy | 192.168.137.147 | haproxy-01 将请求负载至后端web |
haproxy | 192.168.137.148 | haproxy-02 将请求负载至后端web |
web1 | 192.168.137.141 | web1静态服务器 |
web2 | 192.168.137.142 | web2静态服务器 |
web3 | 192.168.137.143 | web3动态服务器 |
web4 | 192.168.137.144 | web4动态服务器 |
client | 192.168.137.151 | 客户端虚拟机用于测试 |
配置域名/etc/hosts
二、安装软件及配置
1、在两台lvs-keepalived上安装 ipvsadm和keepalived
yum install -y ipvsadm keepalived
2、在两台haproxy虚拟机上安装haproxy
yum install -y haproxy
3、web3、web4安装Nginx PHP
yum install -y epel-release
yum install -y nginx php-fpm
4、配置网页
web1、web2在/var/www/html/下各自创建index.html,测试内容自定。
web3、web4在/usr/share/nginx/html/下各自创建index.php,添加如下内容
<?
phpinfo()
php?>
配置nginx,添加php模块
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfi