您的位置:首页 > 运维架构 > 网站架构

Keepalived_tengine实现discuz负载均衡和高可用

2016-05-29 22:39 567 查看
前言;
上篇博文《keepalived_nginx实现discuz负载均衡和高可用》讲到,由于nginx将health_check功能放入到了商业版本,导致社区版本的nginx进行负载均衡,无法对后端的RS主机进行健康状态检测,所以现在准备使用tengine来取代nginx。我们只需要将前一章节VS主机上的nginx替换为tengine即可。

配置:
Host VS1
卸载nginx,安装tengine
# yum remove -y nginx
# yum groupinstall -y "Development tools" "Server Platform Development"
# yum install -y pcre-devel
# wget http://tengine.taobao.org/download/tengine-2.0.3.tar.gz # tar -xf tengine-2.0.3.tar.gz
# cd tengine-2.0.3
# ./configure
# make && make install
# echo "PATH=/usr/local/nginx/sbin/:$PATH" > /etc/profile.d/nginx.sh
# exec bash


创建sorry_server页面文件
# cp /usr/local/nginx/html/index.html{,.bak}
# echo "服务维护中,请稍后访问." > /usr/local/nginx/html/index.html


修改tengine配置文件
# vim /usr/local/nginx/conf/nginx.conf
http{
...
# 上游服务器, 即后端RS服务器.
upstream backend {
server 10.0.0.101 weight=1;
server 10.0.0.102 weight=1;
# sorry_server
server 10.0.0.111:8080 backup;
server 10.0.0.112:8080 backup;

# 健康检查
check interval=3000 rise=1 fall=3 timeout=1000 type=http;
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}

server {
listen 80;
server_name localhost;

# 当nginx将php代码送至后端RS处理时请求头中的Host值会是backend.
# php代码在RS上处理时,其内部代码会去请求图片/层叠样式表等静态资源以填充页面.
# 而php代码去请求静态资源时使用的是如http://backend/xxxx.gif这样的url,自然是取不到的.
# 所以我们要在nginx向后代理遇到Host为backend时,将其转换为127.0.0.1.
set $my_host $http_host;
if ($http_host = "backend") {
set $my_host "127.0.0.1";
}

location / {
proxy_pass   http://backend; proxy_set_header Host $my_host;
}
}

server {
listen 8080;
server_name localhost;
charset utf-8;

root   /usr/local/nginx/html;
index  index.html index.htm;

# sorry_server仅提供主页面, 访问其它资源也转至主页面.
location ~ .* {
error_page  404  /;
}
}

启动服务:
# nginx -t
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful
# nginx


Host VS2 做相同的配置

测试:
轮流关闭HostRS1和HostRS2,模拟RS主机宕机情形,可以发现用户仍然可以正常访问,说明用户请求被调度到了正常的RS主机上

# tail /usr/local/nginx/logs/error.log
2016/05/14 14:57:37 [error] 26112#0: check time out with peer: 10.0.0.101:80
2016/05/14 14:57:41 [error] 26112#0: check time out with peer: 10.0.0.101:80
2016/05/14 14:57:46 [error] 26112#0: check time out with peer: 10.0.0.101:80
2016/05/14 14:57:50 [error] 26112#0: check time out with peer: 10.0.0.101:80
2016/05/14 14:57:55 [error] 26112#0: check time out with peer: 10.0.0.101:80
2016/05/14 14:57:59 [error] 26112#0: check time out with peer: 10.0.0.101:80


同时关闭RS1 和 RS2 会提示用户服务维护中
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tengine