1. 程式人生 > >03: HAProxy服務器 、 Keepalived熱備 、 Keepalived+LVS

03: HAProxy服務器 、 Keepalived熱備 、 Keepalived+LVS

init.d usr show mkdir 訪問網站 主配置文件 end rewrite 存儲空間

LVS/DR
不對realserver 主機上的服務做健康性檢查
是基於四層負載均衡集群軟件(端口)

一、使用Haproxy軟件部署網站LB集群
1.1 普通LB集群

客戶端 192.168.4.250

haproxy服務器 192.168.4.100

網站服務器 192.168.4.101/102

配置 haproxy服務器:192.168.4.100
裝包
yum list | grep -i haproxy
rpm -q haproxy || yum -y install haproxy

修改配置文件
cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak

vim /etc/haproxy/haproxy.cfg
[global]
....

[default]
......
stats uri /admin
listen lbweb 0.0.0.0:80
cookie SERVERID rewrite
balance roundrobin
server webA 192.168.4.101:80 cookie app101a check inter 2000 rise 2 fall 5
server webB 192.168.4.102:80 cookie app102b check inter 2000 rise 2 fall 5

:wq #後面的內容刪除

啟動服務#service haproxy start ; chkconfig haproxy on

查看端口 #netstat -utnalp | grep :80
客戶端250 測試 #elinks --dump http://192.168.4.100/test.html
查看web頁面顯示健康性檢查信息 http://192.168.4.100/admin
++++++++++++++++++++++++++++++++++++++++++++++
test.html a.php
]# service haproxy stop
]# mv haproxy.cfg haproxy.cfg-1
]# cp haproxy.cfg.bak haproxy.cfg

1.2 區分業務的LB集群(七層)
]# vim haproxy.cfg

stats uri /admin
frontend weblb 192.168.4.100:80
acl pathhtml path_end -i .html
acl pathphp path_end -i .php

use_backend   htmlgrp  if pathhtml
use_backend   phpgrp  if pathphp
default_backend   htmlgrp

backend htmlgrp
balance roundrobin
server app101 192.168.4.101:80 check
server app102 192.168.4.102:80 check
backend phpgrp
balance roundrobin
server app103 192.168.4.103:80 check
server app104 192.168.4.104:80 check
]# service haproxy start
++++++++++++++++++++++++++++++++++++++++
250 elinks --dump http://192.168.4.100/test.html
web101
web102
250 elinks --dump http://192.168.4.100/a.php
hello web103
hello web104

250 elinks --dump http://192.168.4.100
++++++++++++++++++++++++++++++++++++++
二、使用keepalived軟件部署網站HA集群

  • 能夠實現任意單故障節點的高可用集群。

配置網站高可用集群 103 和 104
1 在主機上都安裝keepalived軟件
#yum list | grep -i keepalived
#rpm -q keepalived || yum -y install keepalived

2 修改服務的主配置文件/etc/keepalived/keepalived.conf
2.1修改主服務器192.168.4.103
vim /etc/keepalived/keepalived.conf
...
state MASTER
priority 150
....
:wq
2.2修改備用服務器192.168.4.104
vim /etc/keepalived/keepalived.conf
....
state BACKUP
priority 100
....
:wq
++++++++++++++++++++++++++++++
3 啟動服務
主服務器
/etc/init.d/keepalived start
chkconfig keepalived on

備用服務器
/etc/init.d/keepalived start
chkconfig keepalived on

4 查看是否獲取到vip 地址
#ip addr show | grep 192.168.4.

4測試
250: elinks --dump http://vip/teset.php
192.168.4.253

++++++++++++++++++++++++++++++++++++++++++++++++
部署 101 和 102 為 HA 高可用集群。
客戶端訪問集群的vip地址是 192.168.4.252

101 <------- 102
思考: HA 集群中 主服務器沒有宕機,但是應用服務(httpd)停止了,為問主服務器 是否會是否vip地址?

service httpd stop
ip addr show | grep 192

elinks --dump http://192.168.4.252/test.html

+++++++++++++++++++++++++++++++++++++++++++++++++
三、使用keepalvied 做lvs分發器的HA集群
3.1 配置KVS/DR模式的LB集群(網站)
3.1.1、配置網站服務器101 、 102
運行網站服務 並編輯網頁文件
修改網絡接口運行參數 、綁定vip 地址

3.1.2、在做分發器的主機100、200上安裝提供lvs服務的軟件ipvsadm

3.2 配置分發器的HA集群 100、200
3.2.1 在2臺分發器主機上分別安裝keepalived軟件
3.2.2 分別修改2臺分發器主機上keepalived服務配置文件
3.2.3 分別啟動2臺分發器主機上的keepalived服務
3.2.4 查看是否獲取到VIP地址 和 虛擬服務及realserver

3.3 測試HA集群的配置
把主服務器宕機後,客戶端仍然能夠正常訪問網站服務。
elinks --dump http://192.168.4.253/test.html
vip 地址

http://192.168.4.253/b.html

http://192.168.4.252/c.html

+++++++++++++++++++++++++++++++++++++++++++
搭建共享存儲服務器 192.168.4.105
配置需求:
配置LB集群中的網站服務把網頁文件存儲在 存儲服務器105主機的/dev/vdd磁盤上。

共享目錄的服務有哪些? NFS Samba tftp ftp
nfs cifs tftp ftp

mount /dev/vdd1 /sharedir
3G

一、配置存儲服務器192.168.4.105
1.1 準備存儲空間: 分區 、 格式化 、 掛載
165 fdisk -l /dev/vdd
165 fdisk /dev/vdd -> n -> p ->1 ->2次回車->w
165 fdisk -l /dev/vdd
169 blkid /dev/vdd1
170 mkfs.ext4 /dev/vdd1
171 blkid /dev/vdd1
172 mkdir /webdir
173 mount /dev/vdd1 /webdir
174 df -h /webdir/

umount /webdir

設置開機掛載 /etc/fstab
[root@storage105 ~]# tail -1 /etc/fstab
UUID=3f505f56-b559-4f95-9d99-8f99d9318e3c /webdir ext4 defaults 0 0
[root@storage105 ~]# mount -a
[root@storage105 ~]# mount | grep /webdir

1.2 共享存儲存儲空間: 裝包 、修改配置文件、啟動服務
184 rpm -q nfs-utils
185 rpm -q nfs-utils || yum -y install nfs-utils
186 rpm -q nfs-utils
]# rpm -q rpcbind || yum -y install rpcbind
]# vim /etc/exports
/webdir *(rw)
:wq
]# chmod o+w /webdir/

193 /etc/init.d/rpcbind status
194 /etc/init.d/rpcbind start
195 /etc/init.d/nfs status
196 /etc/init.d/nfs start
#chkconfig rpcbind on
#chkconfig nfs on

1.3 在本機查看共享信息
[root@storage105 ~]# which showmount
/usr/sbin/showmount
[root@storage105 ~]# rpm -qf /usr/sbin/showmount
nfs-utils-1.2.3-64.el6.x86_64
[root@storage105 ~]#
[root@storage105 ~]# showmount -e localhost
Export list for localhost:
/webdir
[root@storage105 ~]#
二、配置應用服務器192.168.4.101/102
2.1 掛載nfs共享目錄
]# yum -y install nfs-utils
]# showmount -e 192.168.4.105
]# rm -rf /var/www/html/

]# mount -t nfs 192.168.4.105:/webdir /var/www/html/
2.2 在105主機上編寫網頁文件
vim /webdir/test.html
aaaa
cccc
:wq
2.3 客戶端 250 訪問 lb集群能夠看到網頁內容
elinks --dump http://192.168.4.253/test.html

202.96.128.96

03: HAProxy服務器 、 Keepalived熱備 、 Keepalived+LVS