hjkn 发表于 2015-9-5 08:19:15

ha + lvs 整和

  
环境:关闭火墙
   时间同步
   yum配置全

                                                      HA+lvs    ( 高可用+附带均衡 )

               VS         RS ( rr )
         |       /
         VIP ---HA+IB                     
         |       \   
               VS         RS
                        

ip -->调度器 --> 应用服务
ha( 调度器 ) 192.168.2.77( 主 )
            192.168.2.62( 副 )
lvs( 后端应用服务 )192.168.2.53
                   192.168.2.120
vip( 虚拟ip )   192.168.2.117
* 1.调度器 ( ha )
    (1)安装包lftp i
                get heartbeat-3.0.4-2.el6.x86_64.rpmheartbeat-devel-3.0.4-2.el6.x86_64.rpmheartbeat-libs-3.0.4-2.el6.x86_64.rpm
                yum localinstall -y *
    (2)脚本( 在这三个目录下写的脚本都可以识别 )
                ll -d /etc/init.d/ /etc/ha.d/rc.d/   /etc/ha.d/resource.d/
    (3)复制配置模板
                cd /usr/share/doc/heartbeat-3.0.4/
                cp ha.cf haresources authkeys /etc/ha.d/
    (4)修改配置* vim /etc/ha.d/ha.cf
               logfile /var/log/ha-log( 29 )   ( 日志 )
               logfacility   local0   ( 34 )
               keepalive 2            ( 48 )   ( 心跳是2 )
               deadtime 30            ( 56 )   ( 停止30s就判断死亡 )
               warntime 10            ( 61 )   ( 停止10s就判断警告 )
               initdead 60            ( 71 )
               udpport 809            ( 76 )      ( 端口 ,测试时和其他的被管理集群端口不一样)
               bcast   eth0             ( 91 )      ( 广播 )
               auto_failback on         ( 157 )      ( 做主机)
         watchdog /dev/watchdog   ( 207 )
               node    server77.example.com( 211 )   ( 主 )( 必需是主机名 )
               node    server62.example.com( 212 )   ( 副 )
               ping 192.168.2.251       ( 220 )      ( 网关 )
               respawn hacluster /usr/lib64/heartbeat/ipfail( 253 )( 64位 )
               apiauth ipfail gid=haclient uid=hacluster      ( 259 ) ( 用户身份 )
         modprobe softdog
         ll /dev/watchdog
         crw-rw----. 1 root root 10, 130 Sep 27 11:32 /dev/watchdog
            *vim /etc/ha.d/authkeys
               auth 1 ( 23 )
               1 crc( 24 )( 最简单的 )
                chmod 600 /etc/ha.d/authkeys ( 修改权限 )
            *vim /etc/ha.d/haresources
               server77.example.com IPaddr::192.168.2.117/24/eth0 httpd ( 150,此处写的ip是主的被管理机,此ip是没有人使用的 )
    (5)复制给其他被管理主机
                scp /etc/ha.d/haresources /etc/ha.d/authkeys /etc/ha.d/ha.cf 192.168.2.62:/etc/ha.d/
    (6)安装http yum install -y httpd
      如果先做的lvs需要刷新策略   ipvsadm -C   (ipvsadm -l )
      重启网络ip addr show      
    (7)开启服务 /etc/init.d/heartbeat start
    (8)测试    vim /var/www/html/index.html ( server77 )
                页面测试:192.168.2.117( server77 )
    (9)查看日志 tail -f /var/log/ha-log
    (10)查看ip ip addr show
另一个被管理主机
    (1)安装http yum install -y httpd
    (2)安装包   yum localinstall -y *
    (3)开启服务 /etc/init.d/heartbeat start
    (1)进行测试 ( 在一个被管理主机上停一个服务,则访问页面到另一个被管理主机的测试页面 )
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

官方网站:http://zh.linuxvirtualserver.org/
2. lvs ( linux 虚拟服务器 )
client --> IB -- web( 多个 ) 当用户访问量很多时需要负载均衡集群( lvs( 数据库 应用层使用 ) nginx ( 网站访问使用 ) haproxy ( 并发量很大 网站访问使用 ))
有三种模式NAT 隧道 DR
                  VIP      ===         VIP
DR :client --> VS ( VIP 虚拟ip )--> RS ( no-arp )--> client
      m1            m2                  m3
      m1 --> m2 时 变为m1 --> m3,在 RS 上也有VIP和 VS 上的VIP相同,在同一个网内不可以有相同的ip,所以用 no-arp 设置不让RS上的VIP访问外网

client( mac1 )--> lB( 装换mac地址mac2--mac3 ) --> rs --> client
lB-- 指的是 ha主机 ( 调度器 )
rs-- 指的是 lvs主机( 附带均衡 )
1.ha( 调度主机vs)
   ** 把heartbeat停掉后,1-3操作两台都需要做,但是 4-6操作只需要在一台调度机操作即可
    (1)停heartbeat服务/etc/init.d/heartbeat stop( 两台调度机都需要停 )
                         /etc/init.d/heartbeat stop
    (2)安装 ipvsadm服务 yum install -y ipvsadm   (两台调度机都需要装)
                        yum install -y ipvsadm
    (3)解析(此步可不做)vim /etc/hosts ( 如果做解析,则都需要做 )
                         192.168.2.77    server77.example.com
                         192.168.2.62    server62.example.com
                         192.168.2.53    server53.example.com
                         192.168.2.1    server1.example.com
                         192.168.2.120   server120.example.com
    (4)添加虚拟ip   ifconfig eth0:0 192.168.2.117 netmask 255.255.255.0 up
                  ifconfig ( 多了一个vip(虚拟ip))
    (5)加载 rule    -A( 添加服务 )-t ( tcp ) -s ( 方式 )rr ( 轮寻 ) -g( 直连 ) -r ( RS--real server )
                     ipvsadm -A -t 192.168.2.117:80 -s rr      
                     ipvsadm -a -t 192.168.2.117:80 -r 192.168.2.53:80 -g( lvs主机 53 )
                     ipvsadm -a -t 192.168.2.117:80 -r 192.168.2.120:80 -g ( lvs主机 120 )
            etc/init.d/iptables save ( 保存 )
    (6)查看         ipvsadm -l
                     结果:IP Virtual Server version 1.2.1 (size=4096)
                            Prot LocalAddress:Port Scheduler Flags
                            -> RemoteAddress:Port         Forward Weight ActiveConn InActConn
                            TCP192.168.2.117:http rr
                            -> server53.example.com:http    Route   1      0          0         
                            -> server120.example.com:http   Route   1      0          0
                           ( 如果写了解析则是上面的情况是主机名,如果没有写解析则是 ip )
2.lvs ( linux 虚拟服务器 rs)
   ** 1-8操作,在192.168.2.53和192.168.2.120 都需要操作
    (1)安装httpyum install -y httpd
    (2)测试页面   vim /var/www/html/index.html ( 53主机 内容:53 ) ( 120主机 内容:120 )
    (3)绑定vip   ifconfig eth0:0 192.168.2.117 netmask 255.255.255.255 up
    (4)安装arptables_jf服务
                  yum install -y arptables_jf
            arptables 基本思路和iptables一样,不过,arptables处理arp协议有关的包,这些包在iptables中并不会处理·arptables可用于灵活的arp管理,
          如果善于运用的话,不失为一个优秀的arp防火墙.既能防止别的机器对自己进行arp欺骗,又能防止本机病毒或错误程序向其他机器发起arp攻击·
    (5)加载规则    -A, --append chain rule-specification追加规则
         -L, --list 列出规则
         -d, --destination [!] address目的地址
         -s, --source [!] address源地址
         -j, --jump target跳到目标
          arptables -nL ( 查看 )
                  arptables -A IN -d 192.168.2.117 -j DROP   ( 把输入的VIP关掉 )
                  arptables -A OUT -s 192.168.2.117 -j mangle --mangle-ip-s 192.168.2.53 ( 120 ) ( 输出时把VIP装换成自己的地址 )
                  ifconfig( 查看多了一个vip )
    (6)保存规则   /etc/init.d/arptables_jf save
    (7)开启http/etc/init.d/httpd start
    (8)关火墙    iptables -nL

3.测试( 测试机不可以是ha或者是lvs,必须是他们以外的主机才可以测试 )
    (1)网页测试192.168.2.117 ( 不断刷新页面则会出现 53 120 不断切换的页面 )
    (2)在测试主机上arp -a
               结果:? (192.168.2.117) at 52:54:00:44:71:d7 on br0
                     ( 52:54:00:44:71:d7 )此mac地址是ha( 调度机的mac 不是lvs主机的mac )
               在ha( 调度机 ) ifconfig
               结果:eth0      Link encap:EthernetHWaddr 52:54:00:44:71:D7
                     ( HWaddr 52:54:00:44:71:D7 )此mac地址和测试机看到的一样
出现问题:
    (1)刷新页面时页面不动( 是直接访问到了lvs主机,没有访问到ha,应该是lvs主机操作出现了问题 )
    (2)当访问的mac不是ha的mac时,重新加载后页面没有变化,需要删除原来的mac重新检测即可
               arp -d 192.168.1.117( 在测试主机上,删除原来访问的记录 )

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ha和lvs整合
调度器存在单点故障,当调度器坏了,lvs也用不了,所以需要做热备,把ha和lvx整合
/etc/init.d/httpd stop( 192.168.2.53 )( 在lvs上 )
再次访问页面时,会出现错误和120
查看调度列表时两个都在调度( 在ha上 )
检测后端服务的健康状况,维护调度列表
ha( 调度端 )
    (1)把ip停了 ipvsadm -C ( 清除调度表 )
                ifconfig eth0:0 down
    (2)下载包lftp i( 两个调度都需要安装 )
                yum localinstall -y ldirectord-3.9.2-1.2.x86_64.rpm
                /etc/init.d/ldirectord status( 查看是否缺包 )
                yum install -y perl-IO-Socket-INET6
    (3)复制配置文件
                cp /usr/share/doc/packages/ldirectord/ldirectord.cf /etc/ha.d/
                复制原因:vim /etc/init.d/ldirectord
    (4)修改配置文件
            (1) vim /etc/ha.d/ldirectord.cf
                  Sample for an http virtual service
                  virtual=192.168.2.117:80 ( 虚拟ip )
                  real=192.168.2.53:80 gate ( lvs )
                  real=192.168.2.120:80 gate ( lvs )
                  fallback=127.0.0.1:80 gate
                  service=http
                  scheduler=rr
                  #persistent=600
                  #netmask=255.255.255.255
                  protocol=tcp
                  checktype=negotiate
                  checkport=80
                  request="index.html"
          #       receive="Test Page"
          #       virtualhost=www.x.y.z
               scp /etc/ha.d/ldirectord.cf 192.168.2.77:/etc/ha.d/
         (2)vim /etc/ha.d/haresources
                  server77.example.com IPaddr::192.168.2.117/24/eth0 httpd ldirectord
                scp /etc/ha.d/haresources 192.168.2.77:/etc/ha.d/
    (5)开启heartbeat (两个调度都需要开启 )
                /etc/init.d/heartbeat start
    (6)检测
          看日志tail -f /var/log/messages
          调度端口ipvsadm -l
          查看vip在哪调度   ip addr show
    当lvs1主机的httpd挂了的时候
            /etc/init.d/httpd stop   ( 53主机 )
            ipvsadm -l
            TCP192.168.2.177:http rr
            -> server120.example.com:http    Route   1      0          5   
            http://192.168.2.177
            server120
    当lvs1主机和lvs2主机都挂了的时候
            /etc/init.d/httpd stop( 53 )
            /etc/init.d/httpd stop( 120 )
            ipvsadm -l            
            TCP192.168.2.177:http rr
            -> localhost:http               Local   1      0          0
            http://192.168.2.177
            server77
页: [1]
查看完整版本: ha + lvs 整和