实战安装 nginx+keepalvied 实现负载均衡和高可用

1. 两台机器都需要安装nginx和keepalivd

环境配置

[[email protected] ~]# cat /etc/redhat-release

CentOS release 6.8 (Final)

[[email protected] ~]# uname -r

2.6.32-642.6.1.el6.x86_64

软件

nginx-1.6.2.tar.gz keepalived-1.1.19.tar.gz

信息列表

          服务器名称           IP
  nginxproxy1    做主(master)   10.89.3.102
   nginxproxy2    做备(backup)    10.89.3.103
   lamp           做web1(real_server1)    10.89.3.101
   lnmp           做web2(real_server2)    10.89.3.100
   NfsServer      验证服务器     10.89.3.99

#vip  设置在keepalived的配置文件里 10.89.3.168

1.1安装nginx

安装pcre

yum install pcre pcre-devel -y

#安装openssl

yum install openssl openssl-devel -y

#新建nginx用户

useradd nginx -s /sbin/nologin -M

#安装

tar -zxvf nginx-1.6.2.tar.gz

cd nginx-1.6.2

./configure \

--user=nginx \

--group=nginx \

--prefix=/application/nginx1.6.2 \

--with-http_stub_status_module \

--with-http_ssl_module

make && make install

#创建软连接

cd ../

ln -s /application/nginx1.6.2/ /application/nginx

#检查语法

/application/nginx/sbin/nginx -t

nginx: the configuration file /application/nginx1.6.2/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx1.6.2/conf/nginx.conf test is successful

# 启动nginx

[[email protected] tools]# /application/nginx/sbin/nginx

#查看端口,看是否启动了

[[email protected] tools]# netstat -lntup |grep nginx

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      8718/nginx

[[email protected] tools]# lsof -i :80

COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

nginx   8718  root    6u  IPv4  18680      0t0  TCP *:http (LISTEN)

nginx   8719 nginx    6u  IPv4  18680      0t0  TCP *:http (LISTEN)

1.2 安装keepalived

cd /home/alvin/tools/

ln -s /usr/src/kernels/2.6.32-642.6.1.el6.x86_64 /usr/src/linux

yum install openssl openssl-devel -y

#上传keepalived-1.1.19.tar.gz

tar xf keepalived-1.1.19.tar.gz

cd keepalived-1.1.19

./configure

-----------------------

有3个Yes就表示configure OK

config.status: creating keepalived/check/Makefile

config.status: creating keepalived/libipvs-2.6/Makefile

Keepalived configuration

------------------------

Keepalived version       : 1.1.19

Compiler                 : gcc

Compiler flags           : -g -O2

Extra Lib                : -lpopt -lssl -lcrypto

Use IPVS Framework       : Yes

IPVS sync daemon support : Yes

Use VRRP Framework       : Yes

Use Debug flags          : No

[[email protected] keepalived-1.1.19]#

-----------------------

make

make install

#配置规范启动

/bin/cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/

/bin/cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

mkdir /etc/keepalived -p

/bin/cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

/bin/cp /usr/local/sbin/keepalived /usr/sbin/

/etc/init.d/keepalived start

ps -ef|grep keep

root       3483   1365  0 14:56 pts/0    00:00:00 grep keep

--------------------------------

#有3个keepalived -D表示成功

[[email protected] keepalived-1.1.19]# /etc/init.d/keepalived start

Starting keepalived:                                       [  OK  ]

[[email protected] keepalived-1.1.19]# ps -ef|grep keep

root       6342      1  0 17:42 ?        00:00:00 keepalived -D

root       6344   6342  0 17:42 ?        00:00:00 keepalived -D

root       6345   6342  0 17:42 ?        00:00:00 keepalived -D

root       6347   3490  0 17:42 pts/0    00:00:00 grep keep

--------------------------------------------------------------

如果报:configure:error:Popt librarics is required  则:

yum install popt* -y

-------------------------------------------

#打开内核转发

vi /etc/sysctl.conf

# Controls IP packet forwarding

net.ipv4.ip_forward = 1

sysctl -p

---------------------------------

1.3 nginxproxy1 keepalived 配置文件设置

cd /etc/keepalived/

cat keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

[email protected]

}

notification_email_from [email protected]

smtp_server 10.0.0.1

smtp_connect_timeout 30

router_id keepalvied_1

}

vrrp_instance VI_1 {

state MASTER

interface eth0

virtual_router_id 55

priority 150

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

10.89.3.168/24

}

}

virtual_server 10.89.3.168 80 {

delay_loop 6

lb_algo wrr

lb_kind DR

nat_mask 255.255.255.0

persistence_timeout 300

protocol TCP

#ipvsadm -A -t 10.89.3.168 -s wrr -p 20

real_server 10.89.3.100 80 {

weight 1

TCP_CHECK {

connect_timeout 8

nb_get_retry 3

delay_before_retry 3

connect_port 80

}

}

real_server 10.89.3.101 80 {

weight 1

TCP_CHECK {

connect_timeout 8

nb_get_retry 3

delay_before_retry 3

connect_port 80

}

}

}

#另外一台nginxproxy2,keepalived 配置文件设置

[[email protected] ~]# cd /etc/keepalived/

[[email protected] keepalived]# cat keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

[email protected]

}

notification_email_from [email protected]

smtp_server 10.0.0.1

smtp_connect_timeout 30

router_id keepalvied_2

}

vrrp_instance VI_1 {

state BACKUP

interface eth0

virtual_router_id 55

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

10.89.3.168/24

}

}

virtual_server 10.89.3.168 80 {

delay_loop 6

lb_algo wrr

lb_kind DR

nat_mask 255.255.255.0

persistence_timeout 300

protocol TCP

#ipvsadm -A -t 10.89.3.168 -s wrr -p 20

real_server 10.89.3.100 80 {

weight 1

TCP_CHECK {

connect_timeout 8

nb_get_retry 3

delay_before_retry 3

connect_port 80

}

}

real_server 10.89.3.101 80 {

weight 1

TCP_CHECK {

connect_timeout 8

nb_get_retry 3

delay_before_retry 3

connect_port 80

}

}

}

2.测试keepalived vip 是否漂移

#nginxproxy1

[[email protected] keepalived]# ip add |grep 10.89.3

inet 10.89.3.102/24 brd 10.89.3.255 scope global eth0

inet 10.89.3.168/24 scope global secondary eth0

#nginxproxy2

[[email protected] keepalived]# ip add |grep 10.89.3

inet 10.89.3.103/24 brd 10.89.3.255 scope global eth0

#nginxproxy1 上停止keepalived

[[email protected] keepalived]# /etc/init.d/keepalived stop

Stopping keepalived:                                       [  OK  ]

[[email protected] keepalived]#  ip add |grep 10.89.3

inet 10.89.3.102/24 brd 10.89.3.255 scope global eth0

#查看vip是否漂移了

[[email protected] keepalived]# ip add |grep 10.89.3

inet 10.89.3.103/24 brd 10.89.3.255 scope global eth0

inet 10.89.3.168/24 scope global secondary eth0

#结论:keepalived配置采购,可以实现高可用。

3.配置2台 nginxproxy,实现负责均衡。

cd /application/nginx/conf

mkdir extra

[[email protected] extra]# vi upstream01.conf

upstream www_real_servers {

server 10.89.3.100:80 weight=5;

server 10.89.3.101:80 weight=5;

}

server {

listen    80;

server_name www.kjcat.org;

location / {

proxy_pass http://www_real_servers;

}

}

#在nginxproxy1配置文件中包含extra

vi nginx.conf

worker_processes  1;

events {

worker_connections  1024;

}

http {

include       mime.types;

default_type  application/octet-stream;

sendfile        on;

keepalive_timeout  65;

include extra/upstream01.conf;

}

~

--------------------------------------

[[email protected] extra]# vi upstream02.conf

upstream www_real_servers {

server 10.89.3.100:80 weight=5;

server 10.89.3.101:80 weight=5;

}

server {

listen    80;

server_name www.kjcat.org;

location / {

proxy_pass http://www_real_servers;

}

}

#在nginxproxy2配置文件中包含extra

vi nginx.conf

worker_processes  1;

events {

worker_connections  1024;

}

http {

include       mime.types;

default_type  application/octet-stream;

sendfile        on;

keepalive_timeout  65;

include extra/upstream02.conf;

}

#两台机器重新启动nginx

/application/nginx/sbin/nginx -s reload

4.验证测试

#在另外一台服务器上配置解析

[[email protected] ~]# vi /etc/hosts

#增加

10.89.3.168 www.kjcat.org

[[email protected] ~]# curl 10.89.3.168

this is nginx Proxy for LAMP........

[[email protected] ~]# curl 10.89.3.168

This is nginx proxy for LNMP.

[[email protected] ~]# curl 10.89.3.168

this is nginx Proxy for LAMP........

[[email protected] ~]# curl 10.89.3.168

This is nginx proxy for LNMP.

[[email protected] ~]# curl www.kjcat.org

this is nginx Proxy for LAMP........

[[email protected] ~]# curl www.kjcat.org

This is nginx proxy for LNMP.

[[email protected] ~]# curl www.kjcat.org

this is nginx Proxy for LAMP........

[[email protected] ~]# curl www.kjcat.org

This is nginx proxy for LNMP.

#在浏览器中输入IP也可以实现(如果有DNS就可以实现域名 www.kjcat.com访问)

http://10.89.3.168

This is nginx proxy for LNMP.

#刷新后

this is nginx Proxy for LAMP........

时间: 2024-12-22 13:17:23

实战安装 nginx+keepalvied 实现负载均衡和高可用的相关文章

nginx反向代理tomacat+keepalived实现动静分离、负载均衡、高可用

本文的动静分离主要是通过nginx+tomcat来实现,其中nginx处理图片.html.JS.CSS等静态文件,tomcat处理jsp.servlet等动态请求 服务器名称                                 系统版本                           预装软件                     IP地址 Nginx服务器                             CentOS 7 最小安装              Nginx

Nginx之二:负载均衡及高可用

一.Nginx负载均衡及高可用简介 负载均衡是为了解决单个节点压力过大,造成Web服务响应过慢,严重的情况下导致服务瘫痪,无法正常提供服务.负载均衡,将用户的所有HTTP请求均衡的分配到每一台机器上,充分发挥所有机器的性能,提高服务的质量和用户体验.负载均衡常用的Web服务器软件有Nginx.HAProxy.LVS.Apache. Nginx负载均衡是通过upstream模块来实现的,常见四种负载策略: 轮循(默认:将每个请求均匀分配到每台服务器 最少连接:将请求分配给连接数最少的服务器 IP

Keepalived+LVS+Nginx负载均衡之高可用

Keepalived+LVS+Nginx负载均衡之高可用 上一篇写了nginx负载均衡,此篇实现高可用(HA).系统整体设计是采用Nginx做负载均衡,若出现Nginx单机故障,则导致整个系统无法正常运行.针对系统架构设计的高可用要求,我们需要解决Nginx负载均衡出现单机故障时,系统正常运行的需求.所以系统架构引入Keepalived组件,实现系统高可用. 一.Keepalived介绍 Keepalived是分布式部署系统解决系统高可用的软件,结合LVS(Linux Virtual Serve

Nginx反向代理、负载均衡, keepalived高可用

Nginx反向代理.负载均衡,  keepalived高可用 Nginx反向代理.负载均衡,  keepalived高可用 一.Nginx反向代理.负载均衡 1.什么是反向代理.负载均衡 严格的说,Nginx仅仅是作为Nginx Proxv反向代理使用的,因为这个反向代理功能表现的效果是负载均衡集群的效果,所以本文称之为Nginx负载均衡.那么,反向代理和负载均衡有什么区别呢? 普通负载均衡软件,例如大名鼎鼎的LVS,其实现的功能只是对请求数据包的转发(也可能会改写数据包).传递,其中DR模式明

实战 LVS+Keepalived 实现负载均衡和高可用

1.软件介绍 LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统,可以实现LINUX平台下的简单负载均衡. LVS负载均衡有4种负载均衡方式 a.DR 模式 b.NAT 模式 c.Tunnel 模式 d.Full NAT 模式 LVS负载均衡8种调度算法 (rr,wrr,lc,wlc,lblc,lblcr,dh,sh) 各个模式的原理在此不再赘述,本文使用DR模式,rr调度算法. Keepalived 是运行在lvs 之上,它的主要功能是

nginx负载均衡+keepalived高可用完全配置小结

nginx做负载均衡(无高可用) 大致步骤. 1. 前端 nginx安装,pcre安装,具体步骤不解释. 2. 负载配置 A. 默认轮循 在nginx.conf  里加入一行 include upstream.conf,然后所有的负载均衡的配置直接在upstream.conf里配置. [[email protected] conf]# cat upstream.conf upstream httpservers { server 192.168.137.10:80 weight=5; serve

Nginx代理MogileFS并实现负载均衡和高可用

Nginx代理MogileFS并实现负载均衡和高可用 MogileFS nginx 负载均衡 大纲 实验环境 实验步骤 配置MogileFS 配置Nginx 总结 前言 上篇文章我们了解分布式系统和MogileFS的基本使用, 但是那样的架构是有问题的, 本篇文章我们来了解一下如何使用nginx-mogilefs-module-master模块来构建一个不一样的 MogileFS Cluster 实验拓扑 实验环境 主机 IP 功用 node6 172.16.1.7 Nginx,Tracker,

Keepalived_tengine实现discuz负载均衡和高可用

前言: 上篇博文<keepalived_nginx实现discuz负载均衡和高可用>讲到,由于nginx将health_check功能放入到了商业版本,导致社区版本的nginx进行负载均衡,无法对后端的RS主机进行健康状态检测,所以现在准备使用tengine来取代nginx.我们只需要将前一章节VS主机上的nginx替换为tengine即可. 配置: Host VS1 卸载nginx,安装tengine # yum remove -y nginx # yum groupinstall -y &

用haproxy结合keepalived实现基于LNMP的负载均衡和高可用

今天我们讲haproxy结合keepalived实现LNMP的负载均衡和高可用,现在的公司大部分都基于haproxy实现负载均衡.下面以一个事例去给大家详细讲解如何去实现: 一.用haproxy结合keepalived实现基于lnmp的负载均衡和高可用服务,要求: (1)实现动静分离,图片和css,js都分离到专门的静态服务器上 (2)只允许172.17网段用户访问 (3)对于动态请求,要求实现基于cookie的会话保持 整体架构如下: 1.实验前需要关闭防火墙和selinux,以及同步时间.