搭建高可用Web集群

环境部署

创建机器:

pubserver: 192.168.88.240                   # 自动化运维

client1: 192.168.88.10                           #客户机 

nginx1: 192.168.88.11                           #负载均衡调度器

nginx2: 192.168.88.12                           #负载均衡调度器

web1:   192.168.88.100                         #Web工作服务器

web2:   192.168.88.200                         #Web工作服务器

准备测试页面和需要的软件包

[root@lhh ~]# rsync -av web/destroy   192.168.88.100:
sending incremental file list
destroy/
destroy/index.html
destroy/statics/
destroy/statics/images/
destroy/statics/images/qi.jpg
destroy/statics/js/
destroy/statics/js/jquery-1.8.3.min.js

sent 174,339 bytes  received 101 bytes  69,776.00 bytes/sec
total size is 173,968  speedup is 1.00
[root@lhh ~]# rsync -av web/link   192.168.88.200: 
sending incremental file list
link/
link/index.html
link/statics/
link/statics/images/
link/statics/images/qi.jpg
link/statics/js/
link/statics/js/jquery-1.8.3.min.js

sent 181,766 bytes  received 97 bytes  72,745.20 bytes/sec
total size is 181,402  speedup is 1.00
[root@web1 ~]# cp -r destroy/*  /var/www/html/     #将测试页面放入网页根目录
[root@web2 ~]# cp -r link/* /var/www/html


在pubserver主机上编写playbook给web1,web2安装httpd以及启动httpd服务

[root@lhh ~]# cd cluster
[root@lhh cluster]# vim install-http.yaml
---
- hosts: webservers
  tasks:
    - yum:
        name: httpd
    - service:
        name: httpd
        state: started
        enabled: yes
      
[root@lhh cluster]# ansible-playbook install-http.yaml 

PLAY [webservers] **************************************************************************

TASK [Gathering Facts] *********************************************************************
ok: [web1]
ok: [web2]

TASK [yum] *********************************************************************************
changed: [web1]
changed: [web2]

TASK [service] *****************************************************************************
changed: [web1]
changed: [web2]

PLAY RECAP *********************************************************************************
web1                       : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
web2                       : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
[root@web1 ~]# ss -nutlp | grep 80
tcp   LISTEN 0      128          0.0.0.0:80        0.0.0.0:*    users:(("httpd",pid=24840,fd=3),("httpd",pid=24511,fd=3),("httpd",pid=24510,fd=3),("httpd",pid=24509,fd=3),("httpd",pid=24491,fd=3))
[root@web2 ~]# ss -nutlp |grep 80
tcp   LISTEN 0      128          0.0.0.0:80        0.0.0.0:*    users:(("httpd",pid=24243,fd=3),("httpd",pid=24242,fd=3),("httpd",pid=24241,fd=3),("httpd",pid=24223,fd=3))

编写Shell脚本nginx.sh给nginx1,nginx2安装nginx

[root@nginx1 ~]# vim nginx.sh
#!/bin/bash
yum -y install gcc make pcre-devel openssl-devel
tar -xf nginx-1.22.1.tar.gz
cd nginx-1.22.1
./configure
make
make install
[root@nginx2 ~]# vim nginx.sh
#!/bin/bash
yum -y install gcc make pcre-devel openssl-devel
tar -xf nginx-1.22.1.tar.gz
cd nginx-1.22.1
./configure
make
make install
[root@nginx1 nginx]# cd /usr/local/nginx
[root@nginx1 nginx]# sbin/nginx 
[root@nginx1 ~]# ss -nutlp | grep 80
tcp   LISTEN 0      128          0.0.0.0:80        0.0.0.0:*    users:(("nginx",pid=1343,fd=6),("nginx",pid=1342,fd=6))
[root@nginx2 nginx]# cd /usr/local/nginx
[root@nginx2 nginx]# sbin/nginx 
[root@nginx2 ~]# ss -nutlp | grep 80
tcp   LISTEN 0      128          0.0.0.0:80        0.0.0.0:*    users:(("nginx",pid=1343,fd=6),("nginx",pid=1342,fd=6))

修改nginx配置文件搭建Web集群实现负载均衡

[root@nginx1 ~]# vim /usr/local/nginx/conf/nginx.conf
 26     upstream webserver {
 27         server 192.168.88.100:80;
 28         server 192.168.88.200:80;
 29 
 30 }
......
47         location / {
 48             root   html;
 49             index  index.html index.htm;
 50             proxy_pass http://webserver; #调用集群
 51         }
[root@nginx2 ~]# vim /usr/local/nginx/conf/nginx.conf
 26     upstream webserver {
 27         server 192.168.88.100:80;
 28         server 192.168.88.200:80;
 29 
 30 }
......
47         location / {
 48             root   html;
 49             index  index.html index.htm;
 50             proxy_pass http://webserver; #调用集群
 51         }
[root@nginx1 nginx]# sbin/nginx -s reload    #重启nginx   
[root@nginx2 nginx]# sbin/nginx -s reload

在浏览器中输入nginx1地址192.168.88.11查看网页内容

客户端通过访问nginx服务器,nginx服务器将请求分发到Web工作集群,web1收到请求,执行请求,再将完成的页面返还给nginx代理服务器,nginx代理服务器将web页面给客户端。

 多次刷新页面,发现请求会分发到web2工作服务器上,web2服务器完成请求,将完成的页面返还给nginx代理服务器,nginx代理服务器将web页面给客户端。

这样就完成了负载均衡,通过nginx代理服务器分发请求到web工作集群,分散了服务器的压力。

接下来要实现高可用

编写playbook在nginx1,nginx2上安装keepalived

[root@lhh cluster]# vim install-keepalived.yml 
---
- hosts: nginx
  tasks:
    - yum:
        name: keepalived
        state: present
root@lhh cluster]# ansible-playbook install-keepalived.yml 

PLAY [nginx] *******************************************************************************

TASK [Gathering Facts] *********************************************************************
ok: [nginx2]
ok: [nginx1]

TASK [yum] *********************************************************************************
changed: [nginx1]
changed: [nginx2]

PLAY RECAP *********************************************************************************
nginx1                     : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
nginx2                     : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

修改keepalived配置文件

 12    router_id nginx1
 13    vrrp_iptables
 36     virtual_ipaddress {
 37         192.168.88.101
 38     }         #配置VIP 
...
 51     real_server 192.168.88.100 80 {    #修改真实服务器地址
 52         weight 1
 53         TCP_CHECK {
 54             connect_timeout 3
 55             retry 3
 56             delay_before_retry 3
 57         }
 58     }
 59 }
 70     real_server 192.168.88.200 80 {
 71         weight 1
 72      TCP_CHECK {
 73             connect_timeout 3
 74             retry 3
 75             delay_before_retry 3
 76         }
 77     }
[root@nginx2 ~]# vim /etc/keepalived/keepalived.conf
 12    router_id nginx2
 13    vrrp_iptables
 24     state BACKUP
 27     priority 80
....
 33     virtual_ipaddress {
 34         192.168.88.101
 35     }
.....
 real_server 192.168.88.100 80 {
 50         weight 1
 51         TCP_CHECK {
 52             connect_timeout 3
 53             retry 3
 54             delay_before_retry 3
 55         }
 56     }
 57 } 
....
 68     real_server 192.168.88.200 80 {
 69         weight 1
 70      TCP_CHECK {
 71             connect_timeout 3
 72             retry 3
 73             delay_before_retry 3
 74         }
 75     }
#当主没有宕机时,VIP只出现在主服务器上
[root@nginx1 ~]# ip a s| grep 192
    inet 192.168.88.11/24 brd 192.168.88.255 scope global noprefixroute eth0
    inet 192.168.88.101/32 scope global eth0
[root@nginx2 ~]# ip a s| grep 192
    inet 192.168.88.12/24 brd 192.168.88.255 scope global noprefixroute eth0
当主服务器宕机时,VIP通过keepalived故障转移机制将VIP切换到备服务器上
[root@nginx1 ~]# poweroff
[root@nginx2 ~]# ip a s| grep 192
    inet 192.168.88.12/24 brd 192.168.88.255 scope global noprefixroute eth0
    inet 192.168.88.101/32 scope global eth0

通过访问VIP192.168.88.101也能访问到web页面

高可用集群到这搭建完了

但Keepalived不知道服务器上运行了哪些服务,MASTER服务器可以通过跟踪脚本监视本机的80端口,一旦本机80端口失效,则将VIP切换至BACKUP服务器,Keepalived对脚本的要求是,退出码为0表示访问成功;退出码为1表示失败。

[root@nginx1 ~]# vim /etc/keepalived/check_http.sh
#!/bin/bash

ss -tlnp | grep :80 &> /dev/null && exit 0 || exit 1
[root@nginx1 ~]# vim /etc/keepalived/keepalived.conf
 20 vrrp_script chk_http_port{
 21    script "/etc/keepalived/check_http.sh"
 22    interval 2
 23 }
 39     track_script {
 40       chk_http_port        #调用脚本
 41 }
#同理在nginx2服务器上修改配置文件
#关掉nginx服务来测试
[root@nginx1 ~]# ip a s| grep 192
    inet 192.168.88.11/24 brd 192.168.88.255 scope global noprefixroute eth0
[root@nginx1 nginx]# sbin/nginx   
[root@nginx1 nginx]# sbin/nginx   -s stop
[root@nginx1 nginx]# ip a s | grep 192
    inet 192.168.88.11/24 brd 192.168.88.255 scope global noprefixroute eth0
#在nginx2上查看VIP
[root@nginx2 nginx]# ip a s| grep 192
    inet 192.168.88.12/24 brd 192.168.88.255 scope global noprefixroute eth0
    inet 192.168.88.101/32 scope global eth0
#再启动nginx服务在nginx1上查看VIP

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值