[置顶] Keepalived+Nginx实现高可用(HA)

时间:2022-09-15 08:57:11

环境介绍

1.服务器(192.168.20.112):安装keepalivednginx  主节点

2.服务器(192.168.20.113):安装keepalivednginx  从节点

3.服务器(192.168.20.114):安装tomcat

4.服务器(192.168.20.115):安装tomcat

5.两台Nginx端口:80

6.两台tomcat端口:8080

7.VIP192.168.20.116

配置文件

1.Nginx配置文件(两个节点Nginx配置文件一致,Nginx端口可以与tomcat不一致)

worker_processes  1;

events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

#gzip on;
upstream serverpool {
server 192.168.20.114:8080;
server 192.168.20.115:8080;
}

server {
listen 80;
server_name localhost;

location / {
proxy_pass http://serverpool;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

}

2.主节点keeaplived配置文件

! Configuration File for keepalived

global_defs {
notification_email {
986692803@qq.com
1171899551@qq.com
}

notification_email_from mumubuku2014@163.com
smtp_server smtp.163.com
smtp_connect_timeout 30
router_id nginx_master

}
#检测nginx端口是否故障的脚本
vrrp_script chk_http_port {
script "</dev/tcp/127.0.0.1/80"
interval 1
weight -2
}
#为了避免启动keepalived之前没有启动nginx , 在start中首先启动nginx
start() {
/usr/local/nginx/sbin/nginx
sleep 3
echo -n $"Starting $prog: "
daemon keepalived ${KEEPALIVED_OPTIONS}
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
}

vrrp_instance VI_1 {
state MASTER
interface eth1#注意网卡要与本机网卡名称一致
virtual_router_id 51
priority 100#优先权权重,值大的会被先启用
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.20.116#自己设置的VIP
}
track_script {
chk_http_port #启用脚本检测
}
}

3.从节点keeaplived配置文件

! Configuration File for keepalived

global_defs {
notification_email {
986692803@qq.com
1171899551@qq.com
}

notification_email_from mumubuku2014@163.com
smtp_server smtp.163.com
smtp_connect_timeout 30
router_id nginx_backup

}
#检测nginx端口是否故障的脚本
vrrp_script chk_http_port {
script "</dev/tcp/127.0.0.1/80"
interval 1
weight -2
}
#为了避免启动keepalived之前没有启动nginx , 在start中首先启动nginx
start() {
/usr/local/nginx/sbin/nginx
sleep 3
echo -n $"Starting $prog: "
daemon keepalived ${KEEPALIVED_OPTIONS}
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
}

vrrp_instance VI_1 {
state BACKUP
interface eth0#注意网卡要与本机网卡名称一致
virtual_router_id 51
priority 99#优先权权重,值大的会被先启用
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.20.116#自己设置的VIP
}
track_script {
chk_http_port #启用脚本检测
}
}

4.实现步骤

4.1.步骤一:在192.168.20.114192.168.20.115上安装tomcat

4.2步骤二:在192.168.20.112192.168.20.112上安装nginx

4.3步骤三:分别在192.168.20.112192.168.20.112上做nginx反向代理配置和负载均衡配置代理到两台tomcat服务器

4.4步骤四:在192.168.20.112192.168.20.112上安装keepalived

4.5步骤五:修改192.168.20.112192.168.20.112两台服务器的keepalived配置文件

4.6步骤六:启动192.168.20.112192.168.20.112两个节点的keepalived服务

5.高可用原理

    当主节点出故障,keepalived会根据priority的值大小竞争来决定谁是真正的MASTER,脚本检测也是在失败的时候会把权重减去相应的值,比如原来masterpriority=100,如果脚本检测到端口80无法连接,就会priority-2=98权重98小于从节点priority99),此时从节点将竞争成为master,这样就实现了web应用的热备。

注:此处是做两个节点,如果是两个以上节点,那么节点权重最大的将成为主节点。

6.安装说明

整个安装过程很简单,安装keepalived只需执行:

yum install -y keepalived (centos redhat)
    或者
    apt-get install keepalived (debian ubuntu)

NginxTomcat在此就不再赘述,可以参考我的其他文档。


谢谢!