NGINX高可用
任务1 NGINX高可用
1. 准备工作
(1)需要准备两台服务器192.168.174.129(主) 和 192.168.174.139(备)
(2)两台服务器都安装nginx
(3)两台服务器安装keepalived
2. 安装Nginx
(1)使?用Nginx官?方提供的rpm包。
[root@nginx1 ~]# cat /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@nginx1 ~]# yum install -y nginx [root@nginx1 ~]# systemctl start nginx [root@nginx1 ~]# systemctl enable nginx Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
3. 两台安装keepalived
[root@nginx1 ~]# yum install -y keepalived
4. 配置主keepalived
[root@nginx1 ~]# cd /etc/keepalived/ [root@nginx1 keepalived]# cp keepalived.conf keepalived.conf.bak [root@nginx1 keepalived]# > keepalived.conf [root@nginx1 keepalived]# vi keepalived.conf vrrp_garp_interval 0 vrrp_gna_interval 0 global_defs { notification_email { 123456@qq.com } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id lb01 } #VIP1 vrrp_instance VI_1 { state MASTER interface eno16777736 virtual_router_id 55 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.174.50 } } ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "keepalived.conf" 25L, 469C written [root@nginx1 keepalived]#
5. 主keepalived.conf传到备机并修改
[root@nginx1 keepalived]# scp keepalived.conf root@192.168.174.139:/etc/keepalived/
#切换到备机192.168.174.139(备)上去修改keepalived.conf,只需修改
state BACKUP
priority 90
[root@nginx2 ~]# vi /etc/keepalived/keepalived.conf global_defs { notification_email { 123456@qq.com } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id lb01 } #VIP1 vrrp_instance VI_1 { state BACKUP interface eno16777736 virtual_router_id 55 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.174.50 } } ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "/etc/keepalived/keepalived.conf" 25L, 468C written
6. 配置准备nginx
#主192.168.174.129
[root@nginx1 keepalived]# cp /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.bak [root@nginx1 keepalived]# >/usr/share/nginx/html/index.html [root@nginx1 keepalived]# vi /usr/share/nginx/html/index.html web01
#备 192.168.174.139
[root@nginx2 keepalived]# cp /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.bak [root@nginx2 keepalived]# >/usr/share/nginx/html/index.html [root@nginx2 keepalived]# vi /usr/share/nginx/html/index.html web02
7.两台启动服务并开机自启
[root@nginx1 keepalived]# systemctl start keepalived [root@nginx1 keepalived]# systemctl enable keepalived Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
8. 访问各站点(主备及vip)
9. 测试主备切换
在192.168.174.129(主)上,停掉keepalived,测试主备切换
[root@nginx1 keepalived]# systemctl stop keepalived
刷新页面,vip已经切换到备(192.168.137.18上)
再主上启动keepalived
[root@nginx1 keepalived]# systemctl start keepalived
刷新页面,发现又切换回来了
10. nginx存活测试
在主服务器192.168.137.100上添加如下参数:
[root@nginx1 keepalived]# vi keepalived.conf global_defs { notification_email { 123456@qq.com } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id lb01 } vrrp_script chk_nginx { script "/opt/chknginx.sh" interval 1 weight -20 } vrrp_instance VI_1 { state MASTER interface eno16777736 virtual_router_id 55 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.174.50 } track_script { chk_nginx } } vrrp_instance VI_1 { state BACKUP interface eno16777736 virtual_router_id 55 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.174.50 } track_script { chk_nginx } } "keepalived.conf" 50L, 834C written
在/opt下创建chknginx.sh文件,授予权限
[root@nginx1 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@nginx1 keepalived]# chmod 755 /opt/chknginx.sh
在主nginx上关闭服务器
[root@nginx1 keepalived]# shutdown -h now
则vip会漂移到备服务器192.168.174.139上。
浏览器输入 http://192.168.174.50 可查看是否有跳转到备服务器上