部署准备:
两台CentOS 7主机HA1和HA2
CentOS 7 基于rpm包安装Nginx:
由于Base源中没有Nginx,所以要安装EPEL源,命令如下:
wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm rpm -ivh epel-release-latest-7.noarch.rpm yum repolist ##检查是否已添加至源列表
然后就可以用yum安装相应的rpm包了
主备模型部署开始:
1.两台主机各自安装Nginx,并自行设置好测试页
2.keepalived配置文件示例:
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from admin@ka.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id node1 } /*进程状态探测脚本*/ /*务必要定义在实例之外,再在实例内调用*/ /*且脚本名字尽量不要用完整单词,如check等,我第一次用此名导致脚本执行失败,可用ck,chk等代替为妙*/ vrrp_script ck_nginx { script "killall -0 nginx" interval 2 weight -5 } vrrp_instance VI_1 { state MASTER interface eno16777736 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 dev eno16777736 label eno16777736:0 } /*在实例内调用脚本*/ track_script { ck_nginx } }
3.开始测试效果:
①现在备节点启用keepalived,显示
②再在主节点开启keepalived,备节点的日志显示
此时,访问nginx的结果是
③在主节点上停止nginx,再次访问服务,结果是HA2,符合需求
主主模型部署开始:
HA1: HA2:
主 备
备 主
HA1配置文件:
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from admin@ka.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id node1 } vrrp_script ck_nginx { script "killall -0 nginx" interval 2 weight -5 } vrrp_instance VI_1 { state MASTER interface eno16777736 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 dev eno16777736 label eno16777736:0 } track_script { ck_nginx } } vrrp_instance VI_2 { state BACKUP interface eno16777736 virtual_router_id 52 priority 98 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.111 dev eno16777736 label eno16777736:1 } track_script { ck_nginx } }
HA2配置文件:
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from admin@ka.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id node1 } vrrp_script ck_nginx { script "killall -0 nginx" interval 2 weight -5 } vrrp_instance VI_1 { state BACKUP interface eno16777736 virtual_router_id 51 priority 98 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 dev eno16777736 label eno16777736:0 } track_script { ck_nginx } } vrrp_instance VI_2 { state MASTER interface eno16777736 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.111 dev eno16777736 label eno16777736:1 } track_script { ck_nginx } }
测试开始:
①现在HA2上开启keepalived,效果如下:
②再在HA1上开启,效果是:
③回到HA2,使用ifconfig查看结果是:
注意:
脚本中用到的killall命令如果是最小化安装默认是没有此命令的,需要运行:
yum -y install psmisc