yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
官方网站下载 nginx:http://nginx.org/
第一步:把 nginx 的源码包 nginx-1.8.0.tar.gz 上传到 linux 系统
第二步:解压:tar -zxvf nginx-1.8.0.tar.gz
第三步:进入 nginx-1.8.0 目录 使用 configure 命令创建一 makeFile 文件。
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
执行后可以看到 Makefile 文件
第四步:编译
make
第五步:安装
make install
注意:启动 nginx 之前,上边将临时文件目录指定为/var/temp/nginx/client, 需要在/var 下
创建此 目录
mkdir /var/temp/nginx/client -p
进入到 Nginx 目录下的 sbin 目录
cd /usr/local/nginx/sbin
输入命令启动 Nginx
./nginx
启动后查看进程
ps aux|grep nginx
安装启动完毕
关闭nginx
./nginx -s stop
重启
./nginx -s reload
yum -y install kernel-devel*
yum -y install openssl-*
yum -y install popt-devel
yum -y install lrzsz
yum -y install openssh-clients
yum -y install libnl libnl-devel popt
将keepalived-1.2.15.tar.gz上传到服务器/usr/local/src下。
cd /usr/local/src
解压:
tar -zxvf keepalived-1.2.15.tar.gz
进入文件夹:
cd keepalived-1.2.15
执行配置命令
./configure --prefix=/usr/local/keepalived
编译
make
安装
make install
至此安装成功
拷贝执行文件
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
将init.d文件拷贝到etc下,加入开机启动项
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
将keepalived文件拷贝到etc下,加入网卡配置
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
创建keepalived文件夹
mkdir -p /etc/keepalived
将keepalived配置文件拷贝到etc下
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
添加可执行权限
chmod +x /etc/init.d/keepalived
chkconfig --add keepalived #添加时必须保证/etc/init.d/keepalived存在
chkconfig keepalived on
添加完可查询系统服务是否存在:chkconfig --list
启动:service keepalived start
停止:service keepalived stop
重启:service keepalived restart
1.在系统服务目录里创建nginx.service文件
vi /lib/systemd/system/nginx.service
2.内容如下
[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
3.设置开机启动
systemctl enable nginx.service
4.其他命令
启动nginx服务
systemctl start nginx.service
设置开机启动
systemctl enable nginx.service
停止开机自启动
systemctl disable nginx.service
查看服务当前状态
systemctl status nginx.service
重新启动服务
systemctl restart nginx.service
查看所有已启动的服务
systemctl list-units --type=service
# systemctl start firewalld
# firewall-cmd --zone=public --add-port=80/tcp --permanent
# systemctl restart firewalld.service
# firewall-cmd --permanent --zone=public --list-ports
#systemctl enable firewalld
主nginx
修改主nginx下/etc/keepalived/keepalived.conf文件
! Configuration File for keepalived
global_defs {
## keepalived 自带的邮件提醒需要开启 sendmail 服务。 建议用独立的监控或第三方 SMTP
router_id liuyazhuang133 ## 标识本节点的字条串,通常为 hostname
}
## keepalived 会定时执行脚本并对脚本执行的结果进行分析,动态调整 vrrp_instance 的优先级。如果脚本执行结果为 0,并且 weight 配置的值大于 0,则优先级相应的增加。如果脚本执行结果非 0,并且 weight配置的值小于 0,则优先级相应的减少。其他情况,维持原本配置的优先级,即配置文件中 priority 对应的值。
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" ## 检测 nginx 状态的脚本路径
interval 2 ## 检测时间间隔
weight -20 ## 如果条件成立,权重-20
}
## 定义虚拟路由, VI_1 为虚拟路由的标示符,自己定义名称
vrrp_instance VI_1 {
state MASTER ## 主节点为 MASTER, 对应的备份节点为 BACKUP
interface eth1 ## 绑定虚拟 IP 的网络接口,与本机 IP 地址所在的网络接口相同, 我的是 eth1
virtual_router_id 33 ## 虚拟路由的 ID 号, 两个节点设置必须一样, 可选 IP 最后一段使用, 相同的 VRID 为一个组,他将决定多播的 MAC 地址
mcast_src_ip 192.168.136.13 ## 本机 IP 地址
priority 100 ## 节点优先级, 值范围 0-254, MASTER 要比 BACKUP 高
nopreempt ## 优先级高的设置 nopreempt 解决异常恢复后再次抢占的问题
advert_int 1 ## 组播信息发送间隔,两个节点设置必须一样, 默认 1s
## 设置验证信息,两个节点必须一致
authentication {
auth_type PASS
auth_pass 1111 ## 真实生产,按需求对应该过来
}
## 将 track_script 块加入 instance 配置块
track_script {
chk_nginx ## 执行 Nginx 监控的服务
} #
# 虚拟 IP 池, 两个节点设置必须一样
virtual_ipaddress {
192.168.136.10 ## 虚拟 ip,可以定义多个
}
}
备nginx
修改备nginx下/etc/keepalived/keepalived.conf文件
配置备nginx时需要注意:需要修改state为BACKUP , priority比MASTER低,virtual_router_id和master的值一致
! Configuration File for keepalived
global_defs {
router_id liuyazhuang133
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 33
mcast_src_ip 192.168.136.14
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.136.10
}
}
keepalived是通过检测keepalived进程是否存在判断服务器是否宕机,如果keepalived进程在但是nginx进程不在了那么keepalived是不会做主备切换,所以我们需要写个脚本来监控nginx进程是否存在,如果nginx不存在就将keepalived进程杀掉。
在主nginx上需要编写nginx进程检测脚本(check_nginx.sh),判断nginx进程是否存在,如果nginx不存在就将keepalived进程杀掉,check_nginx.sh内容如下:
#!/bin/bash
# 如果进程中没有nginx则将keepalived进程kill掉
A=`ps -C nginx --no-header |wc -l` ## 查看是否有 nginx进程 把值赋给变量A
if [ $A -eq 0 ];then ## 如果没有进程值得为 零
service keepalived stop ## 则结束 keepalived 进程
fi
将check_nginx.sh拷贝至/etc/keepalived下,
脚本测试:
将nginx停止,将keepalived启动,执行脚本:sh /etc/keepalived/check_nginx.sh
至此结束完成了两台nginx漂移功能的实现