@Great-Chinese
2016-12-28T09:56:11.000000Z
字数 6649
阅读 886
Linux系统架构
1,配置主服务器 192.168.31.127
# 临时修改hostname为master
hostname master
bash # 测试一下
# 永久性更改hostname,修改下面的配置文件
vim /etc/sysconfig/network
# 关闭防火墙
iptables -F
getenforce
vim /etc/hosts # 修改指向
192.168.31.127 master
192.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
# 安装heartbeat
yum install -y heartbeat
# 安装libnet
yum install -y libnet
2,配置备选端 192.168.31.116
# 临时修改hostname为slave
hostname slave
bash # 测试一下
# 永久性更改hostname,修改下面的配置文件
vim /etc/sysconfig/network
# 关闭防火墙
iptables -nvL
getenforce
vim /etc/hosts # 增加内容如下
192.168.31.127 master
192.168.31.116 slave
bash 测试一下
# 下载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
# 安装heartbeat
yum install -y heartbeat
# 安装libnet
yum 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 authkeys
auth 3
#1 crc
#2 sha1 HI!
3 md5 Hello!
chmod 600 authkeys
# 配置haresources
vim haresources
master 192.168.31.110/24/eth0:0 nginx
# 配置ha.cf
vim ha.cf
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 30
warntime 10
initdead 60
udpport 694
ucast eth0 192.168.31.116
auto_failback on
node master
node slave
ping 192.168.31.1
respawn hacluster /usr/lib64/heartbeat/ipfail
# 配置好上面的3个文件后,就上传到备选端
scp authkeys haresources ha.cf slave:/etc/ha.d/
# 安装nginx
yum 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.cf
vim ha.cf # 其它不变,只更改下面的ip地址为对方的地址
ucast eth0 192.168.31.127
# 安装nginx
yum install -y nginx
3,测试
# 在主服务器上启动heartbeat
/etc/init.d/heartbeat start
ifconfig
ps 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
# 在主服务器上关闭iptables
iptables -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 dir
bash
yum 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_redirects
echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/eth0/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/eth1/send_redirects
# director 设置nat防火墙
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 192.168.139.0/24 -j MASQUERADE
# director设置ipvsadm
IPVSADM='/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.sh
ipvsadm -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 rs1
bash
# 编辑此文件
vim /etc/sysconfig/network-scripts/ifcfg-eth0 # 更改下面的网关地址
GATEWAY=192.168.31.133(主机内网ip)
# 启动网卡
ifdown eth0; ifup eth0
# 启动80端口
/etc/init.d/nginx start
curl localhost
hostname rs2
bash
# 编辑此文件
vim /etc/sysconfig/network-scripts/ifcfg-eth0 # 更改下面的网关地址
GATEWAY=192.168.31.133(主机内网ip)
# 启动网卡
ifdown eth0; ifup eth0
# 启动80端口
/etc/init.d/nginx start
curl localhost
ipvsadm -C # 先清除nat的所有规则
ipvsadm -ln # 查看
iptables -t nat -F # 再清除防火墙规则
ifdown eth1; # 再关闭多的网卡
# 编辑此配置文件
vim /usr/local/sbin/lvs_dr.sh # 增加内容如下
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/sbin/ipvsadm
vip=192.168.31.111 # 虚拟IP
rs1=192.168.31.128
rs2=192.168.31.129
ifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 up
route 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再开启--2
ifconfig eth0:0 down
ifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 up
route 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
# 执行此文件--2
sh /usr/local/sbin/lvs_dr.sh
# 查看权重是否成功
ipvsadm -ln
# 编辑此配置文件
vim /usr/local/sbin/lvs_rs.sh #增加内容如下
#! /bin/bash
vip=192.168.31.111
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "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
# 安装keepalived
yum install -y keepalived
vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER #备用服务器上为 BACKUP
interface eth0
virtual_router_id 51
priority 100 #备用服务器上为90
advert_int 1
authentication {
auth_type PASS
auth_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 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.31.129 80 {
weight 100 # (权重)
TCP_CHECK {
connect_timeout 10 # (10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
# 复制此文件到备选服务端上
scp /etc/keepalived/keepalived.conf 192.168.31.128:/etc/keepalived/keepalived.conf
# 查看没有有虚拟IP
ifconfig
ipvsadm -ln
ifconfig eth0:0 down
# 启动keepalived,vip会自动启动
/etc/init.d/keepalived start
# 在另一台机器上测试
curl 192.168.31.111
# 安装keepalived
yum install -y keepalived
# 编辑此配置文件
vim /etc/keepalived/keepalived.conf # 修改下面2行,其它内容不变
state BACKUP #备用服务器上为 BACKUP
priority 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.com
curl -x192.168.31.129:80 www.123.com
# 针对目录