2017最新nginx+keepalived+centos7安装配置过程

一、 地址规划

nginx1-10.1.1.2

nginx2-10.1.1.3

vip-10.1.1.4

修改两台主机名:hostnamectl set-hostname nginx1

hostnamectl set-hostname nginx2

二、在nginx1和nginx2上分别安装nginx,这里用的是最新的稳定版

先安装依赖包

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

官网下载nginx-1.10.3源码包

tar xzvf nginx-1.10.3.tar.gz 
   mkdir -p /usr/local/nginx
   mv nginx-1.10.3 /usr/local/
    cd /usr/local/nginx-1.10.3/
  ./configure --prefix=/usr/local/nginx
   make && make install

安装后注册nginx服务,使其可以开机启动
   vi /lib/systemd/system/nginx.service
  [Unit]
  Description=nginx
  After=network.target
 
  [Service]
  Type=forking
  ExecStart=/usr/local/nginx/sbin/nginx
  ExecReload=/usr/local/nginx/sbin/nginx -s reload
  ExecStop=/usr/local/nginx/sbin/nginx -s quit
  PrivateTmp=true
 
  [Install]
  WantedBy=multi-user.target

设置nginx开机启动命令: systemctl enable nginx

启动nginx systemctl start nginx

三、分别在nginx1-2上安装keepalived

centos7可以直接yum安装keepalived,注意配置文件和脚本,否则不成功

keepalived -v
Keepalived v1.2.13 (11/05,2016) 版本信息

yum -y install keepalived
  mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.orginal
  vi /etc/keepalived/keepalived.conf

nginx1-master配置文件如下(为了方便你的粘贴就不上截图了):
 more /etc/keepalived/keepalived.conf (nginx1-master)

! Configuration file for keepalived
global_defs {
   notification_email {
    [email protected]
        }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
#监控nginx
vrrp_script chk_nginx {
    script "/etc/keepalived/script/monitor_nginx.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state MASTER  #主从设置 MASTER
    interface eth1  #网卡名
    virtual_router_id 51
    mcast_src_ip 10.1.1.2 #本机ip
    priority 51  #从机小于主机
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass chtopnet
    }
    virtual_ipaddress {
        10.1.1.4  #VIP 的IP
    }
    track_script {
        chk_nginx  #检测脚本
    }

}

virtual_server 10.1.1.4 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

real_server 10.1.1.2 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }

nginx2-backup配置文件如下:

more /etc/keepalived/keepalived.conf (nginx1-backup)

! Configuration file for keepalived
global_defs {
   notification_email {
    [email protected]
        }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
#监控NGINX
vrrp_script chk_nginx {
    script "/etc/keepalived/script/monitor_nginx.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state BACKUP  #主从设置 MASTER
    interface eth1  #网卡名
    virtual_router_id 50
    mcast_src_ip 10.1.1.2 #本机ip
    priority 50  #从机小于主机
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass chtopnet
    }
    virtual_ipaddress {
        10.1.1.4  #VIP 的IP
    }
    track_script {
        chk_nginx  #检测脚本
    }

}

virtual_server 10.1.1.4 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

real_server 10.1.1.2 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
    real_server 10.1.1.3 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}
监测脚本如下(nginx1-2相同):

more /etc/keepalived/monitor_nginx.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
        systemctl stop keepalived
    fi
fi

hostnamectl set-hostname nginx2
# 安装ipvsadm ()

#  yum install ipvsadm

在两台nginx分别执行脚本,绑定虚拟IP,抑制ARP广播

#!/bin/bash
#description: Config realserver

VIP=10.1.1.4
 
/etc/rc.d/init.d/functions
 
case "$1" in
start)
       /sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
       /sbin/route add -host $VIP dev lo:0
       echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
       echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
       echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
       echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
       sysctl -p >/dev/null 2>&1
       echo "RealServer Start OK"
       ;;
stop)
       /sbin/ifconfig lo:0 down
       /sbin/route del $VIP >/dev/null 2>&1
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
       echo "RealServer Stoped"
       ;;
*)
       echo "Usage: $0 {start|stop}"
       exit 1
esac
 
exit 0

时间: 2024-12-05 19:26:00

2017最新nginx+keepalived+centos7安装配置过程的相关文章

Centos7 安装配置OpenLdap服务及OpenLdap管理工具

我们上一篇文章中介绍了,Centos7+Openvpn使用Windows AD(LDAP)验证登录的配置介绍.说到LDAP服务,我们知道不止windows有,linux下也有,比如openldap,sambaLDAP服务,具体就不多介绍了,我们今天主要介绍一下Centos7 安装配置OpenLdap及Ldap管理工具等操作,为后面的Centos7+Openvpn+openldap验证登录做好基础. 我们首先配置一个指定源,我们源地址指向了阿里云的仓库源 cd /etc/yum.repos.d v

Centos7安装配置Apache+PHP+Mysql+phpmyadmin

转载自: Centos7安装配置Apache+PHP+Mysql+phpmyadmin 一.安装Apache yum install httpd 安装成功后,Apache操作命令: systemctl start httpd //启动apache systemctl stop httpd //停止apache systemctl restart httpd //重启apache systemctl enable httpd //设置apache开机启动 异常处理我再阿里云上配置并出现启动Apac

CentOS7安装配置redis-3.0.0

清园 沉没的Atlantis CentOS7安装配置redis-3.0.0 一.安装必要包 yum install gcc 二.linux下安装 #下载 wget http://download.redis.io/releases/redis-3.0.0.tar.gz tar zxvf redis-3.0.0.tar.gz cd redis-3.0.0 #如果不加参数,linux下会报错 make MALLOC=libc  安装好之后,启动文件 #启动redis src/redis-server

openvpn安装配置过程

前言 之前为了方便远程办公时访问公司的内部系统,如:svn.OA.wiki.禅道等等:通通在防火墙上做了端口映射.然后 有个内部系统被黑了,各种弱口令没办法.果断关闭端口映射,看来还是得搭建个VPN服务器,vpn设备感觉大材小用.马上就想到了开源的openvpn, 下面就来介绍openvpn的安装配置过程. openvpn简介 官方网站:https://openvpn.net 打不开请爬墙 openssl原理:http://www.178linux.com/archives/2704 参考书生的

minicom在虚拟机(linux)安装配置过程

1. minicom需要ncurses库的支持,否则安装会有问题. A. 下载ncurses.我选择是ncurses-5.6.tar.gz 下载地址:http://directory.fsf.org/project/ncurses/ B. 解压缩到/opt/ncurses目录下.# tar zxvf ncurses-5.5.tar.gz –C /opt/ncurses C. 到ncurses目录下,配置编译ncurses. 源码安装的三个步骤(configure.make.make instal

游戏服务端pomelo完整安装配置过程

游戏服务端pomelo安装配置 一.安装环境 debian 7.0 amd64 二.安装需要的组件 1.安装nodejs 注:debian下nodejs没有相应的apt包,所以无法用apt-get安装,只能通过nodejs的源码包安装, 这里有比较全的其他系统环境下安装nodejs的方式https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager a.安装所需的组件,python g++ #apt-get i

Centos7安装配置gitlab

Centos7安装配置gitlab 这篇文字我会介绍在Centos7上安装gitlab,配置gitlab的smtp,并且创建项目demo. sudo yum install openssh-server sudo yum install postfix sudo yum install cronie sudo service postfix start sudo chkconfig postfix on sudo lokkit -s http -s ssh 使用清华大学gitlab的镜像http

CentOS7安装配置DNS服务器

准备工作(假设名称为bigcloud.local) #更改主机名称 #vi /etc/sysconfig/network # Created by anaconda NETWORKING=yes HOSTNAME=bigcloud.local #修改文件/etc/hosts,内容如下: 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1         localhost local

sonar安装配置过程

1.下载sonarqube(下载社区版不需要许可证),sonarscanner,安装jdk,数据库,本次安装使用jdk1.7+mysql5.7(安装配置过程不详述,自行百度).2.数据库新建数据库sonar.3.解压sonarqube进入conf目录下,sonar.properties添加配置 #数据库地址 sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewr