keepalived部署+nginx高可用
nginx+keepalived搞性能web网络架构实战配置:
环境准备:
keepalived+nginx-1: 192.168.1.23
keepalived+nginx-2: 192.168.1.25
keepalived环境部署:(需要关闭防火墙与selinux)
yum方式部署:
yum install keepalived* -y #配置文件: /etc/keepalived/keepalived.conf
源码方式部署:
#下载版本keepalived-1.4.5版本/安装配置: wget https://www.keepalived.org/software/keepalived-1.4.5.tar.gz tar xf keepalived-1.4.5.tar.gz cd keepalived-1.4.5 ./configure --prefix=/usr/local/keepalived/ make make install mkdir /etc/keepalived/ cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ cp keepalived-1.4.5/keepalived/keepalived.service /etc/systemd/system/ ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/ cp keepalived-1.4.5/keepalived/etc/init.d/keepalived /etc/init.d/ chmod 755 /etc/init.d/keepalived systemctl enable keepalived systemctl start keepalived ps -ef|gre keepalived
keepalived结合nginx实现高可用:(nginx安装步骤省略)两台keepalived+nginx服务的keepalived配置文件/nginx进程脚本 差不多一样:需要注意的是空格不能多写 nopreempt不抢占参数 只对BACKUP生效,对MASTER无效。
keepalived配置文件内容如下:
! Configuration File for keepalived global_defs { notification_email { support@jfedu.net } notification_email_from wgkgood@163.com smtp_server 127.0.0.1 smtp_connect_timeout 30 } vrrp_script chk_nginx { script "/data/sh/auto_check_nginx.vip.sh" interval 2 weight 2 } #VIP1 vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 151 priority 100 nopreempt advert_int 5 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.198 } track_script { chk_nginx } }
监控nginx服务进程脚本:
vim /data/sh/auto_check_nginx.vip.sh
NGX_MUX="$(ps -ef|grep nginx|grep -v "grep"|grep -v "check"|wc -l)" if [ $NGX_MUX -eq 0 ];then systemctl stop keepalived fi
配置keepalived主主模式:
! Configuration File for keepalived global_defs { notification_email { support@jfedu.net } notification_email_from wgkgood@163.com smtp_server 127.0.0.1 smtp_connect_timeout 30 } vrrp_script chk_nginx { script "/data/sh/auto_check_nginx.vip.sh" interval 2 weight 2 } #VIP1 vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 151 priority 90 nopreempt advert_int 5 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.198 } track_script { chk_nginx } } #VIP2 vrrp_instance VI_2 { state MASTER interface ens33 virtual_router_id 152 priority 100 nopreempt advert_int 5 authentication { auth_type PASS auth_pass 2222 } virtual_ipaddress { 192.168.1.199 } track_script { chk_nginx } }