LVS的NAT模型实现

实验环境

192.168.31.169 CIP 代表客户机
192.168.31.170 VIP 代表LVS设备外网地址
192.168.160.136 DIP 代表LVS设备内网地址
192.168.160.140 RIP 代表http服务器
192.168.160.150 RIP 代表http服务器

192.168.160.136配置

开启IP路由转发

vim /etc/sysctl.conf
net.ipv4.ip_forward=1

安装ipvsadm

yum install ipvsadm -y

新增集群服务,算法用rr,指向VIP的80端口

ipvsadm -A -t 192.168.31.170:80 -s rr

新增集群服务的RS,LVS类型选择NAT

ipvsadm -a -t 192.168.31.170:80 -r 192.168.160.140 -m
ipvsadm -a -t 192.168.31.170:80 -r 192.168.160.150 -m

查看搭建的集群服务

ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.31.170:80 rr
  -> 192.168.160.140:80           Masq    1      0          0         
  -> 192.168.160.150:80           Masq    1      0          0

192.168.160.140配置

安装httpd服务

yum install httpd -y


systemctl start httpd

写入网站页面,为了观察效果,两个页面内容不同,实际生产中需要的是完全相同的页面

echo 192.168.160.140 RS1 > /var/www/html/index.html

把37.136添加到网关

vim /etc/sysconfig/network-scripts/ifcfg-eth0
GATEWAY=192.168.37.136

192.168.160.150配置

安装httpd服务

yum install httpd -y


systemctl start httpd

写入网站页面,为了观察效果,两个页面内容不同,实际生产中需要的是完全相同的页面

echo 192.168.160.150 RS2 > /var/www/html/index.html

把37.136添加到网关

vim /etc/sysconfig/network-scripts/ifcfg-eth0
GATEWAY=192.168.37.136

目前已完成轮询访问

增加权重的轮询访问实现

修改136原配置,把rr改成wrr

ipvsadm -E -t 192.168.31.170:80 -s wrr

修改150设备的权重,改成6,140改成2

ipvsadm -e -t 192.168.31.170:80 -r 192.168.160.150 -m -w 6
ipvsadm -e -t 192.168.31.170:80 -r 192.168.160.140 -m -w 2

搭建workpress,并增加一台数据库服务器

192.168.160.160 数据库服务器

136配置

暂时删除150配置,测试WordPress是否部署成功

ipvsadm -d -t 192.168.31.170:80 -r 192.168.160.150:80

测试完成后重新添加

ipvsadm -a -t 192.168.31.170:80 -r 192.168.160.150 -m 

140配置

安装php-fpm php-mysql服务

yum install php-fpm php-mysql -y

启动php-fpm程序

systemctl start php-fpm

配置fcgi

vim /etc/httpd/conf.d/fcgi.conf

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/$1

修改默认主页

vim /etc/httpd/conf/httpd.conf 
    DirectoryIndex index.php index.html

解压WordPress

tar xvf wordpress-5.0.4-zh_CN.tar.gz

拷贝配置文件并改名

cd wordpress/
cp wp-config-sample.php wp-config.php
vim wp-config.php

/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');

/** MySQL数据库用户名 */
define('DB_USER', 'wordpress');

/** MySQL数据库密码 */
define('DB_PASSWORD', 'centos');

/** MySQL主机 */
define('DB_HOST', '192.168.160.160');

重启服务

systemctl restart httpd php-fpm

复制配置信息到150

scp -r wordpress 192.168.160.150:/var/www/html/
scp /etc/httpd/conf/httpd.conf 192.168.160.150:/etc/httpd/conf/
scp /etc/httpd/conf.d/fcgi.conf 192.168.160.150:/etc/httpd/conf.d/

150配置

重启服务

systemctl restart httpd php-fpm

160配置

创建wordpress库,并允许wordpress账号拥有权限

mysql -e "create database wordpress;grant all on wordpress.* to wordpress@'192.168.160.%' identified by 'centos'"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值