为了账号安全,请及时绑定邮箱和手机立即绑定

手把手教你在centos7环境配置keepalived+nginx高可用框架

标签:
Linux 架构

环境准备

两台虚拟机,centos7

主nginx负载均衡器:192.168.5.129 (通过keepalived配置了VIP:192.168.5.100供外使用)
副nginx负载均衡器:192.168.5.130(通过keepalived配置了VIP:192.168.5.100供外使用)

1.安装 keepalived-1.4.1

 tar xf keepalived-1.3.4.tar.gz
 cd keepalived-1.3.4
 ./configure --prefix=/usr/local/keepalived
 make && make install

2.配置keepalived文件

复制/sbin/keepalived到/usr/sbin下> cp /keepalived/sbin/keepalived /usr/sbin/keepalived默认会读取/etc/keepalived/keepalived.conf配置文件> mkdir /etc/keepalived> cp /keepalived/etc/keepalived/keepalived.conf /etc/keepalived/复制sysconfig文件到/etc/sysconfig下> cp /keepalived/etc/sysconfig/keepalived /etc/sysconfig/复制启动脚本到/etc/init.d下> cd /keepalived-1.3.4
> cp ./keepalived/etc/init.d/keepalived /etc/init.d/
> chmod 755 /etc/init.d/keepalived

3.配置keepalived.conf启动文件

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0}

vrrp_instance VI_1 {
    state MASTER(BACKUP)    interface ens33    -- 注意网卡名字
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    
    virtual_ipaddress {        192.168.5.100
        192.168.5.101
    }
}

 https://img1.sycdn.imooc.com//5b68662300014b0a05350324.jpg

4.启动与验证keepalievd  <ip addr list>

service keepalived start
systemctl status keepalived.service

https://img1.sycdn.imooc.com//5b686657000158f008250367.jpg  

ps -aux|grep keepalived

 https://img1.sycdn.imooc.com//5b68663a0001da8107520089.jpg


 5. 设置centos7开机自动启动服务

vim /lib/systemd/system/keepalived.service
[Unit]
Description=Keepalived
After=syslog.target network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/var/run/keepalived.pid
ExecStart=/usr/local/keepalived/sbin/keepalived -D
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
 [Install]
WantedBy=multi-user.target

1;systemctl daemon-reload  重新加载 

2:systemctl enable keepalived.service  设置开机自动启动 

3:systemctl disable keepalived.service 取消开机自动启动 

4:systemctl start keepalived.service 启动 

5:systemctl stop keepalived.service停止


设置nginx开机自启动

vim /lib/systemd/system/nginx.service

[Unit]
Description=nginx 
After=network.target 
   
[Service] 
Type=forking 
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true 
   [Install] 
WantedBy=multi-user.target
systemctl enable nginx.service

systemctl start nginx.service    启动nginx

systemctl stop nginx.service    结束nginx

systemctl restart nginx.service    重启nginx

6.配置监听文件

vi /root/check_nginx.sh
#!/bin/sh
count=`ps aux | grep nginx | grep -v grep | wc -l`
if [ $count -eq 0 ]
then
  echo "running....., please starting...."
  systemctl start nginx.service
  sleep 2
else
  echo "running...."
fi
1chmod +x /root/check_nginx.sh


加入到计划
crontab -e*/1 * * * * /root/check_nginx.sh >>/root/check_nginx.log

追加keepalived.conf文件

! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}

vrrp_script check_nginx {
script "/root/check_nginx.sh"
interval 2
weight 2
}

vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
mcast_src_ip 192.168.5.129
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 111111
}

track_script { ######执行监控nginx进程的脚本
check_nginx
}

virtual_ipaddress {
192.168.5.100
}
}

7.验证

(1)主副均正常开启服务

https://img1.sycdn.imooc.com//5b68671100018c5803710169.jpg

(2)主服务器停掉

https://img1.sycdn.imooc.com//5b68678f00010d4003970117.jpg


点击查看更多内容
10人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消