(2)本次实验设置了一个VIP(Virtual IP)为172.18.38.99,用户只需要访问这个IP地址即可获得网页服务。其中,nginx主机为172.18.38.100,备机为172.18.38.101。Web服务器A为172.18.38.200,Web服务器B为172.18.38.201。
1,在http语句块中定义调度规则
vim /etc/nginx/nginx.conf
http {
.....
upstream webser {
server 172.18.38.200:80;
server 172.18.38.201:80;
}
.....
}
2,而后在server中调用
vim /etc/nginx/conf.d/vhost.conf
server {
listen 172.18.38.99:80;
server_name www.a.com;
location / {
proxy_pass http://webser;
}
}
2,重启nginx,使配置生效
systemctl restart nginx
1,安装keepalived
yum install keepalived
2,修改keepalived配置文件
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id proxy1
vrrp_mcast_group4 224.1.1.1
}
vrrp_script chk_nginx {
script "killall -0 nginx && exit 0 || exit 1"
interval 1
weight -30
fall 2
rise 2
}
vrrp_instance VI_1 {
state MASTER
interface ens37
virtual_router_id 66
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
172.18.38.99/16
}
track_script {
chk_nginx
}
}
关键配置:
1,定义nginx健康检查脚本
vrrp_script chk_nginx {
script "killall -0 nginx && exit 0 || exit 1"
interval 1 #以秒触发一次
weight -30 #nginx宕机就立马减去有限级30
fall 2 #检查两次如果都是宕机就表示nginx挂掉了
rise 2 #宕机之后两次检查nginx是活着的就重新+30优先级
}
2,调用
track_script {
chk_nginx
3,启动keepalived
systemctl start keepalived
systemctl enable keepalived
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id proxy2
vrrp_mcast_group4 224.1.1.1
}
vrrp_instance VI_1 {
state BACKUP
interface ens37
virtual_router_id 66
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
172.18.38.99/16
}
}
7,启动keepalived
systemctl start keepalived
systemctl enable keepalived
1,安装httpd软件
1,安装httpd软件
yum install http
2,启动服务
systemctl start httpd
systemctl enable httpd
2,生成web页面,
A主机
echo web_server_A > /var/www/html/index.html
b主机
echo web_server_B > /var/www/html/index.html
1,不当任何服务
[root@testsrvr ~]# for i in {1..10};do sleep 0.5;curl 172.18.38.99;done
web_server_B
web_server_B
web_server_A
web_server_A
web_server_B
web_server_B
web_server_A
web_server_A
web_server_B
web_server_B
2,宕一台keepalived服务
[root@ke_nginx_S ~]# systemctl stop keepalived.service
[root@testsrvr ~]# for i in {1..10};do sleep 0.5;curl 172.18.38.99;done
web_server_B
web_server_B
web_server_A
web_server_A
web_server_B
web_server_B
web_server_A
web_server_A
web_server_B
web_server_A
3,宕掉nginx_master服务器
[root@ke_nginx-M ~]# systemctl stop nginx
[root@testsrvr ~]# for i in {1..100};do sleep 0.5;curl 172.18.38.99;done
web_server_A
web_server_A
web_server_B
web_server_B
web_server_A
curl: (7) couldn‘t connect to host
curl: (7) couldn‘t connect to host
curl: (7) couldn‘t connect to host
curl: (7) couldn‘t connect to host
web_server_B
web_server_B
web_server_A
web_server_A
原文地址:http://blog.51cto.com/13598893/2094057