ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

NGINX高可用

2021-05-27 13:29:20  阅读:216  来源: 互联网

标签:NGINX centos13 可用 keepalived 192.168 nginx systemctl root


准备两台主机,都安装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上

标签:NGINX,centos13,可用,keepalived,192.168,nginx,systemctl,root
来源: https://blog.csdn.net/weixin_49042937/article/details/117320733

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有