keepalived+haproxy搭建web羣集

haproxy配置文件詳解參考:haproxyhtml

環境以下:linux

2.png

1、準備:
web

一、下載haproxy 軟件包,haproxy   提取碼: 9it6 redis

二、web 網站可使用Apache、Nginx、搭建均可以,這裏爲了方便我就直接使用系統盤帶的httpd服務了。算法

web網站的搭建可參考:基於 Linux 安裝 web 服務及基本配置基於 Centos 7 搭建Nginxvim

三、配置防火牆放行流量後端

四、我這裏使用的所有是centos 7系統,注意,該環境不是生產環境,如果在生產環境中,確定還有後端存儲來存放網頁文件,web服務器讀取存儲服務器上的網頁返回給客戶端。這樣纔可保證網頁內容的一致性。centos

2、開始搭建:
bash

一、配置主服務器:服務器

[root@haproxy1 /]# yum -y install pcre-devel bzip2-devel keepalived          # 安裝相關的依賴包和軟件包
[root@haproxy1 media]# tar zxf haproxy-1.5.19.tar.gz -C /usr/src/
[root@haproxy1 media]# cd /usr/src/haproxy-1.5.19/
[root@haproxy1 haproxy-1.5.19]# make TARHET=linux26  &&  make  install         # 編譯安裝,TARGET表示64位操做系統
[root@haproxy1 haproxy-1.5.19]# mkdir /etc/haproxy                    # 建立用來存放主配文件的目錄
[root@haproxy1 haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/             # 將編譯包中的主配文件複製到配置文件目錄,注意啊:這個配置文件是在編譯包中,注意看路徑
[root@haproxy1 /]# vim /etc/haproxy/haproxy.cfg 
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
        log /dev/log    local0 info
        log /dev/log    local0 notice
        #log loghost    local0 info
        maxconn 4096
        #chroot /usr/share/haproxy                # 將此行註釋掉
        uid 99
        gid 99
        daemon
        #debug
        #quiet
defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        redispatch
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000
listen  webcluster  0.0.0.0:80                             # 將端口號修改成80,webcluster爲羣集名稱,可修改
        option  httpchk GET  /index.html
        balance roundrobin                            # 表示採用輪詢算法
        server  web1 192.168.1.20:80 check inter 2000 fall 3                  # 兩個web節點
        server  web2 192.168.1.30:80 check inter 2000 fall 3
[root@haproxy1 /]# cp /usr/src/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy             # 複製服務控制腳本
[root@haproxy1 /]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy                  # 建立連接使命令使用更方便
[root@haproxy1 /]# chmod +x /etc/init.d/haproxy                      # 添加執行權限
[root@haproxy1 /]# chkconfig --add /etc/init.d/haproxy                # 添加爲系統服務
[root@haproxy1 /]# /etc/init.d/haproxy start                 # 啓動
Starting haproxy (via systemctl):                          [  OK  ]
[root@haproxy1 /]# vim /etc/rsyslog.d/haproxy.conf              # 配置日誌文件,寫入以下內容
if ($programname == 'haproxy' and $syslogseverity-text == 'info') then -/var/log/haproxy/haproxy-info.log
& ~ 
if ($programname == 'haproxy' and $syslogseverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log
& ~ 
[root@haproxy1 /]# systemctl restart rsyslog.service                 # 重啓日誌服務

配置 keepalived :

[root@haproxy1 /]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS1          # 主從調度器名稱區分開
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33              # 修改網卡名稱
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress { 
        192.168.1.100          # 填寫漂移地址
    }
}
[root@haproxy1 /]# systemctl restart keepalived             # 重啓服務使配置生效

二、配置從服務器:

[root@haproxy2 /]#  yum -y install pcre-devel bzip2-devel  keepalived
[root@haproxy2 media]# tar zxf haproxy-1.5.19.tar.gz -C /usr/src/
[root@haproxy2 media]# cd /usr/src/haproxy-1.5.19/
[root@haproxy2 haproxy-1.5.19]# make TARGET=linux26 && make install
[root@haproxy2 haproxy-1.5.19]# mkdir /etc/haproxy
[root@haproxy2 haproxy-1.5.19]# scp root@192.168.1.10:/etc/haproxy/haproxy.cfg /etc/haproxy/          # 圖個方便直接複製
root@192.168.1.10's password: 
haproxy.cfg                                          100%  570     0.6KB/s   00:00    
[root@haproxy2 /]# cp /usr/src/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy           # 複製服務控制腳本
[root@haproxy2 /]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@haproxy2 /]# chmod +x /etc/init.d/haproxy 
[root@haproxy2 /]# chkconfig --add /etc/init.d/haproxy 
[root@haproxy2 /]# /etc/init.d/haproxy start 
Starting haproxy (via systemctl):                          [  OK  ]
[root@haproxy2 /]# scp root@192.168.1.10:/etc/rsyslog.d/haproxy.conf /etc/rsyslog.d/
root@192.168.1.10's password: 
haproxy.conf                                         100%  226     0.2KB/s   00:00   
[root@haproxy2 /]# systemctl restart rsyslog.service                 # 重啓服務使配置生效
[root@haproxy2 haproxy-1.5.19]# scp root@192.168.1.10:/etc/keepalived/keepalived.conf /etc/keepalived/
root@192.168.1.10's password: 
keepalived.conf                                      100% 3511     3.4KB/s   00:00    
[root@haproxy2 /]# vim /etc/keepalived/keepalived.conf            # 修改主配置文件,修改以下幾個部分
....................... 省略部分
   router_id LVS2               # 主從調度器區分ID
}
vrrp_instance VI_1 {
    state BACKUP                       # 狀態修改成 BACKUP
    interface ens33                 
    virtual_router_id 51
    priority 90                           # 優先級調低
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.100                # 漂移地址
    }
}
[root@haproxy2 /]# systemctl restart keepalived

兩個web節點配置(兩個配置相同):

[root@web2 /]# yum -y install httpd


[root@web2 /]# echo server2.com > /var/www/html/index.html          # 建立測試網頁

[root@web2 /]# systemctl start httpd 

[root@web2 /]# systemctl enable httpd

截圖03.png

截圖04.png

固然,在實際生產環境中網頁是同樣的,這裏我爲了驗證出效果,因此作了兩個不一樣的測試文件。

相關文章
相關標籤/搜索