Introduction to Linux Clusters
- By function: high availability and load balancing
- A high-availability cluster usually consists of two servers, one working and the other as redundant.
- Load balancing cluster: A server is required as a distributor (distributing user requests to the back-end server for processing), and a server that provides services to users, at least 2 servers
- Highly available open source software: heartbeat, keepalived
- Load balancing software: LVS, keepalived, haproxy, nginx, commercial: F5, Netsacler
keepalived+Nginx experimental part
Master: 192.168.206.128
From (backup): 192.168.206.129
Virtual IP (VIP): 192.168.206.200
Use keepalived to achieve high availability of Nginx
echo 'this is master' > /usr/share/nginx/html/index.html master node
echo 'this is slave' > /usr/share/nginx/html/index.html standby node
Keepalived+Nginx installation
Keepalived configuration
There are three main modules: core, check, vrrp
Two machines are required to install keepalived+Nginx
yum -y install keepalived nginx
Nginx configuration
Two options to configure Nginx
#下载
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0.tar.gz
#所需安装包
yum -y install gcc pcre-devel zlib zlib-devel
#编译运行
./configure --prefix=/usr/local/nginx
#安装
make && make install
Nginx service configuration
#进入对应文件
cd /usr/local/nginx/sbin
./nginx #启动nginx
./nginx -s quit 或者 ./nginx -s stop #结束nginx
./nginx -s reload #重启nginx
keepalived configuration file
main configuration file
!Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
#keepalived会定时执行脚本并对脚本执行的结果进行分析,动态调整vrrp_instance的优先级。
vrrp_script chk_nginx {
#运行脚本,脚本内容下面有,就是起到一个nginx宕机以后,自动开启服务
script "/etc/keepalived/nginx_check.sh"
interval 1 #检测时间间隔
weight -2 #如果条件成立的话,则权重 -2
}
vrrp_instance VI_1 {
state MASTER #来决定主从
interface ens33 # 绑定 IP 的网卡口,根据自己的机器填写 (修)
virtual_router_id 120 # 虚拟路由的 ID 号, 两个节点设置必须一样
mcast_src_ip 192.168.206.128 #填写本机ip (修)
priority 100 # 节点优先级,主要比从节点优先级高
advert_int 1 # 组播信息发送间隔,两个节点设置必须一样,默认 1s
authentication {
auth_type PASS
auth_pass 1111
}
# 将 track_script 块加入 instance 配置块
track_script {
chk_nginx #执行 Nginx 监控的服务
}
virtual_ipaddress {
192.168.206.200/24 # 虚拟ip,通过虚拟Ip切换进行nginx主备切换功能(修)
}
}
configuration file
! Configuration File for keepalived
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" #运行脚本,脚本内容下面有,就是起到一个nginx宕机以后,自动开启服务
interval 1 #检测时间间隔
weight -2 #如果条件成立的话,则权重 -20
}
vrrp_instance VI_1 {
state BACKUP #来决定主从
interface ens33 # 绑定 IP 的网卡名称,根据自己的机器填写
virtual_router_id 120 # 虚拟路由的 ID 号, 两个节点设置必须一样
mcast_src_ip 192.168.206.129 #填写本机ip
priority 99 # 节点优先级,主要比从节点优先级高
advert_int 1 # 组播信息发送间隔,两个节点设置必须一样,默认 1s
#nopreempt #设置为不抢夺VIP
authentication {
auth_type PASS
auth_pass 1111
}
# 将 track_script 块加入 instance 配置块
track_script {
chk_nginx #执行 Nginx 监控的服务
}
virtual_ipaddress {
192.168.206.200/24 # 虚拟ip
}
}
Configure monitoring script
Command configuration
vi /etc/keepalived/nginx_check.sh
chmod 755 /etc/keepalived/nginx_check.sh
Monitoring script
#文件位置 /usr/local/sbin/check.sh
#!/bin/bash
#时间变量,用于记录日志
d=`date --date today +%Y%m%d_$H:%M:%S`
#计算Nginx进程数量
n=`ps -C nginx --no-heading | wc -l`
#如果进程为0,则启动nginx,并且再次检测nginx进程数量
if[$n -eq "0"]; then
/etc/init.d/nginx start
n2=`ps -C nginx --no-heading|wc -l`
#如果还是为0,说明nginx无法启动,此时需要关闭keepalived
if[$n2 -eq "0"]; then
echo "$d nginx down,keepalived will stop" >> /var/log/check.log
systemctl stop keepalived
fi
fi
Keepalived+Nginx achieves high availability
#主从配置都开启keepalived服务+Nginx服务
# 启动 | 停止 | 重启
service keepalived start | stop | restart
#主从设备同时开启keepalived服务,访问VIP地址
#两个设备同时ip a,显示VIP地址为192.168.206.200
curl 192.168.206.200
#输出结果this is master
#主设备关闭Nginx,访问VIP地址
#从设备显示VIP地址为从设备的IP地址
curl 192.168.206.200
#输出结果 this is slave