作者:teemitop | 来源:互联网 | 2023-07-25 14:46
集群介绍根据功能划分为两大类:高可用和负载均衡高可用集群通常为两台服务器,一台工作,另外一台作为冗余,当提供服务的机器宕机,冗余将接替继续提供服务业内有个综合评分比如4个九,甚至5
集群介绍
- 根据功能划分为两大类:高可用和负载均衡
- 高可用集群通常为两台服务器,一台工作,另外一台作为冗余,当提供服务的机器宕机,冗余将接替继续提供服务
- 业内有个综合评分比如4个九,甚至5个九六个九来衡量服务的高可用,一般大企业的核心业务都有高可用,如果一个机器故障,一分钟之内就可以切换到另一台上
- 实现高可用的开源软件有:heartbeat、keepalived
- 负载均衡集群,需要有一台服务器作为分发器,它负责把用户的请求分发给后端的服务器处理,在这个集群里,除了分发器外,就是给用户提供服务的服务器了,这些服务器数量至少为2
- 实现负载均衡的开源软件有LVS、keepalived、haproxy、nginx,商业的有F5、Netscaler
keepalived介绍
- 在这里我们使用keepalived来实现高可用集群,因为heartbeat在centos6上有一些问题,影响实验效果,并更新不及时
- keepalived通过VRRP(Virtual Router Redundancy Protocl即虚拟路由冗余协议)来实现高可用。
- 在这个协议里会将多台功能相同的路由器(其实是一台机器)组成一个小组,这个小组里会有1个master角色和N(N>=1)个backup角色。
- master会通过组播的形式向各个backup发送VRRP协议的数据包,当backup收不到master发来的VRRP数据包时,就会认为master宕机了。此时就需要根据各个backup的优先级来决定谁成为新的mater。
- Keepalived要有三个模块,分别是core、check和vrrp。其中core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析,check模块负责健康检查,vrrp模块是来实现VRRP协议的。
用keepalived配置高可用集群
- 准备两台机器130和132,130作为master,132作为backup
- 两台机器都执行yum install -y keepalived
- 两台机器都安装nginx,其中130上已经编译安装过nginx,132上需要yum安装nginx: yum install -y nginx
- 之所以选择nginx,在生产环境中,好多企业把它作为负载均衡器,如果它挂掉,影响后面好多web服务器,所以不能出现单点故障
- 编辑130上的keepalived配置文件,
[root@akuilinux01 ~]# > /etc/keepalived/keepalived.conf
[root@akuilinux01 ~]# vim !$
vim /etc/keepalived/keepalived.conf
global_defs { #这个全局定义参数
notification_email { #出现问题给邮箱发邮件,
aming@aminglinux.com
}
notification_email_from root@aminglinux.com #由哪一个邮件发送
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx { #检测一个服务是否正常,需要有个脚本
script "/usr/local/sbin/check_ng.sh"
interval 3 # 3秒检测一次
}
vrrp_instance VI_1 { # 定义master相关的
state MASTER #如果是从就是backup
interface ens33 #定义网卡
virtual_router_id 51 # 定义路由器的id
priority 100 #定义权重
advert_int 1
authentication { #认证相关
auth_type PASS
auth_pass aminglinux>com
}
virtual_ipaddress { #定义vip(公有的ip)
192.168.21.100
}
track_script { #加载服务
chk_nginx
}
}
- 在130上编辑监控脚本
[root@akuilinux01 ~]# vim /usr/local/sbin/check_ng.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进程数量,
#如果还为0,说明nginx无法启动,此时需要关闭keepalived(防止脑裂)
if [ $n -eq "0" ]; then
/etc/init.d/nginx start
n2=`ps -C nginx --no-heading|wc -l`
if [ $n2 -eq "0" ]; then
echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
systemctl stop keepalived
fi
fi
- 给脚本755权限,并启动keepalived服务
[root@akuilinux01 ~]# chmod 755 /usr/local/sbin/check_ng.sh
[root@akuilinux01 ~]# systemctl start keepalived
[root@akuilinux01 ~]# ps aux |grep keepa
root 2895 0.0 0.0 118608 1388 ? Ss 23:20 0:00 /usr/sbin/keepalived -D
root 2896 0.1 0.1 127468 3304 ? S 23:20 0:00 /usr/sbin/keepalived -D
root 2899 0.3 0.1 127408 2836 ? S 23:20 0:00 /usr/sbin/keepalived -D
root 2961 0.0 0.0 112676 980 pts/1 S+ 23:20 0:00 grep --color=auto keepa
- 检测下nginx可以自动加载不
[root@akuilinux01 ~]# ps aux |grep nginx
root 1016 0.0 0.0 46008 1260 ? Ss 20:52 0:00 nginx: master process /usr/localnginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 1017 0.0 0.2 48496 4152 ? S 20:52 0:00 nginx: worker process
nobody 1018 0.0 0.2 48496 3896 ? S 20:52 0:00 nginx: worker process
root 2982 0.0 0.0 112680 980 pts/1 S+ 23:20 0:00 grep --color=auto nginx
[root@akuilinux01 ~]# /etc/init.d/nginx stop
Stopping nginx (via systemctl): [ 确定 ]
[root@akuilinux01 ~]# ps aux |grep nginx
root 3266 1.0 0.0 46008 1252 ? Ss 23:22 0:00 nginx: master process /usr/localnginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 3267 0.0 0.2 48496 3892 ? S 23:22 0:00 nginx: worker process
nobody 3268 0.0 0.2 48496 3892 ? S 23:22 0:00 nginx: worker process
root 3273 0.0 0.0 112676 984 pts/1 S+ 23:22 0:00 grep --color=auto nginx
- 日志
[root@akuilinux01 ~]# less /var/log/messages
- 查看vip
[root@akuilinux01 ~]# ip add
1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:87:fb:87 brd ff:ff:ff:ff:ff:ff
inet 192.168.21.128/24 brd 192.168.21.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.21.100/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::68fd:e6fa:c781:f5a6/64 scope link
valid_lft forever preferred_lft forever
3: ens37: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:87:fb:91 brd ff:ff:ff:ff:ff:ff
inet 192.168.110.136/24 brd 192.168.110.255 scope global dynamic ens37
valid_lft 1278sec preferred_lft 1278sec
inet6 fe80::c559:4a92:72f1:b448/64 scope link
valid_lft forever preferred_lft forever
-
132上编辑配置文件
[root@akuilinux02 ~]# > /etc/keepalived/keepalived.conf
[root@akuilinux02 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
aming@aminglinux.com
}
notification_email_from root@aminglinux.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/usr/local/sbin/check_ng.sh"
interval 3
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass aminglinux>com
}
virtual_ipaddress {
192.168.21.100
}
track_script {
chk_nginx
}
}
- 132上编辑监控脚本
[root@akuilinux02 ~]# vim /usr/local/sbin/check_ng.sh
#时间变量,用于记录日志
d=`date --date today +%Y%m%d_%H:%M:%S`
#计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`
#如果进程为0,则启动nginx,并且再次检测nginx进程数量,
#如果还为0,说明nginx无法启动,此时需要关闭keepalived
if [ $n -eq "0" ]; then
systemctl start nginx
n2=`ps -C nginx --no-heading|wc -l`
if [ $n2 -eq "0" ]; then
echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
systemctl stop keepalived
fi
fi
- 给脚本755权限并启动keepalived
[root@akuilinux02 ~]# chmod 755 /usr/local/sbin/check_ng.sh
[root@akuilinux02 ~]# systemctl start keepalived
测试
- curl或者浏览器打开
[root@akuilinux01 ~]# curl localhost@192.168.21.128
master,master.
[root@akuilinux01 ~]# curl localhost@192.168.21.129
backup,backup.
[root@akuilinux01 ~]# curl localhost@192.168.21.100
master,master.
- 测试高可用
[root@akuilinux01 ~]# systemctl stop keepalived
[root@akuilinux01 ~]# curl localhost@192.168.21.100
backup,backup.
[root@akuilinux01 ~]# systemctl start keepalived
[root@akuilinux01 ~]# curl localhost@192.168.21.100
master,master.
扩展
- heartbeat和keepalived比较
- DRBD工作原理和配置
- mysql+keepalived
集群介绍,keepalived介绍,用keepalived配置高可用集群