超详细!nginx高可用方案:keepalived+nginx
Posted 龙叔运维
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了超详细!nginx高可用方案:keepalived+nginx相关的知识,希望对你有一定的参考价值。
nginx的高可用,广为流传的便是keepalived+nginx了,下面就详细介绍下这个方案的搭建
1·环境准备
192.168.30.135【主】
192.168.30.128【备】
VIP:192.168.30.128
主备都安装nginx,并且将index页面加入IP信息(方便后续观察VIP飘逸,主备切换)
2·安装
【1】下载安装包:[root@bogon src]# wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz
【2】解压安装包:[root@bogon src]# tar xvf keepalived-1.3.5.tar.gz
【3】编译安装
[root@bogon src]# cd keepalived-1.3.5
[root@bogon keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived
[root@bogon keepalived-1.3.5]# make
[root@bogon keepalived-1.3.5]# make install
需要安装openssl 不然会报错
yum -y install openssl-devel
3·注册为系统服务
【1】将解压之后的安装包中的keepalived文件复制到/etc/init.d/目录下
[root@bogon keepalived-1.3.5]# cp /root/keepalived/keepalived-1.3.5/keepalived/etc/init.d/keepalived /etc/init.d/keepalived
[root@bogon keepalived-1.3.5]# ll /etc/init.d/ | grep keepalived
-rwxr-xr-x 1 root root 1308 8月 2 14:15 keepalived
【2】修改/etc/init.d/keepalived启动脚本
需要修改的数据有以下几项:
# config: /etc/keepalived/keepalived.conf 文件的位置;
# Source configuration file (we set KEEPALIVED_OPTIONS there) 文件的位置。. /etc/sysconfig/keepalived
修改为实际安装的文件路径,我的安装路径如下:
# config: /usr/local/keepalived/etc/keepalived/keepalived.conf
. /usr/local/keepalived/etc/sysconfig/keepalived
另外添加如下变量,并修改文件中相应的部分:
keepalived=/usr/local/keepalived/sbin/keepalived
keepalived_config=/usr/local/keepalived/etc/keepalived/keepalived.conf
keepalived_pid=/var/run/keepalived.pid
【3】systemctl命令管理keepalived
systemctl start keepalived.service
systemctl stop keepalived.service
systemctl restart keepalived.service
systemctl status keepalived.service
如果报错,可以journalctl -xe查看具体失败信息
4·配置
配置文件:/usr/local/keepalived/etc/keepalived/keepalived.conf
下面只是最简单的配置,更多的可以自行查询
4.1·主节点配置
global_defs
router_id LVS_DEVEL
vrrp_instance VI_1
state MASTER
interface ens33
virtual_router_id 51
priority 100
mcast_src_ip 192.168.30.135
advert_int 1
authentication
auth_type PASS
auth_pass 1111
virtual_ipaddress
192.168.30.140
4.2·备节点配置
global_defs
router_id LVS_DEVEL
vrrp_instance VI_1
state BACKUP
interface ens33
virtual_router_id 51
priority 95
mcast_src_ip 192.168.30.128
advert_int 1
authentication
auth_type PASS
auth_pass 1111
virtual_ipaddress
192.168.30.140
5·使用验证
按照上面的配置并启动之后,在主节点192.168.30.135上就可以看到配置文件中配置的VIP 192.168.30.140 了(备节点看不到这个vip)
访问VIP 192.168.30.140 也可以看到访问到了主节点的nginx上
将主节点的keepalive停掉之后,会发现访问VIP会访问到备节点的nginx上
但是如果主机点恢复了,那么VIP又会自动漂移会主节点
一般keepalived的使用都会匹配值健康检测脚本,如果检测到对应的实例不可用,则干掉本机的keepalived,将VIP切换到备节点
6·问题记录
6.1·Unable to find configuration fille
这里是因为默认的配置文件路径还有没有修改的地方,需要修改如下文件的相应内容
vi /usr/local/keepalived/etc/sysconfig/keepalived
6.2·PID file /usr/local/keepalived/var/run/keepalived.pid not readable (yet?) after start
查看下面文件中pid文件的路径配置,建议配置成 /var/run/keepalived.pid
vi /usr/lib/systemd/system/keepalived.service
vi /etc/init.d/keepalived
6.3·keepalive启动ok,但是vip无法访问(无法ping通)
将keepalived.conf中的vrrp_strict删除或者注释掉
创作打卡挑战赛 赢取流量/现金/CSDN周边激励大奖以上是关于超详细!nginx高可用方案:keepalived+nginx的主要内容,如果未能解决你的问题,请参考以下文章
超详细!nginx高可用方案:keepalived+nginx