cents7+nginx+keepalived高可用

操作系统:CentOS 7
Master主节点:172.19.4.215    nginx+keepalived-master
Backup备节点:172.19.4.216    nginx+keepalived-backup
VIP   :172.19.4.222
主备节点通用命令提示符:[root@server]     主节点命令提示符:[root@Master]   备节点命令提示符:[root@Backup]  

环境安装
1、关闭Selinux及防火墙
2、下载安装包
wget https://nginx.org/download/nginx-1.9.9.tar.gz
wget http://keepalived.org/software/keepalived-2.0.7.tar.gz
3、安装必要环境
yum install gcc openssl-devel pcre-devel zlib-devel libnl-devel libnfnetluink-devel -y
4、安装nginx
[root@server src]tar -zxvf  nginx-1.9.9.tar.gz
[root@server src]cd nginx-1.9.9
[root@server nginx-1.9.9]useradd www -M -s /sbin/nologin
[root@server nginx-1.9.9] vim auto/cc/gcc  将这句“#CFLAGS="$CFLAGS -g“”注释掉后保存并关闭文件
[root@server nginx-1.9.9]#./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre
[root@server nginx-1.9.9]make
[root@server nginx-1.9.9]make install
[root@server nginx-1.9.9]cd ..
5、安装keepalived
[root@server src]tar -zxvf keepalived-2.0.7.tar.gz
[root@server src]cd keepalived-2.0.7
[root@server keepalived-2.0.7]./configure
[root@server keepalived-2.0.7]make
[root@server keepalived-2.0.7]make install
[root@server keepalived-2.0.7]cd ..
6、设置变量及开机启动
[root@server src]cp /usr/local/src/keepalived-1.3.2/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/
[root@server src]cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@server src]mkdir /etc/keepalived
[root@server src]cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@server src]cp /usr/local/sbin/keepalived /usr/sbin/
[root@server src]echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local     #设置nginx开机自启
[root@server src]echo "/etc/init.d/keepalived start" >> /etc/rc.local      #设置keepalivded开机自启
[root@server src]chmod 777 /etc/rc.d/er.local
7、配置Nginxf服务(验证节点使用)
[root@Master]cd /usr/local/nginx/html/
[root@Master html]# mv index.html index.html.bak 
[root@Master html]#vi index.html
Node-1
保存并关闭文件
[root@Backup]cd /usr/local/nginx/html/
[root@Backup html]# mv index.html index.html.bak 
[root@Backup html]#vi index.html
Node-2
保存并关闭文件
8、配置keepalived服务
[root@server ~]$  cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@Master ~]$  vi /etc/keepalived/keepalived.conf
global_defs {
  # Keepalived process identifier
  router_id nginx
}

# Script to check whether Nginx is running or not
vrrp_script check_nginx {
  script "/bin/check_nginx.sh"
  interval 2
  weight 50
}

# Virtual interface - The priority specifies the order in which the assigned interface to take over in a failover
vrrp_instance VI_01 {
  state MASTER
  interface ens32   #设置为自己的网卡
  virtual_router_id 151
  priority 110

  # The virtual ip address shared between the two NGINX Web Server which will float
  virtual_ipaddress {
    172.19.4.222/24
  }
  track_script {
    check_nginx
  }
  authentication {
    auth_type AH
    auth_pass secret
  }
}
保存并关闭文件
[root@Backup~]$  vi /etc/keepalived/keepalived.conf
global_defs {
  # Keepalived process identifier
  router_id nginx
}

# Script to check whether Nginx is running or not
vrrp_script check_nginx {
  script "/bin/check_nginx.sh"
  interval 2
  weight 50
}

# Virtual interface - The priority specifies the order in which the assigned interface to take over in a failover
vrrp_instance VI_01 {
  state BACPUP
  interface ens32   #设置为自己的网卡
  virtual_router_id 151
  priority 100

  # The virtual ip address shared between the two NGINX Web Server which will float
  virtual_ipaddress {
    172.19.4.222/24
  }
  track_script {
    check_nginx
  }
  authentication {
    auth_type AH
    auth_pass secret
  }
}
保存并关闭文件
[root@server ~]vi /bin/check_nginx.sh
#!/bin/sh
if [ -z "`pidof nginx`" ]; then
  exit 1
fi
保存并关闭文件
[root@server ~]chmod 777 /bin/check_nginx.sh
[root@server ~]sh /bin/chenk_nginx.sh
[root@server ~]echo "sh /bin/chenk_nginx.sh" >> /etc/rc.local 
[root@server ~]reboot   #重启服务器