二、Keepalived 简介
Keepalived 是一种高性能的服务器高可用或热备解决方案,Keepalived 可以用来防止服务器单点故障的发生,通过配合 Nginx 可以实现 web前端服务的高可用。Keepalived 以 VRRP 协议为实现基础,用 VRRP协议来实现高可用性(HA)。VRRP(Virtual Router Redundancy Protocol)协议是用于实现路由器冗余的协议,VRRP协议将两台或多台路由器设备虚拟成一个设备,对外提供虚拟路由器 IP(一个或多个),而在路由器组内部,如果实际拥有这个对外 IP 的路由器如果工作正常的话就是 MASTER,或者是通过算法选举产生,MASTER实现针对虚拟路由器 IP 的各种网络功能,如 ARP 请求,ICMP,以及数据的转发等;其他设备不拥有该虚拟 IP,状态是 BACKUP,除了接收 MASTER 的VRRP 状态通告信息外,不执行对外的网络功能。当主机失效时,BACKUP 将接管原先 MASTER 的网络功能。 VRRP 协议使用多播数据来传输 VRRP 数据,VRRP 数据使用特殊的虚拟源 MAC 地址发送数据而不是自身网卡的 MAC 地址,VRRP 运行时只有 MASTER 路由器定时发送 VRRP 通告信息,表示 MASTER 工作正常以及虚拟路由器 IP(组),BACKUP 只接收 VRRP 数据,不发送数据,如果一定时间内没有接收到 MASTER 的通告信息,各 BACKUP 将宣告自己成为 MASTER,发送通告信息,重新进行 MASTER 选举状态。
三、方案规划
主机基本信息:
VIP:10.200.1.59
IP1:10.200.1.248 Nginx 默认端口:80 默认主从:MASTER
IP2:10.200.1.89 Nginx 默认端口:80 默认主从:BACKUP
[root@mical_linux_nginx01 ~]# uname -r
3.10.0-957.el7.x86_64
[root@mical_linux_nginx01 ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@mical_linux_nginx01 ~]# uname -a
Linux mical_linux_nginx01 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@mical_linux_nginx01 ~]#
CentOS Linux release 7.6.1810 (Core)
安装版本:
keepalived-2.0.8.tar.gz
nginx-1.14.2.tar.gz
四 、安装
1:关闭防火墙并禁止开机启动
systemctl stop firewalld
systemctl disable firewalld
2:两台机子执行同样的操作
下载 wget http://nginx.org/download/nginx-1.14.2.tar.gz
下载安装所需要的RPM包并上传到服务器上 随便一个地方就行我上传到了/opt下面
本人已经整理好了安装keepalived+nginx的所有rpm包 放心下载使用就行,地址为https://download.csdn.net/download/zhanaolu4821/11571722
如果还需要别的rpm包的话可以在http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/ 下载即可
切换到rpm所在的开发包上
cd /opt/nginxYL #切换到rpm所在的目录
ll #查看所有包
rpm -ivh *.rpm --nodeps --force #强制安装所有rpm包
3:安装nginx
cd /opt/ #切换到nginx所在的目录
ll #查看所有包
tar -zxvf nginx-1.14.2.tar.gz
cd nginx-1.14.2
./configure
# 编译
make
#安装
make install
# 运行nginx
cd /usr/local/nginx/sbin
./nginx
添加到systemctl启动项
编写脚本/usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
将服务添加到开机自启动
systemctl enable nginx
开启服务 查看状态
systemctl start nginx
systemctl status nginx
默认端口为80;配置文件默认在/user/local/nginx/conf/nginx.conf
4:验证
正确之后,浏览器打开localhost:8080,如果可访问 welcome to nginx
页面,表示配置完成。
启动成功 可以修改下页面便于好区分
5:两台机子都执行
安装 Keepalived (http://www.keepalived.org/download.html)
1、上传或下载 keepalived(keepalived-2.0.8.tar.gz)到 /opt/ 目录
2、解压安装
cd /opt/
tar -zxvf keepalived-2.0.18.tar.gz
cd keepalived-2.0.18
./configure --prefix=/usr/local/keepalived
make && make install
添加到开机自启动
systemctl enable keepalived
进行相应的配置
mkdir /etc/keepalived
编辑脚本/etc/keepalived/nginx_check.sh,脚本内容如下(nginx _check.sh),两个节点都需要该脚本。
cd /etc/keepalived/
vi nginx_check.sh
脚本内容如下
#!/bin/bash
set -x
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
echo `date`': nginx is not healthy, try to systemctl stop keepalived' >> /etc/keepalived/keepalived.log
systemctl stop keepalived
fi
给脚本添加可执行权限
chmod +x /etc/keepalived/nginx_check.sh
节点10.200.1.248配置Keepalived
修改配置文件/etc/keepalived/keepalived.conf,配置vip为10.200.1.59
mkdir /etc/keepalived
编辑脚本/etc/keepalived/keepalived.conf配置文件。
cd /etc/keepalived/
vi keepalived.conf
内容如下
! Configuration File for keepalived
global_defs {
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP #另一台为MASTER其他配置一样
interface ens192
virtual_router_id 51
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.200.1.59
}
track_script {
chk_nginx
}
}
启动Keepalived服务
systemctl start keepalived #启动
systemctl start keepalived # 查看状态
六:验证Keepalived服务IP漂移
两个节点同同时启动 nginx keepalived 通过 ip -a 查看vip所在的那一台机子上 ,分别停止 nginx keepalived 查看ip飘逸是否成功