keepalived高可用
一、高可用介绍
1.什么是高可用
一般是指2台机器启动着完全相同的业务系统,当有一台机器down机了,另外一台服务器就能快速的接管,对于访问的用户是无感知的。
2. Keepalived服务VRRP的工作原理
Keepalived高可用对之间是通过VRRP进行通信的,VRRP是通过竞选机制来确定主备的,主的优先级高于备,因此,工作时主会优先获得所有的资源,备节点处于等待状态,当主宕机的时候,备节点就会接管主节点的资源,然后顶替主节点对外提供服务。
在Keepalived服务对之间,只有作为主的服务器会一直发送VRRP广播包,告诉备它还活着,此时备不会抢占主,当主不可用时,即备监听不到主发送的广播包时,就会启动相关服务接管资源,保证业务的连续性。
二、keepalived+haproxy高可用
环境说明:
主机为H1和H2作为负载均衡器,一主一备。rs1和rs2作为后端服务器。
操作系统 | 主机名 | 环境角色 | IP | 安装软件 |
---|---|---|---|---|
Centos8 | H1 | 负载均衡(主) | 192.168.16.128/24 | haproxy+keepalived |
Centos8 | H2 | 负载均衡(备) | 192.168.16.130/24 | haproxy+keepalived |
Centos7 | rs1 | 后端服务器 | 192.168.16.131/24 | httpd |
rh8 | rs2 | 后端服务器 | 192.168.16.129/24 | httpd |
在所有主机上面关闭防火墙和selinux
[root@rs1 ~]# systemctl stop --now firewalld
[root@rs1 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@rs1 ~]# setenforce 0
[root@rs1 ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@H2 haproxy]# systemctl stop --now firewalld
[root@H2 ~]# systemctl disable firewalld
[root@H2 ~]# setenforce 0
[root@H1 haproxy]# systemctl stop --now firewalld
[root@H1 ~]# systemctl disable firewalld
[root@H1 ~]# setenforce 0
[root@H1 ~]#
[root@rs2 ~]# systemctl stop --now firewalld
[root@rs2 ~]# systemctl disable firewalld
[root@rs2 ~]# setenforce 0
[root@rs2 ~]#
配置yum源
# H1 和 H2 配置阿里云源和epel源
[root@H2 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@H2 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@H2 yum.repos.d]# yum clean all
[root@H2 yum.repos.d]# yum makecache
[root@H2 yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@H2 yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@H2 yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@H2 yum.repos.d]# yum makecache
[root@H1