NGINX高可用性
1.准备工作
(1)需要准备两台服务器192.168.60.78(主) 和 192.168.60.10(备)
(2)两台服务器都安装nginx
(3)在两台服务器安装keepalived
2.安装Nginx
(1)使⽤用Nginx官⽅方提供的rpm包。
[root@xianxin ~]# vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
(2)执行yum安装
[root@nginx ~]# yum install nginx -y
[root@nginx ~]# systemctl start nginx
[root@nginx ~]# systemctl enable nginx
3.安装keepalived
[root@test ~]# yum install keepalived -y
4.配置主keepalived
[root@xianxin ~]# cd /etc/keepalived/
[root@xianxin keepalived]# cp keepalived.conf keepalived.conf.bak
[root@xianxin keepalived]# > keepalived.conf
[root@xianxin 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.73
}
}
5. 主keepalived.conf传到备机并修改
[root@xianxin keepalived]# scp keepalived.conf root@192.168.60.10:/etc/keepalived/
The authenticity of host '192.168.60.10 (192.168.60.10)' can't be established.
ECDSA key fingerprint is SHA256:Rgttz3lGgV5Puj6bFsCWckmHlaIjTT/8Id/Tc6qKyrA.
ECDSA key fingerprint is MD5:e6:ad:67:8b:95:e8:f5:62:72:14:4a:58:87:dc:59:22.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.60.10' (ECDSA) to the list of known hosts.
root@192.168.60.10's password:
keepalived.conf 100% 345 432.0KB/s 00:00
#切换到备机192.168.60.10(备)上去修改keepalived.conf,只需修改
state BACKUP
priority 90
[root@aslb ~]# cd /etc/keepalived/
[root@aslb 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.73
}
}
6. 配置准备nginx
#主192.168.60.78
[root@xianxin yum.repos.d]# cp /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.bak
[root@xianxin yum.repos.d]# >/usr/share/nginx/html/index.html
[root@xianxin yum.repos.d]# vi /usr/share/nginx/html/index.html
web01
#备192.168.60.10
[root@aslb yum.repos.d]# cp /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.bak
[root@aslb yum.repos.d]# >/usr/share/nginx/html/index.html
[root@aslb yum.repos.d]# vi /usr/share/nginx/html/index.html
web02
7.启动服务并开机自启
[root@xianxin yum.repos.d]# systemctl start nginx
[root@xianxin yum.repos.d]# systemctl start keepalived
[root@xianxin yum.repos.d]# systemctl enable nginx
[root@xianxin yum.repos.d]# systemctl enable keepalived
8. 访问各站点(主备及vip)
如果出现这种情况
将vi keepalived.conf里的配置文件vip换一个IP地址
virtual_ipaddress {
192.168.60.73
}
修改完之后重启
systemctl restart keepalived
或
systemctl stop keepalived
systemctl start keepalived
9.测试主备切换
在192.168.60.78(主)上,停掉keepalived,测试主备切换
[root@xianxin keepalived]# systemctl stop keepalived
刷新页面,vip已经切换到备(192.168.60.10上)
再主上启动keepalived
[root@xianxin keepalived]# systemctl start keepalived
10.nginx存活测试
在主服务器192.168.137.100上添加如下参数:
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.73
} #这
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.73
}
track_script {
chk_nginx
}
}
在主nginx上关闭服务器
[root@xianxin keepalived]# shutdown -h now
则vip会漂移到备服务器192.168.60.10上。
浏览器输入 http://192.168.60.73 可查看是否有跳转到备服务器上