nginx+keepalived简单实现高可用

一、所需资源

两台虚拟机:172.20.53.85,172.20.53.87。在两台机器上装上nginx+keepalived

VIP:172.20.53.250

二、安装nginx

1.创建安装目录

  

mkdir -p /usr/soft

2.进入安装目录并下载nginx

wget http://nginx.org/download/nginx-1.14.0.tar.gz

3.解压

tar -zxvf nginx-1.14.0

4.新建用于安装nginx的文件夹

mkdir nginx

5.将安装路径指定到新建的文件夹

(检查过程可能会因确少某些依赖包报错,根据需要先安装完依赖包再执行)

./configure --prefix=/usr/soft/nginx

6.编译安装

make
make install

三、安装配置keepalived

1.安装

yum install -y keepalived

2.配置

# 172.20.53.85(主机)
$ vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
  state MASTER  #主从标志
  interface ens0  #网卡标志
  virtual_router_id 51 
  priority 100  #权重,主机必须比备机大
  advert_int 1
  authentication {
      auth_type PASS
      auth_pass 1111
  }
  virtual_ipaddress {
      172.20.53.250  #虚拟ip
  }
}
# 172.20.53.87(备机)
$ vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
  state BACKUP
  interface eth0
  virtual_router_id 51
  priority 90
  advert_int 1
  authentication {
      auth_type PASS
      auth_pass 1111
  }
  virtual_ipaddress {
      172.20.53.250
  }
}

3.开启服务

systemctl enable keepalived.service
systemctl start keepalived.service

4.查看虚拟IP是否绑定主机

ip addr show eth0

eth0为网卡标识,可通过ifconfig查看。

 

 此时虚拟IP已绑定主机,若发现备机也同时绑定了,先关闭防火墙和selinux再重试

5.结合nginx

  • 为方便区分主备机,修改一下nginx首页标题
vim /usr/soft/nginx/html/index.html

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!(85)</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
  • 访问虚拟IP

 

 

已经访问到主机(85)的nginx。关闭主机(85)的keepalived服务再重新访问,则跳转到备机(87)了

 

 

 

  •  高可用思路

   可以编写脚本检测主机nginx是否挂掉,当挂掉尝试启动nginx,如果启动失败则关闭keepalived服务,虚拟ip自动跳转到备机

  • 添加检测脚本
vim /data/script/nginx_check.sh
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
  systemctl start nginx
  sleep 2
  if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
      systemctl stop keepalived.service
  fi
fi
  • 配置keepalived配置文件
# 主机(85)配置
global_defs {
 router_id LVS_DEVEL
}
vrrp_script chk_nginx {
  script "/data/script/nginx_check.sh"
  interval 2
  weight -20
}
vrrp_instance VI_1 {
  state MASTER
  interface eth0
  virtual_router_id 51
  priority 100
  advert_int 1
  authentication {
      auth_type PASS
      auth_pass 1111
  }
  track_script {
      chk_nginx
  }
  virtual_ipaddress {
      172.20.53.250
  }
}
# 备机(87)配置
global_defs {
 router_id dreamer1
}
vrrp_script chk_nginx {
  script "/etc/keepalived/nginx_check.sh" 
  interval 2 
  weight -20
}
vrrp_instance VI_1 {
  state BACKUP
  interface eth0
  virtual_router_id 51
  priority 90
  advert_int 1
  authentication {
      auth_type PASS
      auth_pass 1111
  }
  track_script {
      chk_nginx 
  }
  virtual_ipaddress {
      172.20.53.250
  }
}
  • 重启并查看keepalived日志
tailf /var/log/messages -n 50 | grep 'Keepalive'

 

 成功加载脚本

posted @ 2020-06-16 00:09  不速之客  阅读(723)  评论(0编辑  收藏  举报