keepalived nginx完结nginx的高可用

作者:操作系统

nginx的高可用


二、相关的测验:

6.1测验关闭主nginx节点上的keepalived服务器,发绑定的vip在主节点未有

yum install -y net-snmp.x86_64net-snmp-devel.x86_64


[[email protected] html]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[[email protected] html]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[[email protected] html]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[[email protected] html]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[[email protected] html]# ifconfig lo:1 172.16.22.100 broadcast 172.16.22.100 netmask 255.255.255.255 up
[[email protected] html]# route add -host 172.16.22.100 dev lo:1
[root@host102 conf.d]# yum -y install keepalived
[root@host102 conf.d]# cd /etc/keepalived/
[root@host102 keepalived]# cp keepalived.conf keepalived.conf.1
[root@host102 keepalived]# vim keepalived.conf
global_defs {
   notification_email {
     abc@mail.com
   }
   notification_email_from abc@mail.com
   smtp_server smtp.mail.com
   smtp_connect_timeout 30
   router_id HA_MASTER1  #表示运行keepalived服务器的一个标识,发邮件时显示在邮件主题中的信息
}
vrrp_script chk_http_port {
script "/usr/local/keepalived/nginx.sh" ####检测nginx状态的脚本链接
interval 2
weight 2
}
vrrp_instance VI_2 {   #vrrp实例
    state BACKUP     #MASTER/BACKUP
    interface eno16777736    ####HA 监测网络接口
    virtual_router_id 51  #虚拟路由标识,是一个数字,同一个VRRP实例使用唯一的标识,master和backup要一样
    priority 80          #用于主从模式,优先级主高于100,从低于100
    advert_int 1           #主备之间的通告间隔秒数
    authentication {        #认证用于主从模式,mater和backup配置一样
        auth_type PASS          ###主备切换时的验证
        auth_pass 1111          #密码
    }
track_script {
chk_http_port ### 执行监控的服务
}
    virtual_ipaddress {

 192.168.1.200/24 dev eno16777736 label eno16777736:1   ###########虚拟ip
    }
}
[root@host102 keepalived]# mkdir -p /usr/local/keepalived
[root@host102 keepalived]# vim /usr/local/keepalived/nginx.sh
#!/bin/bash
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
[root@host102 keepalived]# chmod 755 /usr/local/keepalived/nginx.sh
[root@host102 keepalived]# systemctl start keepalived
[root@host102 keepalived]# ifconfig -a
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.102  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::20c:29ff:fe87:fd0e  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:87:fd:0e  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)

在别的一台backup机器上做同样操作,注意/etc/keepalived/keepalived.conf要修改的地点如下:

Master:

注:Master和Backup上边安装ipvsadm重假使为着查看ipvs的平整

3.由此yum安装配备nginx节点,两台一样的章程

操作系统 1


[[email protected] ~]# cd /var/www/html/
[[email protected] html]# cat index.html #建一个测试网页
<h1>this is apache1</h1>

6.测验:通过浏览器访谈测验.

CentOS 7下Nginx服务器的安装配备  操作系统,http://www.linuxidc.com/Linux/2017-04/142986.htm

[[email protected] nginx]# service nginx restart
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]
[[email protected] nginx]# service nginx restart
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]

[root@host101 ~]# vim /etc/hosts
192.168.1.200   ng-vip
192.168.1.101   ng-master
192.168.1.102   ng-slave
192.168.1.161   web1
192.168.1.162   web2

[root@host101 ~]# yum clean all
[root@host101 ~]# systemctl stop firewalld.service
[root@host101 ~]# systemctl disable firewalld.service
[root@host101 ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

具有的软件包均为yum方式安装,yum源均在用Ali云服务器所采取的。

两侧分别重启服务

①、能够是heartbeat ldirectord这种重量级的;

表明:本实验为双节点nginx为两台apache服务器提供负载均衡,本文不是做lvs,所以realserver不是配置在keepalived.conf而是在nginx的布署文件中upstream。
此架构需思量的标题:
1)Master没挂,则Master占有vip且nginx运行在Master上
2)Master挂了,则backup抢占vip且在backup上运行nginx服务
3)假设master服务器上的nginx服务挂了,则vip能源转移到backup服务器上
4)检查测量检验后端服务器的符合规律化情形
Master和Backup两侧都张开nginx服务,无论Master照旧Backup,当在那之中的八个keepalived服务截至后,vip都会上浮到keepalived服务还在的节点上,即便要想使nginx服务挂了,vip也漂浮到另多少个节点,则必得用剧本也许在布局文件之中用shell命令来调整。

! Configuration File for keepalived
global_defs {
  notification_email {
    yangry@linuxidc.com
  }
  notification_email_fromyangry@linuxidc.com
  smtp_server mail.linuxidc.com
  smtp_connect_timeout 30
  router_id LVS_MASTER1  #
意味着运维keepalived服务器的一个标志,发邮件时显示在邮件主旨中的新闻
}
vrrp_script chk_http_port {
script "/usr/local/keepalived/nginx.sh"####
检测nginx状态的脚本链接
interval 2
weight 2
}
vrrp_instance VI_2 {  #vrrp
实例
    stateMASTER    #MASTER/BACKUP
    interface eth1  ####HA 
监测互联网接口
    virtual_router_id 51  #
设想路由标志,是一个数字,同贰个VENVISIONRP实例使用唯一的标志,master和backup要一律
    priority100          #
用以着力形式,优先级主高于100,从低于100
    advert_int1          #
主备之间的通告间隔秒数
    authentication{        #
证实用于中央情势,mater和backup配置同样
      auth_type PASS          ###
主备切换时的验证
      auth_pass 1111          #
密码
    }
track_script {
chk_http_port ### 
实行监督的劳务
}
    virtual_ipaddress {
       
 192.168.1.100 dev eth1 label eth1:0  ###########
虚拟ip 
    }
}


3、开启服务

参考:

4.启动nginx

=================================

[[email protected] html]# service httpd start
Starting httpd:                 [  OK  ]
[[email protected] ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.22.100:80 wrr
  -> 172.16.22.3:80               Route   1      0          0       
  -> 172.16.22.4:80               Route   1      0          0
[root@host101 keepalived]# systemctl stop keepalived.service 
[root@host101 keepalived]# ifconfig -a
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.101  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::20c:29ff:fefe:6f3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:fe:06:f3  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)

vip在却在备节点上出现
[root@host102 keepalived]# ifconfig -a
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.102  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::20c:29ff:fe87:fd0e  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:87:fd:0e  txqueuelen 1000  (Ethernet)

eno16777736:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.200  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:87:fd:0e  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)

通过浏览器访问测试http://192.168.1.200/,可发现流量依然在web1和web2之间跳转。

成立自定义配置文件

Master和Backup两侧都张开nginx服务,无论Master仍然Backup,当当中的贰个keepalived服务停止后,vip都会飘浮到keepalived服务还在的节点上,


6.2重复运维主节点的keepalived服务,开掘vip又再次漂移会主节点

Linux下安装PHP景况并安排Nginx协理php-fpm模块  http://www.linuxidc.com/Linux/2017-05/144333.htm


3、开启服务

[root@host101 keepalived]# systemctl stop nginx.service 
[root@host101 keepalived]# ifconfig -a
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.101  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::20c:29ff:fefe:6f3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:fe:06:f3  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)

[root@host101 keepalived]# systemctl status keepalived 
keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled)
   Active: inactive (dead)

通过浏览器访问测试http://192.168.1.200/,可发现流量依然在web1和web2之间跳转。

看日志能够观望,两台服务器的 MASTRE 和 BACUKUP 已经都例行了。


apache1能够访谈

4.在主nginx服务器上安装keepalived,并安排nginx服务不奇怪检查评定脚本

操作系统 2

本文出自 “技能之路---桀” 博客,请必须保留此出处

[[email protected] ~]# yum -y install httpd

2.配置web1,web2的apache服务,两台同样的格局

1安装蒙受所需信赖包

[[email protected] ~]# cd /var/www/html/
[[email protected] html]# cat index.html #建一个测试网页
<h1>this is apache1</h1>

2、修改配置文件

5.在备nginx服务器上安装keepalived,并计划nginx服务正常检查评定脚本,与主略有不相同

service nginx start

尝试情形:

 6.4再度运转主节点的nginx和keepalived服务后,VIP又漂回主节点。

vim /usr/local/keepalived/nginx.sh

4、开启keepalived和nginx的服务

操作系统 3

[root@host101 keepalived]# systemctl start nginx.service 
[root@host101 keepalived]# systemctl start keepalived
通过浏览器访问测试http://192.168.1.200/,可发现流量依然在web1和web2之间跳转。

yum install -y nginx

1234567891011 [centos]name=sohu-centosbaseurl=http://mirrors.sohu.com/centos/$releasever/os/$basearchgpgcheck=1enable=0gpgkey=http://mirrors.sohu.com/centos/RPM-GPG-KEY-CentOS-6[epel]name=sohu-epelbaseurl=http://mirrors.sohu.com/fedora-epel/$releasever/$basearch/enable=1gpgcheck=0

操作系统 4




在看此时的backup日志

首先必需旗帜显明后端服务器的正规状态检查测试keepalived在这种架构上是无计可施检验的,后端服务器的常规情状检测是有nginx来判断的,不过nginx的检验机制有一定的瑕疵,后端服务器某一个宕机之后,nginx还是会散发央浼给它,在自然的光阴内后端服务响应不了,nginx则会发放别的一个服务器,然后当客商的央浼来了,nginx会一段时间内不会把央浼分发给已经宕机的服务器,可是过一段时间后,nginx照旧会把分发须求发给宕机的服务器上。


配置步骤如下
1.起首化4台测验server,该关的关了

操作系统 5


③、以上二种艺术都以依附于脚本,keepalived的面世缓和了不依据于脚本,也能够对后端realserver的不荒谬意况检查,keepalived的布局文件之中能够自行生成ipvs的条条框框,并且自动物检疫查测量检验后端realserver的情事,当后端realserver不可能提供劳动了,keepalived会自行将其在ipvs法规里面删除,当后端realserver能够提供劳务了,又自行的在ipvs准则里面增加。

[root@host161 ~]# yum -y install httpd
[root@host161 ~]# systemctl start httpd
[root@host161 ~]# systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multiuser.target.wants/httpd.service'
[root@host161 ~]# cat /var/www/html/index.html
hello this lvs-web1

[root@host162 ~]# yum -y install httpd
[root@host162 ~]# systemctl start httpd
[root@host162 ~]# systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multiuser.target.wants/httpd.service'
[root@host162 ~]# cat /var/www/html/index.html
hello this lvs-web2

service httpd start



[root@host101 keepalived]# systemctl start keepalived.service   
[root@host101 keepalived]# ifconfig -a
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.101  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::20c:29ff:fefe:6f3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:fe:06:f3  txqueuelen 1000  (Ethernet)

eno16777736:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.200  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:fe:06:f3  txqueuelen 1000  (Ethernet)
通过浏览器访问测试http://192.168.1.200/,可发现流量依然在web1和web2之间跳转。

yum install -y keepalived


[[email protected] ~]# yum -y install keepalived ipvsadm

6.3关门nginx主节点上的nginx服务,开掘vip从主节点无影无踪,keepalived服务关闭,vip在备节点上面世。

Nginx服务的SSL认证和htpasswd认证  http://www.linuxidc.com/Linux/2017-04/142478.htm

apache1:

②、能够是keepalived lvs这种轻量级的建设方案。(本博客首要写keepalived lvs轻量级的施工方案),

[root@host101 conf.d]# yum -y install keepalived
[root@host101 conf.d]# cd /etc/keepalived/
[root@host101 keepalived]# cp keepalived.conf keepalived.conf.1
[root@host101 keepalived]# vim keepalived.conf
global_defs {
   notification_email {
     abc@mail.com
   }
   notification_email_from abc@mail.com
   smtp_server smtp.mail.com
   smtp_connect_timeout 30
   router_id HA_MASTER1  #表示运行keepalived服务器的一个标识,发邮件时显示在邮件主题中的信息
}
vrrp_script chk_http_port {
script "/usr/local/keepalived/nginx.sh" ####检测nginx状态的脚本链接
interval 2
weight 2
}
vrrp_instance VI_2 {   #vrrp实例
    state MASTER     #MASTER/BACKUP
    interface eno16777736    ####HA 监测网络接口
    virtual_router_id 51  #虚拟路由标识,是一个数字,同一个VRRP实例使用唯一的标识,master和backup要一样
    priority 100          #用于主从模式,优先级主高于100,从低于100
    advert_int 1           #主备之间的通告间隔秒数
    authentication {        #认证用于主从模式,mater和backup配置一样
        auth_type PASS          ###主备切换时的验证
        auth_pass 1111          #密码
    }
track_script {
chk_http_port ### 执行监控的服务
}
    virtual_ipaddress {

 192.168.1.200/24 dev eno16777736 label eno16777736:1   ###########虚拟ip
    }
}
[root@host101 keepalived]# mkdir -p /usr/local/keepalived
[root@host101 keepalived]# vim /usr/local/keepalived/nginx.sh
#!/bin/bash
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
[root@host101 keepalived]# chmod 755 /usr/local/keepalived/nginx.sh
[root@host101 keepalived]# systemctl start keepalived
[root@host101 keepalived]# ifconfig -a
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.101  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::20c:29ff:fefe:6f3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:fe:06:f3  txqueuelen 1000  (Ethernet)

eno16777736:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.200  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:fe:06:f3  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)

刷新一下

[[email protected] ~]# yum -y install keepalived
[[email protected] ~]#tar xf nginx-1.4.2.tar.gz
[[email protected] ~]#yum -y groupinstall "Development tools" "Server  Platform Development"
[[email protected] ~]#yum -y install pcre-devel
[[email protected] ~]# cd nginx-1.4.2
[[email protected] nginx-1.4.2]# groupadd nginx
[[email protected] nginx-1.4.2]# useradd -r -g nginx nginx
[[email protected] nginx-1.4.2]#./configure 
--prefix=/usr
--sbin-path=/usr/sbin/nginx
--conf-path=/etc/nginx/nginx.conf 
--error-log-path=/var/log/nginx/error.log 
--http-log-path=/var/log/nginx/access.log 
--pid-path=/var/run/nginx/nginx.pid  
--lock-path=/var/lock/nginx.lock 
--user=nginx 
--group=nginx 
--with-http_ssl_module 
--with-http_flv_module 
--with-http_stub_status_module 
--with-http_gzip_static_module 
--http-client-body-temp-path=/var/tmp/nginx/client/
--http-proxy-temp-path=/var/tmp/nginx/proxy/
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
--http-scgi-temp-path=/var/tmp/nginx/scgi
--with-pcre
[[email protected] nginx-1.4.2]# make && make install

[root@host101 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/x86_64/
gpgcheck=0
enabled=1
[root@host101 ~]# yum clean all
[root@host101 ~]# yum -y install nginx
[root@host101 ~]# vim /usr/share/nginx/html/index.html
<h1>Welcome to ng-master!</h1>
[root@host101 ~]# cd /etc/nginx/conf.d/
[root@host101 conf.d]# mv default.conf default.conf.1
[root@host101 ~]# vim /etc/nginx/conf.d/web.conf
    upstream myapp1 {
        server web1;
        server web2;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
[root@host101 ~]# systemctl restart nginx.service

[root@host102 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/x86_64/
gpgcheck=0
enabled=1
[root@host102 ~]# yum clean all
[root@host102 ~]# yum -y install nginx
[root@host102 ~]# vim /usr/share/nginx/html/index.html
<h1>Welcome to ng-master!</h1>
[root@host102 ~]# cd /etc/nginx/conf.d/
[root@host102 conf.d]# mv default.conf default.conf.1
[root@host102 ~]# vim /etc/nginx/conf.d/web.conf
    upstream myapp1 {
        server web1;
        server web2;
    }
    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
[root@host102 ~]# systemctl restart nginx.service

vim/etc/keepalived/keepalived.conf



前言



4)检验后端服务器的正规状态


怎么要用keepalived lvs?

操作系统 6

nginx完成后端realserver的负荷均衡


1.    系统景况均为CentOS release 6.5 (Final)三12位,均在设想机上得逞促成。



2.安装nginx

OS:Centos 6.4(redhat 6.4)

Master:

apache1:

apache2:

4、修改内核参数和布署vip

机械上设置keepalived


何况是基于wrr算法,平均负载到realserver上

yum -y install httpd



操作系统 7

yum源:

拓扑图的统一盘算:

CentOS 6.8 安装LNMP环境(Linux Nginx MySQL PHP)  http://www.linuxidc.com/Linux/2017-04/142880.htm

此框架结构需思量的标题

apache2:

那会儿能够在浏览器测量试验一下,输入IP:192.168.1.135

本文由ca88发布,转载请注明来源

关键词: ca88网址 nginx Linux负载均衡 Keepalived ca312亚洲城手