CentOS7 keepalive+nginx高可用配置

I.准备


VIP:10.1.125.244
IP:10.1.125.152
IP:10.1.125.89 
http://10.1.125.152:88/
http://10.1.125.89:88/

http://10.1.125.244:88/


I.安装ngixn依赖包
yum  install  gcc  gcc-c++  make  automake  autoconf  libtool  pcre  pcre-devel  zlib  zlib-devel openssl openssl-devel 
 




I.安装Nginx

上传Nginx(nginx-1.6.2.tar.gz) /usr/local/src 


# cd /usr/local/src/  
# tar -zxvf nginx-1.6.2.tar.gz 
# cd nginx-1.6.2 
# ./configure --prefix=/usr/local/nginx 
# make && make install 


II.修改配置

vi /usr/local/nginx/conf/nginx.conf 
user  root;
worker_processes  1;


#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;


    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';


    #access_log  logs/access.log  main;


    sendfile        on;
    #tcp_nopush     on;


    #keepalive_timeout  0;
    keepalive_timeout  65;


    #gzip  on;


    server {
        listen       88;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}






II.修改index.html

vi /usr/local/nginx/html/index.html 


1  10.1.125.152





II.打开端口

 vi /etc/sysconfig/iptables 
## Nginx 
-A INPUT -m state --state NEW -m tcp -p tcp --dport 88 -j ACCEPT 
# service iptables restart 


或者关闭防火墙:
关闭防火墙:centos7 默认用的是firewall
systemctl stop firewalld.service
service iptables stop   




I.启动重启 Nginx 

# /usr/local/nginx/sbin/nginx -t 

 
# /usr/local/nginx/sbin/nginx 
重启 
/usr/local/nginx/sbin/nginx -s reload 
 

 

开机启动
# vi /etc/rc.local
加入 
/usr/local/nginx/sbin/nginx 



 

 


I.安装keepalived keepalived-1.2.18.tar.gz 

 
 
# cd /usr/local/src 
# tar -zxvf keepalived-1.2.18.tar.gz 
# cd keepalived-1.2.18 
# ./configure --prefix=/usr/local/keepalived 
# make && make install 
 
I.安装keepalived服务
默认目录是/usr/local 
# mkdir /etc/keepalived 
# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ 
复制到默认路径:
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ 
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ 
#---这句不用 ln -s /usr/local/sbin/keepalived /usr/sbin/ 
# ln -s /usr/local/keepalived/sbin/keepalived /sbin/ 
如果存在先删除find keep*  rm keep
cp /usr/local/keepalived/sbin/keepalived  /usr/sbin/







开机启动
# chkconfig keepalived on 




配置:10.1.125.152

# vi /etc/keepalived/keepalived.conf 
! Configuration File for keepalived


global_defs {
   router_id master
}


vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    weight -20
}






vrrp_instance VI_1 {
    state MASTER
    interface eno16777736    # ifconfig 查看IP对应的网卡
    virtual_router_id 51
    mcast_src_ip 10.1.125.152 #localIP
    priority 100
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }


    track_script {
        chk_nginx
    }


    virtual_ipaddress {
        10.1.125.244#vip
    }
}




II.配置监控

vi /etc/keepalived/nginx_check.sh 


 #!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
    /usr/local/nginx/sbin/nginx
    sleep 2
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        killall keepalived
    fi
fi






配置:10.1.125.89

# vi /etc/keepalived/keepalived.conf 
! Configuration File for keepalived


global_defs {
   router_id slave1
}
vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    weight -20
}


vrrp_instance VI_1 {
    state BACKUP
    interface eno16777736
    virtual_router_id 51
    mcast_src_ip 10.1.125.89 
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }


    track_script {
        chk_nginx
    }


    virtual_ipaddress {
        10.1.125.244
    }
}






I.监控和上一个相同
vi /etc/keepalived/nginx_check.sh 




授权
# chmod +x /etc/keepalived/nginx_check.sh 


启动keepalived

[root@edu-proxy-01 sbin]# service keepalived start 
Starting keepalived (via systemctl):                       [  确定  ]




查看VIP映射:
arp -a 10.1.125.244
arp -a 10.1.125.89
arp -a 10.1.125.152
对比mac地址








 

I.错误

[root@edu-proxy-01 sbin]# service keepalived start 
Starting keepalived (via systemctl):  Job for keepalived.service failed. See 'systemctl status keepalived.service' and 'journalctl -xn' for details.
                                                           [失败]
[root@edu-proxy-01 sbin]# systemctl status keepalived.service 
keepalived.service - SYSV: Start and stop Keepalived
   Loaded: loaded (/etc/rc.d/init.d/keepalived)
   Active: failed (Result: exit-code) since 三 2016-03-30 15:02:23 CST; 8s ago
  Process: 16720 ExecStart=/etc/rc.d/init.d/keepalived start (code=exited, status=1/FAILURE)


3月 30 15:02:23 edu-proxy-01 systemd[1]: Starting SYSV: Start and stop Keepalived...
3月 30 15:02:23 edu-proxy-01 keepalived[16720]: Starting keepalived: Usage: /sbin/keepalived {start|stop|reload|restart|condrestart|status}
3月 30 15:02:23 edu-proxy-01 keepalived[16720]: [失败]
3月 30 15:02:23 edu-proxy-01 systemd[1]: keepalived.service: control process exited, code=exited status=1
3月 30 15:02:23 edu-proxy-01 systemd[1]: Failed to start SYSV: Start and stop Keepalived.
3月 30 15:02:23 edu-proxy-01 systemd[1]: Unit keepalived.service entered failed state.
解决
[root@edu-proxy-01 sbin]# cd /usr/sbin/
[root@edu-proxy-01 sbin]# rm -f keepalived 
[root@edu-proxy-01 sbin]# cp /usr/local/keepalived/sbin/keepalived  /usr/sbin/



CentOS7 keepalive+nginx高可用配置_第1张图片





下载安装包配置文件等地址:

http://download.csdn.net/detail/dingsai88/9476850






你可能感兴趣的:(负载均衡,架构设计)