JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

Keepalived+Nginx高可用配置

wys521 2024-09-18 02:41:23 精选教程 32 ℃ 0 评论

很久就想试试Keepalived+Nginx高可用怎么配置,一直没有动手。正所谓"纸上得来终觉浅,绝知此事要躬行"。So let's go, step by step.


主要需要Keepalived+Nginx+Apache,所以要安装这几个。我用的centos7的虚拟机。IP和端口规划如下:


作用 IP 端口

web 192.168.37.128 8828

web 192.168.37.129 8829

Nginx 192.168.37.128 80

Nginx 192.168.37.129 80


keepalived虚拟IP :192.168.37.120 (前提是要配置好固定IP,虚拟IP在这个范围接近就可以了)

建议web的IP在其他机器上,没有更多的虚拟机自己试验,所以web和nginx放在一台上。


图例仅供参考


1,搭建apache


充当web服务器,用apache搞web方便快捷。


yum install httpd

修改配置 /etc/httpd/conf/httpd.conf


#Listen 12.34.56.78:80

Listen 8829

另外一个上面修改 /etc/httpd/conf/httpd.conf

#Listen 12.34.56.78:80

Listen 8828

另外为了方便,修改/usr/share/httpd/noindex/index.html 可以区分是那个web上面的。


systemctl start httpd


启动apache,可以分别访问网页试试:192.168.37.128:8828

192.168.37.129:8829

2,搭建Nginx


rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm


yum install nginx


/etc/nginx/nginx.conf (下面列出部分配置,其他不用修改)


http {

include /etc/nginx/mime.types;

default_type application/octet-stream;


upstream srvlist {

server 192.168.37.128:8828;

server 192.168.37.129:8829;

server 127.0.0.1:8080 backup;

}

这里面包含一个/etc/nginx/conf.d/default.conf


server {

listen 80;

server_name localhost;


#charset koi8-r;

#access_log /var/log/nginx/host.access.log main;


location / {

proxy_pass http://srvlist;

#root /usr/share/nginx/html;

index index.html index.htm;

}


在两台虚拟机上做同样的配置,然后启动Nginx。

service nginx start

这样可以到浏览器上面测试下,输入 http://192.168.37.129/ 或者 http://192.168.37.128/


刷新几次,发现页面内容在128,129之间自动切换。这样就明白了负载均衡是什么意思了。



3,配置Keepalived


上面配置了Nginx应该说已经不错了,能够在自动把请求代理到128或者129的web上。但是如果128上面的Nginx挂了呢?外面的用户不知道我该去访问192.168.37.129,这个地址他是不知道的。反过来,129挂了,也是这样的情形。能不能自动在两个Nginx之间自动切换,自动检测,发现其中一个挂了,地址自动切换到另外一台,对用户来说是透明的。对了,keepalived就是为这个而来的。它通过一个虚拟的IP,检测两个主机的状态,自动切换。详细的关于Keepalived可以百度下。


首先当然是安装Keepalived。建议最好是自己编译安装,各种版本的依赖的坑,很容易掉进去。


yum install keepalived


修改配置/etc/keepalived/keepalived.conf


global_defs {

notification_email {

test@163.com

}

notification_email_from test@163.com

smtp_server smtp.163.com

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_instance VI_1 {

state MASTER

interface eno16777736

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 111111

}

virtual_ipaddress {

192.168.37.120

}

}

在另外一台上面安装,修改配置/etc/keepalived/keepalived.conf



global_defs {

notification_email {

test@163.com

}

notification_email_from test@163.com

smtp_server smtp.163.com

smtp_connect_timeout 30

router_id LVS_DEVEL

}


vrrp_instance VI_1 {

state BACKUP

interface eno16777736

virtual_router_id 51

priority 50

advert_int 1

authentication {

auth_type PASS

auth_pass 111111

}

virtual_ipaddress {

192.168.37.120

}

}

主要的不同就是state,priority。一般来说把master的priority配高点。


interface eno16777736 这里也要注意,看了网上很多把eno16777736 配置成eth0,eth2,dev等等,开始一直不明白,原来是要网卡的名称。


可以自己用命令ifconfig看下,你是用的哪个,写哪个就可以了。


两台机器配置好了之后,就可以开始启动了。


service keepalived restart

不出意外,浏览器http://192.168.37.120/可以看到。如果没有看到,也不要急,遇到困难多看看日志/var/log/messages


仔细看启动的过程,有很多信息可以给你提示。


我在129上面成功了,128上面遇到:


Dec 26 22:15:07 vm11 Keepalived_vrrp[10099]: Opening file '/etc/keepalived/keepalived.conf'.

Dec 26 22:15:07 vm11 Keepalived_vrrp[10099]: Unable to load ipset library - libipset.so.3: cannot open shared object file: No such file or directory

Dec 26 22:15:07 vm11 Keepalived_vrrp[10099]: VRRP_Instance(VI_1) removing protocol VIPs.

好像是缺了什么包,百度了,没有结果。试试看


yum install libnl3-devel ipset-devel

又发现问题:


Dec 26 22:25:28 vm11 Keepalived_vrrp[10366]: Opening file '/etc/keepalived/keepalived.conf'.

Dec 26 22:25:29 vm11 kernel: Netfilter messages via NETLINK v0.30.

Dec 26 22:25:29 vm11 kernel: ip_set: protocol 6

Dec 26 22:25:29 vm11 Keepalived_vrrp[10366]: VRRP_Instance(VI_1) removing protocol VIPs.

是不是和防火墙有关啊。果断决定 systemctl stop firewalld


Dec 26 22:33:05 vm11 Keepalived_vrrp[10987]: Using LinkWatch kernel netlink reflector...

Dec 26 22:33:05 vm11 Keepalived_vrrp[10987]: VRRP_Instance(VI_1) Entering BACKUP STATE

Dec 26 22:33:05 vm11 Keepalived_vrrp[10987]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]

貌似这次正常了。浏览器访问下,OK了


关掉129上面的Nginx,service nginx stop,还是可以访问,说明生效了。在看下日志/var/log/messages


Dec 26 22:34:28 vm11 Keepalived_vrrp[10987]: VRRP_Instance(VI_1) Transition to MASTER STATE

Dec 26 22:34:29 vm11 Keepalived_vrrp[10987]: VRRP_Instance(VI_1) Entering MASTER STATE

Dec 26 22:34:29 vm11 Keepalived_vrrp[10987]: VRRP_Instance(VI_1) setting protocol VIPs.

Dec 26 22:34:29 vm11 Keepalived_vrrp[10987]: Sending gratuitous ARP on eno16777736 for 192.168.37.120

Dec 26 22:34:29 vm11 Keepalived_vrrp[10987]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eno16777736 for 192.168.37.120

Dec 26 22:34:29 vm11 Keepalived_vrrp[10987]: Sending gratuitous ARP on eno16777736 for 192.168.37.120

惊喜的发现128上面的Entering MASTER STATE


再关掉128上的service nginx stop,192.168.37.120访问不了,两个都挂了,神仙也没有办法了。


是的,重启试试service nginx start,一切又回来了。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表