首页 > 系统相关 >Nginx_高可用配置(keepalived+nginx主从热备双机架构)!

Nginx_高可用配置(keepalived+nginx主从热备双机架构)!

时间:2022-12-08 20:00:52浏览次数:52  
标签:热备 Slave root keepalived local nginx Master 双机


系统版本 Centos7
IP:192.168.8.145 Master
IP:192.168.8.150 Slave

一.Linux安装nginx (主从都安装NGINX)
1.安装依赖
[root@Master ~]# yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

[root@Slave ~]# yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

2.创建一个文件夹
[root@Master ~]# cd /usr/local
[root@Master local]# mkdir nginx
[root@Master local]# cd nginx/
[root@Master nginx]#

[root@Slave ~]# cd /usr/local
You have new mail in /var/spool/mail/root
[root@Slave local]# mkdir nginx
[root@Slave local]# cd nginx/
[root@Slave nginx]#

3.将nginx安装包放在nginx文件夹下并解压或者使用wget联网下载
[root@Master nginx]# wget https://nginx.org/download/nginx-1.16.1.tar.gz
[root@Master nginx]# tar -zxvf nginx-1.16.1.tar.gz

[root@Slave nginx]# wget https://nginx.org/download/nginx-1.16.1.tar.gz
[root@Slave nginx]# tar -zxvf nginx-1.16.1.tar.gz

4.编译并make
[root@Master nginx]# cd nginx-1.16.1
[root@Master nginx-1.16.1]# ./configure --prefix=/usr/local/nginx
[root@Master nginx-1.16.1]# make
[root@Master nginx-1.16.1]# make install

修改nginx访问界面html
[root@Master nginx-1.16.1]# cd /usr/local/nginx/html/
You have new mail in /var/spool/mail/root
[root@Master html]# ll
total 8
-rw-r–r-- 1 root root 494 Oct 12 03:23 50x.html
-rw-r–r-- 1 root root 612 Oct 12 03:23 index.html
[root@Master html]# vi index.html

Master ip:192.168.8.145

[root@Slave nginx]# cd nginx-1.16.1
[root@Slave nginx-1.16.1]# ./configure --prefix=/usr/local/nginx
[root@Slave nginx-1.16.1]# make
[root@Slave nginx-1.16.1]# make install

修改nginx访问界面html
[root@Slave nginx-1.16.1]# cd /usr/local/nginx/html/
You have new mail in /var/spool/mail/root
[root@Slave html]# ll
total 8
-rw-r–r-- 1 root root 494 Oct 12 03:23 50x.html
-rw-r–r-- 1 root root 612 Oct 12 03:23 index.html
[root@Slave html]# vi index.html

Slave ip:192.168.8.150

常用命令
#启动
/usr/local/nginx/sbin/nginx

访问验证:
​​​ http://192.168.8.145/​​ http://192.168.8.150/

Nginx_高可用配置(keepalived+nginx主从热备双机架构)!_linux


Nginx_高可用配置(keepalived+nginx主从热备双机架构)!_linux_02

#修改配置文件重新加载
/usr/local/nginx/sbin/nginx -s reload

#关闭
/usr/local/nginx/sbin/nginx

//—二:安装Keepalived—
1.联网下载到/usr/local目录下并解压有可能会提示连接不成功,加上它提示的命令再下载就好了
[root@Master html]# cd /usr/local
[root@Master local]# wget https://www.keepalived.org/software/keepalived-1.4.2.tar.gz
[root@Master local]# tar -zxvf keepalived-1.4.2.tar.gz

[root@Slave html]# cd /usr/local
You have new mail in /var/spool/mail/root
[root@Slave local]# wget https://www.keepalived.org/software/keepalived-1.4.2.tar.gz
[root@Slave local]# tar -zxvf keepalived-1.4.2.tar.gz

2.安装相关依赖,有不用下载了
[root@Master local]# yum install -y gcc openssl-devel popt-devel

[root@Slave local]# yum install -y gcc openssl-devel popt-devel

3.编译安装
[root@Master local]# cd keepalived-1.4.2
[root@Master keepalived-1.4.2]# ./configure --prefix=/usr/local/keepalived
[root@Master keepalived-1.4.2]# make
[root@Master keepalived-1.4.2]# make install

[root@Slave local]# cd keepalived-1.4.2
[root@Slave keepalived-1.4.2]# ./configure --prefix=/usr/local/keepalived
[root@Slave keepalived-1.4.2]# make
[root@Slave keepalived-1.4.2]# make install

4.相关配置,复制就完了
[root@Master keepalived-1.4.2]# pwd
/usr/local/keepalived-1.4.2
You have new mail in /var/spool/mail/root
[root@Master keepalived-1.4.2]# cp /usr/local/keepalived-1.4.2/keepalived/etc/init.d/keepalived /etc/init.d/
[root@Master keepalived-1.4.2]# mkdir /etc/keepalived
[root@Master keepalived-1.4.2]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@Master keepalived-1.4.2]# cp /usr/local/keepalived-1.4.2/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@Master keepalived-1.4.2]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

[root@Slave keepalived-1.4.2]# pwd
/usr/local/keepalived-1.4.2
You have new mail in /var/spool/mail/root
[root@Slave keepalived-1.4.2]# cp /usr/local/keepalived-1.4.2/keepalived/etc/init.d/keepalived /etc/init.d/
[root@Slave keepalived-1.4.2]# mkdir /etc/keepalived
[root@Slave keepalived-1.4.2]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@Slave keepalived-1.4.2]# cp /usr/local/keepalived-1.4.2/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@Slave keepalived-1.4.2]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@Slave keepalived-1.4.2]#

使用keepalived保证nginx的高可用(一主一从),我的主服务器ip为(192.168.8.145),从机(192.168.8.150)
在两条服务器分别安装nginx和keepalived,修改keeplived.conf文件

vim /etc/keepalived/keepalived.conf

主机配置文件:

! Configuration File for keepalived

global_defs {
#一个没重复的名字即可
router_id LVS_DEVEL
}

检测nginx是否运行

vrrp_script chk_nginx {
script “/etc/keepalived/nginx_check.sh”
interval 2
#权重减少20,减少完要比从机的值小
weight -20
}

vrrp_instance VI_1 {

MASTER代表主机

state MASTER

网卡名字 ip addr 可以查看

interface eno16777736

同一个keepalived集群的virtual_router_id相同

virtual_router_id 51

权重,master要大于slave

priority 100

主备通讯时间间隔

advert_int 1

如果两节点的上联交换机禁用了组播,则采用vrrp单播通告的方式

本机ip

unicast_src_ip 192.168.8.145
unicast_peer {

其他机器ip

192.168.8.150
}

设置nopreempt防止抢占资源

nopreempt

主备保持一致

authentication {
auth_type PASS
auth_pass 1111
}

与上方nginx运行状况检测呼应

track_script {
chk_nginx
}

virtual_ipaddress {

虚拟ip地址(VIP,一个尚未占用的同一网段内网ip即可,方便跨域访问)

192.168.8.166
}
}

[root@Master keepalived-1.4.2]# cd /etc/keepalived/
[root@Master keepalived]# ll
total 4
-rw-r–r-- 1 root root 3550 Oct 12 03:41 keepalived.conf
[root@Master keepalived]# cp keepalived.conf keepalived.conf.bak
[root@Master keepalived]# vi /etc/keepalived/keepalived.conf

global_defs {
router_id LVS_DEVEL
}

vrrp_script chk_nginx {
script “/etc/keepalived/nginx_check.sh”
interval 2
weight -20
}

vrrp_instance VI_1 {
state MASTER
interface enp0s3
virtual_router_id 51
priority 100
advert_int 1
unicast_src_ip 192.168.8.145
unicast_peer {
192.168.8.150
}

nopreempt
authentication {
auth_type PASS
auth_pass 1111
}

track_script {
chk_nginx
}

virtual_ipaddress {
192.168.8.166
}
}


从机配置文!
[root@Slave keepalived-1.4.2]# cd /etc/keepalived/
You have new mail in /var/spool/mail/root
[root@Slave keepalived]# ll
total 4
-rw-r–r-- 1 root root 3550 Oct 12 03:42 keepalived.conf
[root@Slave keepalived]# cp keepalived.conf keepalived.conf.bak
[root@Slave keepalived]# vi /etc/keepalived/keepalived.conf

global_defs {
router_id LVS_DEVEL
}

vrrp_script chk_nginx {
script “/etc/keepalived/nginx_check.sh”
interval 2
weight -20
}

vrrp_instance VI_1 {
state Slave
interface enp0s3
virtual_router_id 51
priority 90
advert_int 1

unicast_src_ip 192.168.8.150
unicast_peer {
192.168.8.145
}

nopreempt

authentication {
auth_type PASS
auth_pass 1111
}

track_script {
chk_nginx
}

virtual_ipaddress {
192.168.8.166
}
}

主从机都需配置检测nginx是否在运行,不在允许就直接启动nginx的脚本,和keepalived放在一起
脚本名称 nginx_check.sh
如果 nginx 停止运行,尝试启动,如果无法启动则杀死本机的 keepalived 进程,keepalived将虚拟 ip 绑定到 BACKUP 机器上。内容如下:

#!/bin/bash
A=​​​ps -C nginx –no-header |wc -l​​​ if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ ​​ps -C nginx --no-header |wc -l​​ -eq 0 ];then
killall keepalived
fi
fi

[root@Master keepalived]# pwd
/etc/keepalived
[root@Master keepalived]# vi nginx_check.sh
#!/bin/bash
A=​​​ps -C nginx –no-header |wc -l​​​ if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ ​​ps -C nginx --no-header |wc -l​​ -eq 0 ];then
killall keepalived
fi
fi

[root@Slave keepalived]# pwd
/etc/keepalived
[root@Slave keepalived]# vi nginx_check.sh
#!/bin/bash
A=​​​ps -C nginx –no-header |wc -l​​​ if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ ​​ps -C nginx --no-header |wc -l​​ -eq 0 ];then
killall keepalived
fi
fi

只要配置好了,以后直接启动keepalived就好了,keepalived运行之后就会检测nginx是否在运行,不在运行就通过脚本去启动

自动重启不了解决方案

查看脚本是否有运行的权限
如果你是root登陆的话(不是的话,切换到root用户,对*.sh 赋可执行的权限)

chmod 777*.sh
或者
chmod +x *.sh

主:
[root@Master keepalived]# chmod +x nginx_check.sh
[root@Master keepalived]# ll
total 12
-rw-r–r-- 1 root root 572 Oct 12 03:55 keepalived.conf
-rw-r–r-- 1 root root 3550 Oct 12 03:48 keepalived.conf.bak
-rwxr-xr-x 1 root root 205 Oct 12 04:04 nginx_check.sh
[root@Master keepalived]#

备:
[root@Slave keepalived]# chmod +x nginx_check.sh
[root@Slave keepalived]# ll
total 12
-rw-r–r-- 1 root root 575 Oct 12 04:02 keepalived.conf
-rw-r–r-- 1 root root 3550 Oct 12 03:57 keepalived.conf.bak
-rwxr-xr-x 1 root root 205 Oct 12 04:04 nginx_check.sh
[root@Slave keepalived]#

keepalived常用命令

#启动
service keepalived start
#停止
service keepalived stop
#查看状态
service keepalived status

重新加载nginx

/usr/local/nginx/sbin/nginx -s reload

开始测试
直接访问主机的nginx服务
​ http://192.168.8.145/​​

直接访问从机的nginx服务
​ http://192.168.8.150/​​

通过keepalived 设置的虚拟ip进行访问
​ http://192.168.8.166/​​

Nginx_高可用配置(keepalived+nginx主从热备双机架构)!_html_03


Nginx_高可用配置(keepalived+nginx主从热备双机架构)!_linux_04


Nginx_高可用配置(keepalived+nginx主从热备双机架构)!_运维_05

检查启动:
ps -ef | grep keepalived

keepalived运行时,会启动3个进程,分别为:core(核心进程),check和vrrp


标签:热备,Slave,root,keepalived,local,nginx,Master,双机
From: https://blog.51cto.com/u_15906694/5923320

相关文章

  • Docker学习笔记十:Docker安装Nginx
    准备下载命令:dockerpullnginx安装可参考Docker Hub官网说明的镜像的用法  安装 第一步:简单安装创建容器命令:dockerrun-d--name=nginx-p8111:8080......
  • NGINX 开源社区捉虫征集令
    原文作者:NGINX中文社区官方团队原文链接:​​NGINX开源社区捉虫征集令​​转载来源:NGINX官方网站一元复始,万象更新!NGINX社区版本更新啦!内容上,除了严肃的技术内容,我们开设......
  • 直播服务器搭建NGINX-RTMP+JAVA
    百试百灵的直播服务器搭建教程!1.环境准备Linux服务器一台(外网、内网都行)Docker环境安排一下NGINX-RTMP镜像推流工具​​点击下载​​拉流工具​​点击下载​​2.开撸进入......
  • 用nginx缓存静态文件
      这篇教程说明你应该怎样配置nginx、设置HTTP头部过期时间,用Cache-Control中的max-age标记为静态文件(比如图片、CSS和Javascript文件)设置一个时间,这样用户的......
  • Nginx rewrite 详解
    Nginxrewrite详解本篇主要介绍nginx的rewrite重定向这个功能进行详解介绍,以及介绍它的使用场景1.rewrite基本介绍rewrite是实现URL重写的关键指令,根据reg......
  • Nginx加权轮询负载均衡
    Nginx官方支持的后端服务负载均衡策略有加权轮询和IP哈希,默认采用加权轮询策略。加权轮询流程图代码流程staticngx_http_upstream_rr_peer_t*ngx_http_upstream_ge......
  • 显示本机IP并判断IP地址,批量创建用户,安装nginx服务并启动
    完成一个shell脚本,脚本的作用。1.运行脚本可以显示出本机的ip地址2.如果ip地址中有3这个数字,那么就打印出当前的系统时间3.如果ip地址中不含3这个数字,就批量建立用户mag......
  • 使用 NGINX 在 Kubernetes 中对 TCP 和 UDP 流量进行负载均衡
    原文作者:AmirRawdatofF5原文链接:​​​​使用NGINX在Kubernetes中对TCP和UDP流量进行负载均衡​​转载来源:NGINX官方网站除了HTTP流量之外,NGINXIngressCont......
  • Zabbix监控Nginx
      在实际生产中会经常需要自定义一些各种中间件的监控项,自己一个个添加的话太繁琐了,之所以在zabbix官网和第三方网站都会提供大量的监控模板,有的写的非常可以,我们在生产中......
  • Nginx常用命令
    查看版本号./nginx-v启动./nginx查看状态ps-ef|grepnginx停止./nginx-sstop重启./nginx-sreload ......