keepalived+nginx+httpd搭建高可用服务器,避免单点故障

环境准备:
服务器1:192.168.2.121,nginx+keepalived
服务器2:192.168.2.122,nginx+keepalived
服务器3:192.168.2.123,httpd
服务器4:192.168.2.124,httpd
vip:192.168.2.88

1、keepalived安装及配置
yum install keepalived
vim /etc/keepalived/keepalived.conf
主服务器:
   router_id LVS_01
   vrrp_skip_check_adv_addr
#  vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_script check_nginx {
        script "/usr/local/keepalived/check_nginx_pid.sh"   #nginx服务检查脚本
        interval 1
        weight -2
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.2.88/24
    }
    track_script{
        check_nginx
    }
}
备用服务器:
   router_id LVS_02
   vrrp_skip_check_adv_addr
#   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script check_nginx {  
        script "/usr/local/keepalived/check_nginx_pid.sh"   #nginx服务检查脚本
        interval 1
        weight -2
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 12345678
   }
    virtual_ipaddress {
        192.168.2.88/24
    }
    track_script{
        check_nginx
    }
}

在主备服务器中新建nginx运行检测脚本:
vim /usr/local/keepalived/check_nginx_pid.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
    /usr/sbin/nginx                #重启nginx
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then    #nginx重启失败
        exit 1
    else
        exit 0
    fi
else
    exit 0
fi

chmod 755 check_nginx_pid.sh

service keepalived start#启动keepalived
ip add ens33#查看虚拟IP有没有生成
ps aux |grep keepalived#查看服务运行情况

2、nginx安装及配置
配置nginx的yum源
vi /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key

yum install nginx -y
vim /etc/nginx/nginx.conf#修改nginx主配置文件
vim /etc/nginx/conf.d/fzjh.conf#定义新的服务器
主服务器:
upstream tomcatsvr{
        server 192.168.30.123 weight=1;
        server 192.168.30.124 weight=3;
}


server {
    listen       80 default_server;
    server_name  192.168.2.121;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        proxy_pass http://tomcatsvr;
        index  index.html index.htm;
    }
备用服务器:
upstream tomcatsvr{
        server 192.168.30.123 weight=1;
        server 192.168.30.124 weight=3;
}


server {
    listen       80 default_server;
    server_name  192.168.2.122;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        proxy_pass http://tomcatsvr;
        index  index.html index.htm;
    }

nginx#启动nginx
nginx -s stop#停止nginx

3、httpd安装及配置:
yum install http
vim /var/www/html/index.html#配置站点内容
systemctl start httpd#启动httpd服务
systemctl status httpd#查看服务运行状态

通过脚本访问vip,并停掉一台keepalived服务器,查看vip地址是否发生飘移
 for i in {1..100};do curl 192.168.2.88;sleep 1;done

PS:如果vip无法通信,请将/etc/keepalived/keepalived.conf中的#  vrrp_strict注销

posted @ 2019-07-08 13:39  BicycleBoy  阅读(649)  评论(0编辑  收藏  举报