實(shí)驗(yàn)環(huán)境
1. host1 host2 host3 host4 都是 CentOS7.3 系統(tǒng)竭钝;
2. host3 host4 作為 Real Server些膨,搭建Web服務(wù)纤怒;
3. host1 host2 作為LVS負(fù)載均衡高可用集群椅挣;
4. HA Cluster的配置前提:
(1) 各節(jié)點(diǎn)時(shí)間必須同步压语;
ntp, chrony
(2) 確保iptables及selinux不會成為阻礙止吐;
(3) 各節(jié)點(diǎn)之間可通過主機(jī)名互相通信(對KA并非必須)裳朋;
建議使用/etc/hosts文件實(shí)現(xiàn)匪蝙;
(4) 確保各節(jié)點(diǎn)的用于集群服務(wù)的接口支持MULTICAST(多播)通信敛摘;
D類:224-239
配置Real Server
- 安裝Nginx并修改測試頁:
# RS1:
[root@host3 ~]#yum -y install nginx
[root@host3 ~]#echo "<h1>RS1:host3</h1>" > /usr/share/nginx/html/index.html
[root@host3 ~]#cat /usr/share/nginx/html/index.html
<h1>RS1:host3</h1>
[root@host3 ~]#systemctl start nginx
[root@host3 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port
LISTEN 0 128 *:80
LISTEN 0 128 *:22
LISTEN 0 100 127.0.0.1:25
LISTEN 0 128 :::80
LISTEN 0 128 :::22
LISTEN 0 100 ::1:25
[root@host3 ~]#
------------------------------------------------------------------------------------------------------
# RS2:
[root@host4 ~]#yum -y install nginx
[root@host4 ~]#echo "<h1>RS2:host4</h1>" > /usr/share/nginx/html/index.html
[root@host4 ~]#cat /usr/share/nginx/html/index.html
<h1>RS2:host4</h1>
[root@host4 ~]#systemctl start nginx
[root@host4 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port
LISTEN 0 128 *:80
LISTEN 0 128 *:22
LISTEN 0 100 127.0.0.1:25
LISTEN 0 128 :::80
LISTEN 0 128 :::22
LISTEN 0 100 ::1:25
[root@host4 ~]#
------------------------------------------------------------------------------------------------------
# 測試:
>.host1:
[root@host1 ~]#curl http://192.168.10.13
<h1>RS1:host3</h1>
[root@host1 ~]#curl http://192.168.10.14
<h1>RS2:host4</h1>
[root@host1 ~]#
>.host2:
[root@host2 ~]#curl http://192.168.10.13
<h1>RS1:host3</h1>
[root@host2 ~]#curl http://192.168.10.14
<h1>RS2:host4</h1>
[root@host2 ~]#
- 修改RS的內(nèi)核參數(shù)并配置VIP
RS1
RS1上給權(quán)限
RS1上執(zhí)行腳本并查看lo:0的IP地址
RS2
RS2上給權(quán)限
RS2上執(zhí)行腳本并查看lo:0的IP地址
搭建LVS集群
給兩臺VS服務(wù)器
host1 host2
上 安裝ipvsadm
和keepalived
# VS1:
[root@host1 ~]#yum -y install ipvsadm keepalived
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# VS2:
[root@host2 ~]#yum -y install ipvsadm keepalived
配置LVS高可用集群服務(wù)器
- 配置VS服務(wù)器host1的keepalived服務(wù)并啟動:
[root@host1 keepalived]#cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
ngt@mgl.com
}
notification_email_from grh_ngt@mgl.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id host1
vrrp_mcast_group4 224.89.51.18
}
vrrp_instance VI_1 {
state MASTER <-- 配置 host1 為 MASTER
interface ens33
virtual_router_id 89
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass nQyVIaY1
}
virtual_ipaddress {
192.168.10.88 <-- 虛擬 IP 為 VIP
}
}
virtual_server 192.168.10.88 80 { <-- 配置 VS 服務(wù)器 host1
delay_loop 6
lb_algo rr <-- LVS調(diào)度算法
lb_kind DR <--LVS工作模式
nat_mask 255.255.255.255
protocol TCP
real_server 192.168.10.13 80 { <-- 添加第一臺 RS服務(wù)器 host3
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
real_server 192.168.10.14 80 { <--添加第二臺RS服務(wù)器 host4
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
}
[root@host1 keepalived]#systemctl start keepalived
- 配置VS服務(wù)器host2的keepalived服務(wù)并啟動:
只需把
vrrp_instance VI_1
的state
改為BACKUP
即可门烂,其他與VS1
保持一致
[root@host1 keepalived]#cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
ngt@mgl.com
}
notification_email_from grh_ngt@mgl.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id host1
vrrp_mcast_group4 224.89.51.18
}
vrrp_instance VI_1 {
state BACKUP <-- 配置 host2 為 BACKUP
interface ens33
virtual_router_id 89
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass nQyVIaY1
}
virtual_ipaddress {
192.168.10.88 <-- 虛擬 IP 為 VIP
}
}
virtual_server 192.168.10.88 80 { <-- 配置 VS 服務(wù)器 host2
delay_loop 6
lb_algo rr <-- LVS調(diào)度算法
lb_kind DR <--LVS工作模式
nat_mask 255.255.255.255
protocol TCP
real_server 192.168.10.13 80 { <-- 添加第一臺 RS服務(wù)器 host3
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
real_server 192.168.10.14 80 { <--添加第二臺RS服務(wù)器 host4
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
}
[root@host2 keepalived]#systemctl start keepalived
測試:
如何測試的詳細(xì)步驟我就不細(xì)寫了,貼個(gè)圖意思意思下:
Sorry Server
在兩臺VS上也安裝Nginx兄淫,設(shè)置頁面內(nèi)容為Say sorry的message屯远!
- VS1上:
[root@host1 keepalived]#yum -y install nginx
[root@host1 keepalived]#echo "<h1>Sorry from Director 1</h1>" > /usr/share/nginx/html/index.html
[root@host1 keepalived]#systemctl start nginx
- VS2上:
[root@host2 keepalived]#yum -y install nginx
[root@host2 keepalived]#echo "<h1>Sorry from Director 2</h1>" > /usr/share/nginx/html/index.html
[root@host2 keepalived]#systemctl start nginx
- 編輯配置文件:
很簡單,只需在兩臺VS的配置文件keepalived.conf的virtual_server里加一行sorry_server 127.0.0.1 80 即可捕虽!
- 測試Sorry Server 的效果: