Nginx+keepalived高可用配置
一、准备和部署说明
l 环境准备
nginx master服务器192.168.10.62
nginx back 服务器192.168.10.63
keepalived Master服务器192.168.10.62
Keepalived slave 服务器192.168.10.63
VIP 虚拟地址 192.168.10.66
l 集群架构图
二、安装nginx
2.1、下载安装包
http://nginx.org/en/download.html
下载稳定版nginx-1.21.3.tar.gz 到/usr/local/src下解压
2.2、安装插件
yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
为了后续准备我们另外下载2个插件模块:
nginx_upstream_check_module-0.3.0.tar.gz
地址:https://github.com/yaoweibin/nginx_upstream_check_module/releases
nginx-goodies-nginx-sticky-module-ng-bd312d586752.tar.gz
地址:https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/downloads
(建议在/usr/local/src下解压后将目录重命名为nginx-sticky-module-ng-1.2.5) —— 后端做负载均衡解决session sticky问题(与upstream_check模块结合使用需要另外打补丁,请参考nginx负载均衡配置实战
地址:http://seanlook.com/2015/06/02/nginx-cache-check/
)。
2.3、编译安装
2.3、nginx配置
2.4、验证
访问地址:
http://192.168.10.62/group1/M00/00/00/wKgKQGFVkXyAD5GeAAAADo5Qat81150824
2.5、systemctl启动方式添加nginx.service服务
三、安装keepalived
3.1、卸载原有的安装
卸载程序
cd /usr/local/src/keepalived
make uninstall #执行卸载
删除相关文件
l rm -f /usr/local/sbin/keepalived
l rm -rf /usr/local/keepalived
l rm -rf /etc/keepalived
l rm -f /etc/rc.d/init.d/keepalived
l rm -rf /usr/sbin/keepalived
l rm -f /etc/sysconfig/keepalived
3.2、安装配置keepalived
https://blog.csdn.net/UserFrank/article/details/112770989
https://www.cnblogs.com/xxoome/p/8621677.html
3.3、确认使用的网卡
ip a
3.4、使用wget命令下载,下载位置/usr/local/src
keepalived下载地址:http://www.keepalived.org/download.html
3.5、安装依赖插件:
3.6、编译安装:
3.7、运行前配置
3.8、Master修改配置文件:vi /etc/keepalived/keepalived.conf
3.9、Slave修改配置文件:vi /etc/keepalived/keepalived.conf
3.10、nginx监听脚本:
脚本添加如下内容:
防止出现脑裂现象(主备同时获取了VIP地址),也可以直接关闭防火墙
3.11、启动服务并且验证:
查看服务启动情况:
查看启动日志:
查看keepalived日志
配置成功后的效果。ens33是网卡名字;192.168.10.66是虚拟ip,已经成功绑定到网卡上。
四、测试
4.1:启动192.160.10.62 和192.160.10.63 nginx 服务
systemctl start nginx
结果:显示 192.168.10.62的nginx服务
4.2、停止 192.160.10.62 nginx
Systemctl stop nginx
结果服务跳到了 192.168.10.63
参考资料
l keepalived官网:https://www.keepalived.org/doc/installing_keepalived.html
l Nginx 官网:http://nginx.org/en/download.html
l https://www.cnblogs.com/bluestorm/p/4574688.html