Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

一、集群介绍

根据功能划分为两大类:高可用和负载均衡

高可用集群通常为两台服务器,一台工作,另外一台作为冗余,当提供服务的机器宕机,冗余将接替继续提供服务 实现高可用的开源软件有: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的优先级来决定谁成为新的master。

Keepalived要有三个模块,分别是core、check和vrrp。其中core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析,check模块负责健康检查,vrrp模块是来实现VRRP协议的

三、用keepalived配置高可用集群

准备两台机器101和106,101作为master,106作为backup

两台机器都执行yum install -y keepalived

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

 一、101主:

两台机器都安装nginx,其中130上已经编译安装过nginx,106上需要yum安装nginx: yum install -y nginx

设定vip为100

编辑101上keepalived配置文件,内容从

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

配置文件路径

/etc/keepalived/keepalived.conf

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

编辑配置文件:

vim /etc/keepalived/keepalived.conf

https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D21Z/master_keepalived.conf获取

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

编辑过程:

清空配置里面东西,并复制网页里边模板;

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

主101编辑监控脚本,内容:

https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D21Z/master_check_ng.sh获取

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

编辑检测脚本

vim /usr/local/sbin/check_ng.sh

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

查看

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

给脚本755权限

chmod 777 /usr/local/sbin/check_ng.sh

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

主101启动服务

systemctl start keepalived

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

再次查看nginx,他会自动启动,说明keepalived里边监控脚本/usr/local/sbin/check_ng.sh生效了

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

日志

less /var/log/messages

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

查看ip地址

ip add

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

启动keepalived

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

检查防火墙是否关闭,backup机器同样需要关闭

iptables -nvL

getenforce

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

二、backup配置

关闭firewalld防火墙

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

backup 106上编辑配置文件,内容:

https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D21Z/backup_keepalived.conf获取

[root@davery01 ~]# vim /etc/keepalived/keepalived.conf 与主上IP地址有点不同,priority权重也不同90,主上是100

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

106上编辑监控脚本,内容:

https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D21Z/backup_check_ng.sh获取

vim /usr/local/sbin/check_ng.sh

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

给脚本755权限

chmod 777 /usr/local/sbin/check_ng.sh

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

106上也启动服务 systemctl start keepalived

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

这时候就配置完成了

在网页上输入主ip 192.168.1.101会出现如下内容

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

查看nginx的网页配置文件位置

ls /usr/local//nginx/conf/vhost/

Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

测试高可用

先确定好两台机器上nginx差异,比如可以通过curl -I 来查看nginx版本

测试1:关闭master上的nginx服务

测试2:在master上增加iptabls规则 iptables -I OUTPUT -p vrrp -j DROP

测试3:关闭master上的keepalived服务

测试4:启动master上的keepalived服务

上一篇:与Xamarin.Forms跨平台的用户界面


下一篇:使用javascript实现html文字不可选