一、基于CentOS7和、构建LVS-DR群集
准备四台虚拟机
ip | 作用 |
---|---|
192.168.27.150 | 客户端 |
192.168.27.151 | LVS |
192.168.27.152 | RS |
192.168.27.152 | RS |
关闭防火墙
[root@localhost ~]# systemctl stop firewalld
安装ifconfig
yum install net-tools.x86_64 -y
1、DS上
1.1 配置LVS虚拟IP
安装ipvsadm
yum install ipvsadm -y
增加IP
ifconfig ens33:200 192.168.27.200 netmask 255.255.255.255 up
1.2 手工执行配置添加LVS服务并增加两天RS
[root@localhost ~]# ipvsadm -C
[root@localhost ~]# ipvsadm -A -t 192.168.27.200:80 -s rr
[root@localhost ~]# ipvsadm -a -t 192.168.27.200:80 -r 192.168.27.151:80 -g
[root@localhost ~]# ipvsadm -a -t 192.168.27.200:80 -r 192.168.27.152:80 -g
2、在RS端(第三、四台)上
2.1 配置WEB服务器
yum install httpd -y
2.2 配置默认主页
hostname -I 取地址
[root@backup ~]# echo "web test page, ip is `hostname -I`." > /var/www/html/index.html
2.3 启动服务
[root@backup ~]# systemctl start httpd
2.4 测试:在客户端访问WEB服务器
[root@localhost ~]# curl 192.168.27.147
web test page, ip is 192.168.27.147 .
[root@localhost ~]# curl 192.168.27.148
web test page, ip is 192.168.27.148 .
2.5 绑定VIP
ifconfig lo:200 192.168.27.200 netmask 255.255.255.255 up
2.6 配置主机路由
route add -host 192.168.27.200 dev lo
2.7 抑制ARP响应
调整内核参数,关闭arp响应
echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
二、配置nginx负载均衡
环境准备
主机名 ip地址 作用
node1 192.168.27.140 负载均衡服务器
node2 192.168.27.141 Web服务器
node3 192.168.27.142 Web服务器
配置
1、配置主机文件vhost.conf
[root@node1 ~]# vim /etc/nginx/conf.d/vhost.conf
upstream web_pools {
server 192.168.27.141:80;
server 192.168.27.142:80;
}
server {
listen 80;
server_name www1.ming.com;
location / {
proxy_pass http://web_pools;
}
}
解释:proxy_pass 是反向代理
2、检查语法并重启服务
#检查语法是否正确
[root@node1 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#重启服务
[root@node1 ~]# systemctl restart nginx
3、配置hosts文件
#在linux中修改
[root@node1 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.27.140 www1.ming.com
#测试
[root@node1 ~]# for((i=1;i<=4;i++))
> do
> curl www1.ming.com
> done
web test page,ip is 192.168.27.141
web test page ,ip is 192.168.27.142
web test page,ip is 192.168.27.141
web test page ,ip is 192.168.27.142