Keepalived实现高可用Nginx反向代理和基于NAT的LVS及分析

1. 前言

keepalived是一个C语言开发的,能够基于Linux基础架构提供一个HA实现的软件。HA是基于VRRP协议实现,可以为LVS、Nginx、HAProxy等实现的LB提供高可用。

下图是keepalived的软件架构图

主要核心模块:

2. 实验前提

搭建时间服务器

为192.168.23.0/24网络提供统一时钟,此网络中所有机器都开启ntpd守护进程,时间源指向192.168.23.123这台时间服务器。即在配置文件中server部分增加一条,并启动ntpd。

# vim /etc/ntp.conf

server 192.168.23.123

# service ntpd start

# chkconfig ntpd on

3. 高可用反向代理

这里使用LNNMP,在第一级N上实现反向代理的高可用。实验中上游服务器用2台nginx的WEB服务器,Mysql和PHP略去。LNNMP可以参照博文《Nginx之LNMP、LNNMP、LNNNMP架构实现及缓存技术》。

3.1规划

3.2 keepalived的安装

CentOS6.4之后就已经收录了keepalived,因此直接可以yum安装。

# yum –y install keepalived# rpm -ql keepalived /etc/keepalived/etc/keepalived/keepalived.conf/etc/rc.d/init.d/keepalived/etc/sysconfig/keepalived/usr/sbin/keepalived……

可以看到默认的一个配置文件已经放在了/etc/keepalived下,只要对其根据规划修改就可以了。

3.3 各服务器配置

3.3.1 WEB1配置

nginx.conf

# vim /usr/local/nginx/conf/nginx.confuser nginx; worker_processes auto;#pid/var/run/nginx.pid; error_log /var/log/nginx/error.log;events { use epoll; worker_connections 1024;}http { includemime.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; sendfileon; #tcp_nopushon; #keepalive_timeout 0; keepalive_timeout 5; #gzip on; server { listen80;server_name WEB1;add_header X-upS WEB1-$server_addr:$server_port;#charset koi8-r;#access_log logs/host.access.log main;location / { root /web/nginx/static;index index.html index.htm;}#error_page 404/404.html;# redirect server error pages to the static page /50x.html #error_page 500 502 503 504 /50x.html;location = /50x.html {root html;} }}

index.html

# vim /web/nginx/static/index.htmlstaticOK! This is a static page of WEB1

3.3.2 WEB2配置

nginx.conf

# vim /usr/local/nginx/conf/nginx.confuser nginx; worker_processes auto;#pid/var/run/nginx.pid; error_log /var/log/nginx/error.log;events { use epoll; worker_connections 1024;}http { includemime.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; sendfileon; #tcp_nopushon; #keepalive_timeout 0; keepalive_timeout 5; #gzip on; server { listen8080;server_name WEB2;add_header X-upS WEB2-$server_addr:$server_port;#charset koi8-r;#access_log logs/host.access.log main;location / { root /web/nginx/static;index index.html index.htm;}#error_page 404/404.html;# redirect server error pages to the static page /50x.html #error_page 500 502 503 504 /50x.html;location = /50x.html {root html;} } }

index.html

# vim /web/nginx/static/index.htmlstaticOK! This is a static page of WEB2

3.3.3 Proxy1(keepalived MASTER)

nginx.conf

# vim /usr/local/nginx/conf/nginx.confuser nginx;worker_processes auto;#pid/var/run/nginx.pid; error_log /var/log/nginx/error.log;events { use epoll; worker_connections 1024;}http { includemime.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; sendfileon; #tcp_nopushon; #keepalive_timeout 0; keepalive_timeout 5; #gzip on; upstream static { server 192.168.23.80;server 192.168.23.81:8080;server 127.0.0.1:8080 backup; } server { listen80;server_name localhost;add_header X-Proxy Proxy100-$server_addr:$server_port;#charset koi8-r;#access_log logs/host.access.log main;location ~ \.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d;}location ~ \.(js|css)$ { expires 1h;}location / { proxy_pass ;}#error_page 404/404.html;# redirect server error pages to the static page /50x.html #error_page 500 502 503 504 /50x.html;location = /50x.html {root html;} } server { listen8080;server_name sorry;add_header X-Sorry SorryServer-$server_addr:$server_port;location / {root html;rewrite .* /maintenance.html break;} }}

maintenance.html

# vim /usr/local/nginx/html/maintenance.htmlSorryDown for maintenance

keepalived.conf

于是夜莺会在黎明到来之前勇敢的将胸膛顶住蔷薇的刺,

Keepalived实现高可用Nginx反向代理和基于NAT的LVS及分析

相关文章:

你感兴趣的文章:

标签云: