20.keepalived负载均衡--高可用

1.1keepalived服务概念说明

keepalived软件能干什么?
Keepalived软件起初是专为LVS负载均衡软件设计的,
用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能

Keepalived软件主要是通过VRRP协议实现高可用功能的。
VRRP是Virtual Router Redundancy Protocol(虚拟路由器冗余协议)的缩写,
VRRP出现的目的就是为了解决静态路由单点故障问题的,它能够保证当个别节点宕机时,
整个网络可以不间断地运行

keepalived软件工作原理?(重点)
原理
1)VRRP协议,全称Virtual Router Redundancy Protocol,中文名为虚拟路由冗余协议,
VRRP的出现是为了解决静态路由的单点故障。
2)VRRP是用过IP多播的方式(默认多播地址(224.0.0.18))实现高可用对之间通信的。
3)工作时主节点发包,备节点接包,当备节点接收不到主节点发的数据包的时候,
就启动接管程序接管主节点的资源。备节点可以有多个,通过优先级竞选,
但一般Keepalived系统运维工作中都是一对。

keepalived软件主要功能?
①. 管理LVS负载均衡软件
②. 实现对LVS集群节点健康检查功能
③. 作为系统网络服务的高可用功能

1.2部署keepalived高可用服务:

1)确认反向代理服务是否工作正常
第一个里程:在lb01/lb02上测试web服务器是否可以正常
lb01/lb02上nginx配置

[[email protected] scripts]# cat /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream oldboy {
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80;
}
server {
listen 80;
server_name www.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 80;
server_name bbs.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
7.8.9服务器上nginx配置

[[email protected] ~]# cat /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.org;
root html/www;
index index.html index.htm;
}
server {
listen 80;
server_name bbs.etiantian.org;
root html/bbs;
index index.html index.htm;
}

}
验证负载均衡功能是否正常

curl -H host:www.etiantian.org 10.0.0.7/oldboy.html
curl -H host:www.etiantian.org 10.0.0.8/oldboy.html
curl -H host:www.etiantian.org 10.0.0.9/oldboy.html
curl -H host:bbs.etiantian.org 10.0.0.7/oldboy.html
curl -H host:bbs.etiantian.org 10.0.0.8/oldboy.html
curl -H host:bbs.etiantian.org 10.0.0.9/oldboy.html

第二个里程:在浏览器上测试访问lb01/lb02
解析hosts文件,将域名解析为10.0.0.5,进行测试访问
解析hosts文件,将域名解析为10.0.0.6,进行测试访问
scp -rp /application/nginx/conf/nginx.conf 10.0.0.6:/application/nginx/conf/ ---测试前同步lb01和lb02配置文件

2)安装部署高可用keepalived服务
第一个里程:安装keepalived服务软件
yum install -y keepalived

第二个里程:编写keepalived配置文件
vim /etc/keepalived/keepalived.conf
man keepalived.conf --- 配置文件说明信息
配置文件结构:
GLOBAL CONFIGURATION --- 全局配置()
VRRPD CONFIGURATION --- vrrp配置()
LVS CONFIGURATION --- LVS服务相关配置
advert_int 1 --- 通告的间隔时间,即主端每隔多久向从端发送心跳信息
lb01主负载均衡器配置
global_defs {
router_id lb01
}

vrrp_instance gorup01 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
/etc/init.d/keepalived reload

lb02配置信息
global_defs {
router_id lb02
}

vrrp_instance group01 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
/etc/init.d/keepalived reload
3)进行测试访问
本地电脑中的hosts
10.0.0.3 www.etiantian.org
10.0.0.3 bbs.etiantian.org
访问http://www.etiantian.org/仍然可以实现负载均衡

1.3 部署keepalived高可用服务问题

同时在keepalived高可用集群中,出现了两个虚拟IP地址信息,这种情况就称为脑裂

脑裂情况出现原因:
1. 心跳线出现问题
   网卡配置有问题
   交换设备有问题
   线缆连接有问题
2. 有防火墙软件阻止问题
3. virtual_router_id配置数值不正确
总之:只要备服务器收不到组播包,就会成为主,而主资源没有释放,就会出现脑裂

利用shell脚本实现监控管理:
备用设备有VIP就是表示不正常
01. 真正实现主备切换
02. 出现脑裂情况了

[[email protected] keepalived]# ip a|grep 10.0.0.3
inet 10.0.0.3/24 scope global secondary eth0:1

[[email protected] keepalived]# ip a|grep -c 10.0.0.3
1

#!/bin/bash
check_info=$(ip a|grep -c 10.0.0.3)
if [ $check_info -ne 0 ]
then
echo "keepalived server error!!!"
fi

1.4 实现nginx反向代理监控虚拟IP地址

如果不配置监听,那么即可以通过NGINX所在的服务器上的其他网卡访问,
也可以通过虚拟Ip访问,这样是不安全的,所以要配置Nginx只监听虚拟Ip,下面是配置
1)编写nginx反向代理配置
server {
listen 10.0.0.3:80;
server_name www.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 10.0.0.3:80;
server_name bbs.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
/application/nginx/sbin/nginx -s stop
/application/nginx/sbin/nginx
netstat -lntup|grep nginx
tcp 0 0 10.0.0.3:80 0.0.0.0:* LISTEN 53334/nginx

实现监听本地网卡上没有的IP地址
echo ‘net.ipv4.ip_nonlocal_bind = 1‘ >>/etc/sysctl.conf
sysctl -p

1.5将keepalived服务和反向代理nginx服务建立联系

nginx反向代理服务停止,keepalived服务也停止
1)编写脚本
#!/bin/bash
web_info=$(ps -ef|grep [n]ginx|wc -l)
if [ $web_info -lt 2 ]
then
/etc/init.d/keepalived stop
fi

2)运行脚本,实现监控nginx服务
编辑keepalived服务配置文件
vrrp_script check_web {
#定义一个监控脚本,脚本必须有执行权限
script "/server/scripts/check_web.sh"
#指定脚本间隔时间
interval 2
#脚本执行完成,让优先级值和权重值进行运算,从而实现主备切换
weight 2
}

track_script {
check_web
}
[[email protected] scripts]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id lb01
}
vrrp_script check_web {
#定义一个监控脚本,脚本必须有执行权限
script "/server/scripts/check_web.sh"
#指定脚本间隔时间
interval 2
#脚本执行完成,让优先级值和权重值进行运算,从而实现主备切换
weight 2
}

vrrp_instance gorup01 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
track_script {
check_web
}
}
chmod +x check_web.sh --- 修改脚本可执行权限
停止nginx服务,可发现keepalived服务也被关闭,虚拟ip去了另一台服务器上

1.6实现高可用集群架构中双主配置(互为主备配置)

lb01

[[email protected] scripts]#cat /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream oldboy {
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80;
}
server {
listen 10.0.0.3:80;
server_name www.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 10.0.0.4:80;
server_name bbs.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}

[[email protected] scripts]#cat /etc/keepalived/keepalived.conf
global_defs {
router_id lb01
}
vrrp_instance gorup01 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
vrrp_instance gorup02 {
state BACKUP
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24 dev eth0 label eth0:1
}
}

lb02

[[email protected] scripts]#cat /etc/keepalived/keepalived.conf
global_defs {
router_id lb02
}
vrrp_instance gorup01 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
vrrp_instance gorup02 {
state MASTER
interface eth0
virtual_router_id 52
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24 dev eth0 label eth0:1
}
}

[[email protected] scripts]# cat /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream oldboy {
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80;
}
server {
listen 10.0.0.3:80;
server_name www.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 10.0.0.4:80;
server_name bbs.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}

本地hosts
10.0.0.3 www.etiantian.org
10.0.0.4 bbs.etiantian.org

原文地址:https://blog.51cto.com/10983441/2426100

时间: 2024-07-30 18:39:04

20.keepalived负载均衡--高可用的相关文章

HAproxy+Keepalived负载均衡-高可用web站

haproxy+keepalived负载均衡高可用web站   OS IP 子网掩码 路由网关 Centos6.6 HAproxy Keepalived Eth0:192.168.26.210 255.255.252.0 192.168.25.3 VIP:192.168.27.210 Centos6.6 HAporxy Keepalived Eth0:192.168.26.211 255.255.252.0 192.168.25.3 VIP:192.168.27.210 Centos6.6(WE

Nginx+Keepalived负载均衡高可用(双机热备)

Nginx+Keepalived负载均衡高可用(双机热备) 1.1 Nginx安装及配置 1.2 Keepalived安装及配置 1.3 WebServer安装 1.4 测试Nginx+Keepalived 环境如下: CentOS 6.4_64K eepalived-1.2.12 Nginx-1.4.4 vip:192.168.10.50 master:192.168.10.11 backup:192.168.10.12 webserver1:192.168.10.13 webserver2:

LVS+Keepalived负载均衡高可用如此简单?

今天简单写下lvs+keepalive实现负载均衡和高可用的功能,仅供参考!关于它们的详细介绍这里就不描述了,大家可以自行搜索下! lvs+keepalived网络拓扑图: 一.准备一个vip和4台虚拟机: vip:192.168.1.100 向外提供服务的ip,一般是绑定域名的 192.168.1.10 LB1_master调度器IP 192.168.1.13  LB2_slave调度器IP 192.168.1.11 nginx服务器IP 192.168.1.12  apache服务器IP 二

实现基于Haproxy_NAT+Keepalived负载均衡高可用架构

实验思路: 1.做这个实验首先可以想象一个场景,用户访问webserver的时候首先会经过调度器,首先需要明白的一点就是一般公司一般是在内网,客户端是通过外网访问webserver的. 2.haproxy是一个负载均衡器,Keepalived通过VRRP功能能再结合LVS负载均衡软件即可部署一个高性能的负载均衡集群系统,也就是说haproxy是解决后端realserver负载均衡的问题,keepalived是解决调度器的高可用的问题. 3.haproxy检测到后端服务器处于不健康的状态的时候会把

实现基于Haproxy+Keepalived负载均衡高可用架构

一:环境准备 centos系统服务器4台,两台用于做haproxy主从架构, 两台作为后端server,服务器配置好yum源,防火墙关闭, 关闭selinux,各节点时钟服务同步,各节点之间可以通过主机名互相通信. 二:安装步骤 1.iptables –F &&setenforing 清空防火墙策略,关闭selinux. 2.拿两台服务器都使用yum方式安haproxy,keepalived 服务 3.后端服务器配置好基于LNMP架构的web服务 当准备工作做好之后,就可以修改配置文件啦,

LVS/DR + keepalived负载均衡高可用实现

一.keepalived简介 keepalived是分布式部署解决系统高可用的软件,结合lvs(LinuxVirtual Server)使用,解决单机宕机的问题.keepalived是一个基于VRRP协议来实现IPVS的高可用的解决方案.对于LVS负载均衡来说,如果前端的调度器direct发生故障,则后端的realserver是无法接受请求并响应的.因此,保证前端direct的高可用性是非常关键的,否则后端的服务器是无法进行服务的.而我们的keepalived就可以用来解决单点故障(如LVS的前

CentOS Linux 负载均衡高可用WEB集群之Nginx+Keepalived配置

Nginx+Keepalived实现负载均衡高可用的WEB服务集群,nginx作为负载均衡器,keepalived作为高可用,当其中的一台负载均衡器(nginx)发生故障时可以迅速切换到备用的负载均衡器(nginx),保持业务的连续性. 1.服务器的环境配置及IP分配 操作系统:CentOS release 6.7 (Final) nginx版本:nginx/1.8.0 keepalived版本:Keepalived v1.2.13 Nginx + keepalived服务器的IP分配表 服务器

CentOS Linux 负载均衡高可用WEB集群之LVS+Keepalived配置

CentOS Linux 负载均衡高可用WEB集群之LVS+Keepalived配置 LB集群是locd balance集群的简称.翻译成中文是:负载均衡集群的意思:集群是一组相互独立的.通过高速网络互联的计算机相互之间构成一个组合,并以单一的系统的模式加以管理.LVS是Linux Virtual Server的简写,翻译中文是Linux虚拟服务器,是一个虚拟的服务器集群系统. 负载均衡集群:是为了企业提供更为实用,性价比更高的系统机构解决方案.负载均衡集群把用户的请求尽可能的平均分发到集群的各

负载均衡高可用之LVS+Keepalived(DR/主备)+apache

负载均衡高可用之LVS+Keepalived(DR/主备)+apache 介绍: LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一. LVS集群采用IP负载均衡技术和基于内容请求分发技术.调度器具有很好的吞吐率,将请求均衡地转移到不同的服务器上执行,且调度器自动屏蔽掉服务器的故障,从而将一组服务器构成一个高性能的.高可用的虚拟服务器.整个服务器集群的结构对客户