腾讯云CVM搭建keepalived+nginx高可用(主备热备+双机热备模式)
【前言】本文是在腾讯云CVM下,搭建云服务器keepalived+nginx主备热备+双机热备。
一、【环境搭建】
1、环境准备
nginx安装自行百度,下面介绍安装keepalived。
安装keepalived:
1
| yum install -y keepalived
|
常用命令:
1 2 3 4 5 6 7 8 9 10 11 12
| systemctl start keepalived
systemctl stop keepalived 重启 systemctl restart keepalived
systemctl status keepalived
systemctl enable keepalived
systemctl disable keepalived
|
配置keepalived.conf:
1
| vim /etc/keepalived/keepalived.conf
|
二、【keepalived相关配置】
1、nginx脚本: 创建 check_nginx.sh 文件,判断nginx进程没有数值,尝试重启nginx服务,睡眠2秒,再判断nginx进程有没有数值,没有则停止keepalived服务,高可用虚拟ip则会自动转到备机。
1 2
| cd /etc/keepalived vim check_nginx.sh
|
check_nginx.sh 内容:
1 2 3 4 5 6 7 8 9 10
| #!/bin/bash counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then /usr/bin/nginx sleep 2 counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then systemctl stop keepalived fi fi
|
2、keepalived邮件报警配置
说明: 实际生产中不适用keepalived自带的邮件功能,所以使用shell脚本发邮件实现报警。
linux 配置发邮件参考《Linux(centos8) 腾讯云CVM配置SMTPS发邮件》
创建 notify.sh 文件:
1 2
| cd /etc/keepalived vim check_nginx.sh
|
notify.sh 内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #!/bin/bash contact=lisuibi@163.com notify() { mailsubject="$(hostname) to be $1, vip转移" mailbody="$(date +'%F %T'): vrrptransition, $(hostname) changed to be $1" echo "$mailbody" | mail -s "$mailsubject" $contact } case $1 in master) notify master ;; backup) notify backup ;; fault) notify fault ;; *) echo "Usage: $(basename$0) {master|backup|fault}" exit 1 ;; esac
|
以上就是keepalived邮件报警shell脚本。
3、主备热备模式
- keepalived 配置
此配置为主备热备模式配置
配置文件位置:
/etc/keepalived/keepalived.conf
vim /etc/keepalived/keepalived.conf
主服务器配置:在 主服务器172.16.0.7 上, VIP设置为172.16.0.8,配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| ! Configuration File for keepalived global_defs { router_id LVS_DEVEL1 }
vrrp_script check_nginx { script "/etc/keepalived/check_nginx.sh" interval 2 weight -5 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 nopreempt priority 150 advert_int 5 authentication { auth_type PASS auth_pass 1111 } unicast_src_ip 172.16.0.7 unicast_peer { 172.16.0.9 } virtual_ipaddress { 172.16.0.8 } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" notify_stop "/etc/keepalived/notify.sh stop" garp_master_delay 1 garp_master_refresh 5 track_interface { eth0 } track_script { check_nginx } }
|
备用服务器:在备用服务器172.16.0.9 上,配置跟主服务器一样,就三不同一相同。
1. router_id 不同
2. state BACKUP不同
3. priority不同
4. virtual_router_id 必相同。
配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| ! Configuration File for keepalived global_defs { router_id LVS_DEVEL2 }
vrrp_script check_nginx { script "/etc/keepalived/check_nginx.sh" interval 2 weight -5 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 nopreempt priority 100 advert_int 5 authentication { auth_type PASS auth_pass 1111 } unicast_src_ip 172.16.0.9 unicast_peer { 172.16.0.7 } virtual_ipaddress { 172.16.0.8 } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" notify_stop "/etc/keepalived/notify.sh stop" garp_master_delay 1 garp_master_refresh 5 track_interface { eth0 } track_script { check_nginx } }
|
- 启动keepalived测试,查看高可用虚拟ip
重启两台keepalived
1
| syetemctl restart keepalived
|
ip addr 查看ip,可以看到VIP在主服务器上。
1、测试下VIP是否可用:
2、停止主服务器keepalived,查看vip是否漂移
在停止主服务器keepalived后,再请求地址,查看结果,均请求的备用服务器
至此,keepalived+nginx 主备热备模式完成。
4、双机热备模式
- keepalived 配置
此配置为双机热备模式配置
当了解主备模式配置后,双机模式就容易配置多了。只需要在每台keepalived配置文件,加上一个vrrp_instance命名vrrp_instance VI_2即可,更改几个参数,设置另外一个VIP:172.16.0.6
172.16.0.7服务器(keep1) keepalive配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| ! Configuration File for keepalived global_defs { router_id LVS_DEVEL1 }
vrrp_script check_nginx { script "/etc/keepalived/check_nginx.sh" interval 2 weight -5 }
vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 nopreempt priority 150 advert_int 5 authentication { auth_type PASS auth_pass 1111 } unicast_src_ip 172.16.0.7 unicast_peer { 172.16.0.9 } virtual_ipaddress { 172.16.0.8 } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" notify_stop "/etc/keepalived/notify.sh stop" garp_master_delay 1 garp_master_refresh 5 track_interface { eth0 } track_script { check_nginx } }
vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 52 nopreempt priority 100 advert_int 5 authentication { auth_type PASS auth_pass 1111 } unicast_src_ip 172.16.0.7 unicast_peer { 172.16.0.9 } virtual_ipaddress { 172.16.0.6 } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" notify_stop "/etc/keepalived/notify.sh stop" garp_master_delay 1 garp_master_refresh 5 track_interface { eth0 } track_script { check_nginx } }
|
172.16.0.9服务器(keep2) keepalive配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| ! Configuration File for keepalived global_defs { router_id LVS_DEVEL2 }
vrrp_script check_nginx { script "/etc/keepalived/check_nginx.sh" interval 2 weight -5 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 nopreempt priority 100 advert_int 5 authentication { auth_type PASS auth_pass 1111 } unicast_src_ip 172.16.0.9 unicast_peer { 172.16.0.7 } virtual_ipaddress { 172.16.0.8 } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" notify_stop "/etc/keepalived/notify.sh stop" garp_master_delay 1 garp_master_refresh 5 track_interface { eth0 } track_script { check_nginx } }
vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 52 nopreempt priority 150 advert_int 5 authentication { auth_type PASS auth_pass 1111 } unicast_src_ip 172.16.0.9 unicast_peer { 172.16.0.7 } virtual_ipaddress { 172.16.0.6 } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" notify_stop "/etc/keepalived/notify.sh stop" garp_master_delay 1 garp_master_refresh 5 track_interface { eth0 } track_script { check_nginx } }
|
配置好后分别启动 keep1和keep2,查看绑定VIP情况
- 启动keepalived测试,查看高可用虚拟ip
看到以下结果即为配置成功:
1、测试下VIP是否可用:
2、停止一台服务器keepalived,查看vip是否漂移
停止172.16.0.7 keepalived 服务,查看172.16.0.9 服务器 IP:
1 2
| systemctl stop keepalived ip addr
|
在172.16.0.7 服务器keepalived停止的情况下,再次请求服务地址,查看结果,均请求的172.16.0.9服务器
重新启动172.16.0.7 服务器的keepalived服务,可以看到又重新绑回。
至此,keepalived+nginx 双机热备模式完成。
【项目部署】
项目部署我是采用nginx反向代理到VIP,也可以在腾讯云申请EIP,在高可用虚拟IP页面绑定EIP即可通过域名解析到EIP进行访问。
【总结】
访问keepalived配置的2个VIP都可以正常调度,当停止其中一台keepalived节点,同样也可以正常访问。至此,腾讯云CVM搭建keepalived+nginx高可用(主备热备+双机热备模式)就完成了。