Keepalived搭建nginx负载均衡高可用前段设计
    nginx+keepalived环境:
master:192.168.0.61(vip:192.168.0.70)
backup:192.168.0.62(vip:192.168.0.70)
realserver192.168.0.63
realserver192.168.0.64(实验规定为四台realserver,自己只模拟了两台)
所有安装包全为老师上课时所用
    此架构需考虑的问题
1Master没挂,则Master占有vipnginx运行在Master
2Master挂了,则backup抢占vip且在backup上运行nginx服务
3)如果master服务器上的nginx服务挂了,则vip资源转移到backup服务器上
MasterBackup两边都开启nginx服务,无论Master还是Backup,当其中的一个keepalived服务停止后,vip都会漂移到keepalived服务还在的节点上,
如果要想使nginx服务挂了,vip也漂移到另一个节点,则必须用脚本或者在配置文件里面用shell命令来控制。
一、源码编译nginx
1.编译安装前准备
[root@badboy61 peijie]#yum install gcc gcc-c++ automake autoconf zlib zlib-devel openssl openssl-devel pcre pcre-devel ipvsadm libnl* popt* -y
其中包括了keepalived需要的ipvsadmlibnl-devel等,在安装keepalived时不再安装
2.获取包并解压
[root@badboy61 peijie]#tar zxvf nginx-1.2.
[root@badboy61 peijie]#cd /nginx-1.2.8/
3.编译
[root@badboy61 nginx-1.2.8]#./configure --prefix=/server/nginx-1.2.8 --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module
....
Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + md5: using system crypto library
  + sha1: using system crypto library
  + using system zlib library
  nginx path prefix: "/server/nginx-1.2.8"
  nginx binary file: "/server/nginx-1.2.8/sbin/nginx"
nginx部署前端项目  nginx configuration prefix: "/server/nginx-1.2.8/conf"
  nginx configuration file: "/server/nginx-1.2.8/f"
  nginx pid file: "/server/nginx-1.2.8/logs/nginx.pid"
  nginx error log file: "/server/nginx-1.2.8/logs/error.log"
  nginx http access log file: "/server/nginx-1.2.8/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
    显示上面文字表示编译成功
4.安装
[root@badboy61 nginx-1.2.8]#echo $?
0
[root@badboy61 nginx-1.2.8]#make -j 4 && make install
5.检测配置文件并启动
[root@badboy61 nginx-1.2.8]# /server/nginx-1.2.8/sbin/nginx  -t
nginx: the configuration file /server/nginx-1.2.8/f syntax is ok         
nginx: configuration file /server/nginx-1.2.8/f test is successful
显示上面文字表示配置文件没有问题
[root@badboy61 nginx-1.2.8]#/server/nginx-1.2.8/sbin/nginx
6.设置开机启动
[root@badboy61 nginx-1.2.8]#echo /server/nginx-1.2.8/sbin/nginx >> /etc/rc.local
2、配置nginx
[root@badboy61 nginx-1.2.8]#useradd -u 8000 -s /sbin/nologin nginx
#添加nginx用户
[root@badboy61 nginx-1.2.8]#vim /server/nginx-1.2.8/f
user nginx nginx ;                      #nginx 用户
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include      pes;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream web {                    #定义web
        server 192.168.0.63;
        server 192.168.0.64;        #实验要四台。现在只模拟了两台
        }
    server {
        listen      80;
        server_name  localhost;
        location / {
            proxy_pass web; #调用web
        }
        error_page  500 502 503 504  /50x.html;
        location = /50x.html {
            root  html;
        }
    }
}
三、编译安装keepalived(这部分主从操作相同)
1.安装ipvsadm
安装keepalived之前有几个依赖包需要装一下
安装 openssl    yum install openssl*
安装popt        yum install popt*
安装ipvsadm    yum isntall ipvsadm
安装libnl-dev  yum install libnl-dev*
不装这些包在编译后可能会出错,因为在安装nginx的时候已经安装,此处不再安装
2、解压并编译安装keepalived
[root@badboy61 peijie]#tar zxvf keepalived-1.2.
[root@badboy61 peijie]#cd ./keepalived-1.2.16/
[root@badboy61keepalived-1.2.16]#./configure                                --prefix=/usr/local/keepalived
------------------------
Keepalived version          : 1.2.16
Compiler                : gcc
Compiler flags            : -g -O2
Extra Lib               : -Wl,-z,relro -Wl,-z,now -L/usr/lib64 -lnetsnmpagent -lnetsnmphelpers -lnetsnmpmibs -lnetsnmp -Wl,-E -Wl,-rpath,/usr/lib64/perl5/CORE -lssl -lcrypto -lcrypt  -lnl
Use IPVS Framework          : Yes
IPVS sync daemon support          : Yes
IPVS use libnl          : Yes
Use VRRP Framework          : Yes
Use VRRP VMAC            : Yes    #这几项必须为yes
SNMP support            : Yes
SHA1 support            : No
Use Debug flags          : No

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。