准备两台主机,都安装NGINX、keepalived。
一、主的安装配置
安装NGINX
[root@centos13 ~]# vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
[root@centos13 ~]# yum -y install nginx
[root@centos13 ~]# systemctl start nginx
[root@centos13 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
安装keepalive
[root@centos13 ~]# yum -y install keepalived
配置主keepalived
[root@centos13 keepalived]# 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.60.64
}
}
二、备的安装配置
安装NGINX
[root@centos13 ~]# vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
[root@centos13 ~]# yum -y install nginx
[root@centos13 ~]# systemctl start nginx
[root@centos13 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
安装keepalived
[root@centos13 ~]# yum -y install keepalived
三、将主keepalived传到备
[root@centos13 keepalived]# scp keepalived.conf root@192.168.60.14:/etc/keepalived/
The authenticity of host '192.168.60.14 (192.168.60.14)' can't be established.
ECDSA key fingerprint is SHA256:W54gRXpEFUKJ574kXPabJEep5nZIeOqs0eBe0LzvYGI.
ECDSA key fingerprint is MD5:30:9a:bc:ab:b3:94:88:d6:bc:e2:d4:b9:c0:e5:c6:f1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.60.14' (ECDSA) to the list of known hosts.
root@192.168.60.14's password:
keepalived.conf 100% 346 465.0KB/s 00:00
四、修改备上的keepalived配置
[root@centos14 ~]# cd /etc/keepalived/
[root@centos14 keepalived]# ls
keepalived.conf
[root@centos14 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 50
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.60.64
}
}
五、配置准备NGINX
主:
[root@centos13 keepalived]# cp /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.bak
[root@centos13 keepalived]# >/usr/share/nginx/html/index.html
[root@centos13 keepalived]# vi /usr/share/nginx/html/index.html
web01
备:
[root@centos14 keepalived]# cp /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.bak
[root@centos14 keepalived]# >/usr/share/nginx/html/index.html
[root@centos14 keepalived]# vi /usr/share/nginx/html/index.html
wed02
六、启动服务并开机自启动
主:
[root@centos13 keepalived]# systemctl start nginx
[root@centos13 keepalived]# systemctl status nginx
[root@centos13 keepalived]# systemctl enable nginx
[root@centos13 keepalived]# systemctl start keepalived
[root@centos13 keepalived]# systemctl status keepalived
[root@centos13 keepalived]# systemctl enable keepalived
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
备:
[root@centos14 keepalived]# systemctl start nginx
[root@centos14 keepalived]# systemctl status nginx
[root@centos14 keepalived]# systemctl enable nginx
[root@centos14 keepalived]# systemctl start keepalived
[root@centos14 keepalived]# systemctl status keepalived
[root@centos14 keepalived]# systemctl enable keepalived
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
七、访问各个站点
http://192.168.60.13
http://192.168.60.14
http://192.168.60.64
八、测试主备切换
将主192.168.60.13上的keepalived停用掉
[root@centos13 keepalived]# systemctl stop keepalived
刷新页面,vip已切换到备192.168.60.14上面
再从主上启动keepalived
[root@centos13 keepalived]# systemctl start keepalived
刷新页面
可以看到又切回来了
九、NGINX的存活测试
在主192.168.60.13上,keepalived配置文件中添加参数
[root@centos13 keepalived]# vi keepalived.conf
global_defs {
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_nginx {
script "/opt/chknginx.sh"
interval 1
weight -20
}
#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.60.64
}
track_script {
chk_nginx
}
}
#VIP1
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 50
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.60.64
}
track_script {
chk_nginx
}
}
在/opt下创建chknginx.sh文件,并进行授权
[root@centos13 keepalived]# vi /opt/chknginx.sh
#!/bin/bash
A=`ps -C nginx --no-header|wc -l`
if [ $A -eq 0 ];then
systemctl start nginx
if [ `ps -C nginx --no-header|wc -l` -eq 0 ];then
killall keepalived
fi
fi
[root@centos13 keepalived]# chmod 775 /opt/chknginx.sh
关闭主192.168.60.13服务器
[root@centos13 keepalived]# shutdown -h now
PolicyKit daemon disconnected from the bus.
We are no longer a registered authentication agent.
vip会跳转到备192.168.60.14上
在浏览器输入192.168.60.64查看是否跳转到备192.168.60.14上