zoukankan      html  css  js  c++  java
  • 搭建 CentOS 6 服务器(15)

    (一)Keepalived

    (1)安装

    Shell代码  收藏代码
    1. # cd /usr/local/src  
    2. # wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz  
    3. # tar zxvf keepalived-1.2.15.tar.gz  
    4. # cd keepalived-1.2.15  
    5. # ./configure  
    6. # make && make install  



    (2)配置

    Shell代码  收藏代码
    1. # cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/  
    2. # cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/  
    3. # mkdir /etc/keepalived  
    4. # cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/  
    5. # cp /usr/local/sbin/keepalived /usr/sbin/  
    6. # mv /etc/keepalived/keepalived.cfg /etc/keepalived/keepalived.cfg.org  
    7. # vi /etc/keepalived/keepalived.conf  
    8.     ! Configuration File for keepalived  
    9.       
    10.     global_defs {  
    11.        notification_email {  
    12.          cluster-admin@example.org  
    13.        }  
    14.        notification_email_from noreply@example.org  
    15.        smtp_server smtp.example.org  
    16.        smtp_connect_timeout 30  
    17.        router_id act  
    18.     }  
    19.       
    20.     include haproxy_servers.conf  #设置HAProxy  
    21.     include lvs_*_servers.conf      #设置LVS  
    22. # /etc/init.d/keepalived start  



    (二)HAProxy

    (1)安装

    Shell代码  收藏代码
    1. # cd /usr/local/src  
    2. # wget http://www.haproxy.org/download/1.5/src/haproxy-1.5.11.tar.gz  
    3. # tar zxvf haproxy-1.5.11.tar.gz  
    4. # cd haproxy-1.5.11  
    5. # make TARGET=linux2628 CPU=x86_64 USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1  
    6. # make install  



    (2)添加用户

    Shell代码  收藏代码
    1. # useradd -s /usr/sbin/nologin -r haproxy  



    (3)SSL证书

    Shell代码  收藏代码
    1. # mkdir -p /etc/rensn/certs  
    2. # openssl req -x509 -nodes -newkey rsa:2048 -keyout /etc/rensn/certs/haproxy.pem -out /etc/rensn/certs/haproxy.pem -days 365   
    3. # cd /etc/rensn/certs  
    4. # chmod 600 haproxy.pem  



    (4)配置

    Shell代码  收藏代码
    1. # cp /usr/local/sbin/haproxy* /usr/sbin/  
    2. # cp /usr/local/src/haproxy-1.5.11/examples/haproxy.init /etc/init.d/haproxy  
    3. # chmod +x /etc/init.d/haproxy  
    4. # mkdir -p /etc/haproxy  
    5. # cp /usr/local/src/haproxy-1.5.11/examples/examples.cfg /etc/haproxy/haproxy.cfg  
    6. # mkdir -p /var/lib/haproxy  
    7. # touch /var/lib/haproxy/stats  
    8. # vi /etc/haproxy/haproxy.cfg  
    9.     global  
    10.         # 设置日志  
    11.         log         127.0.0.1 local2 info  
    12.         chroot    /var/lib/haproxy  
    13.         pidfile     /var/run/haproxy.pid  
    14.         # 最大链接数  
    15.         maxconn     256  
    16.         # 运行的用户  
    17.         user        haproxy  
    18.         group       haproxy  
    19.         # 启动服务  
    20.         daemon  
    21.         # 最大SSL链接数  
    22.         maxsslconn     256  
    23.         # Diffie-Hellman  
    24.         tune.ssl.default-dh-param 2048  
    25.         # 运行HAProxy的线程数(建议为1)  
    26.         nbproc  1  
    27.   
    28.     defaults  
    29.         # Layer4负载均衡  
    30.         mode              tcp  
    31.         # 日志设置继承global  
    32.         log                global  
    33.         # 获取HTTP请求日志  
    34.         option             httplog  
    35.         # 后端未响应的超时时间  
    36.         timeout connect    10s  
    37.         # 后端的超时时间  
    38.         timeout client     30s  
    39.         # 服务器超时时间  
    40.         timeout server     30s  
    41.   
    42.     # 前端定义 ( http-in 为任意字符 )  
    43.     frontend http-in  
    44.         # 监听80端口  
    45.         bind *:80  
    46.         # 默认的后端定义  
    47.         default_backend    backend_servers  
    48.         # 传递X-Forwarded-For  
    49.         option             forwardfor  
    50.         # 监听443端口  
    51.         bind *:443 ssl crt /etc/rensn/certs/haproxy.pem  
    52.   
    53.     # 后端定义  
    54.     backend backend_servers  
    55.         # 负载均衡方式  
    56.         balance            roundrobin  
    57.         # 后端服务器的定义  
    58.         server             www01 192.168.21.100:80 check  
    59.         server             www02 192.168.21.110:80 check  
    60.         server             www02 192.168.21.120:80 check disabled  
    61. # service haproxy start  



    (5)keepalived设置

    Shell代码  收藏代码
    1. # vi /etc/keepalived/haproxy_servers.conf  
    2.    vrrp_script chk_haproxy {  
    3.       script "killall -0 haproxy"   # verify the pid existance  
    4.       interval 2                    # check every 2 seconds  
    5.       weight 2                      # add 2 points of prio if OK  
    6.    }  
    7.      
    8.    vrrp_instance VI_1 {  
    9.       state MASTER              # MASTER on master, BACKUP on backup  
    10.       interface eth1                # interface to monitor  
    11.       virtual_router_id 51          # Assign one ID for this route (tcpdump vrrp)  
    12.       priority 101                  # 101 on master, 100 on backup  
    13.       virtual_ipaddress {  
    14.           192.168.21.100            # the virtual IP  
    15.       }  
    16.       track_script {  
    17.           chk_haproxy  
    18.       }  
    19.    }  
    20. # /etc/init.d/keepalived restart  



    (三)LVS

    (1)安装
    Linux内核里已经包含了ip_vs模块,只需要安装管理工具

    Shell代码  收藏代码
    1. # yum -y install ipvsadm  



    (2)网络设置

    Shell代码  收藏代码
    1. # vi /etc/sysctl.conf  
    2.     net.ipv4.ip_forward = 1  
    3.     net.ipv4.conf.default.rp_filter = 0  
    4. # sysctl -p  
    5. # cat /proc/sys/net/ipv4/ip_forward   
    6.     1  
    7. # sevice network restart  



    (3)LB设置

    Shell代码  收藏代码
    1. # ipvsadm -C  
    2. # ipvsadm -A -t 192.168.21.100:80  
    3. # ipvsadm -ln  
    4. # service ipvsadm save  



    (4)keepalived设置

    Shell代码  收藏代码
      1. # vi /etc/keepalived/lvs_http_servers.conf  
      2.     virtual_server <lvs_srv_ip> 80 {  
      3.       delay_loop   20  
      4.       lvs_sched    lc  
      5.       lvs_method   NAT  
      6.       protocol     TCP  
      7.   
      8.       real_server  <web1_srv_ip> 80 {  
      9.         weight 1  
      10.         inhibit_on_failure  
      11.         HTTP_GET {  
      12.           url {  
      13.             path /  
      14.             status_code 200  
      15.           }  
      16.           connect_timeout 5  
      17.           nb_get_retry 3  
      18.           delay_before_retry 20  
      19.         }  
      20.       }  
      21.      real_server  <web2_srv_ip> 80 {  
      22.         weight 1  
      23.         inhibit_on_failure  
      24.         HTTP_GET {  
      25.           url {  
      26.             path /  
      27.             status_code 200  
      28.           }  
      29.           connect_timeout 5  
      30.           nb_get_retry 3  
      31.           delay_before_retry 20  
      32.         }  
      33.     }  
      34. # /etc/init.d/keepalived restart
  • 相关阅读:
    《算法竞赛进阶指南》0x12 队列 POJ2259 Team Queue
    《算法竞赛进阶指南》0x11栈 单调栈求矩形面积 POJ2559
    《算法竞赛进阶指南》0x11 栈 求解中缀表达式
    19.职责链模式(Chain of Responsibility Pattern)
    16.观察者模式(Observer Pattern)
    17.解释器模式(Interpreter Pattern)
    15. 迭代器模式(Iterator Pattern)
    14.命令模式(Command Pattern)
    12.代理模式(Proxy Pattern)
    13.模板方法(Template Method)
  • 原文地址:https://www.cnblogs.com/kyli816/p/4629123.html
Copyright © 2011-2022 走看看