Keepalived 是一种高性能的服务器高可用或热备解决方案, Keepalived 可以用来防止服务器单点故障的发生,通过配合 Nginx 可以实现 web 前端服务的高可用。 VIP IP 主机名 Nginx端口 默认主从 192.168.2.10 192.168.2.5 wangdun1 88 MASTER 192.168.2.10 192.168.2.6 Wangdun2 88 BACKUP CentOS 6.5 x64 [root@wangdun1 ~]# yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel [root@wangdun1 ~]# cd /usr/local/src/ [root@wangdun1 src]# wget http://nginx.org/download/nginx-1.13.0.tar.gz [root@wangdun1]# tar -zxvf nginx-1.13.0.tar.gz [root@wangdun1]# cd nginx-1.13.0 [root@wangdun1]#./configure --prefix=/usr/local/nginx [root@wangdun1]# make && make install [root@wangdun1 ~]# vim /usr/local/nginx/conf/nginx.conf 修改 两个节点Nginx 首页内容: [root@wangdun1 ~]# vim /usr/local/nginx/html/index.html [root@wangdun1 ~]# /usr/local/nginx/sbin/nginx 重启 Nginx [root@wangdun1 ~]# /usr/local/nginx/sbin/nginx -s reload 8.设置 Nginx 开机启动 9.分别访问两个 Nginx 上传keepalived-1.2.18.tar.gz 到 /usr/local/src 目录 [root@wangdun1 ~]# cd /usr/local/src [root@wangdun1 ~]# tar -zxvf keepalived-1.2.18.tar.gz [root@wangdun1 ~]# cd keepalived-1.2.18 [root@wangdun1 ~]#./configure --prefix=/usr/local/keepalived [root@wangdun1 ~]# make && make install 因为没有使用 keepalived 的默认路径安装(默认是/usr/local) ,安装完成之后,需要做一些工作复制默认配置文件到默认路径 [root@wangdun1 ~]# mkdir /etc/keepalived [root@wangdun1 ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ [root@wangdun1 ~]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ [root@wangdun1 ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ [root@wangdun1 ~]# ln -s /usr/local/sbin/keepalived /usr/sbin/ [root@wangdun1 ~]# ln -s /usr/local/keepalived/sbin/keepalived /sbin/ 设置 keepalived 服务开机启动 [root@wangdun1 ~]# chkconfig keepalived on (1) MASTER 节点配置文件(192.168.2.5) (2)BACKUP 节点配置文件(192.168.2.6) 编写 Nginx 状态检测脚本 /etc/keepalived/nginx_check.sh脚本要求:如果 nginx 停止运行,尝试启动,如果无法启动则杀死本机的 keepalived 进程, keepalied将虚拟 ip 绑定到 BACKUP 机器上。 内容如下: 保存后,给脚本赋执行权限: [root@wangdun1 ~]# chmod +x /etc/keepalived/nginx_check.sh [root@wangdun1 ~]# service keepalived start 同时启动192.168.2.5和192.168.2.6上的Nginx和Keepalived,我们通过VIP(192.168.2.10)来访问Nginx,如下 [root@wangdun1 ~]# service keepalived stop [root@wangdun1 ~]# /usr/local/nginx/sbin/nginx -s stop 此时,再通过VIP(192.168.2.10)来访问Nginx,如下 我们再开启192.168.2.5上的Keepalived和Nginx [root@wangdun1 ~]# service keepalived start 因为我们写了脚本nginx_check.sh,这个脚本会为我们自动启动Nginx。 此时,我们再通过VIP(192.168.2.10)来访问Nginx,如下 至此,Keepalived + Nginx 实现高可用 Web 负载均衡搭建完毕。
keepalived-1.2.18.tar.gz
nginx-1.13.0.tar.g
我们关闭192.168.2.5上的Keepalived和Nginx