keepalived + nginx双主 实战

  安装nginx    

                nginx 下载地址 http://nginx.org/download/nginx-1.8.0.tar.gz

      

                       安装nginx的依赖关系  

                       yum install pcre pcre-devel openssl openssl-devel -y 

                       创建用户  useradd www

   

接下来编译安装nginx   

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

       make      make install 

        /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  指定配置文件

        /usr/local/nginx/sbin/nginx -s reload   启动nginx    

             nginx安装完成 我这里只安装一台 多台一样             

keepalived 安装 

       keepalived  下载地址 http://www.keepalived.org/software/keepalived-1.2.19.tar.gz 

                                

      安装基础环境       

                       yum -y install gcc gcc-c++ pcre-devel zlib-devel openssl-devel 

     

       内核开发包安装

                        yum install  kernel-devel kernel  -y            

            

          编译安装keepalived    

      /configure --with-kernel-dir=/usr/src/kernels/2.6.32-573.3.1.el6.x86_64/   

      make && make install

     

       将安装完的keepalived 拷贝到目录                       

          DIR=/usr/local/
         ;cp $DIR/etc/rc.d/init.d/keepalived 
         /etc/rc.d/init.d/ ; cp $DIR/etc/sysconfig/keepalived /etc/sysconfig/ ;
          mkdir -p /etc/keepalived ; cp $DIR/sbin/keepalived /usr/sbin/
            

           chkconfig keepalived on

             

 编辑配置文件 默认配置文件不存在

                       vim  /etc/keepalived/keepalived.conf 

                        

        msater 配置文件   

! configuration file for keepalived
#by:v
global_defs {

}

vrrp_script chk_run {
script /root/my.sh
interval 2
weight 2
}

# VIP1                                         # 第一台

vrrp_instance VI_1 {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 151
priority 102
advert_int 1  
nopreempt                           #不抢占   当master挂了之后 飘到backup 上面    master修好之后   不抢占  
authentication {
auth_typepass        
auth_pass 1111
}

virtual_ipaddress {
192.168.80.43
                       
}

track_script {
chk_run

}
}


# VIP2                                # 第二台

vrrp_instance VI_1 {
state BACKUP
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 150
priority 100
advert_int 1  
authentication {
auth_typepass        
auth_pass 2222
}

virtual_ipaddress {
192.168.80.44
                       
}

track_script {
chk_run

  }
}

 backup 配置文件   

                       vim  /etc/keepalived/keepalived.conf 

    

vrrp_script chk_run {
script "/root/my.sh"
interval 2
weight 2
}

# VIP1
vrrp_instance VI_1 {
state BACKUP
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 151
priority 100
advert_int 1

authentication {
auth_typepass
auth_pass 1111
}

virtual_ipaddress {
192.168.80.43
}

track_script {
chk_run

}

}

# VIP2
vrrp_instance VI_1 {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 150
priority 102
advert_int 1
nopreempt

authentication {
auth_typepass
auth_pass 2222
}

virtual_ipaddress {
192.168.80.44
}

track_script {
chk_run

   }

}

         

  在上面我们定义了引my.sh脚本  通过my.sh脚本检测 ngixn 是否正常 我们来写一个脚本   

        

#!/bin/bash
A=`netstat -ltnp |grep 80 |wc -l`
if [ $A -eq 0 ];then
/etc/rc.d/init.d/keepalived stop
fi

chmod +x my.sh   

                     

          启动keepalived服务     到此完成  

 

/etc/rc.d/init.d/keepalived start    

          

                       

 

原文地址:https://www.cnblogs.com/hanwei999/p/5051358.html