Nginx+keelplivs
先准备4台虚拟机
第一台虚拟机
yum -y install keepalived.x86_64 ipvsadm.x86_64
sysctl -a | grep ip_forward #开启内核IP转发
echo “net.ipv4.ip_forward = 1” #输出net.ipv4.ip_forward = 1
echo “net.ipv4.ip_forward = 1” >> /etc/sysctl.conf ; sysctl -p#验证net.ipv4.ip_forward = 1
vim /etc/keepalived/keepalived.conf #进入keepalived.conf把文件内容改为下列
vrrp_instance VI_1 {
state MASTER #备用服务器为BACKUP
interface ens33
virtual_router_id 51
priority 100 #备用服务为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.126.150 #vip地址
}
}
virtual_server 192.168.126.150 80 {
delay_loop 6
lb_algo rr #调度算法:rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq
lb_kind DR #lvs工作模式:DR|TUN|NAT
persistence_timeout 0
protocol TCP
real_server 192.168.126.137 80 { #改为nginx主机3的ip
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.126.130 80 { #改为nginx主机4的ip
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
systemctl restart keepalived.service #重启keeplived
第二台虚拟机
yum -y install keepalived.x86_64 ipvsadm.x86_64
sysctl -a | grep ip_forward #开启内核IP转发
echo “net.ipv4.ip_forward = 1” #输出net.ipv4.ip_forward = 1
echo “net.ipv4.ip_forward = 1” >> /etc/sysctl.conf ; sysctl -p#验证net.ipv4.ip_forward = 1
vim /etc/keepalived/keepalived.conf #进入keepalived.conf把文件内容改为下列
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.126.150 #vip地址
}
}
virtual_server 192.168.126.150 80 {
delay_loop 6
lb_algo rr #调度算法:rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq
lb_kind DR #lvs工作模式:DR|TUN|NAT
persistence_timeout 0
protocol TCP
real_server 192.168.126.137 80 { #改为nginx主机3的ip
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.126.130 80 { #改为nginx主机4的ip
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
systemctl restart keepalived.service #重启keeplived
第三台虚拟机
yum –y install nginx
vim /etc/nginx/nginx.conf #修改默认文件
42:root /web; #可以改成任意位置
mkdir /web #创建修改的路径
vim /web/index.html #在路径中写测试页面
11111111
chown –Rf nginx:nginx /web #赋权
systemctl restart nginx #重启nginx
cat /web/index.html #查看测试页面
11111111
sysctl -a | grep ip_forward #开启内核IP转发
echo “net.ipv4.ip_forward = 1” #输出net.ipv4.ip_forward = 1
echo “net.ipv4.ip_forward = 1” >> /etc/sysctl.conf ; sysctl -p#验证net.ipv4.ip_forward = 1
vim lvsdr.sh #添加内容如下
ifconfig lo:0 192.168.126.150 broadcast 192.168.126.150 netmask 255.255.255.255 up
route add -host 192.168.126.150 dev lo:0
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
chmod +x lvsdr.sh #赋权
./lvsdr.sh #运行脚本
第四台虚拟机
Yum-y install nginx
vim /etc/nginx/nginx.conf #修改默认文件
42:root /web; #可以改成任意位置
mkdir /web #创建修改的路径
vim /web/index.html #在路径中写测试页面
22222222
chown –Rf nginx:nginx /web #赋权
systemctl restart nginx #重启nginx
cat /web/index.html #查看测试页面
22222222
sysctl -a | grep ip_forward #开启内核IP转发
echo “net.ipv4.ip_forward = 1” #输出net.ipv4.ip_forward = 1
echo “net.ipv4.ip_forward = 1” >> /etc/sysctl.conf ; sysctl -p#验证net.ipv4.ip_forward = 1
vim lvsdr.sh #添加内容如下
ifconfig lo:0 192.168.126.150 broadcast 192.168.126.150 netmask 255.255.255.255 up
route add -host 192.168.126.150 dev lo:0
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
chmod +x lvsdr.sh #赋权
./lvsdr.sh #运行脚本
可以在第一台或第二台测试也可以通过浏览器进行测试
虚拟机测试
[root@localhost ~]# curl 192.168.126.150
11111111
[root@localhost ~]# curl 192.168.126.150
22222222
浏览器测试
在浏览器输入vip地址进行测试