利用keepalived实现高可用nginx
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用keepalived实现高可用nginx相关的知识,希望对你有一定的参考价值。
实验拓扑图
(1)本次基于VMware Workstation搭建一个四台Linux(CentOS 7.4)系统所构成的一个服务器集群,其中两台nginx做前端调度服务器(一台为主机,另一台为备机),另外两台作为真实的Web服务器(向外部提供http服务,这里仅仅使用了CentOS默认自带的http服务,没有安装其他的类似Tomcat、Jexus服务)。
(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。
一,配置nginx反向代理服务器,在两台nginx上都做一遍下面的操作
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
二,配置keepalived+nginx_master服务器**
1,安装keepalived
yum install keepalived
2,修改keepalived配置文件
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
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
三,配置keepalived+nginx_slave服务器
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
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
四,配置后端web服务器
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,不当任何服务
[[email protected] ~]# 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服务
[[email protected]_nginx_S ~]# systemctl stop keepalived.service
[[email protected] ~]# 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服务器
[[email protected]_nginx-M ~]# systemctl stop nginx
[[email protected] ~]# 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
以上是关于利用keepalived实现高可用nginx的主要内容,如果未能解决你的问题,请参考以下文章