一个合格的集群诚该具有的特性:
(1)负载均衡 LVS Nginx Haproxy
(2)健康检查 for 调度器/节点服务器 Keepalived
(3)故障转移
Keepalived实现原理剖析
注意:
VRRP报文的组播地址224.0.0.18,协议号为112,TTL为255.
Keepalived安装与启动
健康检查(探针)
(2)发送心跳消息 ping/ pong
(2)TCP端口检查 向一台主机的IP:PORT发起TCP三次握手连接
(3)HTTP URL检查 向一台主机的 http://IP:PORT/…URL路径 发送HTTP GET请求方法,主机响应返回2XX 3XX,认为健康检查正常,如响应返回4XX 5XX,则认为健康检查异常
配置负载调度器(主、备相同)
--------------------------------------------------Keepalived部署+LVS-DR模式----------------------------------------------------
###关闭和禁止防火墙开机自启功能
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
vim /etc/selinux/config
SELINUX=disabled
(1)Keepalived部署
###安装keepalived和ipvsadm
yum -y install keepalived ipvsadm
ipvsadm-save > /etc/sysconfig/ipvsadm
systemctl enable --now ipvsadm
systemctl start ipvsadm
ipvsadm -ln
modprobe ip_vs
cat /proc/net/ip_vs
修改keepalived的主配置文件
cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak
vim /etc/keepalived/keepalived.conf
----------------------修改以下内容----------------------
smtp_server 127.0.0.1
router_id LVS_01
#vrrp_skip_check_adv_addr #以下4行一定要注释或删除
#vrrp_strict
#vrrp_garp_interval 0
#vrrp_gna_interval 0
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
}
virtual_ipaddress {
192.168.80.188
}
systemctl restart keepalived.service
systemctl enable keepalived.service
ip addr
(2)部署LVS
virtual_server 192.168.80.188 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 0
protocol TCP
real_server 192.168.80.20 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.80.30 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
scp keepalived.conf 192.168.80.50:`pwd`
systemctl restart keepalived.service
ip addr
ipvsadm -ln
备调度器设置
(1)Keepalived部署
###安装keepalived和ipvsadm
yum -y install keepalived ipvsadm
ipvsadm-save > /etc/sysconfig/ipvsadm
systemctl enable --now ipvsadm
systemctl start ipvsadm
ipvsadm -ln
modprobe ip_vs
cat /proc/net/ip_vs
修改keepalived的主配置文件
vim /etc/keepalived/keepalived.conf
----------------------修改以下内容----------------------
smtp_server 127.0.0.1
router_id LVS_02
#vrrp_skip_check_adv_addr #以下4行一定要注释或删除
#vrrp_strict
#vrrp_garp_interval 0
#vrrp_gna_interval 0
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
}
virtual_ipaddress {
192.168.100.188
}
systemctl restart keepalived.service
systemctl enable keepalived.service
ip addr
(2)部署LVS
virtual_server 192.168.80.188 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 0
protocol TCP
real_server 192.168.80.20 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.80.30 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
###关闭和禁止防火墙开机自启功能
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
vim /etc/selinux/config
SELINUX=disabled
(1)配置虚拟IP地址并添加VIP路由信息(VIP:192.168.80.188)
cd /etc/sysconfig/network-scripts/
cp ifcfg-lo ifcfg-lo:0
vim ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=192.168.80.188
NETMASK=255.255.255.255
systemctl restart network
ifconfig
vim /etc/rc.local
/sbin/route add -host 192.168.80.188 dev lo:0
(2)调整内核的ARP响应参数以阻止更新VIP的MAC地址,避免发生冲突
vim /etc/sysctl.conf
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
sysctl -p
(3)安装Apache服务
yum -y install httpd
cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak
cd /var/www/html
rz -E
canshu.png
vim /var/www/html/index.html
LVS Web
RIP:192.168.80.20 www.VIP188.com
look,my girl!!!
systemctl restart httpd
netstat -lntp | grep httpd
浏览器访问
http://192.168.80.188