公网Nginx+keepalived高可用
一、 方案规划
Ip 192.168.1.128 192.168.1.138 192.168.1.136 Vip 主机名 bejmuse17 bejmuse23 默认主从 MASTER BACKUP
二、 准备材料
keepalived-1.2.18.tar.gz nginx-1.10.2.tar.gz
nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip
三、 安装 1、
依赖包的安装:
编译nginx所需的依赖包(用root用户,生产环境有yum源)
#yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
(生产环境,执行语句提示安装包已经安装)
2、 上传安装文件
将keepalived-1.2.18.tar.gz、nginx-1.10.2.tar.gz nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip 上传到
/home/user/yy/
3、 Nginx编译安装
(1) 解压并安装Nginx的sticky插件 cd /home/user/yy/
unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip (2)解压并安装编译Nginx
#cd /home/user/yy/
#tar -zxvf nginx-1.10.2.tar.gz #cd nginx-1.10.2
#./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-
http_gzip_static_module --add-module=/home/user/yy/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ --with-stream #make && make install
(3)系统防火墙进行关闭
#service iptables stop
(4)检测是否安装成功
/usr/local/nginx/sbin/nginx -t 安装成功会显示 success
(5)启动Nginx
启动命令:
# /usr/local/nginx/sbin/nginx 重启命令:
# /usr/local/nginx/sbin/nginx -s reload
(6)设置Nginx开机启动
# vi /etc/rc.local 加入:
/usr/local/nginx/sbin/nginx
4、 Keepalived编译安装
#cd /home/user/yy/
#tar -zxvf keepalived-1.2.18.tar.gz #cd keepalived-1.2.18
#./configure --prefix=/usr/local/keepalived #make && make install
5、
将keepalived设置成系统服务(因为没有使用keepalived的默认路径安装(默认是/usr/local),安装完成之后,需要做一些工作)
#mkdir /etc/keepalived
#cp /usr/local/keepalived/etc/keepalived/keepalived.conf #/etc/keepalived/
#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ #cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ #ln -s /usr/local/sbin/keepalived /usr/sbin/ #ln -s /usr/local/keepalived/sbin/keepalived /sbin/ #chkconfig keepalived on
6、
高可用的配置(所有配置文件在修改前请自行备份)
备份:
#cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak 编辑:
#vi /etc/keepalived/keepalived.conf
(1)master
! Configuration File for keepalived
global_defs {
router_id bejmuse17}
vrrp_script chk_nginx {
script \ interval 2 weight -20 }
vrrp_instance VI_138 { state MASTER interface eth0
virtual_router_id 138 mcast_src_ip 192.168.1.128 priority 100 nopreempt advert_int 1
authentication { auth_type PASS auth_pass 1111
}