当前位置:首页 > 开发 > 操作系统 > 正文

搭建 CentOS 6 服务器(15) - Keepalived、HAProxy、LVS

发表于: 2015-04-15   作者:rensanning   来源:转载   浏览:
摘要: (一)Keepalived (1)安装 # cd /usr/local/src # wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz # tar zxvf keepalived-1.2.15.tar.gz # cd keepalived-1.2.15 # ./configure # make &a
(一)Keepalived

(1)安装
# cd /usr/local/src
# wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz
# tar zxvf keepalived-1.2.15.tar.gz
# cd keepalived-1.2.15
# ./configure
# make && make install


(2)配置
# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
# mkdir /etc/keepalived
# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
# cp /usr/local/sbin/keepalived /usr/sbin/
# mv /etc/keepalived/keepalived.cfg /etc/keepalived/keepalived.cfg.org
# vi /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived
    
    global_defs {
       notification_email {
         cluster-admin@example.org
       }
       notification_email_from noreply@example.org
       smtp_server smtp.example.org
       smtp_connect_timeout 30
       router_id act
    }
    
    include haproxy_servers.conf  #设置HAProxy
    include lvs_*_servers.conf      #设置LVS
# /etc/init.d/keepalived start


(二)HAProxy

(1)安装
# cd /usr/local/src
# wget http://www.haproxy.org/download/1.5/src/haproxy-1.5.11.tar.gz
# tar zxvf haproxy-1.5.11.tar.gz
# cd haproxy-1.5.11
# make TARGET=linux2628 CPU=x86_64 USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1
# make install


(2)添加用户
# useradd -s /usr/sbin/nologin -r haproxy


(3)SSL证书
# mkdir -p /etc/rensn/certs
# openssl req -x509 -nodes -newkey rsa:2048 -keyout /etc/rensn/certs/haproxy.pem -out /etc/rensn/certs/haproxy.pem -days 365 
# cd /etc/rensn/certs
# chmod 600 haproxy.pem


(4)配置
# cp /usr/local/sbin/haproxy* /usr/sbin/
# cp /usr/local/src/haproxy-1.5.11/examples/haproxy.init /etc/init.d/haproxy
# chmod +x /etc/init.d/haproxy
# mkdir -p /etc/haproxy
# cp /usr/local/src/haproxy-1.5.11/examples/examples.cfg /etc/haproxy/haproxy.cfg
# mkdir -p /var/lib/haproxy
# touch /var/lib/haproxy/stats
# vi /etc/haproxy/haproxy.cfg
    global
        # 设置日志
        log         127.0.0.1 local2 info
        chroot    /var/lib/haproxy
        pidfile     /var/run/haproxy.pid
        # 最大链接数
        maxconn     256
        # 运行的用户
        user        haproxy
        group       haproxy
        # 启动服务
        daemon
        # 最大SSL链接数
        maxsslconn     256
        # Diffie-Hellman
        tune.ssl.default-dh-param 2048
        # 运行HAProxy的线程数(建议为1)
        nbproc  1

    defaults
        # Layer4负载均衡
        mode              tcp
        # 日志设置继承global
        log                global
        # 获取HTTP请求日志
        option             httplog
        # 后端未响应的超时时间
        timeout connect    10s
        # 后端的超时时间
        timeout client     30s
        # 服务器超时时间
        timeout server     30s

    # 前端定义 ( http-in 为任意字符 )
    frontend http-in
        # 监听80端口
        bind *:80
        # 默认的后端定义
        default_backend    backend_servers
        # 传递X-Forwarded-For
        option             forwardfor
        # 监听443端口
        bind *:443 ssl crt /etc/rensn/certs/haproxy.pem

    # 后端定义
    backend backend_servers
        # 负载均衡方式
        balance            roundrobin
        # 后端服务器的定义
        server             www01 192.168.21.100:80 check
        server             www02 192.168.21.110:80 check
        server             www02 192.168.21.120:80 check disabled
# service haproxy start


(5)keepalived设置
# vi /etc/keepalived/haproxy_servers.conf
   vrrp_script chk_haproxy {
      script "killall -0 haproxy"   # verify the pid existance
      interval 2                    # check every 2 seconds
      weight 2                      # add 2 points of prio if OK
   }
   
   vrrp_instance VI_1 {
      state MASTER              # MASTER on master, BACKUP on backup
      interface eth1                # interface to monitor
      virtual_router_id 51          # Assign one ID for this route (tcpdump vrrp)
      priority 101                  # 101 on master, 100 on backup
      virtual_ipaddress {
          192.168.21.100            # the virtual IP
      }
      track_script {
          chk_haproxy
      }
   }
# /etc/init.d/keepalived restart


(三)LVS

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


(2)网络设置
# vi /etc/sysctl.conf
    net.ipv4.ip_forward = 1
    net.ipv4.conf.default.rp_filter = 0
# sysctl -p
# cat /proc/sys/net/ipv4/ip_forward 
    1
# sevice network restart


(3)LB设置
# ipvsadm -C
# ipvsadm -A -t 192.168.21.100:80
# ipvsadm -ln
# service ipvsadm save


(4)keepalived设置
# vi /etc/keepalived/lvs_http_servers.conf
    virtual_server <lvs_srv_ip> 80 {
      delay_loop   20
      lvs_sched    lc
      lvs_method   NAT
      protocol     TCP

      real_server  <web1_srv_ip> 80 {
        weight 1
        inhibit_on_failure
        HTTP_GET {
          url {
            path /
            status_code 200
          }
          connect_timeout 5
          nb_get_retry 3
          delay_before_retry 20
        }
      }
     real_server  <web2_srv_ip> 80 {
        weight 1
        inhibit_on_failure
        HTTP_GET {
          url {
            path /
            status_code 200
          }
          connect_timeout 5
          nb_get_retry 3
          delay_before_retry 20
        }
    }
# /etc/init.d/keepalived restart


搭建 CentOS 6 服务器(15) - Keepalived、HAProxy、LVS

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
一、理论知识(原理) 我们不仅要知其然,而且要知其所以然,所以先给大家准备一些理论知识课,这样
http://www.bitbi.biz/centos-6-2-keepalivedhaproxy-%E5%AE%89%E8%A3%85%E6%B5%8B%E8%AF%95-2912/
(一)MySQL (1)安装 # yum -y install http://dev.mysql.com/get/mysql-community-release-el6-5.no
(一)MySQL (1)安装 # yum -y install http://dev.mysql.com/get/mysql-community-release-el6-5.no
以下是在VirtualBox里的安装过程: 1. 选择“Select Install or upgrade existing system” 2. 选择
以下是在VirtualBox里的安装过程: 1. 选择“Select Install or upgrade existing system” 2. 选择
(1)安装Rails 1)安装需要的Package # yum -y install gcc # yum -y install gcc-c++ # yum -y ins
(1)安装Rails 1)安装需要的Package # yum -y install gcc # yum -y install gcc-c++ # yum -y ins
Centos6.5搭建lvs(dr)+keepalived集群 一、原理 ① client(192.168.6.58)向目标vip发出请求,Dire
转:http://www.vpsa.net/?post=4 lvs + keepalived负载均衡上线差不多一年,非常稳定,没出过问题,就
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号