环境两台LB(nginx)、两台web(nginx/apache都行)

安装httpd

web01

  1. [root@web01 /]# /etc/init.d/iptables stop
  2. iptables: Setting chains to policy ACCEPT: filter [ OK ]
  3. iptables: Flushing firewall rules: [ OK ]
  4. iptables: Unloading modules: [ OK ]
  5.  
  6. [root@web01 /]# yum -y install httpd
  7. [root@web01 /]# vim /etc/httpd/conf/httpd.conf
  8. Servername 127.0.0.1
  9. [root@web01 /]# echo "web01_192.168.119.130" > /var/www/html/index.html
  10. [root@web01 /]# /etc/init.d/httpd start
  11. Starting httpd: [ OK ]
  12. [root@web01 /]# curl 192.168.119.130
  13. web01_192.168.119.

web02

  1. [root@web02 /]# /etc/init.d/iptables stop
  2. iptables: Setting chains to policy ACCEPT: filter [ OK ]
  3. iptables: Flushing firewall rules: [ OK ]
  4. iptables: Unloading modules: [ OK ]
  5. [root@web02 /]# yum -y install httpd
  6. [root@web02 /]# vim /etc/httpd/conf/httpd.conf
  7. ServerName 127.0.0.1
  8. [root@web02 /]# echo "web02_192.168.119.131" > /var/www/html/index.html
  9. [root@web02 /]# /etc/init.d/httpd start
  10. Starting httpd: [ OK ]
  11. [root@web02 /]# curl 192.168.119.131
  12. web02_192.168.119.

两台LB

lb01和lb02配置相同

环境准备

  1. [root@lb01 /]# yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++

下载软件

  1. [root@lb01 /]# wget http://nginx.org/download/nginx-1.6.3.tar.gz

解压、配置、编译、安装

  1. [root@lb01 conf]# useradd nginx -s /sbin/nologin -M
    [root@lb01 /]# tar zxvf nginx-1.6..tar.gz
  2. [root@lb01 /]# cd nginx-1.6.3
  3.  
  4. [root@lb01 nginx-1.6.3]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_modul
  1. [root@lb01 nginx-1.6.]# make && make install
  2. [root@lb01 nginx-1.6.]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
  3. [root@lb01 nginx-1.6.3]# cd /usr/local/nginx/conf/
  4. [root@lb01 conf]# ll nginx.conf nginx.conf.default
  5. -rw-r--r--. 1 root root 2656 Sep 26 06:33 nginx.conf
  6. -rw-r--r--. 1 root root 2656 Sep 26 06:33 nginx.conf.default
  7. [root@lb01 conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf
  8. [root@lb01 conf]# vim nginx.conf
  9. worker_processes 1;
  10. events {
  11. worker_connections 1024;
  12. }
  13. http {
  14. include mime.types;
  15. default_type application/octet-stream;
  16. sendfile on;
  17. keepalive_timeout 65;
  18. upstream web_pools {
  19. server 192.168.119.130:80 weight=5;
  20. server 192.168.119.131:80 weight=5;
  21.  
  22. }
  23. server {
  24. listen 80;
  25. server_name localhost;
  26. location / {
  27. root html;
  28. index index.html index.htm;
  29. proxy_pass http://web_pools;
  30. include proxy.conf;
  31. }
  32. error_page 500 502 503 504 /50x.html;
  33. location = /50x.html {
  34. root html;
  35. }
  36. }
  37. }
  38.  
  39. [root@lb01 conf]# cat proxy.conf
  40. proxy_redirect off;
  41. proxy_set_header Host $host;
  42. proxy_set_header X-Forwarded-For $remote_addr;
  43. proxy_connect_timeout 90;
  44. proxy_send_timeout 90;
  45. proxy_read_timeout 90;
  46. proxy_buffer_size 4K;
  47. proxy_buffers 3 32K;
  48. proxy_busy_buffers_size 64k;
  49. proxy_temp_file_write_size 64k;

启动nginx

  1. [root@lb01 conf]# nginx -t
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  4. [root@lb01 conf]# nginx
  5. [root@lb01 conf]# netstat -anpt | grep nginx
  6. tcp 0.0.0.0: 0.0.0.0:* LISTEN /nginx
  1. [root@lb01 conf]# cat /etc/hosts
  2. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  3. :: localhost localhost.localdomain localhost6 localhost6.localdomain6
  4. 192.168.119.128 www.test.com

测试

  1. [root@lb01 conf]# curl www.test.com
  2. web02_192.168.119.
  3. [root@lb01 conf]# curl www.test.com
  4. web01_192.168.119.
  5. [root@lb01 conf]# curl www.test.com
  6. web02_192.168.119.
  7. [root@lb01 conf]# curl www.test.com
  8. web01_192.168.119.
  9. [root@lb01 conf]# curl www.test.com
  10. web02_192.168.119.

lb02配置与lb01相同步骤略、直接测试

  1. [root@lb02 conf]# nginx -t
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  4. [root@lb02 conf]# nginx
  5.  
  6. [root@lb02 conf]# echo "192.168.119.129 www.test.com" >> /etc/hosts
  7. [root@lb02 conf]# cat /etc/hosts
  8. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  9. :: localhost localhost.localdomain localhost6 localhost6.localdomain6
  10. 192.168.119.129 www.test.com
  11.  
  12. [root@lb02 conf]# curl www.test.com
  13. web01_192.168.119.
  14. [root@lb02 conf]# curl www.test.com
  15. web02_192.168.119.
  16. [root@lb02 conf]# curl www.test.com
  17. web01_192.168.119.
  18. [root@lb02 conf]# curl www.test.com
  19. web02_192.168.119.
  20. [root@lb02 conf]# curl www.test.com
  21. web01_192.168.119.

两台LB安装都已完成

在两台LB上安装keepalived

环境配置

LB01和LB02配置相同

  1. [root@lb01 /]# yum -y install kernel-devel

做个软连接  用tab键补全2.6.32-642.6.2.el6.x86_64

  1. [root@lb01 /]# ln -s /usr/src/kernels/2.6.-642.4..el6.x86_64/ /usr/src/linux
  2. [root@lb01 /]# ll /usr/src/
  3. total
  4. drwxr-xr-x. root root Sep debug
  5. drwxr-xr-x. root root Sep : kernels
  6. lrwxrwxrwx. root root Sep : linux -> /usr/src/kernels/2.6.-642.4..el6.x86_64/

下载软件

  1. [root@lb01 /]# wget http://www.keepalived.org/software/keepalived-1.2.16.tar.gz
  2. ---- ::-- http://www.keepalived.org/software/keepalived-1.2.16.tar.gz
  3. Resolving www.keepalived.org... 37.59.63.157, :41d0::7a9d::
  4. Connecting to www.keepalived.org|37.59.63.157|:... connected.
  5. HTTP request sent, awaiting response... OK
  6. Length: (339K) [application/x-gzip]
  7. Saving to: keepalived-1.2..tar.gz
  8.  
  9. %[=================================================>] , .96K/s in 2m 30s
  10.  
  11. -- :: (2.27 KB/s) - keepalived-1.2..tar.gz saved [/]
  12.  
  13. [root@lb01 /]# ll keepalived-1.2..tar.gz
  14. -rw-r--r--. root root Mar keepalived-1.2..tar.gz

解压、配置、编译、安装

  1. [root@lb01 /]# tar zxvf keepalived-1.2..tar.gz
  2. [root@lb01 /]# cd keepalived-1.2.
  3. [root@lb01 keepalived-1.2.]# ./configure
  4. Keepalived configuration
  5. ------------------------
  6. Keepalived version : 1.2.
  7. Compiler : gcc
  8. Compiler flags : -g -O2
  9. Extra Lib : -lssl -lcrypto -lcrypt
  10. Use IPVS Framework : Yes
  11. IPVS sync daemon support : Yes
  12. IPVS use libnl : No
  13. fwmark socket support : Yes
  14. Use VRRP Framework : Yes
  15. Use VRRP VMAC : Yes
  16. SNMP support : No
  17. SHA1 support : No
  18. Use Debug flags : No
  19.  
  20. [root@lb01 keepalived-1.2.]# make && make install

配置规范启动

  1. [root@lb01 keepalived-1.2.]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ #生成启动脚本
  2. [root@lb01 keepalived-1.2.]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ #配置启动脚本的参数
  3. [root@lb01 keepalived-1.2.]# mkdir /etc/keepalived #创建默认的keepalived配置文件路径
  4. [root@lb01 keepalived-1.2.]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ #把keepalived.conf模板拷贝到/etckeepalived下
  5. [root@lb01 keepalived-1.2.]# cp /usr/local/sbin/keepalived /usr/sbin/
  6. [root@lb01 keepalived-1.2.]# /etc/init.d/keepalived start
  7. Starting keepalived: [ OK ]
  8. [root@lb01 keepalived-1.2.]# ps -ef | grep keep
  9. root : ? :: keepalived -D
  10. root : ? :: keepalived -D
  11. root : ? :: keepalived -D
  12. root : pts/ :: grep keep
  13. [root@lb01 keepalived-1.2.]# /etc/init.d/keepalived stop
  14. Stopping keepalived: [ OK ]

修改配置文件

先备份一个配置文件

lb01配置文件

  1. [root@lb01 keepalived-1.2.]# cd /etc/keepalived/
  2. [root@lb01 keepalived]# cp keepalived.conf keepalived.conf.bak
  3. [root@lb01 keepalived]# vim keepalived.conf
  4. ! Configuration File for keepalived
  5.  
  6. global_defs {
  7. notification_email {
  8. acassen@firewall.loc
  9. failover@firewall.loc
  10. sysadmin@firewall.loc
  11. }
  12. notification_email_from Alexandre.Cassen@firewall.loc
  13. smtp_server 192.168.200.1
  14. smtp_connect_timeout
  15. router_id LVS_01
  16. }
  17.  
  18. vrrp_instance VI_1 {
  19. state MASTER
  20. interface eth0
  21. virtual_router_id
  22. priority
  23. advert_int
  24. authentication {
  25. auth_type PASS
  26. auth_pass
  27. }
  28. virtual_ipaddress {
  29. 192.168.119.150/
  30. }
  31. }

lb02配置文件

  1. [root@lb02 keepalived-1.2.]# cd /etc/keepalived/
  2. [root@lb02 keepalived]# cp keepalived.conf keepalived.conf.bak
  3. [root@lb02 keepalived]# vim keepalived.conf
  4. ! Configuration File for keepalived
  5.  
  6. global_defs {
  7. notification_email {
  8. acassen@firewall.loc
  9. failover@firewall.loc
  10. sysadmin@firewall.loc
  11. }
  12. notification_email_from Alexandre.Cassen@firewall.loc
  13. smtp_server 192.168.200.1
  14. smtp_connect_timeout
  15. router_id LVS_02
  16. }
  17.  
  18. vrrp_instance VI_1 {
  19. state BACKUP
  20. interface eth0
  21. virtual_router_id
  22. priority
  23. advert_int
  24. authentication {
  25. auth_type PASS
  26. auth_pass
  27. }
  28. virtual_ipaddress {
  29. 192.168.119.150/
  30. }
  31. }

启动keepalived

  1. [root@lb01 keepalived]# /etc/init.d/keepalived start
  2. Starting keepalived: [ OK ]
  3. [root@lb02 keepalived]# /etc/init.d/keepalived start
  4. Starting keepalived: [ OK ]

在master(lb01)查看虚拟IP(192.168.119.150)

提示:使用ifconfig是查不到的;使用 ip add查询

  1. [root@lb01 keepalived]# ip add | grep 192.168.119.150
  2. inet 192.168.119.150/ scope global secondary eth0
  3. [root@lb01 keepalived]#

在backup(lb02)查看(没有虚拟IP)

  1. [root@lb02 keepalived]# ip add | grep 192.168.119.150

把master的keepalived服务down、虚拟IP就会自动切换到backup

  1. [root@lb01 keepalived]# /etc/init.d/keepalived stop
  2. Stopping keepalived: [ OK ]
  3. [root@lb01 keepalived]# ip add | grep 192.168.119.150
  4. [root@lb01 keepalived]#

backup查询

  1. [root@lb02 keepalived]# ip add | grep 192.168.119.150
  2. [root@lb02 keepalived]# ip add | grep 192.168.119.150
  3. [root@lb02 keepalived]# ip add | grep 192.168.119.150
  4. [root@lb02 keepalived]# ip add | grep 192.168.119.150
  5. [root@lb02 keepalived]# ip add | grep 192.168.119.150
  6. inet 192.168.119.150/ scope global secondary eth0
  7. [root@lb02 keepalived]# ip add | grep 192.168.119.150
  8. inet 192.168.119.150/ scope global secondary eth0

当master的keepalived服务启动时、虚拟IP会自动从backup切回到master

测试web是否正常

  1. [root@lb01 keepalived]# curl 192.168.119.150
  2. web01_192.168.119.
  3. [root@lb01 keepalived]# curl 192.168.119.150
  4. web02_192.168.119.
  5. [root@lb01 keepalived]# curl 192.168.119.150
  6. web01_192.168.119.
  7. [root@lb01 keepalived]# curl 192.168.119.150
  8. web02_192.168.119.
  9. [root@lb01 keepalived]# curl 192.168.119.150
  10. web01_192.168.119.
  11. [root@lb01 keepalived]# curl 192.168.119.150
  12. web02_192.168.119.
  13. [root@lb01 keepalived]# curl 192.168.119.150
  14. web01_192.168.119.

Nginx反向代理+keepalived的更多相关文章

  1. Nginx反向代理,负载均衡,redis session共享,keepalived高可用

    相关知识自行搜索,直接上干货... 使用的资源: nginx主服务器一台,nginx备服务器一台,使用keepalived进行宕机切换. tomcat服务器两台,由nginx进行反向代理和负载均衡,此 ...

  2. nginx反向代理docker registry报”blob upload unknown"解决办法

    问题症状:keepalived+nginx反向代理后端docker registry群集时,使用docker客户机向registry push镜像时出现 "blob upload unkno ...

  3. Linux系统——Nginx反向代理与负载均衡

    集群集群是指一组(若干个)相互独立的计算机,利用高速通信网路组成的一个较大的计算机服务系统,每个集群节点(即集群中的每台计算机)都是运用各自服务的独立服务器.这些服务器之间可以彼此通信,协同向用户提供 ...

  4. LVS和nginx反向代理网站架构

    LVS和nginx反向代理网站架构 nginx反向代理和lvs的dr都存在单点,要keepalived做高可用,但是成本高了 f

  5. Linux实战教学笔记30:Nginx反向代理与负载均衡应用实践

    1.1 集群简介 简单地说,集群就是指一组(若干个)相互独立的计算机,利用高速通信网络组成的一个较大的计算机服务系统,每个集群节点(即集群中的每台计算机)都是运行各自服务的独立服务器.这些服务器之间可 ...

  6. nginx 反向代理实现负载均衡*理论

    Nginx负载均衡集群介绍 负载均衡集群提供了一种廉价,有效,透明的方法,来扩展网络设备和服务器的负载,带宽和吞吐量,同时加强了网络数据处理能力,提高了网络的灵活性和可用性. 搭建负载均衡服务的需求: ...

  7. Nginx反向代理与负载均衡应用实践(一)

    Nginx反向代理与负载均衡应用实践(一) 链接:https://pan.baidu.com/s/1xB20bnuanh0Avs4kwRpSXQ 提取码:migq 复制这段内容后打开百度网盘手机App ...

  8. 使用python自动生成docker nginx反向代理配置

    由于在测试环境上用docker部署了多个应用,而且他们的端口有的相同,有的又不相同,数量也比较多,在使用jenkins发版本的时候,不好配置,于是想要写一个脚本,能在docker 容器创建.停止的时候 ...

  9. Nginx反向代理部署指南

    一.反向代理 我们都知道,80端口是web服务的默认端口,其他主机访问web服务器也是默认和80端口进行web交互,而一台服务器也只有一个80端口,这是约定俗成的标准. 我们来看下面两个场景: 1.服 ...

随机推荐

  1. 3.开发Java消息驱动bean实例代码

    java消息服务(JMS)是用于访问企业消息系统的开发商中立的API.企业消息系统可以协助应用软件通过网络进行消息交互.应用程序A发送一条消息到消息服务器的某个目的地(Destination),然后消 ...

  2. EasyUI实现工地领款单项目

    环境搭建 引入jar.配置Spring MVC.web.xml.日志等 建表 自动生成MyBatis相关文件 mybatis-generator-core-1.3.2.jar <?xml ver ...

  3. java多线程系类:JUC线程池:04之线程池原理(三)(转)

    转载请注明出处:http://www.cnblogs.com/skywang12345/p/3509960.html 本章介绍线程池的生命周期.在"Java多线程系列--"基础篇& ...

  4. Spring源码分析——BeanFactory体系之抽象类、类分析(一)

    上一篇介绍了BeanFactory体系的所有接口——Spring源码分析——BeanFactory体系之接口详细分析,本篇就接着介绍BeanFactory体系的抽象类和接口. 一.BeanFactor ...

  5. jetty 9 嵌入式开发示例

    jetty 9 嵌入应用程序后,小型的web应用直接打成一个单独的jar包,就可以直接运行,非常适合做Demo演示或云端集群部署. 主要代码: JettyServer的封装类 package yjmy ...

  6. 微信小程序开发工具使用与设计规范(二)

    [未经作者本人同意,请勿以任何形式转载] 上一篇文章主要分析了微信小程序应用场景和优劣势.本篇你可以学习到: 如何使用小程序开发工具写一个Hello World 微信小程序设计规范 微信小程序项目结构 ...

  7. Dump中查看DataTime时间方法

    例如: 步骤一:根据DumpVC命令获取时间对象信息.需要MT 和 Value参数. 步骤二:根据得到上上面值,执行:? & 0x3FFFFFFFFFFFFFFF.注意:0n 签名是零,不是英 ...

  8. 杂谈 Taglib、EL、OGNL

    本篇主要是自己对 JavaWeb中 EL.OGNL.Taglib 个人理解. 因为以上内容都是在 JSP 的基础下产生的概念, 首先得说说我眼中的 JSP. 她自由的.奔放的,就像辽阔的大海,跨越任何 ...

  9. 【跟着子迟品 underscore】Array Functions 相关源码拾遗 & 小结

    Why underscore 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. 阅读一些著名框架类库的源码,就好像和一个个大师对 ...

  10. JS组件系列——Bootstrap Select2组件使用小结

    前言:在介绍select组件的时候,博主之前分享过一篇JS组件系列——两种bootstrap multiselect组件大比拼,这两个组件的功能确实很强大,只可惜没有图文结合的效果(也就是将图片放入到 ...