@Great-Chinese
2016-12-28T09:56:11.000000Z
字数 6649
阅读 911
Linux系统架构
1,配置主服务器 192.168.31.127
# 临时修改hostname为masterhostname masterbash # 测试一下# 永久性更改hostname,修改下面的配置文件vim /etc/sysconfig/network# 关闭防火墙iptables -Fgetenforcevim /etc/hosts # 修改指向192.168.31.127 master192.168.31.116 slave# 下载epel包wget www.lishiming.net/data/attachment/forum/epel-release-6-8_64.noarch.rpm# 安装epel包rpm -ivh epel-release-6-8_64.noarch.rpm# 安装heartbeatyum install -y heartbeat# 安装libnetyum install -y libnet
2,配置备选端 192.168.31.116
# 临时修改hostname为slavehostname slavebash # 测试一下# 永久性更改hostname,修改下面的配置文件vim /etc/sysconfig/network# 关闭防火墙iptables -nvLgetenforcevim /etc/hosts # 增加内容如下192.168.31.127 master192.168.31.116 slavebash 测试一下# 下载epel包wget www.lishiming.net/data/attachment/forum/epel-release-6-8_64.noarch.rpm# 安装epel包rpm -ivh epel-release-6-8_64.noarch.rpm# 安装heartbeatyum install -y heartbeat# 安装libnetyum install -y libnet
1,配置主服务器 192.168.31.127
cd /usr/share/doc/heartbeat-3.0.4/cp authkeys ha.cf haresources /etc/ha.d/cd /etc/ha.d# 主从验证vim authkeysauth 3#1 crc#2 sha1 HI!3 md5 Hello!chmod 600 authkeys# 配置haresourcesvim haresourcesmaster 192.168.31.110/24/eth0:0 nginx# 配置ha.cfvim ha.cfdebugfile /var/log/ha-debuglogfile /var/log/ha-loglogfacility local0keepalive 2deadtime 30warntime 10initdead 60udpport 694ucast eth0 192.168.31.116auto_failback onnode masternode slaveping 192.168.31.1respawn hacluster /usr/lib64/heartbeat/ipfail# 配置好上面的3个文件后,就上传到备选端scp authkeys haresources ha.cf slave:/etc/ha.d/# 安装nginxyum install -y nginx
2,配置备选端 192.168.31.116
authkeys haresources # 这两个文件不用更改cd /usr/share/doc/heartbeat-3.0.4/cp authkeys ha.cf haresources /etc/ha.d/cd /etc/ha.d# 配置ha.cfvim ha.cf # 其它不变,只更改下面的ip地址为对方的地址ucast eth0 192.168.31.127# 安装nginxyum install -y nginx
3,测试
# 在主服务器上启动heartbeat/etc/init.d/heartbeat startifconfigps aux |grep nginx# 在主服务器上编辑echo "master melody0113" > /usr/share/nginx/html/index.html,那么在web网上打开192.168.31.110 显示master melody0113# 在备选端编辑echo "salve gary88" > /usr/share/nginx/html/index.html,那么在eb网上打开192.168.31.110 显示salve gary88# 在主服务器上关闭iptablesiptables -A INPUT -p icmp -j DROP,然后再刷新网业就会跳转到备选端,显示salve gary88# 在主服务器上删除iptables这条命令iptables -D INPUT -p icmp -j DROP# 停止启动heartbeat/etc/init.d/heartbeat stop
负载均衡开源软件有 nginx、lvs、keepalived
商业的硬件负载设备 F5、Netscale
NAT模式-网络地址转换 Virtualserver via Network address translation(VS/NAT)
TUN模式 virtual server via ip tunneling模式
DR模式-直接路由模式 Virtual server via direct routing (vs/dr)
LVS由前端的负载均衡器(Load Balancer,LB)和后端的真实服务器(Real Server,RS)群组成
RR:轮循调度(Round Robin)
WRR:加权轮循(Weight RR)
LC:最少链接(Least Connections)
WLC:加权最少连接(默认采用的就是这种)(Weighted Least Connections)
hostname dirbashyum install -y ipvsadm# 编辑此配置文件vim /usr/local/sbin/lvs_nat.sh # 增加内容如下#! /bin/bash# director 服务器上开启路由转发功能:echo 1 > /proc/sys/net/ipv4/ip_forward# 关闭icmp的重定向echo 0 > /proc/sys/net/ipv4/conf/all/send_redirectsecho 0 > /proc/sys/net/ipv4/conf/default/send_redirectsecho 0 > /proc/sys/net/ipv4/conf/eth0/send_redirectsecho 0 > /proc/sys/net/ipv4/conf/eth1/send_redirects# director 设置nat防火墙iptables -t nat -Fiptables -t nat -Xiptables -t nat -A POSTROUTING -s 192.168.139.0/24 -j MASQUERADE# director设置ipvsadmIPVSADM='/sbin/ipvsadm'$IPVSADM -C$IPVSADM -A -t 192.168.139.111:80 -s rr$IPVSADM -a -t 192.168.139.111:80 -r 192.168.31.116:80 -m$IPVSADM -a -t 192.168.139.111:80 -r 192.168.31.127:80 -m# 执行此配置文件sh /usr/local/sbin/lvs_nat.shipvsadm -ln # 查看具体IP# 修改权重$IPVSADM -A -t 192.168.139.111:80 -s wrr$IPVSADM -a -t 192.168.139.111:80 -r 192.168.31.116:80 -m -w 2$IPVSADM -a -t 192.168.139.111:80 -r 192.168.31.133:80 -m -w 1
hostname rs1bash# 编辑此文件vim /etc/sysconfig/network-scripts/ifcfg-eth0 # 更改下面的网关地址GATEWAY=192.168.31.133(主机内网ip)# 启动网卡ifdown eth0; ifup eth0# 启动80端口/etc/init.d/nginx startcurl localhost
hostname rs2bash# 编辑此文件vim /etc/sysconfig/network-scripts/ifcfg-eth0 # 更改下面的网关地址GATEWAY=192.168.31.133(主机内网ip)# 启动网卡ifdown eth0; ifup eth0# 启动80端口/etc/init.d/nginx startcurl localhost
ipvsadm -C # 先清除nat的所有规则ipvsadm -ln # 查看iptables -t nat -F # 再清除防火墙规则ifdown eth1; # 再关闭多的网卡# 编辑此配置文件vim /usr/local/sbin/lvs_dr.sh # 增加内容如下#! /bin/bashecho 1 > /proc/sys/net/ipv4/ip_forwardipv=/sbin/ipvsadmvip=192.168.31.111 # 虚拟IPrs1=192.168.31.128rs2=192.168.31.129ifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 uproute add -host $vip dev eth0:0$ipv -C$ipv -A -t $vip:80 -s rr$ipv -a -t $vip:80 -r $rs1:80 -g -w 1$ipv -a -t $vip:80 -r $rs2:80 -g -w 1# 执行此文件sh /usr/local/sbin/lvs_dr.sh# 修改权重 先down再开启--2ifconfig eth0:0 downifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 uproute add -host $vip dev eth0:0$ipv -C$ipv -A -t $vip:80 -s wrr$ipv -a -t $vip:80 -r $rs1:80 -g -w 3$ipv -a -t $vip:80 -r $rs2:80 -g -w 1# 执行此文件--2sh /usr/local/sbin/lvs_dr.sh# 查看权重是否成功ipvsadm -ln
# 编辑此配置文件vim /usr/local/sbin/lvs_rs.sh #增加内容如下#! /bin/bashvip=192.168.31.111ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 uproute add -host $vip lo:0echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/lo/arp_announceecho "1" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/all/arp_announce# 执行此配置文件sh /usr/local/sbin/lvs_rs.sh# 启动vip,lo:0会出现ifconfig# 在web网业上测试,输入vip地址192.168.31.111
# 先清除ipvsadm所有规则ipvsadm -C# 安装keepalivedyum install -y keepalivedvim /etc/keepalived/keepalived.confvrrp_instance VI_1 {state MASTER #备用服务器上为 BACKUPinterface eth0virtual_router_id 51priority 100 #备用服务器上为90advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.31.111}}delay_loop 6 # (每隔10秒查询realserver状态)lb_algo rr # (lvs 算法)lb_kind DR # (Direct Route)persistence_timeout 50 # (同一IP的连接60秒内被分配到同一台realserver)protocol TCP # (用TCP协议检查realserver状态)real_server 192.168.31.128 80 {weight 100 # (权重)TCP_CHECK {connect_timeout 10 # (10秒无响应超时)nb_get_retry 3delay_before_retry 3connect_port 80}}real_server 192.168.31.129 80 {weight 100 # (权重)TCP_CHECK {connect_timeout 10 # (10秒无响应超时)nb_get_retry 3delay_before_retry 3connect_port 80}}}# 复制此文件到备选服务端上scp /etc/keepalived/keepalived.conf 192.168.31.128:/etc/keepalived/keepalived.conf# 查看没有有虚拟IPifconfigipvsadm -lnifconfig eth0:0 down# 启动keepalived,vip会自动启动/etc/init.d/keepalived start# 在另一台机器上测试curl 192.168.31.111
# 安装keepalivedyum install -y keepalived# 编辑此配置文件vim /etc/keepalived/keepalived.conf # 修改下面2行,其它内容不变state BACKUP #备用服务器上为 BACKUPpriority 99 #备用服务器上为90# 启动keepalived/etc/init.d/keepalived start# 测试,如果有一台机器down,系统会自动踢出掉curl 192.168.31.111
cd /usr/local/nginx/conf/vhosts# 针对域名vim lb.conf # 增加内容如下upstream melody {server 192.168.31.128:80 weight=2;server 192.168.31.129:80 weight=1;}server {listen 80;server_name www.123.com;location / {proxy_pass http://melody/;proxy_set_header Host $host;}}# 启动nginx/etc/init.d/nginx start# 测试curl -xlocalhost:80 www.123.com ??????????????????????????????????????????curl -x192.168.31.128:80 www.123.comcurl -x192.168.31.129:80 www.123.com
# 针对目录