一、基于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.confupstream 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