CentOS6下的 LB集群

先做初始化设置:https://blog.csdn.net/n_u_l_l_/article/details/103693449

要先做简单的规划:
CentOS6
RIP1 172.16.12.11
RIP2 172.16.12.12
PIP 172.16.12.21
SIP 172.16.12.22
VIP 172.16.12.20

RIP :

rip1、

[root@rip1 ~]# yum install httpd -y

[root@rip1 ~]# cd /var/www/html/
[root@rip1 html]# ls
[root@rip1 html]# echo -e "<h1>RIP1<h1>\n<h1>172.16.12.11<h1>" >> index.html
[root@rip1 html]# cat index.html 
<h1>RIP1<h1>
<h1>172.16.12.11<h1>

[root@rip1 html]# /etc/init.d/httpd start 
Starting httpd: httpd: apr_sockaddr_info_get() failed for rip1
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [  OK  ]
[root@rip1 html]# chkconfig httpd on   	//开机自启

rip2、

[root@rip2 ~]# yum install httpd -y

[root@rip2 ~]# cd /var/www/html/
[root@rip2 html]# ls
[root@rip2 html]# echo -e "<h1>RIP1<h1>\n<h1>172.16.12.12<h1>" >> index.html
[root@rip2 html]# cat index.html 
<h1>RIP2<h1>
<h1>172.16.12.12<h1>

[root@rip2 html]# /etc/init.d/httpd start 
Starting httpd: httpd: apr_sockaddr_info_get() failed for rip2
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [  OK  ]
[root@rip2 html]# chkconfig httpd on

绑定VIP和配置ARP防火墙:
装软件包

[root@rip1 html]# yum install openssh-clients -y
  • arptables 可以当作是linux下的ARP防火墙

  • arptables 是一个用户空间,用于管理内核中的ARP规则表,规则检查处理的是ARP数据帧。(arptables 类似 iptable,但比iptables简单,它需要载入内核模块arptable_filter)。

  • 正常情况下,arptable_filter 只有一个表filter ,不指定-t 表名 时默认就是filter 表。

  • filter 表有两个链,一个是IN,表示外面发进来的ARP包;另外一个是OUT ,表示本机发出的ARP包。

  • 内建的动作:ACCEPT 放行ARP包;DROP 丢掉ARP包;CONTINUE 继续下一规则;RETURN 不在这个链中继续进行匹配,返回到上一条链的下一条规则.

写个脚本具体实现:
分别发送给两个RIP,脚本里的IP切换成成具体RIP的的IP。

#!/bin/bash
VIP=172.16.x.x
RIP=172.16.x.x
arptables -F
arptables -A IN -d $VIP -j DROP
arptables -A OUT -s $VIP -j mangle --mangle-ip-s $RIP
/sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth0:1

我的就叫这个了。

[root@rip1 ~]# chmod +x  arptables-config.sh 
[root@rip1 ~]# ./arptables-config.sh

执行后输入指令就能看到了:

[root@rip1 ~]# arptables -L 
Chain IN (policy ACCEPT)
target     source-ip            destination-ip       source-hw          destination-hw     hlen   op         hrd        pro       
DROP       anywhere             172.16.12.20         anywhere           anywhere           any    any        any        any       

Chain OUT (policy ACCEPT)
target     source-ip            destination-ip       source-hw          destination-hw     hlen   op         hrd        pro       
mangle     172.16.12.20         anywhere             anywhere           anywhere           any    any        any        any       --mangle-ip-s 172.16.12.11 

Chain FORWARD (policy ACCEPT)
target     source-ip            destination-ip       source-hw          destination-hw     hlen   op         hrd        pro 

ifconfig也能看到VIP。 eth0:1

[root@rip1 ~]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:F6:AA:FE  
          inet addr:172.16.12.11  Bcast:172.16.255.255  Mask:255.255.0.0

eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:F6:AA:FE  
          inet addr:172.16.12.20  Bcast:172.16.12.20  Mask:255.255.255.255

然后就可以设置开机自启了。

[root@rip1 ~]# echo "/root/arptables-config.sh" >> /etc/rc.local 
[root@rip2 ~]# echo "/root/arptables-config.sh" >> /etc/rc.local 

centos6rc.local本来就有执行权限所以不用再分配了。
两个RIP都这样配置。

PIP配置:
[root@pip ~]# vim /etc/sysctl.conf 
[root@pip ~]# sysctl -p 
net.ipv4.ip_forward = 1    <------这个改成1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
[root@pip ~]# yum install piranha -y 

[root@pip ~]# piranha-passwd
New Password: <------设置密码和确认密码
Verify: 
Adding password for user piranha

启动web页面的那个服务:
注意:piranha这个软件有2个服务一个是web的页面,一个是真正的负载均衡服务。现在启动的是web页面。

[root@pip ~]# /etc/init.d/piranha-gui start 

然后在浏览器访问:
http://172.16.12.21:3636/ IP是PIP的
用户名:piranha
密码:就是上边设置的密码。

在这里插入图片描述
点击login登录。

在这里插入图片描述
输入用户名和密码。

在这里插入图片描述
就能看到首页了。

在这里插入图片描述
第二个模块。写PIP的,下边的默认就行。路由算法默认是DR

在这里插入图片描述
第三个模块,启用

在这里插入图片描述
写SIP的信息

在这里插入图片描述
第四个模块 添加

在这里插入图片描述
第四模块的第1子模块

在这里插入图片描述
第四模块的第2子模块

real server

在这里插入图片描述
有几个RIP就添加几个,我的是2个 ,最后是这样。

在这里插入图片描述
第四模块第3子模块,是用来做可用性检查的。

要写个检查脚本:

[root@pip ~]# vim health-httpd.sh
#!/bin/bash
#检查 httpd 的健康状况

curl $1 &> /dev/null
if [ $? -eq 0 ]
then
        echo "ok"
else
        echo "filed"
fi


然后测试下。
[root@pip ~]# chmod +x health-httpd.sh 
[root@pip ~]# ./health-httpd.sh 172.16.12.11
ok
[root@pip ~]# ./health-httpd.sh 172.16.12.12
ok
[root@pip ~]# ./health-httpd.sh 172.16.12.44
filed
这就ok了。

[root@pip ~]# ln health-httpd.sh /bin/
然后链接到bin下。

发到SIP那里。

[root@pip ~]# scp health-httpd.sh 172.16.12.22:/bin/

SIP测试脚本:

[root@sip ~]# health-httpd.sh 172.16.12.11
ok
[root@sip ~]# health-httpd.sh 172.16.12.12
ok

都通过的话就可以在网页输入了:

在这里插入图片描述
然后保存就可以了。

这里的配置文件不用写,但是是通过web网页来配置的。
就是下边这个。打开后就能看到在网页设置的东西了

[root@pip ~]# vim /etc/sysconfig/ha/lvs.cf 

serial_no = 15
primary = 172.16.12.21
service = lvs
backup_active = 1
backup = 172.16.12.22
heartbeat = 1
heartbeat_port = 539
keepalive = 6
deadtime = 18
network = direct
debug_level = NONE
monitor_links = 1
syncdaemon = 0
virtual LB-httpd {
     active = 1
     address = 172.16.12.20 eth0:1
     vip_nmask = 255.255.0.0
     port = 80
     persistent = 50
     pmask = 255.255.255.255
     send = "GET / HTTP/1.0\r\n\r\n"
     expect = "ok"
     use_regex = 0
     send_program = "/bin/health-httpd.sh %h"
     load_monitor = none
     scheduler = wlc
     protocol = tcp
SIP配置:
[root@sip ~]# vim /etc/sysctl.conf 
[root@sip ~]# sysctl -p 

安装软件:

[root@sip ~]# yum install piranha -y

PIP将配置文件发给SIP:

[root@pip ~]# scp /etc/sysconfig/ha/lvs.cf 172.16.12.22:/etc/sysconfig/ha/

PIP启动服务:

[root@pip ~]# /etc/init.d/pulse start 
Starting pulse:                                            [  OK  ]
[root@pip ~]# chkconfig pulse on 

SIP启动服务:

[root@sip ~]# /etc/init.d/pulse start 
Starting pulse:                                            [  OK  ]
[root@sip ~]# chkconfig pulse on 

PIP上能看到浮动IP 了:

[root@pip ~]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:84:0C:FA  
          inet addr:172.16.12.21  Bcast:172.16.255.255  Mask:255.255.0.0

eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:84:0C:FA  
          inet addr:172.16.12.20  Bcast:172.16.255.255  Mask:255.255.0.0

SIP上没有浮动IP

访问测试:

在这里插入图片描述
能看到测试页。证明ok

测试高可用:

1、关掉PIP:

PIP:poweroff

SIP:浮动IP跑到SIP上了,这个会有延迟,要等一会

[root@sip ~]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:CA:E6:7D  
          inet addr:172.16.12.22  Bcast:172.16.255.255  Mask:255.255.0.0

eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:CA:E6:7D  
          inet addr:172.16.12.20  Bcast:172.16.255.255  Mask:255.255.0.0

服务没有停止。

2、现在访问的是RIP1 那里,然后停掉RIP1。

在这里插入图片描述
RIP1:

[root@rip1 ~]# /etc/init.d/httpd stop
Stopping httpd:                                            [  OK  ]

然后刷新网页发现到RIP2 了

在这里插入图片描述
到这里高可用和负载均衡基本完成了,关闭1台机器,总能保证服务不停止。

再加一组

nginx1:172.16.12.13
nginx2:172.16.12.14
VIP:172.16.12.20

RIP:
这里VIP是一样的,但是端口不同。nginx设置为8000

server {
        listen       8000;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }

启动服务8000端口

[root@rip3-nginx ~]# netstat -antlup | grep nginx 
tcp        0      0 0.0.0.0:8000                0.0.0.0:*                   LISTEN      1404/nginx  

修改测试页:

[root@rip3-nginx html]# cat index.html 
<h1>RIP3 nginx 172.16.12.13<h1>
[root@rip4-nginx html]# cat index.html 
<h1>RIP4 nginx 172.16.12.14<h1>

PIP:
写健康探测脚本:

[root@pip ~]# vim health-nginx.sh 
#!/bin/bash
#检查 nginx 的健康状况

curl $1:8000 &> /dev/null
if [ $? -eq 0 ]
then
        echo "ok"
else
        echo "filed"
fi
就复制httpd的改就好了

[root@pip ~]# chmod +x health-nginx.sh 
[root@pip ~]# ./health-nginx.sh 172.16.12.13
ok
[root@pip ~]# ./health-nginx.sh 172.16.12.14
ok


[root@pip ~]# ln health-nginx.sh /bin/

[root@pip ~]# scp health-nginx.sh 172.16.12.22:/bin/

SIP:

[root@sip ~]# health-nginx.sh 172.16.12.13
ok
[root@sip ~]# health-nginx.sh 172.16.12.14
ok

然后直接进网页进行配置:

在这里插入图片描述加一组,别的不用动,里边记得修改端口IP还有健康监测脚本就好了。

配置完保存,然后再配置文件中就能看到已经新增加了nginx的那些。

     }
     server rip2-httpd {
         address = 172.16.12.12
         active = 1
         port = 80
         weight = 10
     }
}
virtual LB-nginx {
     active = 1
     address = 172.16.12.20 eth0:1
     vip_nmask = 255.255.0.0
     port = 8000
     persistent = 50
     pmask = 255.255.255.255
     send = "GET / HTTP/1.0\r\n\r\n"
     expect = "ok"
     use_regex = 0
     send_program = "/bin/health-nginx.sh %h"
     load_monitor = none

PIP发给SIP

[root@pip ~]# scp /etc/sysconfig/ha/lvs.cf 172.16.12.22:/etc/sysconfig/ha/

PIP和SIP重启服务:

[root@pip ~]# /etc/init.d/pulse restart 
Shutting down pulse:                                       [  OK  ]
Starting pulse:                                            [  OK  ]

查看状态。有2组。

[root@sip ~]#  ipvsadm -L -n

IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.12.20:80 wlc persistent 50
  -> 172.16.12.11:80              Route   1      0          0         
  -> 172.16.12.12:80              Route   10     0          0         
TCP  172.16.12.20:8000 wlc persistent 50
  -> 172.16.12.13:8000            Route   10     0          0         
  -> 172.16.12.14:8000            Route   10     0          0 

访问测试:加端口8000

在这里插入图片描述看到测试页。OK

加集中存储NFS

NFS:172.16.12.23

安装nfs

yum install nfs-* -y

配置共享

[root@jq-c6-nfs lbnginx]# vim /etc/exports
/lbnfs 172.16.0.0/16(rw,sync)
/lbnginx 172.16.0.0/16(rw,sync)

httpd测试页:

[root@jq-c6-nfs /]# cd lbnfs/
[root@jq-c6-nfs lbnfs]# cat index.html 
<h1>NFS<h1>
<h1>172.16.12.23<h1>
<h1>httpd<h1>

nginx测试页:

[root@jq-c6-nfs /]# cd lbnginx/
[root@jq-c6-nfs lbnginx]# cat index.html 
<h1>NFS<h1>
<h1>172.16.12.23<h1>
<h1>nginx<h1>
[root@jq-c6-nfs /]# /etc/init.d/rpcbind restart 
Stopping rpcbind:                                          [  OK  ]
Starting rpcbind:                                          [  OK  ]
[root@jq-c6-nfs /]# /etc/init.d/nfs restart 
Shutting down NFS daemon:                                  [  OK  ]
Shutting down NFS mountd:                                  [  OK  ]
Shutting down NFS services:                                [  OK  ]
Shutting down RPC idmapd:                                  [  OK  ]
Starting NFS services:                                     [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]

[root@jq-c6-nfs /]# chkconfig rpcbind on
[root@jq-c6-nfs /]# chkconfig nfs on

RIP1:httpd

[root@rip1 www]# mount 172.16.12.23:/lbnfs html/
[root@rip1 www]# cd html/
[root@rip1 html]# ls
index.html
[root@rip1 html]# cat index.html 
<h1>NFS<h1>
<h1>172.16.12.23<h1>
<h1>httpd<h1>

能看到nfs共享出来的内容。

[root@rip1 www]# echo "mount 172.16.12.23:/lbnfs html/" >> /etc/rc.local 

直接写到这里实现开机自动挂载。

RIP2 :类似

[root@rip2 ~]# mount 172.16.12.23:/lbnfs /var/www/html
[root@rip2 ~]# cat /var/www/html/index.html 
<h1>NFS<h1>
<h1>172.16.12.23<h1>
<h1>httpd<h1>
[root@rip2 ~]# 

nginx也一样。

然后直接测试:

在这里插入图片描述

在这里插入图片描述

然后进行健壮性测试,重启关机等等,都能访问。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值