JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

web高可用-基于keepalived和nginx

wys521 2024-09-14 13:34:12 精选教程 27 ℃ 0 评论

一.体系架构

在Keepalived + Nginx高可用负载均衡架构中,keepalived负责实现High-availability (HA) 功能控制前端机VIP(虚拟网络地址),当有设备发生故障时,热备服务器可以瞬间将VIP自动切换过来,实际运行中体验只有2秒钟切换时间,DNS服务可以负责前端VIP的负载均衡。

nginx负责控制后端web服务器的负载均衡,将客户端的请求按照一定的算法转发给后端Real Server处理,而Real Server将响应直接返回给客户端。



应用架构拓扑图

二. 优点

1.实现了可弹性化的架构,在压力增大的时候可以临时添加web服务器添加到这个架构里面去;

2.upstream具有负载均衡能力,可以自动判断后端的机器,并且自动踢出不能正常提供服务的机器;

3.相对于lvs而言,正则分发和重定向更为灵活。而Keepalvied可保证单个nginx负载均衡器的有效性,避免单点故障;

4.用nginx做负载均衡,无需对后端的机器做任何改动。

5.nginx部署在docker容器里,即大量地节约开发、测试、部署的时间,又可以在出现故障时通过镜像快速恢复业务。

三. 系统环境

两台负载机器安装:centos7.2+docker+nginx+keepalived,分别命名为:NGINX_MASTER,NGINX_BACKUP。

后端web服务器,可以是提供web服务的任何架构,分别命名为:WEB_1,WEB_2。

后端数据库机器可任意架构,只要能提供数据库服务即可。

服务器 操作系统 IP地址 安装软件

NGINX_MASTER Centos 7.2 64位 10.141.1.32 docker+nginx+keepalived

NGINX_BACKUP Centos 7.2 64位 10.141.9.2 docker+nginx+keepalived

WEB_1 Centos 7.2 64位 10.141.3.73 docker+web

WEB_2 Centos 7.2 64位 10.141.26.218 docker+web

虚拟IP 10.141.1.33

四. 搭建环境

1. 主机准备

全部主机执行命令

setenforce 0 #关闭selinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

systemctl stop firewalld #关闭防火墙

systemctl stop iptables #关闭iptables

2. docker安装(全部主机执行命令)

a. 在线安装

参考: (https://docs.docker.com/install/linux/docker-ce/centos/#uninstall-old-versions)

yum install docker

b. 离线二进制安装:

参考:(https://www.jianshu.com/p/46b9a351f749)

3. 准备web服务器

a. 启动服务

web1和web2执行,这里使用python启动一个simplehttpserver

touch 123.txt #在web1执行
touch 456.txt #在web2执行
python -m SimpleHTTPserver #web1,web2都执行

b. 检查

curl 10.141.3.73:8000 #返回123.txt

curl 10.141.26.218:8000 #返回456.txt

4. 安装nginx进行负载均衡,在master和backup执行

a. 拉镜像

docker pull nginx

b. vim nginx.conf ,增加 upstream和server

user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
 worker_connections 1024;
}
http {
 include /etc/nginx/mime.types;
 default_type application/octet-stream;
 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
 '$status $body_bytes_sent "$http_referer" '
 '"$http_user_agent" "$http_x_forwarded_for"';
 access_log /var/log/nginx/access.log main;
 upstream linuxidc { 
 server 10.141.3.73:8000; 
 server 10.141.26.218:8000; 
 }
 server {
 listen 80;
 server_name localhost;
 location / {
 root html;
 index index.html index.htm;
 proxy_pass http://linuxidc;
 }
 }
 sendfile on;
 #tcp_nopush on;
 keepalive_timeout 65;
 #gzip on;
 include /etc/nginx/conf.d/*.conf;
}

c. 启动nginx

docker run -it -d -p 80:80 -v /${PWD}/nginx.conf:/etc/nginx/nginx.conf nginx

d. 验证

curl localhost #返回123.txt 或者返回456.txt

5. 搭建keepalived进行热备(在master和backup执行)

a. 安装keepalived

yum install -y keepalived
systemctl start keepalived
systemctl enable keepalived

b. 修改配置文件/etc/keepalived/keepalived.conf

这里使用的是单播模式,解决脑裂问题,云主机(比如阿里云,腾讯云。亚信云等)需要单独申请VIP并绑定主机,否则不能访问VIP

! Configuration File for keepalived

global_defs {

notification_email {

acassen@firewall.loc

failover@firewall.loc

sysadmin@firewall.loc

}

notification_email_from Alexandre.Cassen@firewall.loc

smtp_server 192.168.200.1

smtp_connect_timeout 30

router_id LVS_DEVEL

vrrp_skip_check_adv_addr

#vrrp_strict #单播模式要注释掉

vrrp_garp_interval 0

vrrp_gna_interval 0

}

vrrp_script chk_port { #检测服务是否在运行。有很多方式,比如进程,用脚本检测等等

script "/root/chk_server.sh" #这里通过脚本监测

interval 2 #脚本执行间隔,每2s检测一次

weight -10 #脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -10

fall 2 #检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)

rise 1 #检测1次成功就算成功。但不修改优先级

}

vrrp_instance VI_1 {

state MASTER #backup主机填写BACKUP

unicast_src_ip 10.141.1.32 #写本机地址

unicast_peer {

10.141.9.2 #填写另外一台keepalived主机地址

}

interface eth0 #网卡

virtual_router_id 58 #默认51,可以换一个地址,避免冲突,主备id要一样

priority 100 #权重,backup修改为95,检查失败后优先级变90,低于95会将vip转移到slave

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

10.141.1.33 #虚拟ip

}

track_script {

chk_port

}

}

chk_server.sh脚本

counter=$(netstat -na|grep "LISTEN"|grep "80"|wc -l)

if [ "${counter}" -eq 0 ]; then

exit 0

fi

c. 验证

systemctl restart keepalived

ip a查看master中绑定VIP,backup没有绑定

master上执行systemctl stop keepalived,可以发现VIP漂流到backup上

curl 10.141.1.33 #返回结果为123.txt或者456.txt

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表