wojkxlq 发表于 2018-12-31 11:45:01

18.1 集群介绍;18.2 keepalived介绍;18.3,18.4,18.5 用keepalived配置高可用集群(上,中,下);

  18.1 集群介绍
   1. 根据功能划分为两大类:高可用和负载均衡
   2. 高可用集群通常为两台服务器,一台工作,另外一台作为冗余,当提供服务的机器宕机,冗余将接替继续提供服务
   3. 实现高可用的开源软件有:heartbeat、keepalived
   负载均衡集群,需要有一台服务器作为分发器,它负责把用户的请求分发给后端的服务器处理,在这个集群里,除了分发器外,就是给用户提供服务的服务器了,这些服务器数量至少为2
   4. 实现负载均衡的开源软件有LVS、keepalived、haproxy、nginx,商业的有F5、Netscaler
  18.2 keepalived介绍
  1. 在这里我们使用keepalived来实现高可用集群,因为heartbeat在centos6上有一些问题,影响实验效果
  2.keepalived通过VRRP(Virtual Router Redundancy Protocl)来实现高可用。
  3. 在这个协议里会将多台功能相同的路由器组成一个小组,这个小组里会有1个master角色和N(N>=1)个backup角色。
  4. master会通过组播的形式向各个backup发送VRRP协议的数据包,当backup收不到master发来的VRRP数据包时,就会认为master宕机了。此时就需要根据各个backup的优先级来决定谁成为新的mater。
  5. Keepalived要有三个模块,分别是core、check和vrrp。其中core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析,check模块负责健康检查,vrrp模块是来实现VRRP协议的。
  18.3,18.4,18.5 用keepalived配置高可用集群(上,中,下)
  1.准备两台机器hao1和hao2,
  hao1作为master,hao2作为backup
  2. 两台机器都安装:yum install -y keepalived
  # yum install -y keepalived
  # yum install -y keepalived
  3. 两台机器都安装nginx,可以编译安装nginx
   也可以用yum安装nginx: yum install -y nginx
  安装epel仓库(源码包,默认centos不带nginx包):
   yum install -y epel-release
  yum install -y nginx
  4. 搜索nginx是否启动?
  # ps aux |grep nginx
  # ps aux |grep nginx
  编译启动:/etc/init.d/nginx start
  hao1机器上操作
  5. 清空keepalived.conf文件中的内容(hao1机器):
  # > /etc/keepalived/keepalived.conf
  6. 重新配置keepalived.conf文件(hao1机器):
  # vim /etc/keepalived/keepalived.conf
  插入内容:
   global_defs {
  bal_defs {
     notification_email {
     1071599947@qq.com
     }
     notification_email_from root@hao.com
     smtp_server 127.0.0.1
     smtp_connect_timeout 30
     router_id LVS_DEVEL
  }
  vrrp_script chk_nginx {
      script "/usr/local/sbin/check_ng.sh"
      interval 3
  }
  vrrp_instance VI_1 {
      state MASTER
      interface ens33
      virtual_router_id 51
      priority 100
      advert_int 1
      authentication {
        auth_type PASS
        auth_pass haomima>com
      }   
      virtual_ipaddress {
        192.168.211.100
      }   
      track_script {
        chk_nginx
      }   
  }
  7. 创建check_ng.sh脚本,并添加内容:
  # vim /usr/local/sbin/check_ng.sh
  插入内容:
  #!/bin/bash
  #时间变量,用于记录日志
  d=`date --date today +%Y%m%d_%H:%M:%S`
  #计算nginx进程数量
  n=`ps -C nginx --no-heading|wc -l`
  #如果进程为0,则启动nginx,并且再次检测nginx进程数量,
  #如果继续还是为0,说明nginx无法启动,此时需要关闭keepalived
  if [ $n -eq "0" ]; then
        /etc/init.d/nginx start
  #yum安装的nginx用:systemctl start nginx编译安装的用:/etc/init.d/nginx start
        n2=`ps -C nginx --no-heading|wc -l`
        if [ $n2 -eq "0"]; then
                  echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
                  systemctl stop keepalived
        fi
  fi
  8. 设定check_ng.sh脚本755权限:
  # chmod 755 /usr/local/sbin/check_ng.sh
  9. 启动keepalived:
  # systemctl start keepalived
  10. 搜索keepalived是否启动?
  # ps aux |grep keepalived
  11. 临时关闭getenforce防火墙:
  # setenforce 0
  关闭firewalld防火墙:
  # systemctl stop firewalld
  hao2机器上操作
  12. 清空keepalived.conf文件中的内容(hao1机器):
  # > /etc/keepalived/keepalived.conf
  13. 重新配置keepalived.conf文件(hao1机器):
  # vim /etc/keepalived/keepalived.conf
   global_defs {
     notification_email {
     1071599947@qq.com
     }
     notification_email_from root@hao.com
     smtp_server 127.0.0.1
     smtp_connect_timeout 30
     router_id LVS_DEVEL
  }
  vrrp_script chk_nginx {
      script "/usr/local/sbin/check_ng.sh"
      interval 3
  }
  vrrp_instance VI_1 {
      state BACKUP
      interface ens33
      virtual_router_id 51
      priority 90
      advert_int 1
      authentication {
        auth_type PASS
        auth_pass haomima>com
      }
      virtual_ipaddress {
        192.168.211.100
      }
      track_script {
        chk_nginx
      }
  }
  14. 创建check_ng.sh脚本,并添加内容:
  # vim /usr/local/sbin/check_ng.sh
  #!/bin/bash
  #时间变量,用于记录日志
  d=`date --date today +%Y%m%d_%H:%M:%S`
  #计算nginx进程数量
  n=`ps -C nginx --no-heading|wc -l`
  #如果进程为0,则启动nginx,并且再次检测nginx进程数量,
  #如果继续还是为0,说明nginx无法启动,此时需要关闭keepalived
  if [ $n -eq "0" ]; then
        systemctl start nginx
  #yum安装的nginx用:systemctl start nginx编译安装的用:/etc/init.d/nginx start
        n2=`ps -C nginx --no-heading|wc -l`
        if [ $n2 -eq "0"]; then
                  echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
                  systemctl stop keepalived
        fi
  fi
  15. 设定check_ng.sh脚本755权限:
  # chmod 755 /usr/local/sbin/check_ng.sh
  16. 启动keepalived:
  # systemctl start keepalived
  17. 搜索keepalived是否启动?
  # ps aux |grep keepalived
  18. 临时关闭getenforce防火墙:
  # setenforce 0
  关闭firewalld防火墙:
  # systemctl stop firewalld
  hoa1 hao2机器都打开80端口:
  打开80端口:
  # iptables -I INPUT -p tcp --dport 80 -j ACCEPT
  # iptables -I INPUT -p tcp --dport 80 -j ACCEPT
  游览器访问hao1 hao2 ip:
  测试高可用
  1. 关闭Nginx,再搜索nginx是否启动?
  # systemctl stop nginx
  # ps aux |grep nginx
  https://s4.运维网.com/wyfs02/M00/A4/8D/wKioL1mtYFvSXyXlAAA8R-oltiw103.png2. ip add查看.100是不是在hao1机器上(master):
  # ip add
  https://s5.运维网.com/wyfs02/M01/A4/8D/wKioL1mtYHXC-jgYAAB4ozcpx8U963.png模仿hao1机器挂了,关闭了 keepalived
  # systemctl stop keepalived
  # ip add
  https://s2.运维网.com/wyfs02/M02/05/DC/wKiom1mtYLDyAU-eAAB0obRC-IA810.png3. hao1机器(master)挂了,hao2机器(backup)上,ip add查看.100自动挂载到了hao2上!
  https://s1.运维网.com/wyfs02/M02/05/DC/wKiom1mtYMvh81RbAABrcN5gMto146.png4. hao1机器(master)再启动keepalived .100又挂在hao1机器:
  # systemctl start keepalived
  # ip add
https://s3.运维网.com/wyfs02/M01/A4/8D/wKioL1mtYNXjzzmkAAB_9fADV_E057.png
  




页: [1]
查看完整版本: 18.1 集群介绍;18.2 keepalived介绍;18.3,18.4,18.5 用keepalived配置高可用集群(上,中,下);