Centos7安装Nginx+Keepalived高可用集群搭建

一、准备工作

1、两台Centos7服务器

虚拟机即可,分别为:

  • 192.168.32.131
  • 192.168.32.129

2、下载Nginx压缩包并解压安装

  1. 下载地址

    1. 选择版本下载压缩包:

      https://nginx.org/download/
      
    2. wget命令下载:

      wget https://nginx.org/download/nginx-1.22.1.tar.gz
      
  2. 安装后解压压缩包并进入解压目录

    tar -xvf nginx-1.22.1.tar.gz
    
    cd nginx-1.22.1
    
  3. 安装依赖的插件

    yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
    
  4. 配置安装目录

    ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
    
  5. 安装nginx

    make && make install
    

3、yum安装keepalived

  1. 安装keepalived

    yum install keepalived
    
  2. 安装killall命令插件(重点)

    yum install psmisc
    

二、配置Nginx启动

  1. 进入配置文件目录

    cd /usr/local/nginx/conf
    
    vim nginx.conf
    
  2. 配置文件修改(只修改server部分即可)

    server {
            listen       8081;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   /data/html;
                index  index.html index.htm;
            }
    }
    
  3. 写一个简单的html文件验证nginx的可用性

    mkdir -p /data/html
    
    touch index.html
    
    vim index.html
    

    index.html内容为(129服务器可以修改为“成功启动129”)

    
    
    
        
        
        Document
    
    
    
     

    成功启动131

  4. 启动nginx

    cd /usr/local/nginx/sbin
    
    ./nginx
    
  5. 网页访问

    http://192.168.32.131:8081/
    
  6. 效果
    Centos7安装Nginx+Keepalived高可用集群搭建_第1张图片
    Centos7安装Nginx+Keepalived高可用集群搭建_第2张图片

三、配置Keepalived

1、131节点配置

  1. 进入Keepalived配置文件目录

    cd /etc/keepalived
    
    cp keepalived.conf keepalived.conf.back
    
  2. 修改Keepalived配置文件

    vim keepalived.conf
    

    配置文件内容为:

    ! Configuration File for keepalived
    
    global_defs {
       router_id 192.168.32.131
    }
    
    vrrp_script chk_nginx {
            script "/etc/keepalived/check_nginx.sh"
            interval 2
            weight -20
            fall 2
            rise 1
    }
    
    vrrp_instance VI_1 {
        state MASTER
        interface ens33
        virtual_router_id 51
        priority 100
        advert_int 1
        nopreempt
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        track_script {
            chk_nginx
        }
        virtual_ipaddress {
            192.168.32.200
        }
    }
    

    大致讲解一下配置文件内容:

    • router_id:keepalived节点唯一标识;
    • chk_nginx执行监测脚本的方法,与下方track_script中的方法一定要一样;
    • state:主节点还是从节点,下方配置的129则为BACKUP;
    • interface:网卡名称,通过“ip addr”查看;
    • virtual_router_id:0-255之内,同一局域网内,不同虚拟IP地址的virtual_router_id不能相同;
    • priority:优先级,主节点高于优先级即可,要求正整数;
    • nopreempt:开启不抢占模式;
    • virtual_ipaddress:指定虚拟IP地址。
  3. 增加chk_nginx脚本监测nginx进程

    touch check_nginx.sh
    
    vim check_nginx.sh
    

    脚本内容:

    #!/bin/bash
    A=`ps -C nginx --no-header | wc -l`        
    if [ $A -eq 0 ];then                            
     /usr/local/nginx/sbin/nginx                
     if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
      killall keepalived
     else
      exit 0
     fi
    else
     exit 0
    fi
    
  4. 给脚本文件赋予执行权限

    chmod 777 check_nginx.sh
    
  5. 启动Keepalived

    systemctl start keepalived
    
  6. 验证准备性

    1. 网卡是否正确显示虚拟IP(ip addr) Centos7安装Nginx+Keepalived高可用集群搭建_第3张图片

    2. 访问虚拟IP地址:http://192.168.32.200:8081/ Centos7安装Nginx+Keepalived高可用集群搭建_第4张图片

    3. 手动kill掉nginx,看keepalived是否能够自动启动nginx,再次启动则为成功

      ps -ef | grep nginx
      
      kill -9 两个nginx进程号
      
      # 等个几秒钟
      ps -ef | grep nginx
      

2、129节点配置

只有keepalived.conf文件内容不一致外,其他均相同,验证方式也一样

! Configuration File for keepalived

global_defs {
   router_id 192.168.32.129
}

vrrp_script chk_nginx {
        script "/etc/keepalived/check_nginx.sh"
        interval 2
        weight -20
        fall 2
        rise 1
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        192.168.32.200
    }
}

四、验证正确性

  1. 131和129都启动nginx和keepalived,查询虚拟IP+端口显示的内容为“成功启动131”,这是因为131服务器的为主节点,优先级高于129节点;
  2. 关闭131服务器的keepalived,再次查看虚拟IP+端口,显示的内容则为“成功启动129”,因为监听到131已经失效,虚拟IP已经切换到了129上了;

Centos7安装Nginx+Keepalived高可用集群搭建_第5张图片

  1. 重新启动131服务器的keepalived,再次访问虚拟IP+端口,显示的内容为“成功启动131”。

你可能感兴趣的:(运维相关,nginx,运维)