haproxy+keepalived 主备

来源:互联网 发布:kiki软件收费吗 编辑:程序博客网 时间:2024/06/06 14:56
<pre name="code" class="html">主:192.168.1.102 
备:192.168.1.103
web1:192.168.1.105
web2:192.168.1.104
web3: 192.168.1.106
vip:192.168.1.141
192.168.1.102和192.168.1.103 的 haproxy 配置
global                                                # 全局参数的设置    log         127.0.0.1 local2                      # log语法:log <address_1>[max_level_1] # 全局的日志配置,使用log关键字,                                                      #指定使用127.0.0.上的syslog服务中的local0日志设备,记录日志等级为info的日志    chroot      /var/lib/haproxy                      #改变当前工作目录    pidfile     /var/run/haproxy.pid                  #当前进程id文件    maxconn     4000                                  #最大连接数    user        haproxy                               #所属用户    group     haproxy                                 #所属组    daemon                                            #以守护进程方式运行haproxy    #stats socket /var/lib/haproxy/statsdefaults    mode                    http                      #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK    log                     global                    #应用全局的日志配置    option                  httplog                   # 启用日志记录HTTP请求,默认haproxy日志记录是不记录HTTP请求日志                                                                     option                  dontlognull               # 启用该项,日志中将不会记录空连接。所谓空连接就是在上游的负载均衡器                                                      #或者监控系统为了探测该 服务是否存活可用时,需要定期的连接或者获取某                                                      #一固定的组件或页面,或者探测扫描端口是否在监听或开放等动作被称为空连接;                                                      #官方文档中标注,如果该服务上游没有其他的负载均衡器的话,建议不要使用                                                      #该参数,因为互联网上的恶意扫描或其他动作就不会被记录下来    option http-server-close                          #每次请求完毕后主动关闭http通道    option forwardfor       except 127.0.0.0/8        #如果服务器上的应用程序想记录发起请求的客户端的IP地址,需要在HAProxy                                                      #上配置此选项, 这样 HAProxy会把客户端的IP信息发送给服务器,在HTTP                                                      #请求中添加"X-Forwarded-For"字段。 启用  X-Forwarded-For,在requests                                                      #头部插入客户端IP发送给后端的server,使后端server获取到客户端的真实IP。     option                  redispatch                # 当使用了cookie时,haproxy将会将其请求的后端服务器的serverID插入到                                                      #cookie中,以保证会话的SESSION持久性;而此时,如果后端的服务器宕掉                                                      #了, 但是客户端的cookie是不会刷新的,如果设置此参数,将会将客户的请                                                      #求强制定向到另外一个后端server上,以保证服务的正常。    retries                 3                         # 定义连接后端服务器的失败重连次数,连接失败次数超过此值后将会将对应后端                                                      #服务器标记为不可用    timeout http-request    10s                       #http请求超时时间    timeout queue           1m                        #一个请求在队列里的超时时间    timeout connect         10s                       #连接超时    timeout client          1m                        #客户端超时    timeout server          1m                        #服务器端超时    timeout http-keep-alive 10s                       #设置http-keep-alive的超时时间    timeout check           10s                       #检测超时    maxconn                 3000                      #每个进程可用的最大连接数listen stas  #自定义监听名,任意取    bind *:1099 #监听的地址和端口    mode http  #模式    stats enable #启用状态监控    stats hide-version #隐藏软件版本号    stats auth admin:admin #登陆用户名和密码    stats realm HAproxy\ stats #提示信息,空格之前加\    stats admin if TRUE #当通过认证才可管理    stats uri /stats #访问路径    stats refresh 5 #页面自动刷新间隔frontend  main *:80                                   #监听地址为80    # stats uri /haproxy    acl url_static       path_beg       -i /static /images /javascript /stylesheets    acl url_static       path_end       -i .jpg .gif .png .css .js    use_backend static          if url_static    default_backend             my_webserver          #定义一个名为my_app前端部分。此处将对于的请求转发给后端backend static                                        #使用了静态动态分离(如果url_path匹配 .jpg .gif .png .css .js静态文件则                                                      #访问此后端)    balance     roundrobin                            #负载均衡算法(#banlance roundrobin 轮询,balance source 保存session值,                                                      #支持static-rr,leastconn,first,uri等参数)    server      static 127.0.0.1:80 check             #静态文件部署在本机(也可以部署在其他机器或者squid缓存服务器)backend my_webserver                                  #定义一个名为my_webserver后端部分。PS:此处my_webserver只是一个                                                      #自定义名字而已,但是需要与frontend里面配置项default_backend 值相一致    balance     roundrobin                            #负载均衡算法    server  web01  192.168.1.104:80  check inter 2000 fall 3 weight 30              #定义的多个后端    server  web02  192.168.1.105:80  check inter 2000 fall 3 weight 30              #定义的多个后端    server  web03  192.168.1.106:80  check inter 2000 fall 3 weight 30              
主 /etc/keepalived/keepalived.conf 配置
# Configuration File for keepalived      global_defs {     router_id LVS_DEVEL  }      vrrp_script chk_http_port {       script "/etc/keepalived/check_haproxy.sh"       interval 2       weight 2  }    vrrp_instance VI_1 {      state MASTER                #从keepalived这里改成BACKUP      interface eth0      virtual_router_id 51      priority 100                #从keepalived这里改成99吧,只要从比主小就行,数字是从0~255,数字越大权重越大      advert_int 1      authentication {          auth_type PASS          auth_pass 1111      }      track_script {          chk_http_port      }      virtual_ipaddress {          192.168.1.141      }  } 
haproxy 监控脚本 #<span style="font-family: Arial, Helvetica, sans-serif;">vim /etc/keepalived/check_haproxy.sh  </span>

<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">意思就是先检查haproxy是否启动,如果没有先启动haproxy进程,启动不了则关闭keepalived进程</span>
#!/bin/bash  A=`ps -C haproxy --no-header | wc -l`if [ $A -eq 0 ];then        service haproxy start        echo "Haproxy start"          sleep 3        if [ `ps -C haproxy --no-header | wc -l` -eq 0 ];then                /etc/init.d/keepalived stop                echo "keepalived stop"          fifi}
<span style="font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; font-size: 13px; line-height: 24.0499992370605px; text-indent: 26px;">chmod +x /etc/keepalived/check_haproxy.sh  </span><span style="margin: 0px; padding: 0px; border: 0px; font-size: 13px; font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24.0499992370605px; text-indent: 26px; color: rgb(0, 0, 255); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"> #添加执行权限</span>


0 0
原创粉丝点击