HA LB LVS KEEPALIVED
1HA = high available (主从 主宕掉之后,从自动启动工作)软件 heartbeat
需要关闭 iptables selinux
编辑 /etc/hosts 把两台主机的ip 主机名写进去(两台)
192.168.1.110 master
192.168.1.111 slave 安装heartbeat (yum安装.没有的话安装epel扩展源)
yum install -y heartbeat 由于heartbeat 依赖于包libnet 所以也需要安装
yum install -y libnet 配置(master机器)
需要复制一些配置文件样例
cd /usr/share/doc/heartbeat-3.0.4/
cp authkeys ha.cf haresources/etc/ha.d/ 编辑配置文件首先 需要编辑 authkeys(用来验证对方是否存活 验证方式crc:最简单 md5 第二 shal 最复杂用哪个把#去掉 )
更改权限
chmod 600 authkeys 更改haresources编辑如下一行
#node 10.0.0.170 Filesystem::/dev/sda1::data1::ext2 改为
master 192.168.1.222/24/eth0:0 nginx 主机名 VIP(流动ip) 监测的服务
编辑ha.cf 可改为
debugfile /var/log/ha-debug 排障log
logfile /var/log/ha-log log日志两个差不多
logfacility local0 日志级别
keepalive 2 2秒探测一次
deadtime 30 如果30s不通,就认为对方死掉了
warntime 10 10s不通,发出警告,记录在日志文件中
initdead 60 防止对方重启,预留的时间
udpport 694 心跳线的端口
ucast eth1 192.168.21.100 配置对方ip的可以用bcast(光标)
auto_failback on on如果被启动了,主回复了,被自动取消
node aming 主机1
node aming1 主机2
ping 192.168.21.1 仲裁者 判断一个存活 一般为路由器或交换机
respawn hacluster /usr/lib/heartbeat/ipfail 脚本 监测网络联通性 改完之后需要cp到从上去需要把ha.cf中ip改为对方ip
启动heartbeat (先主后从)
/etc/init.d/heartbeat start (自动启动nginx)
2LB =load balance (负载均衡)(多台服务器同时工作)
常见负载均衡软件 nginx 应用层级别7lvs 网络层4 keepalived
硬件设备F5 Netscale
lvs有三种模式 NAT/TUN/DR
详情可以查看 www.it165.net/admin/html/201401/2248.html
固定调度算法: rr,wrr,dh,sh
动态调度算法:wlc,lc,lblc,lblcr
3 lvs结合keepalived
安装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.0.100
}
}
virtual_server 192.168.0.100 80 {
delay_loop 6 #(每隔10秒查询realserver状态)
lb_algo wlc #(lvs 算法)
lb_kind DR #(Direct Route)
persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver)
protocol TCP #(用TCP协议检查realserver状态)
real_server 192.168.0.21 80 {
weight 100 #(权重)
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.0.22 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
} 以上为主director的配置文件,从director的配置文件只需要修改
state MASTER-> state BACKUP
priority 100 -> priority 90
配置完keepalived后,需要开启端口转发(主从都要做):
echo 1 > /proc/sys/net/ipv4/ip_forward
然后,两个rs上执行 /usr/local/sbin/lvs_dr_rs.sh 脚本
最后,两个director上启动keepalived服务(先主后从):
/etc/init.d/keepalived start
另外,需要注意的是,启动keepalived服务会自动生成vip和ipvsadm规则,不需要再去执行上面提到的/usr/local/sbin/lvs_dr.sh 脚本。
4nginx负载均衡
新增加一个配置文件
cd /usr/local/nginx/conf/vhosts/
vim lb.conf 写入
upstream fanfan {
server 192.168.1.1:80 (weight=2); (权重)
server 192.168.1.2:80 (weight=1);
}
(规定负载均衡有几台机器,都是什么)
server {
listen 80;
server_name www.123.com
location / {
proxy_pass http://fanfan/;
proxy_set_header Host $host;
}
}
(监听80端口域名为代理方式) 启动nginx
ipvsadm -C
iptables -F
页:
[1]