基於Haproxy+Keepalived構建高可用負載均衡集羣

一、在Nginx1/2上編譯安裝nginx服務
1.1 首先安裝Nginx1html

[root@Nginx-1 ~] # yum -y install gcc gcc-c++ make pcre-devel zlib-devel
[root@Nginx-1 ~] # useradd -M -s /sbin/nologin  nginx
[root@Nginx-1 ~] # tar xf nginx-1.6.2.tar.gz -C /usr/src
[root@Nginx-1 ~] # cd /usr/src/nginx-1.6.2
[root@Nginx-1 nginx-1.6.2] # ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
[root@Nginx-1 nginx-1.6.2] # cd /usr/local/nginx/html/
[root@Nginx-1 html] # echo "server 192.168.200.103" > index.html
[root@Nginx-1 html] # /usr/local/nginx/sbin/nginx
[root@Nginx-1 html] # netstat -anpt |grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4503 /nginx

 1.2 安裝Nginx2, 同Nginx1搭建方式是同樣的。
與Nginx1惟一不一樣的是:linux

[root@Nginx-2 html] # echo "server 192.168.200.104" > index.html

 二、安裝Haproxy-1與Haproxy-2 兩臺機器配置一致:nginx

[root@Haproxy-1 ~] # yum -y install gcc gcc-c++ make pcre-devel bzip2-devel
[root@Haproxy-1 ~] # tar xf haproxy-1.4.24.tar.gz -C /usr/src/
[root@Haproxy-1 ~] # cd /usr/src/haproxy-1.4.24/
[root@Haproxy-1 haproxy-1.4.24] # make TARGET=linux26 && make install

 2.1 Haproxy服務器配置
創建haproxy的配置目錄及文件c++

[root@Haproxy-1 haproxy-1.4.24] # mkdir /etc/haproxy
[root@Haproxy-1 haproxy-1.4.24] # cp examples/haproxy.cfg /etc/haproxy/

 2.2 haproxy配置項的介紹
haproxy的配置文件一般分爲三部分: global(全局配置部分) defaults(默認配置部分) listen(應用組件部分)web

[root@Haproxy-1 ~] # vim /etc/haproxy/haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
 
global
     log 127.0.0.1   local0
     log 127.0.0.1   local1 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  web-cluster 0.0.0.0:80
     option httpchk GET /index .html
     balance roundrobin
     server  inst1 192.168.200.103:80 check inter 2000 fall 3
     server  inst2 192.168.200.104:80 check inter 2000 fall 3

2.3 建立自啓動腳本redis

[root@Haproxy-1 ~] # cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@Haproxy-1 ~] # ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@Haproxy-1 ~] # chmod +x /etc/init.d/haproxy
[root@Haproxy-1 ~] # /etc/init.d/haproxy start

2.4 客戶端訪問測試:vim

用瀏覽器打開 http://192.168.200.101 打開一個新的瀏覽器再次訪問 http://192.168.200.101瀏覽器

用瀏覽器打開 http://192.168.200.102 打開一個新的瀏覽器再次訪問 http://192.168.200.102bash

能夠驗證兩次訪問到的結果分別爲:服務器

server 192.168.200.103

server 192.168.200.104

 

三、編譯安裝keepalived服務

[root@Haproxy-1 ~] # yum -y install kernel-devel openssl-devel popt-devel
[root@Haproxy-1 ~] # tar xf keepalived-1.2.13.tar.gz
[root@Haproxy-1 ~] # cd keepalived-1.2.13
[root@Haproxy-1 keepalived-1.2.13] # ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.18-194.el5-i686 && make && make install

 3.1 配置keepalibed 開機啓動腳本

[root@Haproxy-1 ~] # chkconfig --add keepalived
[root@Haproxy-1 ~] # chkconfig keepalived on
[root@Haproxy-1 ~] # chkconfig --list keepalived

 3.2.1 配置keepalibed 主配置文件

 

[root@Haproxy-1 ~] # vim /etc/keepalived/keepalived.conf
! Configuration File for  keepalived
 
vrrp_script chk_http_port {
script "/etc/keepalived/check_haproxy.sh"
interval 2
 }
 
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
 
track_script {
chk_http_port
}
 
virtual_ipaddress {
192.168.200.254
}
}

 3.2.2 第二臺Haproxy配置keepalibed 主配置文件

[root@Haproxy-2 ~] # cat /etc/keepalived/keepalived.conf
! Configuration File for  keepalived
 
vrrp_script chk_http_port {
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight 2
 }
 
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
 
track_script {
chk_http_port
}
 
virtual_ipaddress {
192.168.200.254
}
}

 四、兩臺機器上都配置haproxy檢測腳本

[root@Haproxy-1 ~] # cat /etc/keepalived/check_haproxy.sh
#!/bin/bash
num=` ps  -C haproxy --no-header | wc  -l`
if  [ $num - eq  0 ]
then
    systemctl restart haproxy
     sleep  3
     if  [ ` ps  -C haproxy --no-header | wc  -l` - eq  0 ]
     then
        systemctl stop keepalived
     fi
fi
 
[root@Haproxy-1 ~] # chmod +x /etc/keepalived/check_haproxy.sh
[root@Haproxy-1 ~] # systemctl start keepalived

 5.一、測試VIP地址

[root@Haproxy-1 ~] # ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
     link /ether  00:0c:29:cc:18:a2 brd ff:ff:ff:ff:ff:ff
     inet 192.168.200.101 /24  brd 192.168.200.255 scope global eth0
     inet 192.168.200.254 /32  scope global eth0
     inet6 fe80::20c:29ff:fecc:18a2 /64  scope link
        valid_lft forever preferred_lft forever
 
[root@Haproxy-1 ~] # /etc/init.d/keepalived stop
 
[root@Haproxy-2 ~] # ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
     link /ether  00:0c:29:fd:8a:4e brd ff:ff:ff:ff:ff:ff
     inet 192.168.200.102 /24  brd 192.168.200.255 scope global eth0
     inet 192.168.200.254 /32  scope global eth0
     inet6 fe80::20c:29ff:fefd:8a4e /64  scope link
        valid_lft forever preferred_lft forever

 5.二、測試Haproxy健康檢查

[root@Haproxy-1 ~] # service haproxy stop
Shutting down haproxy:                                     [肯定]
[root@Haproxy-1 ~] # service haproxy status
haproxy (pid 59717) 正在運行...

 5.3 網頁測試:
用瀏覽器打開 http://192.168.200.254
再次打開一個新的瀏覽器再次訪問 http://192.168.200.254

能夠驗證兩次訪問到的結果分別爲:server 192.168.200.103server 192.168.200.104

相關文章
相關標籤/搜索