双机热备之nginx和keepalived
作者:互联网
一:安装前配置:
主机:192.168.220.133
辅机:192.168.220.134
虚拟ip:192.168.220.136
(注:192.168.220.133 对应的是mengyan133主机名,192.168.220.134对应的是mengyan134主机名.这个ip和主机名做了映射,两台服务器做了免密登录操作)
1.1首先安装yum -y install gcc pcre-devel zlib-devel openssl-devel
下载安装包keepalived-1.3.4.tar.gz和nginx-1.9.9.tar.gz
1.2安装nginx
执行tar zxvf nginx-1.9.9.tar.gz -C /usr/local/src/
进入nginx目录:cd /usr/local/src/nginx-1.9.9
添加www用户,其中-M参数表示不添加用户家目录,-s参数表示指定shell类型
useradd www -M -s /sbin/nologin
vi auto/cc/gcc
#将这句注释掉 取消Debug编译模式 大概在179行
#CFLAGS="$CFLAGS -g"
执行configure:
./configure --prefix=/usr/local/nginx
然后make:
make && make install
将nginx服务加入开机启动服务
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
启动nginx :
在/usr/local/nginx/sbin下执行./nginx
停止:./nginx -s stop
查看nginx的进程:ps -ef|grep nginx
将nginx写入环境变量,这样在哪个目录下都可以执行nginx命令:
vi /etc/profile 在文件最下面添加如下:
#NGINX_HOME
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin
保存退出 ,执行source /etc/profile使文件生效
执行 nginx就是启动,执行nginx -s stop就是关闭
nginx -s reload重启
1.3安装keepalived
先删除keepalived
rm -f /usr/local/sbin/keepalived rm -f /usr/local/etc/rc.d/init.d/keepalived
rm -f /usr/local/etc/sysconfig/keepalived rm -rf /usr/local/etc/keepalived
rm -f /usr/local/bin/genhash rm -rf /usr/local/keepalived rm -rf /etc/keepalived
rm -f /etc/rc.d/init.d/keepalived rm -f /usr/sbin/keepalived
rm -f /etc/sysconfig/keepalived rm -f /etc/systemd/system/multi-user.target.wants/keepalived.service
执行tar -zvxf keepalived-1.3.4.tar.gz -C /usr/local/src/
进入/usr/local/src/keepalived-1.3.4/
执行./configure
然后:make && make install
将下面的加入配置
cp /usr/local/src/keepalived-1.3.4/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
配置文件:
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
将keepalive服务加入开机启动服务:
chkconfig keepalived on
启动keepalived:
在安装目录下:service keepalived start
状态:service keepalived status
nginx中配置tomcat:
进入到/usr/local/nginx/conf目录下
vi nginx.conf
在server上面在加一个:
upstream tomcatservers {
server mengyan133:8080 weight=10;
server mengyan134:8080 weight=10;
}
然后在server下的location中把欢迎页去掉,加上:
location / {
# root html;
proxy_pass http://tomcatservers;
index index.html index.htm;
}
重启nginx:nginx -s reload
在页面中查看:
二:将keepalived主副机关联
2.1找到keepalived.conf: vi /etc/keepalived/keepalived.conf
主机:
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.220.136
}
介绍:主节点为MASTER,备份节点为BACKUP
virtual_router_id 51## 虚拟路由ID号
priority 100 ##优先级配置(0-254的值)virtual_ipaddress:虚拟ip(vip),可以指定多个
辅机:
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.220.136
}
}
两台keepalived重启,主机输入 ip add show ens33 (注:这里的ens33是网卡,不知道网卡是多少的可以敲击:ip a 查看):
在去查看辅机的:ip add show ens33 :
可以看到136现在在主机上。停止主机的keepalived:service keepalived stop
再去查看主机的:
查看辅机的:
自动切换到辅机上。在重启主机的keepalived:service keepalived restart
可以看到主机占的权重大,每次主机启动虚拟ip就会跑到主机上
三:将keepalived和nginx自身关联起来并和主副机的keepalived和nginx关联
3.1keepalived只是保证两台服务器之间通信的,不是来看keepalived挂不挂,只是来看nginx挂没挂。nginx挂了但是keepalived没挂虚拟ip不会飘到另一台服务器。所以需要keepalived和nginx相关联(要挂一起挂,虚拟ip才能飘到另一台服务器上):
在/etc/keepalived下创建:nginx_check.sh, vi nginx_check.sh:
![](https://www.icode9.com/i/l/?n=20&i=blog/1396153/202108/1396153-20210813164546912-1495510839.jpg)
![](https://www.icode9.com/i/l/?n=20&i=blog/1396153/202108/1396153-20210813164637616-1678476049.jpg)
![](https://www.icode9.com/i/l/?n=20&i=blog/1396153/202108/1396153-20210813164751294-1417674102.jpg)
![](https://www.icode9.com/i/l/?n=20&i=blog/1396153/202108/1396153-20210813164850126-1974490999.jpg)
![](https://www.icode9.com/i/l/?n=20&i=blog/1396153/202108/1396153-20210813164930887-1979852105.jpg)
![](https://www.icode9.com/i/l/?n=20&i=blog/1396153/202108/1396153-20210813165001463-645087562.jpg)
![](https://www.icode9.com/i/l/?n=20&i=blog/1396153/202108/1396153-20210813165052221-2118066305.jpg)
#!/bin/bash d=`date --date today +%Y%m%d_%H:%M:%S` n=`ps -C nginx --no-heading|wc -l` if [ $n -eq 0 ]; then echo "`date "+%Y-%m-%d--%H:%M:%S"` nginx stop" >> /var/log/check_ng.log /usr/local/nginx/sbin/nginx n2=`ps -C nginx --no-heading|wc -l` if [ $n2 -eq "0" ]; then echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log service keepalived stop fi fi权限: chmod 777 nginx_check.sh (注:除非遇到特殊情况,不然人为的停止nginx,脚本立刻就能再次启动nginx,从而保证服务器正常运行下去。虚拟ip到底怎么飘到另一台服务器上上面已经做了演示。如果想看关闭nginx直接关闭keepalived从而虚拟ip直接飘到另一台服务器的用这个脚本:)
#!/bin/bash if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then service keepalived stop fi然后在keepalived.conf中配置: vrrp_script check_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -20 } track_script { check_nginx } 这里需要主机从起nginx然后重启keepalived 介绍: vrrp_script check_nginx放到vrrp_instance VI_1上面 script "/etc/keepalived/nginx_check.sh" ##执行脚本位置 interval 2 ##检测时间间隔 weight -20 ## 如果条件成立则权重减20(-20) check_nginx 监控脚本放到virtual_ipaddress上面就行 重启keepalived: service keepalived restart 记住:如果用虚拟ip无法访问并且无法ping通的话,原因是keepalived.conf配置中默认vrrp_strict打开了,需要把它注释掉。重启keepalived即可ping通。详情请见: https://blog.csdn.net/charthyf/article/details/81456872
标签:热备,etc,local,keepalived,nginx,usr,ip,双机 来源: https://www.cnblogs.com/gxbmjz0721/p/15138151.html