N25_第二周作业

前言

我们这次使用HAProxy作为负载均衡调度器来实现后端httpd服务的负载均衡和动静分离,实现将来自用户的80端口的http请求转发只后端8080端口的server服务

HAProxy介绍

HAProxy的是一个免费的,非常快速和可靠的解决方案,提供高可用性,负载均衡和代理对TCP和HTTP的应用程序。它特别适用于非常高流量网站。多年来,它已成为标准的开源的负载均衡程序,现在随最主流的Linux发行版,并且通常默认的云平台部署。其运作模式使得其集成到现有的架构非常容易,无风险,同时还提供了可能性不暴露脆弱的Web服务器到网络

实验拓扑

N25_第二周作业

实验环境

VIP1: 192.168.31.6 

主机 IP 功用
C7node1 192.168.31.21, VIP HAproxy, KeepAlived
C7node2 192.168.31.22, VIP HAproxy, KeepAlived
C7node3 192.168.31.23 httpd, php 动态资源
C7node4 192.168.31.24 nginx, 静态资源

注意: 本文实验中所有主机SElinux和iptables都是关闭的,  系统为:CentOS 7.2 x86_64

实验步骤

配置后端httpd服务器

node4 静态服务器,静态资源

[root@c7node4 ~]# yum -y install nginx   #安装nginx服务器
[root@c7node4 ~]# vim /etc/nginx/nginx.conf   #编辑nginx配置文件,修改坚挺端口
    server {
    listen       8080 default_server;   #由80改为8080端口
    server_name  _;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}
[root@c7node4 ~]# echo '<h1> Jingtai server 192.168.31.24:8080</h1>' > /usr/share/nginx/html/index.html
[root@c7node4 ~]# cp /usr/share/backgrounds/*.jpg /usr/share/nginx/html/   #复制一些图片文件到网站根目录做测试
[root@c7node4 ~]# systemctl start nginx.service

N25_第二周作业

node3 动态服务器,动态资源
[root@c7node3 ~]#yum install httpd php          #安装httpd,php
[root@c7node3 ~]#vim /etc/httpd/conf/httpd.conf #修改配置文件,更改监听端口8080
ServerRoot "/etc/httpd"
#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to 
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 8080
[root@c7node3 ~]# systemctl start httpd.service  #启动httpd服务
[root@c7node3 ~]#cat >> /var/www/html/index.php << "EOF"  #创建网页文件
><h1>Dongtai server 192.168.31.23:8080</h1>
><img src="/morning.jpg"/>     #我们动态的网页目录下并没有这张图片
><?php
>phpinfo();
>?>
>EOF

N25_第二周作业

配置HAProxy实现backend负载均衡

[root@c7node1 ~]# yum install  -y haproxy    #安装haproxy
[root@c7node1 ~]# vim /etc/haproxy/haproxy.cfg   #配置文件如下
mode                    http
log                     global
option                  httplog
option                  dontlognull
option http-server-close
option forwardfor       except 127.0.0.0/8
option                  redispatch
retries                 3
timeout http-request    10s
timeout queue           1m
timeout connect         10s
timeout client          1m
timeout server          1m
timeout http-keep-alive 10s
timeout check           10s
maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:80
stats enable
stats hide-version
stats uri /haproxyadm

acl url_static       path_end       -i .jpg .gif .png .css .js .html

default_backend     dongtai
use_backend static          if url_static

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance     roundrobin
server      static 192.168.31.24:8080 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend dongtai
balance     roundrobin
server  dongtai 192.168.31.23:8080 check
[root@c7node1 ~]#systemctl start haproxy.service

测试动静分离效果

我们访问 192.168.31.21  这个是haproxy服务器IP地址

N25_第二周作业

我们关闭 192.168.31.24 这个静态服务器nginx服务后结果
[root@c7node4 ~]# systemctl stop nginx.service

N25_第二周作业

我们再次开启192.168.31.24 这个静态服务器nginx服务后,图片显示正常
[root@c7node4 ~]# systemctl start nginx.service
我们打开了stats页面, 可以通过设置的URI进行访问

N25_第二周作业

配置keepalived

配置c7node1上keepalived的配置文件

[root@c7node4 ~]#vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from zhong@localhost
   smtp_server 127.0.0.1
   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 1111
    }
    virtual_ipaddress {
        192.168.31.6 dev eno16777736 label eno16777736:0   #虚拟vIP
    }
}
[root@c7node4 ~]#systemctl start keepalived.service
[root@c7node4 ~]#ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
   valid_lft forever preferred_lft forever
inet6 ::1/128 scope host 
   valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:13:c2:04 brd ff:ff:ff:ff:ff:ff
inet 192.168.31.21/24 brd 192.168.31.255 scope global eno16777736
   valid_lft forever preferred_lft forever
inet 192.168.31.6/32 scope global eno16777736:0  #虚拟VIP,已经配置上了
   valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe13:c204/64 scope link 
   valid_lft forever preferred_lft forever


#配置c7node2上的keepalived配置文件#
[root@c7node2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
        root@localhost
   }
   notification_email_from zhong@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP            #备节点
    interface eno16777736
    virtual_router_id 52
    priority 95             #权重,应该小于主节点
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.31.6 dev eno16777736 label eno16777736:0
    }
}
[root@c7node2 ~]# systemctl start keepalived.service
[root@c7node2 ~]# ip addr   #发现虚拟VIP没有在备用节点上启用
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
   valid_lft forever preferred_lft forever
inet6 ::1/128 scope host 
   valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:60:20:b7 brd ff:ff:ff:ff:ff:ff
inet 192.168.31.22/24 brd 192.168.31.255 scope global eno16777736
   valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe60:20b7/64 scope link 
   valid_lft forever preferred_lft forever

最终测试

直接访问192.168.31.6  此时VIP在C7node1上面

N25_第二周作业

将C7node1上面的keepalived停止,VIP自动转移到C7node2上面,

N25_第二周作业

总结

我们轻松地通过HAProxy实现资源的动静分离和后端httpd主机的负载均衡,也通过KeepAlived实现HAProxy的高可用, 对于一个集群架构来说,整套架构还不算是很完整的。比如,在keepalived上面没有做后端主机健康检查;后端还没有配置varnish缓存服务器。这些知识还需要在后续的学习中继续来实践。

原创文章,作者:N25_木头钟,如若转载,请注明出处:http://www.178linux.com/63371

(0)
N25_木头钟N25_木头钟
上一篇 2016-12-12 12:37
下一篇 2016-12-12 15:13

相关推荐

  • Varnish基础进阶

    Varnish基础进阶 前言 互联网早已惠及全人类, 我们可以通过网络与家人、朋友进行实时通信, 也能通过网络随时随地在各大电商站点上购物, 我们访问web站点的速度也越来越快, 这背后都是有很多精巧的架构以及各种先进的技术来支撑的, 我们就今天主要聊聊Web的缓存技术, 对于当今的互联网来说, Cahe Is King, 缓存真的有那么神奇么? 就由我来带…

    Linux干货 2016-04-19
  • 计算机的组成和Linux发行版本介绍

    计算机的组成及功能 计算机的五大组成,如下 各部分的作用; 控制单元和算数逻辑单元是CPU的两个主要组成部分  控制单元主要协调各组件与各单元间的工作  算数逻辑单元主要负责程序运算与逻辑判断 内存,DRANM(Dynamic Random Access Memory)动态随机访问内存;CPU读取的数据都是从内存读取来的。 输入单元,下指…

    2017-07-02
  • 初识selinux

    一、selinux介绍     1.selinux历史     SELinux: Secure Enhanced Linux,是美国国家安全局「NSA=The National Security Agency」和SCC(Secure Computing Corporation)开…

    Linux干货 2016-09-21
  • 于浩的第一篇随笔

    人生只有两件事,努力工作,享受生活!

    2018-03-26
  • Centos 系列bind搭建DNS服务加固

        在centos系列版本上运用bind搭建dns服务教程已经有很多,先感谢前人做出的贡献,引用两篇博文,讲解的非常详细。 地址是: 主dns搭建:http://blog.csdn.net/reblue520/article/details/52537014 从dns搭建:http://blog.csdn.net/reblue520/…

    Linux干货 2017-04-16
  • Linux终端类型

    Linux中各种终端的解释 设备终端   键盘鼠标显示器 物理终端( /dev/console ) )   在Linux 系统中,计算机显示器通常被称为控制台终端(Console)。 虚拟终端(tty :teletypewriters, /dev/tty# # 为[1-6])   tty 可有n 个,Ctrl+Alt+…

    Linux干货 2016-10-13

评论列表(1条)

  • 马哥教育
    马哥教育 2016-12-16 23:18

    赞~ keepalived 中的其中一个配置参数 virtual_router_id 需要注意下~~继续加油~