NGINX高可用
准备两台虚拟机
两台虚拟机配置nginx的yum源:vi /etc/yum.repos.d/nginx.repo
添加
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
查看nginx的yum源:cat /etc/yum.repos.d/nginx.repo
两台虚拟机安装nginx服务:yum -y install nginx
两台虚拟机开启nginx服务:systemctl start nginx
两台虚拟机设置开机自动启动nginx服务:systemctl enable nginx
两台虚拟机匀安装keepalived服务:yum -y install keepalived
主虚拟机上配置keepalived服务
切换目录:cd /etc/keepalived/
复制文件:cp keepalived.conf keepalived.conf.bak
> keepalived.conf
配置文件:vi keepalived.conf
添加:
global_defs {
vrrp_garp_interval 0
vrrp_gna_interval 0
}
#VIP1
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.100.84 (100网段必须是自己主机设置的网段 84可以自己随意更改 不过主虚拟机和备虚拟机上的IP必须一样)
}
}
把配置好的keepalived.conf传到备虚拟机上并修改
scp keepalived.conf root@192.168.100.6:/etc/keepalived/
到备虚拟机上先切换目录:cd /etc/keepalived/
修改配置文件:vi keepalived.conf
修改标记红色的地方
global_defs {
vrrp_garp_interval 0
vrrp_gna_interval 0
}
#VIP1
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 99
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.100.84 (vip)
}
}
配置nginx
主虚拟机上
cp /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.bak
> /usr/share/nginx/html/index.html
配置:vi /usr/share/nginx/html/index.html
添加:
web01 (内容随意更改)
备虚拟机上
cp /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.bak
>/usr/share/nginx/html/index.html
配置:vi /usr/share/nginx/html/index.html
添加:
web02 (内容随意更改)
两台虚拟机上
启动nginx服务:systemctl start nginx
设置nginx开机自动启动:systemctl enable nginx
启动keepalived服务:systemctl start keepalived
设置keepalived开机自动启动:systemctl enable keepalived
访问各站点(主备及VIP)
在主虚拟机上停掉keepalived,测试主备切换
systemctl stop keepalived
再在主虚拟机上启动keepalived查看效果
systemctl start keepalived