文章目录
一、简介+部署总览
1. 简介
Keepalived,它是一个高性能的服务器高可用或热备解决方案,Keepalived主要来防止服务器单点故障的发生问题,可以通过其与Nginx、Haproxy等反向代理的负载均衡服务器配合实现web服务端的高可用。Keepalived以VRRP协议为实现基础,用VRRP协议来实现高可用性(HA).VRRP(Virtual Router Redundancy Protocol)协议是用于实现路由器冗余的协议,VRRP协议将两台或多台路由器设备虚拟成一个设备,对外提供虚拟路由器IP(一个或多个)。
2. 部署总览
服务器IP
| hostname
| 节点说明
| 端口
|
192.168.159.140
| nk-01
| nginx+keepalived
| 80
|
192.168.159.141
| nk-02
| nginx+keepalived
| 80
|
二、安装实战
PS:下载地址:
http://www.keepalived.org/download.html
2.1. 安装环境
yum install -y openssl openssl-devel
2.2. 下载软件包
wget https://www.keepalived.org/software/keepalived-2.0.20.tar.gz
2.3. 同步软件包
为了节省时间,将此软件包同步141服务器
scp keepalived-2.0.20.tar.gz root@192.168.159.141:/app/software
2.4. 解压、编译、安装
解压keepalived
tar -zxf keepalived-2.0.20.tar.gz -C /app/
# 编译、安装 keepalived
cd keepalived-2.0.20/ && ./configure --prefix=/app/keepalived
make && make install
编译、安装 keepalived
cd keepalived-2.0.20/ && ./configure --prefix=/app/keepalived
make && make install
三、系统服务
将keepalived安装成Linux系统服务
将keepalived安装成Linux系统服务,因为没有使用keepalived的默认安装路径(默认路径:/usr/local),安装完成之后,需要做一些修改工作
3.1. 配置复制
首先创建文件夹,将keepalived配置文件进行复制
创建文件夹
将keepalived配置文件进行复制
cp /app/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
3.2. 脚本复制
然后复制 keepalived 脚本文件
cp /app/keepalived-2.0.20/keepalived/etc/init.d/keepalived /etc/init.d/
cp /app/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
建立软连接
ln -s /app/sbin/keepalived /usr/sbin/
由于系统默认建立软连接,因此需要先删除默认的
和自己指定安装的脚本文件建立软连接
ln -s /app/keepalived/sbin/keepalived /sbin/
验证
3.3. 服务开机启动
keepalived 服务设置开机启动
可以设置开机启动:systemctl enable keepalived.service
,到此我们安装完毕!
systemctl enable keepalived.service
四、配置修改
4.1. 编辑配置
创建并编辑keepalived.conf文件
vim /etc/keepalived/keepalived.conf
4.2. 140 服务器配置
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER #主节点
interface ens33 ## 绑定虚拟IP的网络接口(网卡),与本机IP地址所在的网络接口相同(我这里是eth0)
virtual_router_id 140 ## 虚拟路由ID号(主备节点一定要相同)
priority 100
mcast_src_ip 192.168.159.140 ## 本机ip地址
advert_int 1 # 组播信息发送间隔,俩个节点必须配置一致,默认1s
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.159.150 ## 虚拟ip,可以指定多个
}
}
4.3. 141 服务器配置
vim /etc/keepalived/keepalived.conf
修改 keepalived.conf 配置文件
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP #从节点
interface ens33 ## 绑定虚拟IP的网络接口(网卡),与本机IP地址所在的网络接口相同(我这里是eth0)
virtual_router_id 140 ## 虚拟路由ID号(主备节点一定要相同)
priority 95
mcast_src_ip 192.168.159.141 ## 本机ip地址
advert_int 1 # 组播信息发送间隔,俩个节点必须配置一致,默认1s
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.159.150 ## 虚拟ip,可以指定多个
}
}
4.4. 启动keepalived
4.5. 查看keepalived运行状态
4.6. 停止keepalived服务