克隆虚拟机
克隆当前虚拟机,查看克隆步骤:VMware克隆虚拟机 ,当前虚拟机为主机,克隆的虚拟机为备用机(主机崩了才会启用备用机)
配置主机
配置虚拟机hosts
文件路径为:/etc/hosts,在底部添加:
1
| 127.0.0.1 KEEPALIVED_NGINX_MASTER
|
Keepalived配置介绍
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
| 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 interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 }
virtual_ipaddress { 192.168.200.16 192.168.200.17 192.168.200.18 } }
|
修改Keepalived配置文件
修改 /etc/keepalived 目录下的keepalived.conf配置文件,除了global_defs和vrrp_instance VI_1节点配置外,下面的节点配置都删除
global_defs.router_id修改为:KEEPALIVED_NGINX_MASTER
把global_defs.vrrp_strict注释掉
vrrp_instance VI_1.state修改为:MASTER
vrrp_instance VI_1.interface修改为:ens33
vrrp_instance VI_1.priority修改为:100
vrrp_instance VI_1.virtual_ipaddress修改为:192.168.1.99
完整keepalived.conf配置如下:
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
| ! 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 KEEPALIVED_NGINX_MASTER vrrp_skip_check_adv_addr vrrp_garp_interval 0 vrrp_gna_interval 0 }
vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.99 } }
|
配置备用机
配置虚拟机hosts
文件路径为:/etc/hosts,在底部添加:
1
| 127.0.0.1 KEEPALIVED_NGINX_BACKUP
|
修改Keepalived配置文件
修改 /etc/keepalived 目录下的keepalived.conf配置文件,除了global_defs和vrrp_instance VI_1节点配置外,下面的节点配置都删除
global_defs.router_id修改为:KEEPALIVED_NGINX_BACKUP
把global_defs.vrrp_strict注释掉
vrrp_instance VI_1.state修改为:BACKUP
vrrp_instance VI_1.interface修改为:ens33
vrrp_instance VI_1.priority修改为:50
vrrp_instance VI_1.virtual_ipaddress修改为:192.168.1.99
完整keepalived.conf配置如下:
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
| ! 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 KEEPALIVED_NGINX_BACKUP vrrp_skip_check_adv_addr vrrp_garp_interval 0 vrrp_gna_interval 0 }
vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.99 } }
|
测试
修改Nginx首页
分别修改主机和备用机的Nginx运行成功的首页,文件路径为:/usr/share/nginx/html/index.html,在首页代码中添加虚拟机IP以方便辨认访问的是哪个虚拟机
启动Keepalived
启动主机和备用机的Keepalived
1
| systemctl start keepalived.service
|
访问
浏览器地址栏输入:http://192.168.1.99/,此时访问的是主机的Nginx
然后停掉主机的Keepalived
1
| systemctl stop keepalived.service
|
刷新一下页面,此时访问的是备用机的Nginx