准备2台虚拟机,ip分别是192.168.37.133,192.168.37.134,分别安装好nginx、keepalived ,可以用docker安装nginx :docker pull nginx:1.10,
安装命令 yum install keepalived
2 编辑 /etc/keepalived/keepalived.conf ,192.168.37.133机器作为MASTER
! Configuration File for keepalived
global_defs {
}
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.37.140
}
}
192.168.37.14 机器作为BACKUP
! Configuration File for keepalived
global_defs {
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 20
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.37.140
}
}
3 .启动nginx 和keepalived
启动nginx:
docker run -p 80:80 --name nginx -v /mydata/nginx/conf.d:/etc/nginx/conf.d -v /mydata/nginx/nginx.conf:/etc/nginx/nginx.conf -v /mydata/nginx/html:/usr/share/nginx/html -v /mydata/nginx/log:/var/log/nginx -d nginx:1.10
启动keepalived:
systemctl start keepalived
4 访问 http://192.168.37.140
当把192.168.37.134的机器关机后,再次访问,访问了备份机器