• 什么是高可用

高可用HA(High Availability)是分布式系统架构设计中必须考虑的因素之一,它通常是指,通过设计减少系统不能提供服务的时间。如果一个系统能够一直提供服务,那么这个可用性则是百分之百,但是天有不测风云。所以我们只能尽可能的去减少服务的故障。

  • 解决的问题

在生产环境上很多时候是以Nginx做反向代理对外提供服务,但是一天Nginx难免遇见故障,如:服务器宕机。当Nginx宕机那么所有对外提供的接口都将导致无法访问。
虽然我们无法保证服务器百分之百可用,但是也得想办法避免这种悲剧,今天我们使用keepalived来实现Nginx的高可用。

  • 双机热备方案

这种方案是国内企业中最为普遍的一种高可用方案,双机热备其实就是指一台服务器在提供服务,另一台为某服务的备用状态,当一台服务器不可用另外一台就会顶替上去。

  • keepalived是什么

Keepalived软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP (Virtual Router Redundancy Protocol ,虚拟路由器冗余协议)功能。因此,Keepalived除了能够管理LVS软件外,还可以作为其他服务(例如:Nginx、Haproxy、MySQL等)的高可用解决方案软件

  • 故障转移机制

Keepalived高可用服务之间的故障切换转移,是通过VRRP协议 来实现的。
在 Keepalived服务正常工作时,主 Master节点会不断地向备节点发送(多播的方式)心跳消息,用以告诉备Backup节点自己还活着,当主 Master节点发生故障时,就无法发送心跳消息,备节点也就因此无法继续检测到来自主 Master节点的心跳了,于是调用自身的接管程序,接管主Master节点的 IP资源及服务。而当主 Master节点恢复时,备Backup节点又会释放主节点故障时自身接管的IP资源及服务,恢复到原来的备用角色。

一.nginx参考单机部署文档

二.keepalived部署

  1. 外网环境下 yum 安装

    1. yum -y install keepalived
  2. 修改 /etc/keepalived 下配置文件

    1. vi /etc/keepalived/keepalived.conf

    主节点配置内容: ```bash ! Configuration File for keepalived

global_defs { router_id app_router }

检测脚本

vrrp_script chk_http_port { script “/etc/keepalived/check_pid.sh” #心跳执行的脚本,检测nginx是否启动 interval 2 #(检测脚本执行的间隔,单位是秒) weight -20 #权重 }

vrrp_instance VI_1 { state MASTER interface ens192 virtual_router_id 66 priority 100 unicast_src_ip 本机IP # 配置单播的源地址 unicast_peer { 从机IP 从机IP #配置单播的目标地址,多个 ip 回车 ip 回车 ip }

  1. advert_int 1
  2. authentication {
  3. auth_type PASS
  4. auth_pass 1111
  5. }
  6. track_script {
  7. chk_http_port #(调用检测脚本)
  8. }
  9. virtual_ipaddress {
  10. 虚拟IP,尽可能在一个网段
  11. }

}

  1. 从节点配置:
  2. ```bash
  3. ! Configuration File for keepalived
  4. global_defs {
  5. router_id app_router # 节点之间相同
  6. }
  7. #检测脚本
  8. vrrp_script chk_http_port {
  9. script "/etc/keepalived/check_pid.sh" #心跳执行的脚本,检测nginx是否启动
  10. interval 2 #(检测脚本执行的间隔)
  11. weight 2 #权重
  12. }
  13. #vrrp 实例定义部分
  14. vrrp_instance VI_1 {
  15. state BACKUP # 指定keepalived的角色,MASTER为主,BACKUP为备
  16. interface ens192 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡) ip addr 查看
  17. virtual_router_id 66 # 虚拟路由编号,主从要一直
  18. priority 50 # 优先级,数值越大,获取处理请求的优先级越高
  19. unicast_src_ip 本机IP # 配置单播的源地址
  20. unicast_peer {
  21. 其他节点IP #配置单播的目标地址
  22. }
  23. advert_int 1 # 检查间隔,默认为1s(vrrp组播周期秒数)
  24. #授权访问
  25. authentication {
  26. auth_type PASS #设置验证类型和密码,MASTER和BACKUP必须使用相同的密码才能正常通信
  27. auth_pass 1111
  28. }
  29. track_script {
  30. chk_http_port #(调用检测脚本)
  31. }
  32. virtual_ipaddress {
  33. 虚拟IP # 定义虚拟ip(VIP),可多设,每行一个
  34. }
  35. }
  • nginx检测脚本:check_pid.sh ```bash

    !/bin/bash

    检测nginx是否启动了

    A=ps -C nginx --no-header |wc -l
    if [ $A -eq 0 ];then #如果nginx没有启动就启动nginx
    1. /usr/local/nginx/sbin/nginx #重启nginx,环境变量不生效,需要全路径
    2. if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then #nginx重启失败,则停掉keepalived服务,进行VIP转移
    3. killall keepalived
    4. fi
    fi
  1. - 授权检测脚本:
  2. ```bash
  3. chmod 775 check_pid.sh
  • nacos检测脚本: check_pid.sh ```bash

    !/bin/bash

    检测nacos是否启动了

    A=ps -ef | grep nacos | grep -v grep | wc -l if [ $A -eq 0 ];then #如果nacos没有启动就启动nginx
    1. /home/nacos/bin/startup.sh #重启nacos,环境变量不生效,需要全路径
    2. if [ `ps -ef | grep nacos | grep -v grep | wc -l` -eq 0 ];then #nacos重启失败,则停
    掉keepalived服务,进行VIP转移
    1. killall keepalived
    2. fi
    fi
  1. - 授权检测脚本
  2. ```bash
  3. chmod 775 check_pid.sh
  • mysql ```shell

    !/bin/bash

    host=127.0.0.1 user=账号 passwd=密码

    安装路径调整

    /usr/bin/mysqladmin -h 127.0.0.1 -u $user -p’$passwd’ ping &>/dev/null if [ $? -eq 0 ] then /usr/bin/mysql -u$user -p$passwd —connect_timeout=5 -e “show databases;” if [ $? -ne 0 ] then /usr/ps aux |grep mysql |grep -v grep | awk ‘{print $2}’ | xargs kill -a /usr/bin/systemctl start mysqld fi else /usr/bin/systemctl start mysqld fi
  1. 启动关闭
  2. ```bash
  3. systemctl start/restart/stop keepalived

三.问题

  • 两台机器VIP都有,发生争抢:

https://www.cnblogs.com/xiaobaozi-95/p/11497295.html

  • 注意检测脚本的执行,脚本名称不要有和检测目标相同字段 nacos,check_pid.sh

    四.搭配nginx负载均衡

    4.1 nacos

    ```shell upstream nacos-servers {
    1. server 10.83.228.63:8848;
    2. server 10.83.228.64:8848;
    3. server 10.83.230.67:8848;
    }

server { listen 8848;

  1. #charset koi8-r;
  2. location / {
  3. proxy_pass http://nacos-servers/;
  4. }
  5. #access_log logs/host.access.log main;
  6. # redirect server error pages to the static page /50x.html
  7. #
  8. error_page 500 502 503 504 /50x.html;
  9. location = /50x.html {
  10. root html;
  11. }
  12. }
  1. <a name="yrg6v"></a>
  2. ## 4.2 fastdfs
  3. ```shell
  4. upstream fastdfs-servers {
  5. server 10.83.228.63:22123;
  6. server 10.83.228.64:22123;
  7. server 10.83.230.67:22123;
  8. }
  9. server {
  10. listen 22123;
  11. #charset koi8-r;
  12. location / {
  13. proxy_pass http://fastdfs-servers/;
  14. }
  15. #access_log logs/host.access.log main;
  16. # redirect server error pages to the static page /50x.html
  17. #
  18. error_page 500 502 503 504 /50x.html;
  19. location = /50x.html {
  20. root html;
  21. }
  22. }