Haproxy Tornado笔记

来源:互联网 发布:2015nba西部决赛数据 编辑:程序博客网 时间:2024/05/05 15:15

Haproxy Tornado笔记

  • haproxy: 负载均衡,动静态分离
  • keepalived: haproxy主备
  • varnish: 静态文件缓存
  • supervisor: 管理Tornado进程

环境

eth0:192.168.31.197
eth1:10.10.100.100 haproxy主机 eth0:192.168.31.159
eth1:10.10.100.101 haproxy备机 eth0:10.10.100.53 真实机器 eth0:10.10.100.54 真实机器 eth0:10.10.100.57 varnish真实机器(静态文件)

真实机器配置

在10.10.100.53和10.10.100.54上安装和配置

supervisor配置

安装

supervisor 安装

配置

vim /etc/supervisor/supervisord.conf 
[group:tornados]programs=haproxy_tornado[program:haproxy_tornado]numprocs=8 #我当前机器10核,开启8个进程numprocs_start=1command=python /var/www/haproxy_tornado/server.py --port=81%(process_num)02d #测试项目process_name=%(program_name)s%(process_num)ddirectory=/var/www/haproxy_tornado/autorestart=trueredirect_stderr=truestdout_logfile=/tmp/haproxy_tornado.logstdout_logfile_maxbytes=500MBstdout_logfile_backups=50stderr_logfile=/tmp/haproxy_tornado.logautostart=trueloglevel=infouser=www-data
supervisorctl reload#加载配置supervisorctl status#查看状态

nginx配置

upstream haproxy_tornado {#supervisor起了8个进程,这轮询这几个进程  server  0.0.0.0:8101;  server  0.0.0.0:8102;  server  0.0.0.0:8103;  server  0.0.0.0:8104;  server  0.0.0.0:8105;  server  0.0.0.0:8106;  server  0.0.0.0:8107;  server  0.0.0.0:8108;}server {    listen 80;    root /var/www/haproxy_tornado/;    index index.html index.htm;    server_name localhost;    access_log /var/log/nginx/haproxy_tornado_access.log;    error_log /var/log/nginx/haproxy_tornado_error.log;    location ~ ^(/static/) {        root /var/www/haproxy_tornado/;        expires 7d;        access_log   off;    }    location ~ ^(/media/).*\.(jpg|jpeg|png|gif)$ {        root /var/www/haproxy_tornado//;        expires 15d;        access_log   off;        try_files $uri =404 /static/empty.gif;    }    location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) {        access_log   off;    }    location / {        proxy_pass         http://haproxy_tornado;        proxy_set_header   Host             $host;        proxy_set_header   X-Real-IP        $remote_addr;        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;    }}
nginx -t #检查配置文件nginx -s reload #加载配置文件

静态主机配置

在10.10.100.57上安装和配置

nginx配置

server {    listen 80;    root /var/www/haproxy_tornado/;    index index.html index.htm;    server_name localhost;    access_log /var/log/nginx/haproxy_tornado_access.log;    error_log /var/log/nginx/haproxy_tornado_error.log;    location ~ ^(/static/) {        proxy_hide_header "Set-Cookie";        proxy_ignore_headers "Set-Cookie";        root /var/www/haproxy_tornado/;        expires 7d;        access_log   off;    }}
nginx -t #检查配置文件nginx -s reload #加载配置文件

varnish配置

vim /etc/varnish/default.vclvcl 4.0;# Default backend definition. Set this to point to your content server.#后端的HTTP服务器IP和端口backend default {    .host = "127.0.0.1";    .port = "8001";}#设置清理缓存的IPacl purgers {    "127.0.0.1";}sub vcl_recv {    if (req.method == "PURGE") {    # PURGE请求的处理        if (!client.ip ~ purgers) {            return(synth(405,"Method not allowed"));        }        #清理缓存        return(purge);    }    if (req.method != "GET" && req.method != "HEAD") {        return (pass);    }     #不缓存认证信息    if (req.http.Authorization ) {        return (pass);    }    #不正常的访问不缓存    if (req.method != "GET" &&            req.method != "HEAD" &&            req.method != "PUT" &&            req.method != "POST" &&            req.method != "TRACE" &&            req.method != "OPTIONS" &&            req.method != "PATCH" &&            req.method != "DELETE") {        return (pipe);    }    return (hash);}sub vcl_backend_response {}sub vcl_deliver { if (obj.hits > 0) {        #如果命中缓存 Response Headers x-cache-lookup= MemCache        set resp.http.x-cache= "MemCache";    } else {        set resp.http.x-cache= "MISS";    }    #删除Response Headers 部分信息    #unset resp.http.Server;    #unset resp.http.X-Drupal-Cache;    #unset resp.http.X-Varnish;    #unset resp.http.Via;    #unset resp.http.Age;    #unset resp.http.Link;}

haproxy主机配置

haproxy配置

vim /etc/haproxy/haproxy.cfg
global    log 0.0.0.0 local0 err    chroot /var/lib/haproxy   #改变当前工作目录    user haproxy         #所属用户    group haproxy        #所属组    daemon                #以守护进程方式运行haproxydefaults    log global    mode    http    #默认的模式mode { tcp|http|health }    option  httplog  #启用记录HTTP请求、会话状态和计时器的功能    option  dontlognull #保证HAProxy不记录上级负载均衡发送过来的用于检测状态没有数据的心跳包。    contimeout 5000      #客户端超时时间,    clitimeout 50000  #设置连接客户端发送数据时的成功连接最长等待时间    srvtimeout 50000 #设置服务器端回应客户度数据发送的最长等待时间    errorfile 400 /etc/haproxy/errors/400.http    errorfile 403 /etc/haproxy/errors/403.http    errorfile 408 /etc/haproxy/errors/408.http    errorfile 500 /etc/haproxy/errors/500.http    errorfile 502 /etc/haproxy/errors/502.http    errorfile 503 /etc/haproxy/errors/503.http    errorfile 504 /etc/haproxy/errors/504.httplisten admin_stats  #统计页面配置    bind 0.0.0.0:8888    option httplog    stats refresh 20s    maxconn 2000    stats uri /stats #统计页面url  http://0.0.0.0:8888/stats    stats realm Haproxy Manager    stats auth admin:admin   #统计页面用户名和密码设置    stats admin if TRUEfrontend  haproxy_tornado    mode http    bind 0.0.0.0:80    option forwardfor    option http-server-close    option http-pretend-keepalive    acl url_static path_beg -i /static    use_backend real_static if url_static #静态主机    default_backend real_haproxy_tornado #默认主机backend real_static #静态主机    balance roundrobin    server S1 10.10.100.57:80 checkbackend real_haproxy_tornado    balance roundrobin    server A 10.10.100.54:80 check    server B 10.10.100.53:80 check

keepalived配置

在haproxy主备上安装和配置

安装

apt-get install keepalived

配置

vim /etc/keepalived/keepalived.conf
global_defs {   router_id LVSMaster}vrrp_script chk_haproxy {    script "/etc/keepalived/check_haproxy.sh"    interval 2    weight 2}vrrp_instance VI_1 {    state MASTER #备机为BACKUP    interface eth0    virtual_router_id 100    priority 100    advert_int 1    authentication {        auth_type PASS        auth_pass 123456    }    virtual_ipaddress {        192.168.31.200    }    track_script {#检测haproxy进程是否存在        chk_haproxy    }}
vim /etc/keepalived/check_haproxy.sh#!/bin/bash#检测haproxy进程是否存在if [ `ps -C haproxy --no-header | wc -l ` -eq 0 ];then    #不存在尝试启动haproxy    service haproxy start    sleep 3    #再次检测haproxy进程是否存在    if [ `ps -C haproxy --no-header | wc -l ` -eq 0 ];then        #如果还没进程停止keepalived进程,VIP绑定到备机提供服务        service keepalived stop    fifi
chmod +x /etc/keepalived/check_haproxy.sh

我现在的订单接口

提供接口给每个项目使用,每天大概会插入200W条左右的数据,查询没统计过

这里写图片描述

配置文件

https://github.com/neo-hu/haproxy_tornado

0 0