一.Linux安装nginx
1.安装依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
2.创建一个文件夹
cd /usr/local mkdir nginx cd nginx
3.将nginx安装包放在nginx文件夹下并解压
或者使用wget联网下载
wget http://nginx.org/download/nginx-1.16.1.tar.gz tar -xvf nginx-1.16.1.tar.gz
4.编译并make
cd nginx-1.16.1 ./configure --prefix=/usr/local/naginx make make install
常用命令
#启动 /usr/local/nginx/sbin/nginx #修改配置文件重新加载 /usr/local/nginx/sbin/nginx -s reload #关闭 /usr/local/nginx/sbin/nginx
二:安装Keepalived
1.联网下载到/usr/local目录下并解压
有可能会提示连接不成功,加上它提示的命令再下载就好了
cd /usr/local wget http://www.keepalived.org/software/keepalived-1.4.2.tar.gz
tar zxvf keepalived-1.4.2.tar.gz
2.安装相关依赖,有不用下载了
yum install -y gcc openssl-devel popt-devel
3.编译
cd keepalived-1.4.2 ./configure --prefix=/usr/local/keepalived make make install
4.相关配置,复制就完了
cp /usr/local/keepalived-1.4.2/keepalived/etc/init.d/keepalived /etc/init.d/ mkdir /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/keepalived-1.4.2/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
使用keepalived保证nginx的高可用(一主一从)
我的主服务器ip为(192.168.118.3),从机(192.168.118.5)
在两条服务器分别安装nginx和keepalived,
修改keeplived.conf文件
vim /etc/keepalived/keepalived.conf
主机配置文件
! Configuration File for keepalived global_defs { #一个没重复的名字即可 router_id LVS_DEVEL } # 检测nginx是否运行 vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2
#权重减少20,减少完要比从机的值小 weight -20 } vrrp_instance VI_1 { # MASTER代表主机 state MASTER # 网卡名字 ip addr 可以查看 interface eno16777736 # 同一个keepalived集群的virtual_router_id相同 virtual_router_id 51 # 权重,master要大于slave priority 100 # 主备通讯时间间隔 advert_int 1 # 如果两节点的上联交换机禁用了组播,则采用vrrp单播通告的方式 # 本机ip unicast_src_ip 192.168.118.3 unicast_peer { # 其他机器ip 192.168.118.5 } # 设置nopreempt防止抢占资源 nopreempt # 主备保持一致 authentication { auth_type PASS auth_pass 1111 } # 与上方nginx运行状况检测呼应 track_script { chk_nginx } virtual_ipaddress { # 虚拟ip地址(VIP,一个尚未占用的内网ip即可) 192.168.118.118 } }
从机配置文! Configuration File for keepalived
global_defs { #一个没重复的名字即可 router_id LVS_DEVEL } # 检测nginx是否运行 vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2
weight -20 } vrrp_instance VI_1 { # 代表是从机 state BACKUP # 网卡名字,ip addr获取 interface eno16777736 # 同一个keepalived集群的virtual_router_id相同 virtual_router_id 51 # 权重,master要大于slave priority 90 # 主备通讯时间间隔 advert_int 1 # 如果两节点的上联交换机禁用了组播,则采用vrrp单播通告的方式 # 本机ip unicast_src_ip 192.168.118.5 unicast_peer { # 其他机器ip 192.168.118.3 } # 设置nopreempt防止抢占资源 nopreempt # 主备保持一致 authentication { auth_type PASS auth_pass 1111 } # 与上方nginx运行状况检测呼应 track_script { chk_nginx } virtual_ipaddress { # 虚拟ip地址(VIP,一个尚未占用的内网ip即可) 192.168.118.118 } }
主从机都需配置检测nginx是否在运行,不在允许就直接启动nginx的脚本,和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
只要配置好了,以后直接启动keepalived就好了,keepalived运行之后就会检测nginx是否在运行,不在运行就通过脚本去启动
自动重启不了解决方案
查看脚本是否有运行的权限
如果你是root登陆的话(不是的话,切换到root用户,对*.sh 赋可执行的权限)
chmod 777*.sh 或者 chmod +x *.sh
keepalived常用命令
#启动 service keepalived start #停止 service keepalived stop #查看状态 service keepalived status
开始测试
直接访问主机的nginx服务
直接访问从机的nginx服务
通过keepalived 设置的虚拟ip进行访问
一切正常,将主机的keepalived服务给停掉,模拟服务器宕机的场景
然后再通过虚拟IP进行访问
成功切换到备用服务器
接下来重启主服务器,再访问虚拟ip
由此可见,当主机dowm掉的时候才会访问从机,当主机连进来的时候又回去访问主机
当主机正常运行通过ip addr命令的时候可以看到网络下多了个虚拟ip,而从机却没有,只要当主机挂了时候从机才会显示xuniip