需求:
两台数据服务器(nginx)
两台lvs调度器
一台客户端
ip地址分配:
(vip:192.168.4.15)
web1:192.168.4.1/24(eth0)
192.168.4.15/32(lo)
web2:192.168.4.2/24(eth0)
192.168.4.15/32(lo)
ds1:192.168.4.10/24(eth0)
192.168.4.15/24(eth0)
gateway:192.168.4.254
ds2:192.168.4.11/24(eth0)
client:192.168.4.100/24(eth0)
[ds1]
1.安装ipvsadm keepalived软件
2.修改iakeepalived配置文件
! Configuration File for keepalived
global_defs {
notification_email {
[email protected] //设置报警收件人邮箱
}
notification_email_from ka@localhost //设置发件人
smtp_server 127.0.0.1 //定义邮件服务器
smtp_connect_timeout 30
router_id ds1 //设置路由ID号
}
vrrp_instance VI_1 {
state MASTER //主服务器为MASTER
interface eth0 //定义网络接口
virtual_router_id 51 //主辅VRID号必须一致
priority 100 //服务器优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111 //主辅服务器密码必须一致
}
virtual_ipaddress {
192.168.4.15 //设置VIP为192.168.0.253
}
}
virtual_server 192.168.4.15 80 {
delay_loop 6
lb_algo rr //设置LVS调度算法为RR
lb_kind DR //设置LVS的模式为DR
protocol TCP
real_server 192.168.4.1 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
real_server 192.168.4.2 80 {
weight 1 //设置权重为1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
} delay_before_retry 3
}
}
3.重新启动keepalived服务
4.查看状态 ipvsadm -ln
[ds2]
1.安装ipvsadm keepalived软件
2.修改iakeepalived配置文件
! Configuration File for keepalived
global_defs {
global_defs {
notification_email {
[email protected] //设置报警收件人邮箱
}
notification_email_from ka@localhost //设置发件人
smtp_server 127.0.0.1 //定义邮件服务器
smtp_connect_timeout 30
router_id dsr2
}
vrrp_instance VI_1 {
state SLAVE
interface eth0
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.15
}
}
virtual_server 192.168.4.15 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.4.1 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.4.2 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
3.重新启动keepalived服务
4.查看状态 ipvsadm -ln
[web1]
1.源码安装nginx
1) 下载nginx源码包
2) 解包 tar -xvf nginx-1.12.2.tar.gz
3) cd nginx-1.12.2/
4) 配置安装 ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx –with-http_ssl_module
5) 安装 make && make install
6) 制作链接ln -s /usr/local/nginx/sbin/nginx /sbin/
7) 启动nginx
2.写测试网页测试 echo web1 > /usr/local/nginx/html/index.html
提示:
配置lo网卡:ip a a dev lo 192.168.4.15/32
修改ia内核:echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[web2]
1.源码安装nginx
1) 下载nginx源码包
2) 解包 tar -xvf nginx-1.12.2.tar.gz
3) cd nginx-1.12.2/
4) 配置安装 ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx –with-http_ssl_module
5) 安装 make && make install
6) 制作链接ln -s /usr/local/nginx/sbin/nginx /sbin/
7) 启动nginx
2.写测试网页测试 echo web2 > /usr/local/nginx/html/index.html
提示:
配置lo网卡:ip a a dev lo 192.168.4.15/32
修改ia内核:echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[测试]
负载均衡测试
用cilent客户端访问 vip地址192.168.4.15 ,刷新可以看到不同内容(轮询算法)
用ipvsadm在ds1上可以查看访问次数
高可用测试
停掉其中一台keepalived服务,访问依然不受影响