Linux基础教程之Keepalived实现Nginx双主高可用负载均衡集群

实验目的:使用keepalived实现Nginx的双主高可用负载均衡集群。

实验环境:两台Nginx proxy(双主Nginx,各需要两块网卡,eth0连接内网,eth1连接外网)、两台web server(请求的负载均衡)、一台client用于验证结果。

Linux基础教程之Keepalived实现Nginx双主高可用负载均衡集群

注意:为了不影响实验结果,在实验开始前先关闭iptables和seLinux

操作步骤:

一、配置IP

1.配置A主机的IP

# ip addr add dev eth0 192.168.10.2/24

2.配置B主机的IP

# ip addr add dev eth0 192.168.10.23/24

3.配置C主机的IP

# ip addr add dev eth0 192.168.10.3/24

4.配置D主机的IP

# ip addr add dev eth0 192.168.10.33/24

二、配置web服务(C和D主机都做同样配置,只需修改默认主页中的IP地址为本机的IP即可,以示区别)

1.安装apache

# yum -y install apache

2.创建默认主页

# vim /var/www/html/index.html

<h1>192.168.10.3</h1>

3.启动apache

# service httpd start

三、配置sorry_server(此服务配置于Nginx proxy主机上,两台Nginx proxy都做同样配置,只需修改默认主页中的IP地址为本机的IP即可,以示区别)

1.安装apache

# yum -y install apache

2.创建默认主页

# vim /var/www/html/index.html

<h1>sorry_server:192.168.10.2</h1>

3.修改监听端口为8080,以免与nginx所监听的端口冲突

# vim /etc/httpd/conf/httpd.conf

Listen 8080

4.启动apache服务

四、配置代理(两台Nginx proxy都做同样配置)

1.安装nginx

# yum -y install nginx

2.定义upstream集群组,在http{}段中定义;

# vim /etc/nginx/nginx.conf

http {

upstream websrvs {

server 192.168.10.3:80;

server 192.168.10.33:80;

server 127.0.0.1:8080 backup;

}

}

3.调用定义的集群组,在server{}段的location{}段中调用;

# vim /etc/nginx/conf.d/default.conf

server {

location / {

proxy_pass http://wersrvs;

index index.html;

}

}

4.启动服务

# service nginx start

五、配置keepalived

A主机上操作

1.安装keepalived

# yum -y install keepalived

2.编辑A主机的配置文件/etc/keepalived/keepalived.conf,作如下配置:

! Configuration File for keepalived

global_defs {

notification_email {

root@localhost

}

notification_email_from keepalived@localhost

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id CentOS6

vrrp_mcast_group4 224.0.100.39

}

vrrp_script chk_down {

script “[[ -f /etc/keepalived/down ]] && exit 1 || exit 0”

interval 1

weight -5

}

vrrp_script chk_nginx {

script “killall -0 nginx && exit 0 || exit 1”

interval 1

weight -5

fall 2

rise 1

}

vrrp_instance ngx {

state MASTER

interface eth1

virtual_router_id 14

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass MDQ41fTp

}

virtual_ipaddress {

192.168.20.100/24 dev eth1

}

track_script {

chk_down

chk_nginx

}

}

vrrp_instance ngx2 {

state BACKUP

interface eth1

virtual_router_id 15

priority 98

advert_int 1

authentication {

auth_type PASS

auth_pass XYZ41fTp

}

virtual_ipaddress {

192.168.20.200/24 dev eth1

}

track_script {

chk_down

chk_nginx

}

}

B主机也作同样配置,稍作修改即可,需要修改的地方如下:

vrrp_instance ngx {

state BACKUP

priority 98

}

vrrp_instance ngx2 {

state MASTER

priority 100

}

六、模拟故障,验证结果

1.启动两台Nginx proxy的keepalived服务

# service keepalived start

2.访问192.168.20.100,结果应是后端的web server轮询响应请求

Linux基础教程之Keepalived实现Nginx双主高可用负载均衡集群

3.访问192.168.20.200,结果应是后端的web server轮询响应请求

Linux基础教程之Keepalived实现Nginx双主高可用负载均衡集群

4.将后端的web server关闭一台,访问192.168.20.100或192.168.20.200,响应请求的将只是另一台正常运行web server的主机

Linux基础教程之Keepalived实现Nginx双主高可用负载均衡集群

5.将后端的web server都关闭,此时访问192.168.20.100或192.168.20.200,响应请求的将只是Nginx proxy中定义的主server中的sorry_server

Linux基础教程之Keepalived实现Nginx双主高可用负载均衡集群

6.关闭一台Nginx proxy 的nginx服务,备server将把IP地址添加到本机,继续提供服务,此时访问192.168.20.100或192.168.20.200并不会有任何察觉

Linux基础教程之Keepalived实现Nginx双主高可用负载均衡集群

相关新闻

历经多年发展,已成为国内好评如潮的Linux云计算运维、SRE、Devops、网络安全、云原生、Go、Python开发专业人才培训机构!